1.1 --- a/extensions/sqlite/sqlite-source/expr.c Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,2617 +0,0 @@
1.4 -/*
1.5 -** 2001 September 15
1.6 -**
1.7 -** The author disclaims copyright to this source code. In place of
1.8 -** a legal notice, here is a blessing:
1.9 -**
1.10 -** May you do good and not evil.
1.11 -** May you find forgiveness for yourself and forgive others.
1.12 -** May you share freely, never taking more than you give.
1.13 -**
1.14 -*************************************************************************
1.15 -** This file contains routines used for analyzing expressions and
1.16 -** for generating VDBE code that evaluates expressions in SQLite.
1.17 -**
1.18 -** $Id$
1.19 -*/
1.20 -#include "sqliteInt.h"
1.21 -#include <ctype.h>
1.22 -
1.23 -/*
1.24 -** Return the 'affinity' of the expression pExpr if any.
1.25 -**
1.26 -** If pExpr is a column, a reference to a column via an 'AS' alias,
1.27 -** or a sub-select with a column as the return value, then the
1.28 -** affinity of that column is returned. Otherwise, 0x00 is returned,
1.29 -** indicating no affinity for the expression.
1.30 -**
1.31 -** i.e. the WHERE clause expresssions in the following statements all
1.32 -** have an affinity:
1.33 -**
1.34 -** CREATE TABLE t1(a);
1.35 -** SELECT * FROM t1 WHERE a;
1.36 -** SELECT a AS b FROM t1 WHERE b;
1.37 -** SELECT * FROM t1 WHERE (select a from t1);
1.38 -*/
1.39 -char sqlite3ExprAffinity(Expr *pExpr){
1.40 - int op = pExpr->op;
1.41 - if( op==TK_SELECT ){
1.42 - return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
1.43 - }
1.44 -#ifndef SQLITE_OMIT_CAST
1.45 - if( op==TK_CAST ){
1.46 - return sqlite3AffinityType(&pExpr->token);
1.47 - }
1.48 -#endif
1.49 - return pExpr->affinity;
1.50 -}
1.51 -
1.52 -/*
1.53 -** Set the collating sequence for expression pExpr to be the collating
1.54 -** sequence named by pToken. Return a pointer to the revised expression.
1.55 -** The collating sequence is marked as "explicit" using the EP_ExpCollate
1.56 -** flag. An explicit collating sequence will override implicit
1.57 -** collating sequences.
1.58 -*/
1.59 -Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
1.60 - CollSeq *pColl;
1.61 - if( pExpr==0 ) return 0;
1.62 - pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
1.63 - if( pColl ){
1.64 - pExpr->pColl = pColl;
1.65 - pExpr->flags |= EP_ExpCollate;
1.66 - }
1.67 - return pExpr;
1.68 -}
1.69 -
1.70 -/*
1.71 -** Return the default collation sequence for the expression pExpr. If
1.72 -** there is no default collation type, return 0.
1.73 -*/
1.74 -CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
1.75 - CollSeq *pColl = 0;
1.76 - if( pExpr ){
1.77 - int op;
1.78 - pColl = pExpr->pColl;
1.79 - op = pExpr->op;
1.80 - if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
1.81 - return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
1.82 - }
1.83 - }
1.84 - if( sqlite3CheckCollSeq(pParse, pColl) ){
1.85 - pColl = 0;
1.86 - }
1.87 - return pColl;
1.88 -}
1.89 -
1.90 -/*
1.91 -** pExpr is an operand of a comparison operator. aff2 is the
1.92 -** type affinity of the other operand. This routine returns the
1.93 -** type affinity that should be used for the comparison operator.
1.94 -*/
1.95 -char sqlite3CompareAffinity(Expr *pExpr, char aff2){
1.96 - char aff1 = sqlite3ExprAffinity(pExpr);
1.97 - if( aff1 && aff2 ){
1.98 - /* Both sides of the comparison are columns. If one has numeric
1.99 - ** affinity, use that. Otherwise use no affinity.
1.100 - */
1.101 - if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
1.102 - return SQLITE_AFF_NUMERIC;
1.103 - }else{
1.104 - return SQLITE_AFF_NONE;
1.105 - }
1.106 - }else if( !aff1 && !aff2 ){
1.107 - /* Neither side of the comparison is a column. Compare the
1.108 - ** results directly.
1.109 - */
1.110 - return SQLITE_AFF_NONE;
1.111 - }else{
1.112 - /* One side is a column, the other is not. Use the columns affinity. */
1.113 - assert( aff1==0 || aff2==0 );
1.114 - return (aff1 + aff2);
1.115 - }
1.116 -}
1.117 -
1.118 -/*
1.119 -** pExpr is a comparison operator. Return the type affinity that should
1.120 -** be applied to both operands prior to doing the comparison.
1.121 -*/
1.122 -static char comparisonAffinity(Expr *pExpr){
1.123 - char aff;
1.124 - assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
1.125 - pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
1.126 - pExpr->op==TK_NE );
1.127 - assert( pExpr->pLeft );
1.128 - aff = sqlite3ExprAffinity(pExpr->pLeft);
1.129 - if( pExpr->pRight ){
1.130 - aff = sqlite3CompareAffinity(pExpr->pRight, aff);
1.131 - }
1.132 - else if( pExpr->pSelect ){
1.133 - aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
1.134 - }
1.135 - else if( !aff ){
1.136 - aff = SQLITE_AFF_NONE;
1.137 - }
1.138 - return aff;
1.139 -}
1.140 -
1.141 -/*
1.142 -** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
1.143 -** idx_affinity is the affinity of an indexed column. Return true
1.144 -** if the index with affinity idx_affinity may be used to implement
1.145 -** the comparison in pExpr.
1.146 -*/
1.147 -int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
1.148 - char aff = comparisonAffinity(pExpr);
1.149 - switch( aff ){
1.150 - case SQLITE_AFF_NONE:
1.151 - return 1;
1.152 - case SQLITE_AFF_TEXT:
1.153 - return idx_affinity==SQLITE_AFF_TEXT;
1.154 - default:
1.155 - return sqlite3IsNumericAffinity(idx_affinity);
1.156 - }
1.157 -}
1.158 -
1.159 -/*
1.160 -** Return the P1 value that should be used for a binary comparison
1.161 -** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
1.162 -** If jumpIfNull is true, then set the low byte of the returned
1.163 -** P1 value to tell the opcode to jump if either expression
1.164 -** evaluates to NULL.
1.165 -*/
1.166 -static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
1.167 - char aff = sqlite3ExprAffinity(pExpr2);
1.168 - return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
1.169 -}
1.170 -
1.171 -/*
1.172 -** Return a pointer to the collation sequence that should be used by
1.173 -** a binary comparison operator comparing pLeft and pRight.
1.174 -**
1.175 -** If the left hand expression has a collating sequence type, then it is
1.176 -** used. Otherwise the collation sequence for the right hand expression
1.177 -** is used, or the default (BINARY) if neither expression has a collating
1.178 -** type.
1.179 -**
1.180 -** Argument pRight (but not pLeft) may be a null pointer. In this case,
1.181 -** it is not considered.
1.182 -*/
1.183 -CollSeq *sqlite3BinaryCompareCollSeq(
1.184 - Parse *pParse,
1.185 - Expr *pLeft,
1.186 - Expr *pRight
1.187 -){
1.188 - CollSeq *pColl;
1.189 - assert( pLeft );
1.190 - if( pLeft->flags & EP_ExpCollate ){
1.191 - assert( pLeft->pColl );
1.192 - pColl = pLeft->pColl;
1.193 - }else if( pRight && pRight->flags & EP_ExpCollate ){
1.194 - assert( pRight->pColl );
1.195 - pColl = pRight->pColl;
1.196 - }else{
1.197 - pColl = sqlite3ExprCollSeq(pParse, pLeft);
1.198 - if( !pColl ){
1.199 - pColl = sqlite3ExprCollSeq(pParse, pRight);
1.200 - }
1.201 - }
1.202 - return pColl;
1.203 -}
1.204 -
1.205 -/*
1.206 -** Generate code for a comparison operator.
1.207 -*/
1.208 -static int codeCompare(
1.209 - Parse *pParse, /* The parsing (and code generating) context */
1.210 - Expr *pLeft, /* The left operand */
1.211 - Expr *pRight, /* The right operand */
1.212 - int opcode, /* The comparison opcode */
1.213 - int dest, /* Jump here if true. */
1.214 - int jumpIfNull /* If true, jump if either operand is NULL */
1.215 -){
1.216 - int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
1.217 - CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
1.218 - return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
1.219 -}
1.220 -
1.221 -/*
1.222 -** Construct a new expression node and return a pointer to it. Memory
1.223 -** for this node is obtained from sqlite3_malloc(). The calling function
1.224 -** is responsible for making sure the node eventually gets freed.
1.225 -*/
1.226 -Expr *sqlite3Expr(
1.227 - sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
1.228 - int op, /* Expression opcode */
1.229 - Expr *pLeft, /* Left operand */
1.230 - Expr *pRight, /* Right operand */
1.231 - const Token *pToken /* Argument token */
1.232 -){
1.233 - Expr *pNew;
1.234 - pNew = sqlite3DbMallocZero(db, sizeof(Expr));
1.235 - if( pNew==0 ){
1.236 - /* When malloc fails, delete pLeft and pRight. Expressions passed to
1.237 - ** this function must always be allocated with sqlite3Expr() for this
1.238 - ** reason.
1.239 - */
1.240 - sqlite3ExprDelete(pLeft);
1.241 - sqlite3ExprDelete(pRight);
1.242 - return 0;
1.243 - }
1.244 - pNew->op = op;
1.245 - pNew->pLeft = pLeft;
1.246 - pNew->pRight = pRight;
1.247 - pNew->iAgg = -1;
1.248 - if( pToken ){
1.249 - assert( pToken->dyn==0 );
1.250 - pNew->span = pNew->token = *pToken;
1.251 - }else if( pLeft ){
1.252 - if( pRight ){
1.253 - sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
1.254 - if( pRight->flags & EP_ExpCollate ){
1.255 - pNew->flags |= EP_ExpCollate;
1.256 - pNew->pColl = pRight->pColl;
1.257 - }
1.258 - }
1.259 - if( pLeft->flags & EP_ExpCollate ){
1.260 - pNew->flags |= EP_ExpCollate;
1.261 - pNew->pColl = pLeft->pColl;
1.262 - }
1.263 - }
1.264 -
1.265 - sqlite3ExprSetHeight(pNew);
1.266 - return pNew;
1.267 -}
1.268 -
1.269 -/*
1.270 -** Works like sqlite3Expr() except that it takes an extra Parse*
1.271 -** argument and notifies the associated connection object if malloc fails.
1.272 -*/
1.273 -Expr *sqlite3PExpr(
1.274 - Parse *pParse, /* Parsing context */
1.275 - int op, /* Expression opcode */
1.276 - Expr *pLeft, /* Left operand */
1.277 - Expr *pRight, /* Right operand */
1.278 - const Token *pToken /* Argument token */
1.279 -){
1.280 - return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
1.281 -}
1.282 -
1.283 -/*
1.284 -** When doing a nested parse, you can include terms in an expression
1.285 -** that look like this: #0 #1 #2 ... These terms refer to elements
1.286 -** on the stack. "#0" means the top of the stack.
1.287 -** "#1" means the next down on the stack. And so forth.
1.288 -**
1.289 -** This routine is called by the parser to deal with on of those terms.
1.290 -** It immediately generates code to store the value in a memory location.
1.291 -** The returns an expression that will code to extract the value from
1.292 -** that memory location as needed.
1.293 -*/
1.294 -Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
1.295 - Vdbe *v = pParse->pVdbe;
1.296 - Expr *p;
1.297 - int depth;
1.298 - if( pParse->nested==0 ){
1.299 - sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
1.300 - return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
1.301 - }
1.302 - if( v==0 ) return 0;
1.303 - p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
1.304 - if( p==0 ){
1.305 - return 0; /* Malloc failed */
1.306 - }
1.307 - depth = atoi((char*)&pToken->z[1]);
1.308 - p->iTable = pParse->nMem++;
1.309 - sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
1.310 - sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
1.311 - return p;
1.312 -}
1.313 -
1.314 -/*
1.315 -** Join two expressions using an AND operator. If either expression is
1.316 -** NULL, then just return the other expression.
1.317 -*/
1.318 -Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
1.319 - if( pLeft==0 ){
1.320 - return pRight;
1.321 - }else if( pRight==0 ){
1.322 - return pLeft;
1.323 - }else{
1.324 - return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
1.325 - }
1.326 -}
1.327 -
1.328 -/*
1.329 -** Set the Expr.span field of the given expression to span all
1.330 -** text between the two given tokens.
1.331 -*/
1.332 -void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
1.333 - assert( pRight!=0 );
1.334 - assert( pLeft!=0 );
1.335 - if( pExpr && pRight->z && pLeft->z ){
1.336 - assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
1.337 - if( pLeft->dyn==0 && pRight->dyn==0 ){
1.338 - pExpr->span.z = pLeft->z;
1.339 - pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
1.340 - }else{
1.341 - pExpr->span.z = 0;
1.342 - }
1.343 - }
1.344 -}
1.345 -
1.346 -/*
1.347 -** Construct a new expression node for a function with multiple
1.348 -** arguments.
1.349 -*/
1.350 -Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
1.351 - Expr *pNew;
1.352 - assert( pToken );
1.353 - pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
1.354 - if( pNew==0 ){
1.355 - sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
1.356 - return 0;
1.357 - }
1.358 - pNew->op = TK_FUNCTION;
1.359 - pNew->pList = pList;
1.360 - assert( pToken->dyn==0 );
1.361 - pNew->token = *pToken;
1.362 - pNew->span = pNew->token;
1.363 -
1.364 - sqlite3ExprSetHeight(pNew);
1.365 - return pNew;
1.366 -}
1.367 -
1.368 -/*
1.369 -** Assign a variable number to an expression that encodes a wildcard
1.370 -** in the original SQL statement.
1.371 -**
1.372 -** Wildcards consisting of a single "?" are assigned the next sequential
1.373 -** variable number.
1.374 -**
1.375 -** Wildcards of the form "?nnn" are assigned the number "nnn". We make
1.376 -** sure "nnn" is not too be to avoid a denial of service attack when
1.377 -** the SQL statement comes from an external source.
1.378 -**
1.379 -** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
1.380 -** as the previous instance of the same wildcard. Or if this is the first
1.381 -** instance of the wildcard, the next sequenial variable number is
1.382 -** assigned.
1.383 -*/
1.384 -void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
1.385 - Token *pToken;
1.386 - sqlite3 *db = pParse->db;
1.387 -
1.388 - if( pExpr==0 ) return;
1.389 - pToken = &pExpr->token;
1.390 - assert( pToken->n>=1 );
1.391 - assert( pToken->z!=0 );
1.392 - assert( pToken->z[0]!=0 );
1.393 - if( pToken->n==1 ){
1.394 - /* Wildcard of the form "?". Assign the next variable number */
1.395 - pExpr->iTable = ++pParse->nVar;
1.396 - }else if( pToken->z[0]=='?' ){
1.397 - /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
1.398 - ** use it as the variable number */
1.399 - int i;
1.400 - pExpr->iTable = i = atoi((char*)&pToken->z[1]);
1.401 - if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
1.402 - sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
1.403 - SQLITE_MAX_VARIABLE_NUMBER);
1.404 - }
1.405 - if( i>pParse->nVar ){
1.406 - pParse->nVar = i;
1.407 - }
1.408 - }else{
1.409 - /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
1.410 - ** number as the prior appearance of the same name, or if the name
1.411 - ** has never appeared before, reuse the same variable number
1.412 - */
1.413 - int i, n;
1.414 - n = pToken->n;
1.415 - for(i=0; i<pParse->nVarExpr; i++){
1.416 - Expr *pE;
1.417 - if( (pE = pParse->apVarExpr[i])!=0
1.418 - && pE->token.n==n
1.419 - && memcmp(pE->token.z, pToken->z, n)==0 ){
1.420 - pExpr->iTable = pE->iTable;
1.421 - break;
1.422 - }
1.423 - }
1.424 - if( i>=pParse->nVarExpr ){
1.425 - pExpr->iTable = ++pParse->nVar;
1.426 - if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
1.427 - pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
1.428 - pParse->apVarExpr =
1.429 - sqlite3DbReallocOrFree(
1.430 - db,
1.431 - pParse->apVarExpr,
1.432 - pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
1.433 - );
1.434 - }
1.435 - if( !db->mallocFailed ){
1.436 - assert( pParse->apVarExpr!=0 );
1.437 - pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
1.438 - }
1.439 - }
1.440 - }
1.441 - if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
1.442 - sqlite3ErrorMsg(pParse, "too many SQL variables");
1.443 - }
1.444 -}
1.445 -
1.446 -/*
1.447 -** Recursively delete an expression tree.
1.448 -*/
1.449 -void sqlite3ExprDelete(Expr *p){
1.450 - if( p==0 ) return;
1.451 - if( p->span.dyn ) sqlite3_free((char*)p->span.z);
1.452 - if( p->token.dyn ) sqlite3_free((char*)p->token.z);
1.453 - sqlite3ExprDelete(p->pLeft);
1.454 - sqlite3ExprDelete(p->pRight);
1.455 - sqlite3ExprListDelete(p->pList);
1.456 - sqlite3SelectDelete(p->pSelect);
1.457 - sqlite3_free(p);
1.458 -}
1.459 -
1.460 -/*
1.461 -** The Expr.token field might be a string literal that is quoted.
1.462 -** If so, remove the quotation marks.
1.463 -*/
1.464 -void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
1.465 - if( ExprHasAnyProperty(p, EP_Dequoted) ){
1.466 - return;
1.467 - }
1.468 - ExprSetProperty(p, EP_Dequoted);
1.469 - if( p->token.dyn==0 ){
1.470 - sqlite3TokenCopy(db, &p->token, &p->token);
1.471 - }
1.472 - sqlite3Dequote((char*)p->token.z);
1.473 -}
1.474 -
1.475 -
1.476 -/*
1.477 -** The following group of routines make deep copies of expressions,
1.478 -** expression lists, ID lists, and select statements. The copies can
1.479 -** be deleted (by being passed to their respective ...Delete() routines)
1.480 -** without effecting the originals.
1.481 -**
1.482 -** The expression list, ID, and source lists return by sqlite3ExprListDup(),
1.483 -** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
1.484 -** by subsequent calls to sqlite*ListAppend() routines.
1.485 -**
1.486 -** Any tables that the SrcList might point to are not duplicated.
1.487 -*/
1.488 -Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
1.489 - Expr *pNew;
1.490 - if( p==0 ) return 0;
1.491 - pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
1.492 - if( pNew==0 ) return 0;
1.493 - memcpy(pNew, p, sizeof(*pNew));
1.494 - if( p->token.z!=0 ){
1.495 - pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
1.496 - pNew->token.dyn = 1;
1.497 - }else{
1.498 - assert( pNew->token.z==0 );
1.499 - }
1.500 - pNew->span.z = 0;
1.501 - pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
1.502 - pNew->pRight = sqlite3ExprDup(db, p->pRight);
1.503 - pNew->pList = sqlite3ExprListDup(db, p->pList);
1.504 - pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
1.505 - return pNew;
1.506 -}
1.507 -void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
1.508 - if( pTo->dyn ) sqlite3_free((char*)pTo->z);
1.509 - if( pFrom->z ){
1.510 - pTo->n = pFrom->n;
1.511 - pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
1.512 - pTo->dyn = 1;
1.513 - }else{
1.514 - pTo->z = 0;
1.515 - }
1.516 -}
1.517 -ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
1.518 - ExprList *pNew;
1.519 - struct ExprList_item *pItem, *pOldItem;
1.520 - int i;
1.521 - if( p==0 ) return 0;
1.522 - pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
1.523 - if( pNew==0 ) return 0;
1.524 - pNew->iECursor = 0;
1.525 - pNew->nExpr = pNew->nAlloc = p->nExpr;
1.526 - pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
1.527 - if( pItem==0 ){
1.528 - sqlite3_free(pNew);
1.529 - return 0;
1.530 - }
1.531 - pOldItem = p->a;
1.532 - for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
1.533 - Expr *pNewExpr, *pOldExpr;
1.534 - pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
1.535 - if( pOldExpr->span.z!=0 && pNewExpr ){
1.536 - /* Always make a copy of the span for top-level expressions in the
1.537 - ** expression list. The logic in SELECT processing that determines
1.538 - ** the names of columns in the result set needs this information */
1.539 - sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
1.540 - }
1.541 - assert( pNewExpr==0 || pNewExpr->span.z!=0
1.542 - || pOldExpr->span.z==0
1.543 - || db->mallocFailed );
1.544 - pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
1.545 - pItem->sortOrder = pOldItem->sortOrder;
1.546 - pItem->isAgg = pOldItem->isAgg;
1.547 - pItem->done = 0;
1.548 - }
1.549 - return pNew;
1.550 -}
1.551 -
1.552 -/*
1.553 -** If cursors, triggers, views and subqueries are all omitted from
1.554 -** the build, then none of the following routines, except for
1.555 -** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
1.556 -** called with a NULL argument.
1.557 -*/
1.558 -#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
1.559 - || !defined(SQLITE_OMIT_SUBQUERY)
1.560 -SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
1.561 - SrcList *pNew;
1.562 - int i;
1.563 - int nByte;
1.564 - if( p==0 ) return 0;
1.565 - nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
1.566 - pNew = sqlite3DbMallocRaw(db, nByte );
1.567 - if( pNew==0 ) return 0;
1.568 - pNew->nSrc = pNew->nAlloc = p->nSrc;
1.569 - for(i=0; i<p->nSrc; i++){
1.570 - struct SrcList_item *pNewItem = &pNew->a[i];
1.571 - struct SrcList_item *pOldItem = &p->a[i];
1.572 - Table *pTab;
1.573 - pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
1.574 - pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
1.575 - pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
1.576 - pNewItem->jointype = pOldItem->jointype;
1.577 - pNewItem->iCursor = pOldItem->iCursor;
1.578 - pNewItem->isPopulated = pOldItem->isPopulated;
1.579 - pTab = pNewItem->pTab = pOldItem->pTab;
1.580 - if( pTab ){
1.581 - pTab->nRef++;
1.582 - }
1.583 - pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
1.584 - pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
1.585 - pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
1.586 - pNewItem->colUsed = pOldItem->colUsed;
1.587 - }
1.588 - return pNew;
1.589 -}
1.590 -IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
1.591 - IdList *pNew;
1.592 - int i;
1.593 - if( p==0 ) return 0;
1.594 - pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
1.595 - if( pNew==0 ) return 0;
1.596 - pNew->nId = pNew->nAlloc = p->nId;
1.597 - pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
1.598 - if( pNew->a==0 ){
1.599 - sqlite3_free(pNew);
1.600 - return 0;
1.601 - }
1.602 - for(i=0; i<p->nId; i++){
1.603 - struct IdList_item *pNewItem = &pNew->a[i];
1.604 - struct IdList_item *pOldItem = &p->a[i];
1.605 - pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
1.606 - pNewItem->idx = pOldItem->idx;
1.607 - }
1.608 - return pNew;
1.609 -}
1.610 -Select *sqlite3SelectDup(sqlite3 *db, Select *p){
1.611 - Select *pNew;
1.612 - if( p==0 ) return 0;
1.613 - pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
1.614 - if( pNew==0 ) return 0;
1.615 - pNew->isDistinct = p->isDistinct;
1.616 - pNew->pEList = sqlite3ExprListDup(db, p->pEList);
1.617 - pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
1.618 - pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
1.619 - pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
1.620 - pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
1.621 - pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
1.622 - pNew->op = p->op;
1.623 - pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
1.624 - pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
1.625 - pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
1.626 - pNew->iLimit = -1;
1.627 - pNew->iOffset = -1;
1.628 - pNew->isResolved = p->isResolved;
1.629 - pNew->isAgg = p->isAgg;
1.630 - pNew->usesEphm = 0;
1.631 - pNew->disallowOrderBy = 0;
1.632 - pNew->pRightmost = 0;
1.633 - pNew->addrOpenEphm[0] = -1;
1.634 - pNew->addrOpenEphm[1] = -1;
1.635 - pNew->addrOpenEphm[2] = -1;
1.636 - return pNew;
1.637 -}
1.638 -#else
1.639 -Select *sqlite3SelectDup(sqlite3 *db, Select *p){
1.640 - assert( p==0 );
1.641 - return 0;
1.642 -}
1.643 -#endif
1.644 -
1.645 -
1.646 -/*
1.647 -** Add a new element to the end of an expression list. If pList is
1.648 -** initially NULL, then create a new expression list.
1.649 -*/
1.650 -ExprList *sqlite3ExprListAppend(
1.651 - Parse *pParse, /* Parsing context */
1.652 - ExprList *pList, /* List to which to append. Might be NULL */
1.653 - Expr *pExpr, /* Expression to be appended */
1.654 - Token *pName /* AS keyword for the expression */
1.655 -){
1.656 - sqlite3 *db = pParse->db;
1.657 - if( pList==0 ){
1.658 - pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
1.659 - if( pList==0 ){
1.660 - goto no_mem;
1.661 - }
1.662 - assert( pList->nAlloc==0 );
1.663 - }
1.664 - if( pList->nAlloc<=pList->nExpr ){
1.665 - struct ExprList_item *a;
1.666 - int n = pList->nAlloc*2 + 4;
1.667 - a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
1.668 - if( a==0 ){
1.669 - goto no_mem;
1.670 - }
1.671 - pList->a = a;
1.672 - pList->nAlloc = n;
1.673 - }
1.674 - assert( pList->a!=0 );
1.675 - if( pExpr || pName ){
1.676 - struct ExprList_item *pItem = &pList->a[pList->nExpr++];
1.677 - memset(pItem, 0, sizeof(*pItem));
1.678 - pItem->zName = sqlite3NameFromToken(db, pName);
1.679 - pItem->pExpr = pExpr;
1.680 - }
1.681 - return pList;
1.682 -
1.683 -no_mem:
1.684 - /* Avoid leaking memory if malloc has failed. */
1.685 - sqlite3ExprDelete(pExpr);
1.686 - sqlite3ExprListDelete(pList);
1.687 - return 0;
1.688 -}
1.689 -
1.690 -/*
1.691 -** If the expression list pEList contains more than iLimit elements,
1.692 -** leave an error message in pParse.
1.693 -*/
1.694 -void sqlite3ExprListCheckLength(
1.695 - Parse *pParse,
1.696 - ExprList *pEList,
1.697 - int iLimit,
1.698 - const char *zObject
1.699 -){
1.700 - if( pEList && pEList->nExpr>iLimit ){
1.701 - sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
1.702 - }
1.703 -}
1.704 -
1.705 -
1.706 -#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
1.707 -/* The following three functions, heightOfExpr(), heightOfExprList()
1.708 -** and heightOfSelect(), are used to determine the maximum height
1.709 -** of any expression tree referenced by the structure passed as the
1.710 -** first argument.
1.711 -**
1.712 -** If this maximum height is greater than the current value pointed
1.713 -** to by pnHeight, the second parameter, then set *pnHeight to that
1.714 -** value.
1.715 -*/
1.716 -static void heightOfExpr(Expr *p, int *pnHeight){
1.717 - if( p ){
1.718 - if( p->nHeight>*pnHeight ){
1.719 - *pnHeight = p->nHeight;
1.720 - }
1.721 - }
1.722 -}
1.723 -static void heightOfExprList(ExprList *p, int *pnHeight){
1.724 - if( p ){
1.725 - int i;
1.726 - for(i=0; i<p->nExpr; i++){
1.727 - heightOfExpr(p->a[i].pExpr, pnHeight);
1.728 - }
1.729 - }
1.730 -}
1.731 -static void heightOfSelect(Select *p, int *pnHeight){
1.732 - if( p ){
1.733 - heightOfExpr(p->pWhere, pnHeight);
1.734 - heightOfExpr(p->pHaving, pnHeight);
1.735 - heightOfExpr(p->pLimit, pnHeight);
1.736 - heightOfExpr(p->pOffset, pnHeight);
1.737 - heightOfExprList(p->pEList, pnHeight);
1.738 - heightOfExprList(p->pGroupBy, pnHeight);
1.739 - heightOfExprList(p->pOrderBy, pnHeight);
1.740 - heightOfSelect(p->pPrior, pnHeight);
1.741 - }
1.742 -}
1.743 -
1.744 -/*
1.745 -** Set the Expr.nHeight variable in the structure passed as an
1.746 -** argument. An expression with no children, Expr.pList or
1.747 -** Expr.pSelect member has a height of 1. Any other expression
1.748 -** has a height equal to the maximum height of any other
1.749 -** referenced Expr plus one.
1.750 -*/
1.751 -void sqlite3ExprSetHeight(Expr *p){
1.752 - int nHeight = 0;
1.753 - heightOfExpr(p->pLeft, &nHeight);
1.754 - heightOfExpr(p->pRight, &nHeight);
1.755 - heightOfExprList(p->pList, &nHeight);
1.756 - heightOfSelect(p->pSelect, &nHeight);
1.757 - p->nHeight = nHeight + 1;
1.758 -}
1.759 -
1.760 -/*
1.761 -** Return the maximum height of any expression tree referenced
1.762 -** by the select statement passed as an argument.
1.763 -*/
1.764 -int sqlite3SelectExprHeight(Select *p){
1.765 - int nHeight = 0;
1.766 - heightOfSelect(p, &nHeight);
1.767 - return nHeight;
1.768 -}
1.769 -#endif
1.770 -
1.771 -/*
1.772 -** Delete an entire expression list.
1.773 -*/
1.774 -void sqlite3ExprListDelete(ExprList *pList){
1.775 - int i;
1.776 - struct ExprList_item *pItem;
1.777 - if( pList==0 ) return;
1.778 - assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
1.779 - assert( pList->nExpr<=pList->nAlloc );
1.780 - for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
1.781 - sqlite3ExprDelete(pItem->pExpr);
1.782 - sqlite3_free(pItem->zName);
1.783 - }
1.784 - sqlite3_free(pList->a);
1.785 - sqlite3_free(pList);
1.786 -}
1.787 -
1.788 -/*
1.789 -** Walk an expression tree. Call xFunc for each node visited.
1.790 -**
1.791 -** The return value from xFunc determines whether the tree walk continues.
1.792 -** 0 means continue walking the tree. 1 means do not walk children
1.793 -** of the current node but continue with siblings. 2 means abandon
1.794 -** the tree walk completely.
1.795 -**
1.796 -** The return value from this routine is 1 to abandon the tree walk
1.797 -** and 0 to continue.
1.798 -**
1.799 -** NOTICE: This routine does *not* descend into subqueries.
1.800 -*/
1.801 -static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
1.802 -static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
1.803 - int rc;
1.804 - if( pExpr==0 ) return 0;
1.805 - rc = (*xFunc)(pArg, pExpr);
1.806 - if( rc==0 ){
1.807 - if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
1.808 - if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
1.809 - if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
1.810 - }
1.811 - return rc>1;
1.812 -}
1.813 -
1.814 -/*
1.815 -** Call walkExprTree() for every expression in list p.
1.816 -*/
1.817 -static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
1.818 - int i;
1.819 - struct ExprList_item *pItem;
1.820 - if( !p ) return 0;
1.821 - for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
1.822 - if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
1.823 - }
1.824 - return 0;
1.825 -}
1.826 -
1.827 -/*
1.828 -** Call walkExprTree() for every expression in Select p, not including
1.829 -** expressions that are part of sub-selects in any FROM clause or the LIMIT
1.830 -** or OFFSET expressions..
1.831 -*/
1.832 -static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
1.833 - walkExprList(p->pEList, xFunc, pArg);
1.834 - walkExprTree(p->pWhere, xFunc, pArg);
1.835 - walkExprList(p->pGroupBy, xFunc, pArg);
1.836 - walkExprTree(p->pHaving, xFunc, pArg);
1.837 - walkExprList(p->pOrderBy, xFunc, pArg);
1.838 - if( p->pPrior ){
1.839 - walkSelectExpr(p->pPrior, xFunc, pArg);
1.840 - }
1.841 - return 0;
1.842 -}
1.843 -
1.844 -
1.845 -/*
1.846 -** This routine is designed as an xFunc for walkExprTree().
1.847 -**
1.848 -** pArg is really a pointer to an integer. If we can tell by looking
1.849 -** at pExpr that the expression that contains pExpr is not a constant
1.850 -** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
1.851 -** If pExpr does does not disqualify the expression from being a constant
1.852 -** then do nothing.
1.853 -**
1.854 -** After walking the whole tree, if no nodes are found that disqualify
1.855 -** the expression as constant, then we assume the whole expression
1.856 -** is constant. See sqlite3ExprIsConstant() for additional information.
1.857 -*/
1.858 -static int exprNodeIsConstant(void *pArg, Expr *pExpr){
1.859 - int *pN = (int*)pArg;
1.860 -
1.861 - /* If *pArg is 3 then any term of the expression that comes from
1.862 - ** the ON or USING clauses of a join disqualifies the expression
1.863 - ** from being considered constant. */
1.864 - if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
1.865 - *pN = 0;
1.866 - return 2;
1.867 - }
1.868 -
1.869 - switch( pExpr->op ){
1.870 - /* Consider functions to be constant if all their arguments are constant
1.871 - ** and *pArg==2 */
1.872 - case TK_FUNCTION:
1.873 - if( (*pN)==2 ) return 0;
1.874 - /* Fall through */
1.875 - case TK_ID:
1.876 - case TK_COLUMN:
1.877 - case TK_DOT:
1.878 - case TK_AGG_FUNCTION:
1.879 - case TK_AGG_COLUMN:
1.880 -#ifndef SQLITE_OMIT_SUBQUERY
1.881 - case TK_SELECT:
1.882 - case TK_EXISTS:
1.883 -#endif
1.884 - *pN = 0;
1.885 - return 2;
1.886 - case TK_IN:
1.887 - if( pExpr->pSelect ){
1.888 - *pN = 0;
1.889 - return 2;
1.890 - }
1.891 - default:
1.892 - return 0;
1.893 - }
1.894 -}
1.895 -
1.896 -/*
1.897 -** Walk an expression tree. Return 1 if the expression is constant
1.898 -** and 0 if it involves variables or function calls.
1.899 -**
1.900 -** For the purposes of this function, a double-quoted string (ex: "abc")
1.901 -** is considered a variable but a single-quoted string (ex: 'abc') is
1.902 -** a constant.
1.903 -*/
1.904 -int sqlite3ExprIsConstant(Expr *p){
1.905 - int isConst = 1;
1.906 - walkExprTree(p, exprNodeIsConstant, &isConst);
1.907 - return isConst;
1.908 -}
1.909 -
1.910 -/*
1.911 -** Walk an expression tree. Return 1 if the expression is constant
1.912 -** that does no originate from the ON or USING clauses of a join.
1.913 -** Return 0 if it involves variables or function calls or terms from
1.914 -** an ON or USING clause.
1.915 -*/
1.916 -int sqlite3ExprIsConstantNotJoin(Expr *p){
1.917 - int isConst = 3;
1.918 - walkExprTree(p, exprNodeIsConstant, &isConst);
1.919 - return isConst!=0;
1.920 -}
1.921 -
1.922 -/*
1.923 -** Walk an expression tree. Return 1 if the expression is constant
1.924 -** or a function call with constant arguments. Return and 0 if there
1.925 -** are any variables.
1.926 -**
1.927 -** For the purposes of this function, a double-quoted string (ex: "abc")
1.928 -** is considered a variable but a single-quoted string (ex: 'abc') is
1.929 -** a constant.
1.930 -*/
1.931 -int sqlite3ExprIsConstantOrFunction(Expr *p){
1.932 - int isConst = 2;
1.933 - walkExprTree(p, exprNodeIsConstant, &isConst);
1.934 - return isConst!=0;
1.935 -}
1.936 -
1.937 -/*
1.938 -** If the expression p codes a constant integer that is small enough
1.939 -** to fit in a 32-bit integer, return 1 and put the value of the integer
1.940 -** in *pValue. If the expression is not an integer or if it is too big
1.941 -** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
1.942 -*/
1.943 -int sqlite3ExprIsInteger(Expr *p, int *pValue){
1.944 - switch( p->op ){
1.945 - case TK_INTEGER: {
1.946 - if( sqlite3GetInt32((char*)p->token.z, pValue) ){
1.947 - return 1;
1.948 - }
1.949 - break;
1.950 - }
1.951 - case TK_UPLUS: {
1.952 - return sqlite3ExprIsInteger(p->pLeft, pValue);
1.953 - }
1.954 - case TK_UMINUS: {
1.955 - int v;
1.956 - if( sqlite3ExprIsInteger(p->pLeft, &v) ){
1.957 - *pValue = -v;
1.958 - return 1;
1.959 - }
1.960 - break;
1.961 - }
1.962 - default: break;
1.963 - }
1.964 - return 0;
1.965 -}
1.966 -
1.967 -/*
1.968 -** Return TRUE if the given string is a row-id column name.
1.969 -*/
1.970 -int sqlite3IsRowid(const char *z){
1.971 - if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1.972 - if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1.973 - if( sqlite3StrICmp(z, "OID")==0 ) return 1;
1.974 - return 0;
1.975 -}
1.976 -
1.977 -/*
1.978 -** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
1.979 -** that name in the set of source tables in pSrcList and make the pExpr
1.980 -** expression node refer back to that source column. The following changes
1.981 -** are made to pExpr:
1.982 -**
1.983 -** pExpr->iDb Set the index in db->aDb[] of the database holding
1.984 -** the table.
1.985 -** pExpr->iTable Set to the cursor number for the table obtained
1.986 -** from pSrcList.
1.987 -** pExpr->iColumn Set to the column number within the table.
1.988 -** pExpr->op Set to TK_COLUMN.
1.989 -** pExpr->pLeft Any expression this points to is deleted
1.990 -** pExpr->pRight Any expression this points to is deleted.
1.991 -**
1.992 -** The pDbToken is the name of the database (the "X"). This value may be
1.993 -** NULL meaning that name is of the form Y.Z or Z. Any available database
1.994 -** can be used. The pTableToken is the name of the table (the "Y"). This
1.995 -** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1.996 -** means that the form of the name is Z and that columns from any table
1.997 -** can be used.
1.998 -**
1.999 -** If the name cannot be resolved unambiguously, leave an error message
1.1000 -** in pParse and return non-zero. Return zero on success.
1.1001 -*/
1.1002 -static int lookupName(
1.1003 - Parse *pParse, /* The parsing context */
1.1004 - Token *pDbToken, /* Name of the database containing table, or NULL */
1.1005 - Token *pTableToken, /* Name of table containing column, or NULL */
1.1006 - Token *pColumnToken, /* Name of the column. */
1.1007 - NameContext *pNC, /* The name context used to resolve the name */
1.1008 - Expr *pExpr /* Make this EXPR node point to the selected column */
1.1009 -){
1.1010 - char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1.1011 - char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1.1012 - char *zCol = 0; /* Name of the column. The "Z" */
1.1013 - int i, j; /* Loop counters */
1.1014 - int cnt = 0; /* Number of matching column names */
1.1015 - int cntTab = 0; /* Number of matching table names */
1.1016 - sqlite3 *db = pParse->db; /* The database */
1.1017 - struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1.1018 - struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
1.1019 - NameContext *pTopNC = pNC; /* First namecontext in the list */
1.1020 - Schema *pSchema = 0; /* Schema of the expression */
1.1021 -
1.1022 - assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
1.1023 - zDb = sqlite3NameFromToken(db, pDbToken);
1.1024 - zTab = sqlite3NameFromToken(db, pTableToken);
1.1025 - zCol = sqlite3NameFromToken(db, pColumnToken);
1.1026 - if( db->mallocFailed ){
1.1027 - goto lookupname_end;
1.1028 - }
1.1029 -
1.1030 - pExpr->iTable = -1;
1.1031 - while( pNC && cnt==0 ){
1.1032 - ExprList *pEList;
1.1033 - SrcList *pSrcList = pNC->pSrcList;
1.1034 -
1.1035 - if( pSrcList ){
1.1036 - for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
1.1037 - Table *pTab;
1.1038 - int iDb;
1.1039 - Column *pCol;
1.1040 -
1.1041 - pTab = pItem->pTab;
1.1042 - assert( pTab!=0 );
1.1043 - iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1.1044 - assert( pTab->nCol>0 );
1.1045 - if( zTab ){
1.1046 - if( pItem->zAlias ){
1.1047 - char *zTabName = pItem->zAlias;
1.1048 - if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1.1049 - }else{
1.1050 - char *zTabName = pTab->zName;
1.1051 - if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1.1052 - if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
1.1053 - continue;
1.1054 - }
1.1055 - }
1.1056 - }
1.1057 - if( 0==(cntTab++) ){
1.1058 - pExpr->iTable = pItem->iCursor;
1.1059 - pSchema = pTab->pSchema;
1.1060 - pMatch = pItem;
1.1061 - }
1.1062 - for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1.1063 - if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
1.1064 - const char *zColl = pTab->aCol[j].zColl;
1.1065 - IdList *pUsing;
1.1066 - cnt++;
1.1067 - pExpr->iTable = pItem->iCursor;
1.1068 - pMatch = pItem;
1.1069 - pSchema = pTab->pSchema;
1.1070 - /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1.1071 - pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1.1072 - pExpr->affinity = pTab->aCol[j].affinity;
1.1073 - if( (pExpr->flags & EP_ExpCollate)==0 ){
1.1074 - pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1.1075 - }
1.1076 - if( i<pSrcList->nSrc-1 ){
1.1077 - if( pItem[1].jointype & JT_NATURAL ){
1.1078 - /* If this match occurred in the left table of a natural join,
1.1079 - ** then skip the right table to avoid a duplicate match */
1.1080 - pItem++;
1.1081 - i++;
1.1082 - }else if( (pUsing = pItem[1].pUsing)!=0 ){
1.1083 - /* If this match occurs on a column that is in the USING clause
1.1084 - ** of a join, skip the search of the right table of the join
1.1085 - ** to avoid a duplicate match there. */
1.1086 - int k;
1.1087 - for(k=0; k<pUsing->nId; k++){
1.1088 - if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1.1089 - pItem++;
1.1090 - i++;
1.1091 - break;
1.1092 - }
1.1093 - }
1.1094 - }
1.1095 - }
1.1096 - break;
1.1097 - }
1.1098 - }
1.1099 - }
1.1100 - }
1.1101 -
1.1102 -#ifndef SQLITE_OMIT_TRIGGER
1.1103 - /* If we have not already resolved the name, then maybe
1.1104 - ** it is a new.* or old.* trigger argument reference
1.1105 - */
1.1106 - if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1.1107 - TriggerStack *pTriggerStack = pParse->trigStack;
1.1108 - Table *pTab = 0;
1.1109 - if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1.1110 - pExpr->iTable = pTriggerStack->newIdx;
1.1111 - assert( pTriggerStack->pTab );
1.1112 - pTab = pTriggerStack->pTab;
1.1113 - }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1.1114 - pExpr->iTable = pTriggerStack->oldIdx;
1.1115 - assert( pTriggerStack->pTab );
1.1116 - pTab = pTriggerStack->pTab;
1.1117 - }
1.1118 -
1.1119 - if( pTab ){
1.1120 - int iCol;
1.1121 - Column *pCol = pTab->aCol;
1.1122 -
1.1123 - pSchema = pTab->pSchema;
1.1124 - cntTab++;
1.1125 - for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
1.1126 - if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
1.1127 - const char *zColl = pTab->aCol[iCol].zColl;
1.1128 - cnt++;
1.1129 - pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1.1130 - pExpr->affinity = pTab->aCol[iCol].affinity;
1.1131 - if( (pExpr->flags & EP_ExpCollate)==0 ){
1.1132 - pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1.1133 - }
1.1134 - pExpr->pTab = pTab;
1.1135 - break;
1.1136 - }
1.1137 - }
1.1138 - }
1.1139 - }
1.1140 -#endif /* !defined(SQLITE_OMIT_TRIGGER) */
1.1141 -
1.1142 - /*
1.1143 - ** Perhaps the name is a reference to the ROWID
1.1144 - */
1.1145 - if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1.1146 - cnt = 1;
1.1147 - pExpr->iColumn = -1;
1.1148 - pExpr->affinity = SQLITE_AFF_INTEGER;
1.1149 - }
1.1150 -
1.1151 - /*
1.1152 - ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1.1153 - ** might refer to an result-set alias. This happens, for example, when
1.1154 - ** we are resolving names in the WHERE clause of the following command:
1.1155 - **
1.1156 - ** SELECT a+b AS x FROM table WHERE x<10;
1.1157 - **
1.1158 - ** In cases like this, replace pExpr with a copy of the expression that
1.1159 - ** forms the result set entry ("a+b" in the example) and return immediately.
1.1160 - ** Note that the expression in the result set should have already been
1.1161 - ** resolved by the time the WHERE clause is resolved.
1.1162 - */
1.1163 - if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
1.1164 - for(j=0; j<pEList->nExpr; j++){
1.1165 - char *zAs = pEList->a[j].zName;
1.1166 - if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1.1167 - Expr *pDup, *pOrig;
1.1168 - assert( pExpr->pLeft==0 && pExpr->pRight==0 );
1.1169 - assert( pExpr->pList==0 );
1.1170 - assert( pExpr->pSelect==0 );
1.1171 - pOrig = pEList->a[j].pExpr;
1.1172 - if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1.1173 - sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
1.1174 - sqlite3_free(zCol);
1.1175 - return 2;
1.1176 - }
1.1177 - pDup = sqlite3ExprDup(db, pOrig);
1.1178 - if( pExpr->flags & EP_ExpCollate ){
1.1179 - pDup->pColl = pExpr->pColl;
1.1180 - pDup->flags |= EP_ExpCollate;
1.1181 - }
1.1182 - if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1.1183 - if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
1.1184 - memcpy(pExpr, pDup, sizeof(*pExpr));
1.1185 - sqlite3_free(pDup);
1.1186 - cnt = 1;
1.1187 - pMatch = 0;
1.1188 - assert( zTab==0 && zDb==0 );
1.1189 - goto lookupname_end_2;
1.1190 - }
1.1191 - }
1.1192 - }
1.1193 -
1.1194 - /* Advance to the next name context. The loop will exit when either
1.1195 - ** we have a match (cnt>0) or when we run out of name contexts.
1.1196 - */
1.1197 - if( cnt==0 ){
1.1198 - pNC = pNC->pNext;
1.1199 - }
1.1200 - }
1.1201 -
1.1202 - /*
1.1203 - ** If X and Y are NULL (in other words if only the column name Z is
1.1204 - ** supplied) and the value of Z is enclosed in double-quotes, then
1.1205 - ** Z is a string literal if it doesn't match any column names. In that
1.1206 - ** case, we need to return right away and not make any changes to
1.1207 - ** pExpr.
1.1208 - **
1.1209 - ** Because no reference was made to outer contexts, the pNC->nRef
1.1210 - ** fields are not changed in any context.
1.1211 - */
1.1212 - if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1.1213 - sqlite3_free(zCol);
1.1214 - return 0;
1.1215 - }
1.1216 -
1.1217 - /*
1.1218 - ** cnt==0 means there was not match. cnt>1 means there were two or
1.1219 - ** more matches. Either way, we have an error.
1.1220 - */
1.1221 - if( cnt!=1 ){
1.1222 - char *z = 0;
1.1223 - char *zErr;
1.1224 - zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1.1225 - if( zDb ){
1.1226 - sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
1.1227 - }else if( zTab ){
1.1228 - sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
1.1229 - }else{
1.1230 - z = sqlite3StrDup(zCol);
1.1231 - }
1.1232 - if( z ){
1.1233 - sqlite3ErrorMsg(pParse, zErr, z);
1.1234 - sqlite3_free(z);
1.1235 - pTopNC->nErr++;
1.1236 - }else{
1.1237 - db->mallocFailed = 1;
1.1238 - }
1.1239 - }
1.1240 -
1.1241 - /* If a column from a table in pSrcList is referenced, then record
1.1242 - ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1.1243 - ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1.1244 - ** column number is greater than the number of bits in the bitmask
1.1245 - ** then set the high-order bit of the bitmask.
1.1246 - */
1.1247 - if( pExpr->iColumn>=0 && pMatch!=0 ){
1.1248 - int n = pExpr->iColumn;
1.1249 - if( n>=sizeof(Bitmask)*8 ){
1.1250 - n = sizeof(Bitmask)*8-1;
1.1251 - }
1.1252 - assert( pMatch->iCursor==pExpr->iTable );
1.1253 - pMatch->colUsed |= ((Bitmask)1)<<n;
1.1254 - }
1.1255 -
1.1256 -lookupname_end:
1.1257 - /* Clean up and return
1.1258 - */
1.1259 - sqlite3_free(zDb);
1.1260 - sqlite3_free(zTab);
1.1261 - sqlite3ExprDelete(pExpr->pLeft);
1.1262 - pExpr->pLeft = 0;
1.1263 - sqlite3ExprDelete(pExpr->pRight);
1.1264 - pExpr->pRight = 0;
1.1265 - pExpr->op = TK_COLUMN;
1.1266 -lookupname_end_2:
1.1267 - sqlite3_free(zCol);
1.1268 - if( cnt==1 ){
1.1269 - assert( pNC!=0 );
1.1270 - sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
1.1271 - if( pMatch && !pMatch->pSelect ){
1.1272 - pExpr->pTab = pMatch->pTab;
1.1273 - }
1.1274 - /* Increment the nRef value on all name contexts from TopNC up to
1.1275 - ** the point where the name matched. */
1.1276 - for(;;){
1.1277 - assert( pTopNC!=0 );
1.1278 - pTopNC->nRef++;
1.1279 - if( pTopNC==pNC ) break;
1.1280 - pTopNC = pTopNC->pNext;
1.1281 - }
1.1282 - return 0;
1.1283 - } else {
1.1284 - return 1;
1.1285 - }
1.1286 -}
1.1287 -
1.1288 -/*
1.1289 -** This routine is designed as an xFunc for walkExprTree().
1.1290 -**
1.1291 -** Resolve symbolic names into TK_COLUMN operators for the current
1.1292 -** node in the expression tree. Return 0 to continue the search down
1.1293 -** the tree or 2 to abort the tree walk.
1.1294 -**
1.1295 -** This routine also does error checking and name resolution for
1.1296 -** function names. The operator for aggregate functions is changed
1.1297 -** to TK_AGG_FUNCTION.
1.1298 -*/
1.1299 -static int nameResolverStep(void *pArg, Expr *pExpr){
1.1300 - NameContext *pNC = (NameContext*)pArg;
1.1301 - Parse *pParse;
1.1302 -
1.1303 - if( pExpr==0 ) return 1;
1.1304 - assert( pNC!=0 );
1.1305 - pParse = pNC->pParse;
1.1306 -
1.1307 - if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1.1308 - ExprSetProperty(pExpr, EP_Resolved);
1.1309 -#ifndef NDEBUG
1.1310 - if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1.1311 - SrcList *pSrcList = pNC->pSrcList;
1.1312 - int i;
1.1313 - for(i=0; i<pNC->pSrcList->nSrc; i++){
1.1314 - assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1.1315 - }
1.1316 - }
1.1317 -#endif
1.1318 - switch( pExpr->op ){
1.1319 - /* Double-quoted strings (ex: "abc") are used as identifiers if
1.1320 - ** possible. Otherwise they remain as strings. Single-quoted
1.1321 - ** strings (ex: 'abc') are always string literals.
1.1322 - */
1.1323 - case TK_STRING: {
1.1324 - if( pExpr->token.z[0]=='\'' ) break;
1.1325 - /* Fall thru into the TK_ID case if this is a double-quoted string */
1.1326 - }
1.1327 - /* A lone identifier is the name of a column.
1.1328 - */
1.1329 - case TK_ID: {
1.1330 - lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1.1331 - return 1;
1.1332 - }
1.1333 -
1.1334 - /* A table name and column name: ID.ID
1.1335 - ** Or a database, table and column: ID.ID.ID
1.1336 - */
1.1337 - case TK_DOT: {
1.1338 - Token *pColumn;
1.1339 - Token *pTable;
1.1340 - Token *pDb;
1.1341 - Expr *pRight;
1.1342 -
1.1343 - /* if( pSrcList==0 ) break; */
1.1344 - pRight = pExpr->pRight;
1.1345 - if( pRight->op==TK_ID ){
1.1346 - pDb = 0;
1.1347 - pTable = &pExpr->pLeft->token;
1.1348 - pColumn = &pRight->token;
1.1349 - }else{
1.1350 - assert( pRight->op==TK_DOT );
1.1351 - pDb = &pExpr->pLeft->token;
1.1352 - pTable = &pRight->pLeft->token;
1.1353 - pColumn = &pRight->pRight->token;
1.1354 - }
1.1355 - lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1.1356 - return 1;
1.1357 - }
1.1358 -
1.1359 - /* Resolve function names
1.1360 - */
1.1361 - case TK_CONST_FUNC:
1.1362 - case TK_FUNCTION: {
1.1363 - ExprList *pList = pExpr->pList; /* The argument list */
1.1364 - int n = pList ? pList->nExpr : 0; /* Number of arguments */
1.1365 - int no_such_func = 0; /* True if no such function exists */
1.1366 - int wrong_num_args = 0; /* True if wrong number of arguments */
1.1367 - int is_agg = 0; /* True if is an aggregate function */
1.1368 - int i;
1.1369 - int auth; /* Authorization to use the function */
1.1370 - int nId; /* Number of characters in function name */
1.1371 - const char *zId; /* The function name. */
1.1372 - FuncDef *pDef; /* Information about the function */
1.1373 - int enc = ENC(pParse->db); /* The database encoding */
1.1374 -
1.1375 - zId = (char*)pExpr->token.z;
1.1376 - nId = pExpr->token.n;
1.1377 - pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1.1378 - if( pDef==0 ){
1.1379 - pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1.1380 - if( pDef==0 ){
1.1381 - no_such_func = 1;
1.1382 - }else{
1.1383 - wrong_num_args = 1;
1.1384 - }
1.1385 - }else{
1.1386 - is_agg = pDef->xFunc==0;
1.1387 - }
1.1388 -#ifndef SQLITE_OMIT_AUTHORIZATION
1.1389 - if( pDef ){
1.1390 - auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1.1391 - if( auth!=SQLITE_OK ){
1.1392 - if( auth==SQLITE_DENY ){
1.1393 - sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1.1394 - pDef->zName);
1.1395 - pNC->nErr++;
1.1396 - }
1.1397 - pExpr->op = TK_NULL;
1.1398 - return 1;
1.1399 - }
1.1400 - }
1.1401 -#endif
1.1402 - if( is_agg && !pNC->allowAgg ){
1.1403 - sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1.1404 - pNC->nErr++;
1.1405 - is_agg = 0;
1.1406 - }else if( no_such_func ){
1.1407 - sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1.1408 - pNC->nErr++;
1.1409 - }else if( wrong_num_args ){
1.1410 - sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1.1411 - nId, zId);
1.1412 - pNC->nErr++;
1.1413 - }
1.1414 - if( is_agg ){
1.1415 - pExpr->op = TK_AGG_FUNCTION;
1.1416 - pNC->hasAgg = 1;
1.1417 - }
1.1418 - if( is_agg ) pNC->allowAgg = 0;
1.1419 - for(i=0; pNC->nErr==0 && i<n; i++){
1.1420 - walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
1.1421 - }
1.1422 - if( is_agg ) pNC->allowAgg = 1;
1.1423 - /* FIX ME: Compute pExpr->affinity based on the expected return
1.1424 - ** type of the function
1.1425 - */
1.1426 - return is_agg;
1.1427 - }
1.1428 -#ifndef SQLITE_OMIT_SUBQUERY
1.1429 - case TK_SELECT:
1.1430 - case TK_EXISTS:
1.1431 -#endif
1.1432 - case TK_IN: {
1.1433 - if( pExpr->pSelect ){
1.1434 - int nRef = pNC->nRef;
1.1435 -#ifndef SQLITE_OMIT_CHECK
1.1436 - if( pNC->isCheck ){
1.1437 - sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1.1438 - }
1.1439 -#endif
1.1440 - sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1.1441 - assert( pNC->nRef>=nRef );
1.1442 - if( nRef!=pNC->nRef ){
1.1443 - ExprSetProperty(pExpr, EP_VarSelect);
1.1444 - }
1.1445 - }
1.1446 - break;
1.1447 - }
1.1448 -#ifndef SQLITE_OMIT_CHECK
1.1449 - case TK_VARIABLE: {
1.1450 - if( pNC->isCheck ){
1.1451 - sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1.1452 - }
1.1453 - break;
1.1454 - }
1.1455 -#endif
1.1456 - }
1.1457 - return 0;
1.1458 -}
1.1459 -
1.1460 -/*
1.1461 -** This routine walks an expression tree and resolves references to
1.1462 -** table columns. Nodes of the form ID.ID or ID resolve into an
1.1463 -** index to the table in the table list and a column offset. The
1.1464 -** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1.1465 -** value is changed to the index of the referenced table in pTabList
1.1466 -** plus the "base" value. The base value will ultimately become the
1.1467 -** VDBE cursor number for a cursor that is pointing into the referenced
1.1468 -** table. The Expr.iColumn value is changed to the index of the column
1.1469 -** of the referenced table. The Expr.iColumn value for the special
1.1470 -** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1.1471 -** alias for ROWID.
1.1472 -**
1.1473 -** Also resolve function names and check the functions for proper
1.1474 -** usage. Make sure all function names are recognized and all functions
1.1475 -** have the correct number of arguments. Leave an error message
1.1476 -** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1.1477 -**
1.1478 -** If the expression contains aggregate functions then set the EP_Agg
1.1479 -** property on the expression.
1.1480 -*/
1.1481 -int sqlite3ExprResolveNames(
1.1482 - NameContext *pNC, /* Namespace to resolve expressions in. */
1.1483 - Expr *pExpr /* The expression to be analyzed. */
1.1484 -){
1.1485 - int savedHasAgg;
1.1486 - if( pExpr==0 ) return 0;
1.1487 -#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
1.1488 - if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1.1489 - sqlite3ErrorMsg(pNC->pParse,
1.1490 - "Expression tree is too large (maximum depth %d)",
1.1491 - SQLITE_MAX_EXPR_DEPTH
1.1492 - );
1.1493 - return 1;
1.1494 - }
1.1495 - pNC->pParse->nHeight += pExpr->nHeight;
1.1496 -#endif
1.1497 - savedHasAgg = pNC->hasAgg;
1.1498 - pNC->hasAgg = 0;
1.1499 - walkExprTree(pExpr, nameResolverStep, pNC);
1.1500 -#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
1.1501 - pNC->pParse->nHeight -= pExpr->nHeight;
1.1502 -#endif
1.1503 - if( pNC->nErr>0 ){
1.1504 - ExprSetProperty(pExpr, EP_Error);
1.1505 - }
1.1506 - if( pNC->hasAgg ){
1.1507 - ExprSetProperty(pExpr, EP_Agg);
1.1508 - }else if( savedHasAgg ){
1.1509 - pNC->hasAgg = 1;
1.1510 - }
1.1511 - return ExprHasProperty(pExpr, EP_Error);
1.1512 -}
1.1513 -
1.1514 -/*
1.1515 -** A pointer instance of this structure is used to pass information
1.1516 -** through walkExprTree into codeSubqueryStep().
1.1517 -*/
1.1518 -typedef struct QueryCoder QueryCoder;
1.1519 -struct QueryCoder {
1.1520 - Parse *pParse; /* The parsing context */
1.1521 - NameContext *pNC; /* Namespace of first enclosing query */
1.1522 -};
1.1523 -
1.1524 -
1.1525 -/*
1.1526 -** Generate code for scalar subqueries used as an expression
1.1527 -** and IN operators. Examples:
1.1528 -**
1.1529 -** (SELECT a FROM b) -- subquery
1.1530 -** EXISTS (SELECT a FROM b) -- EXISTS subquery
1.1531 -** x IN (4,5,11) -- IN operator with list on right-hand side
1.1532 -** x IN (SELECT a FROM b) -- IN operator with subquery on the right
1.1533 -**
1.1534 -** The pExpr parameter describes the expression that contains the IN
1.1535 -** operator or subquery.
1.1536 -*/
1.1537 -#ifndef SQLITE_OMIT_SUBQUERY
1.1538 -void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
1.1539 - int testAddr = 0; /* One-time test address */
1.1540 - Vdbe *v = sqlite3GetVdbe(pParse);
1.1541 - if( v==0 ) return;
1.1542 -
1.1543 -
1.1544 - /* This code must be run in its entirety every time it is encountered
1.1545 - ** if any of the following is true:
1.1546 - **
1.1547 - ** * The right-hand side is a correlated subquery
1.1548 - ** * The right-hand side is an expression list containing variables
1.1549 - ** * We are inside a trigger
1.1550 - **
1.1551 - ** If all of the above are false, then we can run this code just once
1.1552 - ** save the results, and reuse the same result on subsequent invocations.
1.1553 - */
1.1554 - if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1.1555 - int mem = pParse->nMem++;
1.1556 - sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
1.1557 - testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
1.1558 - assert( testAddr>0 || pParse->db->mallocFailed );
1.1559 - sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
1.1560 - }
1.1561 -
1.1562 - switch( pExpr->op ){
1.1563 - case TK_IN: {
1.1564 - char affinity;
1.1565 - KeyInfo keyInfo;
1.1566 - int addr; /* Address of OP_OpenEphemeral instruction */
1.1567 -
1.1568 - affinity = sqlite3ExprAffinity(pExpr->pLeft);
1.1569 -
1.1570 - /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1.1571 - ** expression it is handled the same way. A virtual table is
1.1572 - ** filled with single-field index keys representing the results
1.1573 - ** from the SELECT or the <exprlist>.
1.1574 - **
1.1575 - ** If the 'x' expression is a column value, or the SELECT...
1.1576 - ** statement returns a column value, then the affinity of that
1.1577 - ** column is used to build the index keys. If both 'x' and the
1.1578 - ** SELECT... statement are columns, then numeric affinity is used
1.1579 - ** if either column has NUMERIC or INTEGER affinity. If neither
1.1580 - ** 'x' nor the SELECT... statement are columns, then numeric affinity
1.1581 - ** is used.
1.1582 - */
1.1583 - pExpr->iTable = pParse->nTab++;
1.1584 - addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
1.1585 - memset(&keyInfo, 0, sizeof(keyInfo));
1.1586 - keyInfo.nField = 1;
1.1587 - sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
1.1588 -
1.1589 - if( pExpr->pSelect ){
1.1590 - /* Case 1: expr IN (SELECT ...)
1.1591 - **
1.1592 - ** Generate code to write the results of the select into the temporary
1.1593 - ** table allocated and opened above.
1.1594 - */
1.1595 - int iParm = pExpr->iTable + (((int)affinity)<<16);
1.1596 - ExprList *pEList;
1.1597 - assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
1.1598 - if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1.1599 - return;
1.1600 - }
1.1601 - pEList = pExpr->pSelect->pEList;
1.1602 - if( pEList && pEList->nExpr>0 ){
1.1603 - keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
1.1604 - pEList->a[0].pExpr);
1.1605 - }
1.1606 - }else if( pExpr->pList ){
1.1607 - /* Case 2: expr IN (exprlist)
1.1608 - **
1.1609 - ** For each expression, build an index key from the evaluation and
1.1610 - ** store it in the temporary table. If <expr> is a column, then use
1.1611 - ** that columns affinity when building index keys. If <expr> is not
1.1612 - ** a column, use numeric affinity.
1.1613 - */
1.1614 - int i;
1.1615 - ExprList *pList = pExpr->pList;
1.1616 - struct ExprList_item *pItem;
1.1617 -
1.1618 - if( !affinity ){
1.1619 - affinity = SQLITE_AFF_NONE;
1.1620 - }
1.1621 - keyInfo.aColl[0] = pExpr->pLeft->pColl;
1.1622 -
1.1623 - /* Loop through each expression in <exprlist>. */
1.1624 - for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1.1625 - Expr *pE2 = pItem->pExpr;
1.1626 -
1.1627 - /* If the expression is not constant then we will need to
1.1628 - ** disable the test that was generated above that makes sure
1.1629 - ** this code only executes once. Because for a non-constant
1.1630 - ** expression we need to rerun this code each time.
1.1631 - */
1.1632 - if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
1.1633 - sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
1.1634 - testAddr = 0;
1.1635 - }
1.1636 -
1.1637 - /* Evaluate the expression and insert it into the temp table */
1.1638 - sqlite3ExprCode(pParse, pE2);
1.1639 - sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
1.1640 - sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
1.1641 - }
1.1642 - }
1.1643 - sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
1.1644 - break;
1.1645 - }
1.1646 -
1.1647 - case TK_EXISTS:
1.1648 - case TK_SELECT: {
1.1649 - /* This has to be a scalar SELECT. Generate code to put the
1.1650 - ** value of this select in a memory cell and record the number
1.1651 - ** of the memory cell in iColumn.
1.1652 - */
1.1653 - static const Token one = { (u8*)"1", 0, 1 };
1.1654 - Select *pSel;
1.1655 - int iMem;
1.1656 - int sop;
1.1657 -
1.1658 - pExpr->iColumn = iMem = pParse->nMem++;
1.1659 - pSel = pExpr->pSelect;
1.1660 - if( pExpr->op==TK_SELECT ){
1.1661 - sop = SRT_Mem;
1.1662 - sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1.1663 - VdbeComment((v, "# Init subquery result"));
1.1664 - }else{
1.1665 - sop = SRT_Exists;
1.1666 - sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1.1667 - VdbeComment((v, "# Init EXISTS result"));
1.1668 - }
1.1669 - sqlite3ExprDelete(pSel->pLimit);
1.1670 - pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
1.1671 - if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1.1672 - return;
1.1673 - }
1.1674 - break;
1.1675 - }
1.1676 - }
1.1677 -
1.1678 - if( testAddr ){
1.1679 - sqlite3VdbeJumpHere(v, testAddr);
1.1680 - }
1.1681 -
1.1682 - return;
1.1683 -}
1.1684 -#endif /* SQLITE_OMIT_SUBQUERY */
1.1685 -
1.1686 -/*
1.1687 -** Generate an instruction that will put the integer describe by
1.1688 -** text z[0..n-1] on the stack.
1.1689 -*/
1.1690 -static void codeInteger(Vdbe *v, const char *z, int n){
1.1691 - assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1.1692 - if( z ){
1.1693 - int i;
1.1694 - if( sqlite3GetInt32(z, &i) ){
1.1695 - sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1.1696 - }else if( sqlite3FitsIn64Bits(z) ){
1.1697 - sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
1.1698 - }else{
1.1699 - sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1.1700 - }
1.1701 - }
1.1702 -}
1.1703 -
1.1704 -
1.1705 -/*
1.1706 -** Generate code that will extract the iColumn-th column from
1.1707 -** table pTab and push that column value on the stack. There
1.1708 -** is an open cursor to pTab in iTable. If iColumn<0 then
1.1709 -** code is generated that extracts the rowid.
1.1710 -*/
1.1711 -void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1.1712 - if( iColumn<0 ){
1.1713 - int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1.1714 - sqlite3VdbeAddOp(v, op, iTable, 0);
1.1715 - }else if( pTab==0 ){
1.1716 - sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1.1717 - }else{
1.1718 - int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1.1719 - sqlite3VdbeAddOp(v, op, iTable, iColumn);
1.1720 - sqlite3ColumnDefault(v, pTab, iColumn);
1.1721 -#ifndef SQLITE_OMIT_FLOATING_POINT
1.1722 - if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1.1723 - sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1.1724 - }
1.1725 -#endif
1.1726 - }
1.1727 -}
1.1728 -
1.1729 -/*
1.1730 -** Generate code into the current Vdbe to evaluate the given
1.1731 -** expression and leave the result on the top of stack.
1.1732 -**
1.1733 -** This code depends on the fact that certain token values (ex: TK_EQ)
1.1734 -** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1.1735 -** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1.1736 -** the make process cause these values to align. Assert()s in the code
1.1737 -** below verify that the numbers are aligned correctly.
1.1738 -*/
1.1739 -void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
1.1740 - Vdbe *v = pParse->pVdbe;
1.1741 - int op;
1.1742 - int stackChng = 1; /* Amount of change to stack depth */
1.1743 -
1.1744 - if( v==0 ) return;
1.1745 - if( pExpr==0 ){
1.1746 - sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1.1747 - return;
1.1748 - }
1.1749 - op = pExpr->op;
1.1750 - switch( op ){
1.1751 - case TK_AGG_COLUMN: {
1.1752 - AggInfo *pAggInfo = pExpr->pAggInfo;
1.1753 - struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1.1754 - if( !pAggInfo->directMode ){
1.1755 - sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1.1756 - break;
1.1757 - }else if( pAggInfo->useSortingIdx ){
1.1758 - sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1.1759 - pCol->iSorterColumn);
1.1760 - break;
1.1761 - }
1.1762 - /* Otherwise, fall thru into the TK_COLUMN case */
1.1763 - }
1.1764 - case TK_COLUMN: {
1.1765 - if( pExpr->iTable<0 ){
1.1766 - /* This only happens when coding check constraints */
1.1767 - assert( pParse->ckOffset>0 );
1.1768 - sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
1.1769 - }else{
1.1770 - sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
1.1771 - }
1.1772 - break;
1.1773 - }
1.1774 - case TK_INTEGER: {
1.1775 - codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
1.1776 - break;
1.1777 - }
1.1778 - case TK_FLOAT:
1.1779 - case TK_STRING: {
1.1780 - assert( TK_FLOAT==OP_Real );
1.1781 - assert( TK_STRING==OP_String8 );
1.1782 - sqlite3DequoteExpr(pParse->db, pExpr);
1.1783 - sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
1.1784 - break;
1.1785 - }
1.1786 - case TK_NULL: {
1.1787 - sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1.1788 - break;
1.1789 - }
1.1790 -#ifndef SQLITE_OMIT_BLOB_LITERAL
1.1791 - case TK_BLOB: {
1.1792 - int n;
1.1793 - const char *z;
1.1794 - assert( TK_BLOB==OP_HexBlob );
1.1795 - n = pExpr->token.n - 3;
1.1796 - z = (char*)pExpr->token.z + 2;
1.1797 - assert( n>=0 );
1.1798 - if( n==0 ){
1.1799 - z = "";
1.1800 - }
1.1801 - sqlite3VdbeOp3(v, op, 0, 0, z, n);
1.1802 - break;
1.1803 - }
1.1804 -#endif
1.1805 - case TK_VARIABLE: {
1.1806 - sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
1.1807 - if( pExpr->token.n>1 ){
1.1808 - sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
1.1809 - }
1.1810 - break;
1.1811 - }
1.1812 - case TK_REGISTER: {
1.1813 - sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1.1814 - break;
1.1815 - }
1.1816 -#ifndef SQLITE_OMIT_CAST
1.1817 - case TK_CAST: {
1.1818 - /* Expressions of the form: CAST(pLeft AS token) */
1.1819 - int aff, to_op;
1.1820 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.1821 - aff = sqlite3AffinityType(&pExpr->token);
1.1822 - to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1.1823 - assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1.1824 - assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1.1825 - assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1.1826 - assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1.1827 - assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1.1828 - sqlite3VdbeAddOp(v, to_op, 0, 0);
1.1829 - stackChng = 0;
1.1830 - break;
1.1831 - }
1.1832 -#endif /* SQLITE_OMIT_CAST */
1.1833 - case TK_LT:
1.1834 - case TK_LE:
1.1835 - case TK_GT:
1.1836 - case TK_GE:
1.1837 - case TK_NE:
1.1838 - case TK_EQ: {
1.1839 - assert( TK_LT==OP_Lt );
1.1840 - assert( TK_LE==OP_Le );
1.1841 - assert( TK_GT==OP_Gt );
1.1842 - assert( TK_GE==OP_Ge );
1.1843 - assert( TK_EQ==OP_Eq );
1.1844 - assert( TK_NE==OP_Ne );
1.1845 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.1846 - sqlite3ExprCode(pParse, pExpr->pRight);
1.1847 - codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
1.1848 - stackChng = -1;
1.1849 - break;
1.1850 - }
1.1851 - case TK_AND:
1.1852 - case TK_OR:
1.1853 - case TK_PLUS:
1.1854 - case TK_STAR:
1.1855 - case TK_MINUS:
1.1856 - case TK_REM:
1.1857 - case TK_BITAND:
1.1858 - case TK_BITOR:
1.1859 - case TK_SLASH:
1.1860 - case TK_LSHIFT:
1.1861 - case TK_RSHIFT:
1.1862 - case TK_CONCAT: {
1.1863 - assert( TK_AND==OP_And );
1.1864 - assert( TK_OR==OP_Or );
1.1865 - assert( TK_PLUS==OP_Add );
1.1866 - assert( TK_MINUS==OP_Subtract );
1.1867 - assert( TK_REM==OP_Remainder );
1.1868 - assert( TK_BITAND==OP_BitAnd );
1.1869 - assert( TK_BITOR==OP_BitOr );
1.1870 - assert( TK_SLASH==OP_Divide );
1.1871 - assert( TK_LSHIFT==OP_ShiftLeft );
1.1872 - assert( TK_RSHIFT==OP_ShiftRight );
1.1873 - assert( TK_CONCAT==OP_Concat );
1.1874 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.1875 - sqlite3ExprCode(pParse, pExpr->pRight);
1.1876 - sqlite3VdbeAddOp(v, op, 0, 0);
1.1877 - stackChng = -1;
1.1878 - break;
1.1879 - }
1.1880 - case TK_UMINUS: {
1.1881 - Expr *pLeft = pExpr->pLeft;
1.1882 - assert( pLeft );
1.1883 - if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1.1884 - Token *p = &pLeft->token;
1.1885 - char *z = sqlite3MPrintf(pParse->db, "-%.*s", p->n, p->z);
1.1886 - if( pLeft->op==TK_FLOAT ){
1.1887 - sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
1.1888 - }else{
1.1889 - codeInteger(v, z, p->n+1);
1.1890 - }
1.1891 - sqlite3_free(z);
1.1892 - break;
1.1893 - }
1.1894 - /* Fall through into TK_NOT */
1.1895 - }
1.1896 - case TK_BITNOT:
1.1897 - case TK_NOT: {
1.1898 - assert( TK_BITNOT==OP_BitNot );
1.1899 - assert( TK_NOT==OP_Not );
1.1900 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.1901 - sqlite3VdbeAddOp(v, op, 0, 0);
1.1902 - stackChng = 0;
1.1903 - break;
1.1904 - }
1.1905 - case TK_ISNULL:
1.1906 - case TK_NOTNULL: {
1.1907 - int dest;
1.1908 - assert( TK_ISNULL==OP_IsNull );
1.1909 - assert( TK_NOTNULL==OP_NotNull );
1.1910 - sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1.1911 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.1912 - dest = sqlite3VdbeCurrentAddr(v) + 2;
1.1913 - sqlite3VdbeAddOp(v, op, 1, dest);
1.1914 - sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
1.1915 - stackChng = 0;
1.1916 - break;
1.1917 - }
1.1918 - case TK_AGG_FUNCTION: {
1.1919 - AggInfo *pInfo = pExpr->pAggInfo;
1.1920 - if( pInfo==0 ){
1.1921 - sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1.1922 - &pExpr->span);
1.1923 - }else{
1.1924 - sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1.1925 - }
1.1926 - break;
1.1927 - }
1.1928 - case TK_CONST_FUNC:
1.1929 - case TK_FUNCTION: {
1.1930 - ExprList *pList = pExpr->pList;
1.1931 - int nExpr = pList ? pList->nExpr : 0;
1.1932 - FuncDef *pDef;
1.1933 - int nId;
1.1934 - const char *zId;
1.1935 - int constMask = 0;
1.1936 - int i;
1.1937 - sqlite3 *db = pParse->db;
1.1938 - u8 enc = ENC(db);
1.1939 - CollSeq *pColl = 0;
1.1940 -
1.1941 - zId = (char*)pExpr->token.z;
1.1942 - nId = pExpr->token.n;
1.1943 - pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
1.1944 - assert( pDef!=0 );
1.1945 - nExpr = sqlite3ExprCodeExprList(pParse, pList);
1.1946 -#ifndef SQLITE_OMIT_VIRTUALTABLE
1.1947 - /* Possibly overload the function if the first argument is
1.1948 - ** a virtual table column.
1.1949 - **
1.1950 - ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1.1951 - ** second argument, not the first, as the argument to test to
1.1952 - ** see if it is a column in a virtual table. This is done because
1.1953 - ** the left operand of infix functions (the operand we want to
1.1954 - ** control overloading) ends up as the second argument to the
1.1955 - ** function. The expression "A glob B" is equivalent to
1.1956 - ** "glob(B,A). We want to use the A in "A glob B" to test
1.1957 - ** for function overloading. But we use the B term in "glob(B,A)".
1.1958 - */
1.1959 - if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
1.1960 - pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
1.1961 - }else if( nExpr>0 ){
1.1962 - pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
1.1963 - }
1.1964 -#endif
1.1965 - for(i=0; i<nExpr && i<32; i++){
1.1966 - if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1.1967 - constMask |= (1<<i);
1.1968 - }
1.1969 - if( pDef->needCollSeq && !pColl ){
1.1970 - pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1.1971 - }
1.1972 - }
1.1973 - if( pDef->needCollSeq ){
1.1974 - if( !pColl ) pColl = pParse->db->pDfltColl;
1.1975 - sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
1.1976 - }
1.1977 - sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
1.1978 - stackChng = 1-nExpr;
1.1979 - break;
1.1980 - }
1.1981 -#ifndef SQLITE_OMIT_SUBQUERY
1.1982 - case TK_EXISTS:
1.1983 - case TK_SELECT: {
1.1984 - if( pExpr->iColumn==0 ){
1.1985 - sqlite3CodeSubselect(pParse, pExpr);
1.1986 - }
1.1987 - sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
1.1988 - VdbeComment((v, "# load subquery result"));
1.1989 - break;
1.1990 - }
1.1991 - case TK_IN: {
1.1992 - int addr;
1.1993 - char affinity;
1.1994 - int ckOffset = pParse->ckOffset;
1.1995 - sqlite3CodeSubselect(pParse, pExpr);
1.1996 -
1.1997 - /* Figure out the affinity to use to create a key from the results
1.1998 - ** of the expression. affinityStr stores a static string suitable for
1.1999 - ** P3 of OP_MakeRecord.
1.2000 - */
1.2001 - affinity = comparisonAffinity(pExpr);
1.2002 -
1.2003 - sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1.2004 - pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
1.2005 -
1.2006 - /* Code the <expr> from "<expr> IN (...)". The temporary table
1.2007 - ** pExpr->iTable contains the values that make up the (...) set.
1.2008 - */
1.2009 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.2010 - addr = sqlite3VdbeCurrentAddr(v);
1.2011 - sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
1.2012 - sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
1.2013 - sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1.2014 - sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1.2015 - sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
1.2016 - sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1.2017 - sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1.2018 -
1.2019 - break;
1.2020 - }
1.2021 -#endif
1.2022 - case TK_BETWEEN: {
1.2023 - Expr *pLeft = pExpr->pLeft;
1.2024 - struct ExprList_item *pLItem = pExpr->pList->a;
1.2025 - Expr *pRight = pLItem->pExpr;
1.2026 - sqlite3ExprCode(pParse, pLeft);
1.2027 - sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1.2028 - sqlite3ExprCode(pParse, pRight);
1.2029 - codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
1.2030 - sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1.2031 - pLItem++;
1.2032 - pRight = pLItem->pExpr;
1.2033 - sqlite3ExprCode(pParse, pRight);
1.2034 - codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
1.2035 - sqlite3VdbeAddOp(v, OP_And, 0, 0);
1.2036 - break;
1.2037 - }
1.2038 - case TK_UPLUS: {
1.2039 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.2040 - stackChng = 0;
1.2041 - break;
1.2042 - }
1.2043 - case TK_CASE: {
1.2044 - int expr_end_label;
1.2045 - int jumpInst;
1.2046 - int nExpr;
1.2047 - int i;
1.2048 - ExprList *pEList;
1.2049 - struct ExprList_item *aListelem;
1.2050 -
1.2051 - assert(pExpr->pList);
1.2052 - assert((pExpr->pList->nExpr % 2) == 0);
1.2053 - assert(pExpr->pList->nExpr > 0);
1.2054 - pEList = pExpr->pList;
1.2055 - aListelem = pEList->a;
1.2056 - nExpr = pEList->nExpr;
1.2057 - expr_end_label = sqlite3VdbeMakeLabel(v);
1.2058 - if( pExpr->pLeft ){
1.2059 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.2060 - }
1.2061 - for(i=0; i<nExpr; i=i+2){
1.2062 - sqlite3ExprCode(pParse, aListelem[i].pExpr);
1.2063 - if( pExpr->pLeft ){
1.2064 - sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
1.2065 - jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1.2066 - OP_Ne, 0, 1);
1.2067 - sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1.2068 - }else{
1.2069 - jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
1.2070 - }
1.2071 - sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
1.2072 - sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1.2073 - sqlite3VdbeJumpHere(v, jumpInst);
1.2074 - }
1.2075 - if( pExpr->pLeft ){
1.2076 - sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1.2077 - }
1.2078 - if( pExpr->pRight ){
1.2079 - sqlite3ExprCode(pParse, pExpr->pRight);
1.2080 - }else{
1.2081 - sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1.2082 - }
1.2083 - sqlite3VdbeResolveLabel(v, expr_end_label);
1.2084 - break;
1.2085 - }
1.2086 -#ifndef SQLITE_OMIT_TRIGGER
1.2087 - case TK_RAISE: {
1.2088 - if( !pParse->trigStack ){
1.2089 - sqlite3ErrorMsg(pParse,
1.2090 - "RAISE() may only be used within a trigger-program");
1.2091 - return;
1.2092 - }
1.2093 - if( pExpr->iColumn!=OE_Ignore ){
1.2094 - assert( pExpr->iColumn==OE_Rollback ||
1.2095 - pExpr->iColumn == OE_Abort ||
1.2096 - pExpr->iColumn == OE_Fail );
1.2097 - sqlite3DequoteExpr(pParse->db, pExpr);
1.2098 - sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1.2099 - (char*)pExpr->token.z, pExpr->token.n);
1.2100 - } else {
1.2101 - assert( pExpr->iColumn == OE_Ignore );
1.2102 - sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1.2103 - sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1.2104 - VdbeComment((v, "# raise(IGNORE)"));
1.2105 - }
1.2106 - stackChng = 0;
1.2107 - break;
1.2108 - }
1.2109 -#endif
1.2110 - }
1.2111 -
1.2112 - if( pParse->ckOffset ){
1.2113 - pParse->ckOffset += stackChng;
1.2114 - assert( pParse->ckOffset );
1.2115 - }
1.2116 -}
1.2117 -
1.2118 -#ifndef SQLITE_OMIT_TRIGGER
1.2119 -/*
1.2120 -** Generate code that evalutes the given expression and leaves the result
1.2121 -** on the stack. See also sqlite3ExprCode().
1.2122 -**
1.2123 -** This routine might also cache the result and modify the pExpr tree
1.2124 -** so that it will make use of the cached result on subsequent evaluations
1.2125 -** rather than evaluate the whole expression again. Trivial expressions are
1.2126 -** not cached. If the expression is cached, its result is stored in a
1.2127 -** memory location.
1.2128 -*/
1.2129 -void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1.2130 - Vdbe *v = pParse->pVdbe;
1.2131 - int iMem;
1.2132 - int addr1, addr2;
1.2133 - if( v==0 ) return;
1.2134 - addr1 = sqlite3VdbeCurrentAddr(v);
1.2135 - sqlite3ExprCode(pParse, pExpr);
1.2136 - addr2 = sqlite3VdbeCurrentAddr(v);
1.2137 - if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1.2138 - iMem = pExpr->iTable = pParse->nMem++;
1.2139 - sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1.2140 - pExpr->op = TK_REGISTER;
1.2141 - }
1.2142 -}
1.2143 -#endif
1.2144 -
1.2145 -/*
1.2146 -** Generate code that pushes the value of every element of the given
1.2147 -** expression list onto the stack.
1.2148 -**
1.2149 -** Return the number of elements pushed onto the stack.
1.2150 -*/
1.2151 -int sqlite3ExprCodeExprList(
1.2152 - Parse *pParse, /* Parsing context */
1.2153 - ExprList *pList /* The expression list to be coded */
1.2154 -){
1.2155 - struct ExprList_item *pItem;
1.2156 - int i, n;
1.2157 - if( pList==0 ) return 0;
1.2158 - n = pList->nExpr;
1.2159 - for(pItem=pList->a, i=n; i>0; i--, pItem++){
1.2160 - sqlite3ExprCode(pParse, pItem->pExpr);
1.2161 - }
1.2162 - return n;
1.2163 -}
1.2164 -
1.2165 -/*
1.2166 -** Generate code for a boolean expression such that a jump is made
1.2167 -** to the label "dest" if the expression is true but execution
1.2168 -** continues straight thru if the expression is false.
1.2169 -**
1.2170 -** If the expression evaluates to NULL (neither true nor false), then
1.2171 -** take the jump if the jumpIfNull flag is true.
1.2172 -**
1.2173 -** This code depends on the fact that certain token values (ex: TK_EQ)
1.2174 -** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1.2175 -** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1.2176 -** the make process cause these values to align. Assert()s in the code
1.2177 -** below verify that the numbers are aligned correctly.
1.2178 -*/
1.2179 -void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1.2180 - Vdbe *v = pParse->pVdbe;
1.2181 - int op = 0;
1.2182 - int ckOffset = pParse->ckOffset;
1.2183 - if( v==0 || pExpr==0 ) return;
1.2184 - op = pExpr->op;
1.2185 - switch( op ){
1.2186 - case TK_AND: {
1.2187 - int d2 = sqlite3VdbeMakeLabel(v);
1.2188 - sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1.2189 - sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1.2190 - sqlite3VdbeResolveLabel(v, d2);
1.2191 - break;
1.2192 - }
1.2193 - case TK_OR: {
1.2194 - sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1.2195 - sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1.2196 - break;
1.2197 - }
1.2198 - case TK_NOT: {
1.2199 - sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1.2200 - break;
1.2201 - }
1.2202 - case TK_LT:
1.2203 - case TK_LE:
1.2204 - case TK_GT:
1.2205 - case TK_GE:
1.2206 - case TK_NE:
1.2207 - case TK_EQ: {
1.2208 - assert( TK_LT==OP_Lt );
1.2209 - assert( TK_LE==OP_Le );
1.2210 - assert( TK_GT==OP_Gt );
1.2211 - assert( TK_GE==OP_Ge );
1.2212 - assert( TK_EQ==OP_Eq );
1.2213 - assert( TK_NE==OP_Ne );
1.2214 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.2215 - sqlite3ExprCode(pParse, pExpr->pRight);
1.2216 - codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
1.2217 - break;
1.2218 - }
1.2219 - case TK_ISNULL:
1.2220 - case TK_NOTNULL: {
1.2221 - assert( TK_ISNULL==OP_IsNull );
1.2222 - assert( TK_NOTNULL==OP_NotNull );
1.2223 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.2224 - sqlite3VdbeAddOp(v, op, 1, dest);
1.2225 - break;
1.2226 - }
1.2227 - case TK_BETWEEN: {
1.2228 - /* The expression "x BETWEEN y AND z" is implemented as:
1.2229 - **
1.2230 - ** 1 IF (x < y) GOTO 3
1.2231 - ** 2 IF (x <= z) GOTO <dest>
1.2232 - ** 3 ...
1.2233 - */
1.2234 - int addr;
1.2235 - Expr *pLeft = pExpr->pLeft;
1.2236 - Expr *pRight = pExpr->pList->a[0].pExpr;
1.2237 - sqlite3ExprCode(pParse, pLeft);
1.2238 - sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1.2239 - sqlite3ExprCode(pParse, pRight);
1.2240 - addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
1.2241 -
1.2242 - pRight = pExpr->pList->a[1].pExpr;
1.2243 - sqlite3ExprCode(pParse, pRight);
1.2244 - codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
1.2245 -
1.2246 - sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1.2247 - sqlite3VdbeJumpHere(v, addr);
1.2248 - sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1.2249 - break;
1.2250 - }
1.2251 - default: {
1.2252 - sqlite3ExprCode(pParse, pExpr);
1.2253 - sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
1.2254 - break;
1.2255 - }
1.2256 - }
1.2257 - pParse->ckOffset = ckOffset;
1.2258 -}
1.2259 -
1.2260 -/*
1.2261 -** Generate code for a boolean expression such that a jump is made
1.2262 -** to the label "dest" if the expression is false but execution
1.2263 -** continues straight thru if the expression is true.
1.2264 -**
1.2265 -** If the expression evaluates to NULL (neither true nor false) then
1.2266 -** jump if jumpIfNull is true or fall through if jumpIfNull is false.
1.2267 -*/
1.2268 -void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
1.2269 - Vdbe *v = pParse->pVdbe;
1.2270 - int op = 0;
1.2271 - int ckOffset = pParse->ckOffset;
1.2272 - if( v==0 || pExpr==0 ) return;
1.2273 -
1.2274 - /* The value of pExpr->op and op are related as follows:
1.2275 - **
1.2276 - ** pExpr->op op
1.2277 - ** --------- ----------
1.2278 - ** TK_ISNULL OP_NotNull
1.2279 - ** TK_NOTNULL OP_IsNull
1.2280 - ** TK_NE OP_Eq
1.2281 - ** TK_EQ OP_Ne
1.2282 - ** TK_GT OP_Le
1.2283 - ** TK_LE OP_Gt
1.2284 - ** TK_GE OP_Lt
1.2285 - ** TK_LT OP_Ge
1.2286 - **
1.2287 - ** For other values of pExpr->op, op is undefined and unused.
1.2288 - ** The value of TK_ and OP_ constants are arranged such that we
1.2289 - ** can compute the mapping above using the following expression.
1.2290 - ** Assert()s verify that the computation is correct.
1.2291 - */
1.2292 - op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1.2293 -
1.2294 - /* Verify correct alignment of TK_ and OP_ constants
1.2295 - */
1.2296 - assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1.2297 - assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1.2298 - assert( pExpr->op!=TK_NE || op==OP_Eq );
1.2299 - assert( pExpr->op!=TK_EQ || op==OP_Ne );
1.2300 - assert( pExpr->op!=TK_LT || op==OP_Ge );
1.2301 - assert( pExpr->op!=TK_LE || op==OP_Gt );
1.2302 - assert( pExpr->op!=TK_GT || op==OP_Le );
1.2303 - assert( pExpr->op!=TK_GE || op==OP_Lt );
1.2304 -
1.2305 - switch( pExpr->op ){
1.2306 - case TK_AND: {
1.2307 - sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1.2308 - sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1.2309 - break;
1.2310 - }
1.2311 - case TK_OR: {
1.2312 - int d2 = sqlite3VdbeMakeLabel(v);
1.2313 - sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1.2314 - sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1.2315 - sqlite3VdbeResolveLabel(v, d2);
1.2316 - break;
1.2317 - }
1.2318 - case TK_NOT: {
1.2319 - sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1.2320 - break;
1.2321 - }
1.2322 - case TK_LT:
1.2323 - case TK_LE:
1.2324 - case TK_GT:
1.2325 - case TK_GE:
1.2326 - case TK_NE:
1.2327 - case TK_EQ: {
1.2328 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.2329 - sqlite3ExprCode(pParse, pExpr->pRight);
1.2330 - codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
1.2331 - break;
1.2332 - }
1.2333 - case TK_ISNULL:
1.2334 - case TK_NOTNULL: {
1.2335 - sqlite3ExprCode(pParse, pExpr->pLeft);
1.2336 - sqlite3VdbeAddOp(v, op, 1, dest);
1.2337 - break;
1.2338 - }
1.2339 - case TK_BETWEEN: {
1.2340 - /* The expression is "x BETWEEN y AND z". It is implemented as:
1.2341 - **
1.2342 - ** 1 IF (x >= y) GOTO 3
1.2343 - ** 2 GOTO <dest>
1.2344 - ** 3 IF (x > z) GOTO <dest>
1.2345 - */
1.2346 - int addr;
1.2347 - Expr *pLeft = pExpr->pLeft;
1.2348 - Expr *pRight = pExpr->pList->a[0].pExpr;
1.2349 - sqlite3ExprCode(pParse, pLeft);
1.2350 - sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1.2351 - sqlite3ExprCode(pParse, pRight);
1.2352 - addr = sqlite3VdbeCurrentAddr(v);
1.2353 - codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1.2354 -
1.2355 - sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1.2356 - sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1.2357 - pRight = pExpr->pList->a[1].pExpr;
1.2358 - sqlite3ExprCode(pParse, pRight);
1.2359 - codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
1.2360 - break;
1.2361 - }
1.2362 - default: {
1.2363 - sqlite3ExprCode(pParse, pExpr);
1.2364 - sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
1.2365 - break;
1.2366 - }
1.2367 - }
1.2368 - pParse->ckOffset = ckOffset;
1.2369 -}
1.2370 -
1.2371 -/*
1.2372 -** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1.2373 -** if they are identical and return FALSE if they differ in any way.
1.2374 -**
1.2375 -** Sometimes this routine will return FALSE even if the two expressions
1.2376 -** really are equivalent. If we cannot prove that the expressions are
1.2377 -** identical, we return FALSE just to be safe. So if this routine
1.2378 -** returns false, then you do not really know for certain if the two
1.2379 -** expressions are the same. But if you get a TRUE return, then you
1.2380 -** can be sure the expressions are the same. In the places where
1.2381 -** this routine is used, it does not hurt to get an extra FALSE - that
1.2382 -** just might result in some slightly slower code. But returning
1.2383 -** an incorrect TRUE could lead to a malfunction.
1.2384 -*/
1.2385 -int sqlite3ExprCompare(Expr *pA, Expr *pB){
1.2386 - int i;
1.2387 - if( pA==0||pB==0 ){
1.2388 - return pB==pA;
1.2389 - }
1.2390 - if( pA->op!=pB->op ) return 0;
1.2391 - if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
1.2392 - if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1.2393 - if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
1.2394 - if( pA->pList ){
1.2395 - if( pB->pList==0 ) return 0;
1.2396 - if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1.2397 - for(i=0; i<pA->pList->nExpr; i++){
1.2398 - if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
1.2399 - return 0;
1.2400 - }
1.2401 - }
1.2402 - }else if( pB->pList ){
1.2403 - return 0;
1.2404 - }
1.2405 - if( pA->pSelect || pB->pSelect ) return 0;
1.2406 - if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
1.2407 - if( pA->op!=TK_COLUMN && pA->token.z ){
1.2408 - if( pB->token.z==0 ) return 0;
1.2409 - if( pB->token.n!=pA->token.n ) return 0;
1.2410 - if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
1.2411 - return 0;
1.2412 - }
1.2413 - }
1.2414 - return 1;
1.2415 -}
1.2416 -
1.2417 -
1.2418 -/*
1.2419 -** Add a new element to the pAggInfo->aCol[] array. Return the index of
1.2420 -** the new element. Return a negative number if malloc fails.
1.2421 -*/
1.2422 -static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
1.2423 - int i;
1.2424 - pInfo->aCol = sqlite3ArrayAllocate(
1.2425 - db,
1.2426 - pInfo->aCol,
1.2427 - sizeof(pInfo->aCol[0]),
1.2428 - 3,
1.2429 - &pInfo->nColumn,
1.2430 - &pInfo->nColumnAlloc,
1.2431 - &i
1.2432 - );
1.2433 - return i;
1.2434 -}
1.2435 -
1.2436 -/*
1.2437 -** Add a new element to the pAggInfo->aFunc[] array. Return the index of
1.2438 -** the new element. Return a negative number if malloc fails.
1.2439 -*/
1.2440 -static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
1.2441 - int i;
1.2442 - pInfo->aFunc = sqlite3ArrayAllocate(
1.2443 - db,
1.2444 - pInfo->aFunc,
1.2445 - sizeof(pInfo->aFunc[0]),
1.2446 - 3,
1.2447 - &pInfo->nFunc,
1.2448 - &pInfo->nFuncAlloc,
1.2449 - &i
1.2450 - );
1.2451 - return i;
1.2452 -}
1.2453 -
1.2454 -/*
1.2455 -** This is an xFunc for walkExprTree() used to implement
1.2456 -** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
1.2457 -** for additional information.
1.2458 -**
1.2459 -** This routine analyzes the aggregate function at pExpr.
1.2460 -*/
1.2461 -static int analyzeAggregate(void *pArg, Expr *pExpr){
1.2462 - int i;
1.2463 - NameContext *pNC = (NameContext *)pArg;
1.2464 - Parse *pParse = pNC->pParse;
1.2465 - SrcList *pSrcList = pNC->pSrcList;
1.2466 - AggInfo *pAggInfo = pNC->pAggInfo;
1.2467 -
1.2468 - switch( pExpr->op ){
1.2469 - case TK_AGG_COLUMN:
1.2470 - case TK_COLUMN: {
1.2471 - /* Check to see if the column is in one of the tables in the FROM
1.2472 - ** clause of the aggregate query */
1.2473 - if( pSrcList ){
1.2474 - struct SrcList_item *pItem = pSrcList->a;
1.2475 - for(i=0; i<pSrcList->nSrc; i++, pItem++){
1.2476 - struct AggInfo_col *pCol;
1.2477 - if( pExpr->iTable==pItem->iCursor ){
1.2478 - /* If we reach this point, it means that pExpr refers to a table
1.2479 - ** that is in the FROM clause of the aggregate query.
1.2480 - **
1.2481 - ** Make an entry for the column in pAggInfo->aCol[] if there
1.2482 - ** is not an entry there already.
1.2483 - */
1.2484 - int k;
1.2485 - pCol = pAggInfo->aCol;
1.2486 - for(k=0; k<pAggInfo->nColumn; k++, pCol++){
1.2487 - if( pCol->iTable==pExpr->iTable &&
1.2488 - pCol->iColumn==pExpr->iColumn ){
1.2489 - break;
1.2490 - }
1.2491 - }
1.2492 - if( (k>=pAggInfo->nColumn)
1.2493 - && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
1.2494 - ){
1.2495 - pCol = &pAggInfo->aCol[k];
1.2496 - pCol->pTab = pExpr->pTab;
1.2497 - pCol->iTable = pExpr->iTable;
1.2498 - pCol->iColumn = pExpr->iColumn;
1.2499 - pCol->iMem = pParse->nMem++;
1.2500 - pCol->iSorterColumn = -1;
1.2501 - pCol->pExpr = pExpr;
1.2502 - if( pAggInfo->pGroupBy ){
1.2503 - int j, n;
1.2504 - ExprList *pGB = pAggInfo->pGroupBy;
1.2505 - struct ExprList_item *pTerm = pGB->a;
1.2506 - n = pGB->nExpr;
1.2507 - for(j=0; j<n; j++, pTerm++){
1.2508 - Expr *pE = pTerm->pExpr;
1.2509 - if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
1.2510 - pE->iColumn==pExpr->iColumn ){
1.2511 - pCol->iSorterColumn = j;
1.2512 - break;
1.2513 - }
1.2514 - }
1.2515 - }
1.2516 - if( pCol->iSorterColumn<0 ){
1.2517 - pCol->iSorterColumn = pAggInfo->nSortingColumn++;
1.2518 - }
1.2519 - }
1.2520 - /* There is now an entry for pExpr in pAggInfo->aCol[] (either
1.2521 - ** because it was there before or because we just created it).
1.2522 - ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
1.2523 - ** pAggInfo->aCol[] entry.
1.2524 - */
1.2525 - pExpr->pAggInfo = pAggInfo;
1.2526 - pExpr->op = TK_AGG_COLUMN;
1.2527 - pExpr->iAgg = k;
1.2528 - break;
1.2529 - } /* endif pExpr->iTable==pItem->iCursor */
1.2530 - } /* end loop over pSrcList */
1.2531 - }
1.2532 - return 1;
1.2533 - }
1.2534 - case TK_AGG_FUNCTION: {
1.2535 - /* The pNC->nDepth==0 test causes aggregate functions in subqueries
1.2536 - ** to be ignored */
1.2537 - if( pNC->nDepth==0 ){
1.2538 - /* Check to see if pExpr is a duplicate of another aggregate
1.2539 - ** function that is already in the pAggInfo structure
1.2540 - */
1.2541 - struct AggInfo_func *pItem = pAggInfo->aFunc;
1.2542 - for(i=0; i<pAggInfo->nFunc; i++, pItem++){
1.2543 - if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
1.2544 - break;
1.2545 - }
1.2546 - }
1.2547 - if( i>=pAggInfo->nFunc ){
1.2548 - /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
1.2549 - */
1.2550 - u8 enc = ENC(pParse->db);
1.2551 - i = addAggInfoFunc(pParse->db, pAggInfo);
1.2552 - if( i>=0 ){
1.2553 - pItem = &pAggInfo->aFunc[i];
1.2554 - pItem->pExpr = pExpr;
1.2555 - pItem->iMem = pParse->nMem++;
1.2556 - pItem->pFunc = sqlite3FindFunction(pParse->db,
1.2557 - (char*)pExpr->token.z, pExpr->token.n,
1.2558 - pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
1.2559 - if( pExpr->flags & EP_Distinct ){
1.2560 - pItem->iDistinct = pParse->nTab++;
1.2561 - }else{
1.2562 - pItem->iDistinct = -1;
1.2563 - }
1.2564 - }
1.2565 - }
1.2566 - /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
1.2567 - */
1.2568 - pExpr->iAgg = i;
1.2569 - pExpr->pAggInfo = pAggInfo;
1.2570 - return 1;
1.2571 - }
1.2572 - }
1.2573 - }
1.2574 -
1.2575 - /* Recursively walk subqueries looking for TK_COLUMN nodes that need
1.2576 - ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
1.2577 - ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
1.2578 - */
1.2579 - if( pExpr->pSelect ){
1.2580 - pNC->nDepth++;
1.2581 - walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
1.2582 - pNC->nDepth--;
1.2583 - }
1.2584 - return 0;
1.2585 -}
1.2586 -
1.2587 -/*
1.2588 -** Analyze the given expression looking for aggregate functions and
1.2589 -** for variables that need to be added to the pParse->aAgg[] array.
1.2590 -** Make additional entries to the pParse->aAgg[] array as necessary.
1.2591 -**
1.2592 -** This routine should only be called after the expression has been
1.2593 -** analyzed by sqlite3ExprResolveNames().
1.2594 -**
1.2595 -** If errors are seen, leave an error message in zErrMsg and return
1.2596 -** the number of errors.
1.2597 -*/
1.2598 -int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
1.2599 - int nErr = pNC->pParse->nErr;
1.2600 - walkExprTree(pExpr, analyzeAggregate, pNC);
1.2601 - return pNC->pParse->nErr - nErr;
1.2602 -}
1.2603 -
1.2604 -/*
1.2605 -** Call sqlite3ExprAnalyzeAggregates() for every expression in an
1.2606 -** expression list. Return the number of errors.
1.2607 -**
1.2608 -** If an error is found, the analysis is cut short.
1.2609 -*/
1.2610 -int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
1.2611 - struct ExprList_item *pItem;
1.2612 - int i;
1.2613 - int nErr = 0;
1.2614 - if( pList ){
1.2615 - for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
1.2616 - nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
1.2617 - }
1.2618 - }
1.2619 - return nErr;
1.2620 -}