1434
|
1 /*
|
|
2 ** 2001 September 15
|
|
3 **
|
|
4 ** The author disclaims copyright to this source code. In place of
|
|
5 ** a legal notice, here is a blessing:
|
|
6 **
|
|
7 ** May you do good and not evil.
|
|
8 ** May you find forgiveness for yourself and forgive others.
|
|
9 ** May you share freely, never taking more than you give.
|
|
10 **
|
|
11 *************************************************************************
|
|
12 ** This file contains routines used for analyzing expressions and
|
|
13 ** for generating VDBE code that evaluates expressions in SQLite.
|
|
14 **
|
|
15 ** $Id: expr.c,v 1.258 2006/05/23 23:22:29 drh Exp $
|
|
16 */
|
|
17 #include "sqliteInt.h"
|
|
18 #include <ctype.h>
|
|
19
|
|
20 /*
|
|
21 ** Return the 'affinity' of the expression pExpr if any.
|
|
22 **
|
|
23 ** If pExpr is a column, a reference to a column via an 'AS' alias,
|
|
24 ** or a sub-select with a column as the return value, then the
|
|
25 ** affinity of that column is returned. Otherwise, 0x00 is returned,
|
|
26 ** indicating no affinity for the expression.
|
|
27 **
|
|
28 ** i.e. the WHERE clause expresssions in the following statements all
|
|
29 ** have an affinity:
|
|
30 **
|
|
31 ** CREATE TABLE t1(a);
|
|
32 ** SELECT * FROM t1 WHERE a;
|
|
33 ** SELECT a AS b FROM t1 WHERE b;
|
|
34 ** SELECT * FROM t1 WHERE (select a from t1);
|
|
35 */
|
|
36 char sqlite3ExprAffinity(Expr *pExpr){
|
|
37 int op = pExpr->op;
|
|
38 if( op==TK_AS ){
|
|
39 return sqlite3ExprAffinity(pExpr->pLeft);
|
|
40 }
|
|
41 if( op==TK_SELECT ){
|
|
42 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
|
|
43 }
|
|
44 #ifndef SQLITE_OMIT_CAST
|
|
45 if( op==TK_CAST ){
|
|
46 return sqlite3AffinityType(&pExpr->token);
|
|
47 }
|
|
48 #endif
|
|
49 return pExpr->affinity;
|
|
50 }
|
|
51
|
|
52 /*
|
|
53 ** Return the default collation sequence for the expression pExpr. If
|
|
54 ** there is no default collation type, return 0.
|
|
55 */
|
|
56 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
|
|
57 CollSeq *pColl = 0;
|
|
58 if( pExpr ){
|
|
59 pColl = pExpr->pColl;
|
|
60 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
|
|
61 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
|
|
62 }
|
|
63 }
|
|
64 if( sqlite3CheckCollSeq(pParse, pColl) ){
|
|
65 pColl = 0;
|
|
66 }
|
|
67 return pColl;
|
|
68 }
|
|
69
|
|
70 /*
|
|
71 ** pExpr is an operand of a comparison operator. aff2 is the
|
|
72 ** type affinity of the other operand. This routine returns the
|
|
73 ** type affinity that should be used for the comparison operator.
|
|
74 */
|
|
75 char sqlite3CompareAffinity(Expr *pExpr, char aff2){
|
|
76 char aff1 = sqlite3ExprAffinity(pExpr);
|
|
77 if( aff1 && aff2 ){
|
|
78 /* Both sides of the comparison are columns. If one has numeric
|
|
79 ** affinity, use that. Otherwise use no affinity.
|
|
80 */
|
|
81 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
|
|
82 return SQLITE_AFF_NUMERIC;
|
|
83 }else{
|
|
84 return SQLITE_AFF_NONE;
|
|
85 }
|
|
86 }else if( !aff1 && !aff2 ){
|
|
87 /* Neither side of the comparison is a column. Compare the
|
|
88 ** results directly.
|
|
89 */
|
|
90 return SQLITE_AFF_NONE;
|
|
91 }else{
|
|
92 /* One side is a column, the other is not. Use the columns affinity. */
|
|
93 assert( aff1==0 || aff2==0 );
|
|
94 return (aff1 + aff2);
|
|
95 }
|
|
96 }
|
|
97
|
|
98 /*
|
|
99 ** pExpr is a comparison operator. Return the type affinity that should
|
|
100 ** be applied to both operands prior to doing the comparison.
|
|
101 */
|
|
102 static char comparisonAffinity(Expr *pExpr){
|
|
103 char aff;
|
|
104 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
|
|
105 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
|
|
106 pExpr->op==TK_NE );
|
|
107 assert( pExpr->pLeft );
|
|
108 aff = sqlite3ExprAffinity(pExpr->pLeft);
|
|
109 if( pExpr->pRight ){
|
|
110 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
|
|
111 }
|
|
112 else if( pExpr->pSelect ){
|
|
113 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
|
|
114 }
|
|
115 else if( !aff ){
|
|
116 aff = SQLITE_AFF_NUMERIC;
|
|
117 }
|
|
118 return aff;
|
|
119 }
|
|
120
|
|
121 /*
|
|
122 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
|
|
123 ** idx_affinity is the affinity of an indexed column. Return true
|
|
124 ** if the index with affinity idx_affinity may be used to implement
|
|
125 ** the comparison in pExpr.
|
|
126 */
|
|
127 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
|
|
128 char aff = comparisonAffinity(pExpr);
|
|
129 switch( aff ){
|
|
130 case SQLITE_AFF_NONE:
|
|
131 return 1;
|
|
132 case SQLITE_AFF_TEXT:
|
|
133 return idx_affinity==SQLITE_AFF_TEXT;
|
|
134 default:
|
|
135 return sqlite3IsNumericAffinity(idx_affinity);
|
|
136 }
|
|
137 }
|
|
138
|
|
139 /*
|
|
140 ** Return the P1 value that should be used for a binary comparison
|
|
141 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
|
|
142 ** If jumpIfNull is true, then set the low byte of the returned
|
|
143 ** P1 value to tell the opcode to jump if either expression
|
|
144 ** evaluates to NULL.
|
|
145 */
|
|
146 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
|
|
147 char aff = sqlite3ExprAffinity(pExpr2);
|
|
148 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
|
|
149 }
|
|
150
|
|
151 /*
|
|
152 ** Return a pointer to the collation sequence that should be used by
|
|
153 ** a binary comparison operator comparing pLeft and pRight.
|
|
154 **
|
|
155 ** If the left hand expression has a collating sequence type, then it is
|
|
156 ** used. Otherwise the collation sequence for the right hand expression
|
|
157 ** is used, or the default (BINARY) if neither expression has a collating
|
|
158 ** type.
|
|
159 */
|
|
160 static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
|
|
161 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
|
|
162 if( !pColl ){
|
|
163 pColl = sqlite3ExprCollSeq(pParse, pRight);
|
|
164 }
|
|
165 return pColl;
|
|
166 }
|
|
167
|
|
168 /*
|
|
169 ** Generate code for a comparison operator.
|
|
170 */
|
|
171 static int codeCompare(
|
|
172 Parse *pParse, /* The parsing (and code generating) context */
|
|
173 Expr *pLeft, /* The left operand */
|
|
174 Expr *pRight, /* The right operand */
|
|
175 int opcode, /* The comparison opcode */
|
|
176 int dest, /* Jump here if true. */
|
|
177 int jumpIfNull /* If true, jump if either operand is NULL */
|
|
178 ){
|
|
179 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
|
|
180 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
|
|
181 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
|
|
182 }
|
|
183
|
|
184 /*
|
|
185 ** Construct a new expression node and return a pointer to it. Memory
|
|
186 ** for this node is obtained from sqliteMalloc(). The calling function
|
|
187 ** is responsible for making sure the node eventually gets freed.
|
|
188 */
|
|
189 Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
|
|
190 Expr *pNew;
|
|
191 pNew = sqliteMalloc( sizeof(Expr) );
|
|
192 if( pNew==0 ){
|
|
193 /* When malloc fails, delete pLeft and pRight. Expressions passed to
|
|
194 ** this function must always be allocated with sqlite3Expr() for this
|
|
195 ** reason.
|
|
196 */
|
|
197 sqlite3ExprDelete(pLeft);
|
|
198 sqlite3ExprDelete(pRight);
|
|
199 return 0;
|
|
200 }
|
|
201 pNew->op = op;
|
|
202 pNew->pLeft = pLeft;
|
|
203 pNew->pRight = pRight;
|
|
204 pNew->iAgg = -1;
|
|
205 if( pToken ){
|
|
206 assert( pToken->dyn==0 );
|
|
207 pNew->span = pNew->token = *pToken;
|
|
208 }else if( pLeft && pRight ){
|
|
209 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
|
|
210 }
|
|
211 return pNew;
|
|
212 }
|
|
213
|
|
214 /*
|
|
215 ** When doing a nested parse, you can include terms in an expression
|
|
216 ** that look like this: #0 #1 #2 ... These terms refer to elements
|
|
217 ** on the stack. "#0" means the top of the stack.
|
|
218 ** "#1" means the next down on the stack. And so forth.
|
|
219 **
|
|
220 ** This routine is called by the parser to deal with on of those terms.
|
|
221 ** It immediately generates code to store the value in a memory location.
|
|
222 ** The returns an expression that will code to extract the value from
|
|
223 ** that memory location as needed.
|
|
224 */
|
|
225 Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
|
|
226 Vdbe *v = pParse->pVdbe;
|
|
227 Expr *p;
|
|
228 int depth;
|
|
229 if( pParse->nested==0 ){
|
|
230 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
|
|
231 return 0;
|
|
232 }
|
|
233 if( v==0 ) return 0;
|
|
234 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
|
|
235 if( p==0 ){
|
|
236 return 0; /* Malloc failed */
|
|
237 }
|
|
238 depth = atoi((char*)&pToken->z[1]);
|
|
239 p->iTable = pParse->nMem++;
|
|
240 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
|
|
241 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
|
|
242 return p;
|
|
243 }
|
|
244
|
|
245 /*
|
|
246 ** Join two expressions using an AND operator. If either expression is
|
|
247 ** NULL, then just return the other expression.
|
|
248 */
|
|
249 Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
|
|
250 if( pLeft==0 ){
|
|
251 return pRight;
|
|
252 }else if( pRight==0 ){
|
|
253 return pLeft;
|
|
254 }else{
|
|
255 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
|
|
256 }
|
|
257 }
|
|
258
|
|
259 /*
|
|
260 ** Set the Expr.span field of the given expression to span all
|
|
261 ** text between the two given tokens.
|
|
262 */
|
|
263 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
|
|
264 assert( pRight!=0 );
|
|
265 assert( pLeft!=0 );
|
|
266 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
|
|
267 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
|
|
268 if( pLeft->dyn==0 && pRight->dyn==0 ){
|
|
269 pExpr->span.z = pLeft->z;
|
|
270 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
|
|
271 }else{
|
|
272 pExpr->span.z = 0;
|
|
273 }
|
|
274 }
|
|
275 }
|
|
276
|
|
277 /*
|
|
278 ** Construct a new expression node for a function with multiple
|
|
279 ** arguments.
|
|
280 */
|
|
281 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
|
|
282 Expr *pNew;
|
|
283 assert( pToken );
|
|
284 pNew = sqliteMalloc( sizeof(Expr) );
|
|
285 if( pNew==0 ){
|
|
286 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
|
|
287 return 0;
|
|
288 }
|
|
289 pNew->op = TK_FUNCTION;
|
|
290 pNew->pList = pList;
|
|
291 assert( pToken->dyn==0 );
|
|
292 pNew->token = *pToken;
|
|
293 pNew->span = pNew->token;
|
|
294 return pNew;
|
|
295 }
|
|
296
|
|
297 /*
|
|
298 ** Assign a variable number to an expression that encodes a wildcard
|
|
299 ** in the original SQL statement.
|
|
300 **
|
|
301 ** Wildcards consisting of a single "?" are assigned the next sequential
|
|
302 ** variable number.
|
|
303 **
|
|
304 ** Wildcards of the form "?nnn" are assigned the number "nnn". We make
|
|
305 ** sure "nnn" is not too be to avoid a denial of service attack when
|
|
306 ** the SQL statement comes from an external source.
|
|
307 **
|
|
308 ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
|
|
309 ** as the previous instance of the same wildcard. Or if this is the first
|
|
310 ** instance of the wildcard, the next sequenial variable number is
|
|
311 ** assigned.
|
|
312 */
|
|
313 void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
|
|
314 Token *pToken;
|
|
315 if( pExpr==0 ) return;
|
|
316 pToken = &pExpr->token;
|
|
317 assert( pToken->n>=1 );
|
|
318 assert( pToken->z!=0 );
|
|
319 assert( pToken->z[0]!=0 );
|
|
320 if( pToken->n==1 ){
|
|
321 /* Wildcard of the form "?". Assign the next variable number */
|
|
322 pExpr->iTable = ++pParse->nVar;
|
|
323 }else if( pToken->z[0]=='?' ){
|
|
324 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
|
|
325 ** use it as the variable number */
|
|
326 int i;
|
|
327 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
|
|
328 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
|
|
329 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
|
|
330 SQLITE_MAX_VARIABLE_NUMBER);
|
|
331 }
|
|
332 if( i>pParse->nVar ){
|
|
333 pParse->nVar = i;
|
|
334 }
|
|
335 }else{
|
|
336 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
|
|
337 ** number as the prior appearance of the same name, or if the name
|
|
338 ** has never appeared before, reuse the same variable number
|
|
339 */
|
|
340 int i, n;
|
|
341 n = pToken->n;
|
|
342 for(i=0; i<pParse->nVarExpr; i++){
|
|
343 Expr *pE;
|
|
344 if( (pE = pParse->apVarExpr[i])!=0
|
|
345 && pE->token.n==n
|
|
346 && memcmp(pE->token.z, pToken->z, n)==0 ){
|
|
347 pExpr->iTable = pE->iTable;
|
|
348 break;
|
|
349 }
|
|
350 }
|
|
351 if( i>=pParse->nVarExpr ){
|
|
352 pExpr->iTable = ++pParse->nVar;
|
|
353 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
|
|
354 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
|
|
355 sqliteReallocOrFree((void**)&pParse->apVarExpr,
|
|
356 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
|
|
357 }
|
|
358 if( !sqlite3MallocFailed() ){
|
|
359 assert( pParse->apVarExpr!=0 );
|
|
360 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
|
|
361 }
|
|
362 }
|
|
363 }
|
|
364 }
|
|
365
|
|
366 /*
|
|
367 ** Recursively delete an expression tree.
|
|
368 */
|
|
369 void sqlite3ExprDelete(Expr *p){
|
|
370 if( p==0 ) return;
|
|
371 if( p->span.dyn ) sqliteFree((char*)p->span.z);
|
|
372 if( p->token.dyn ) sqliteFree((char*)p->token.z);
|
|
373 sqlite3ExprDelete(p->pLeft);
|
|
374 sqlite3ExprDelete(p->pRight);
|
|
375 sqlite3ExprListDelete(p->pList);
|
|
376 sqlite3SelectDelete(p->pSelect);
|
|
377 sqliteFree(p);
|
|
378 }
|
|
379
|
|
380 /*
|
|
381 ** The Expr.token field might be a string literal that is quoted.
|
|
382 ** If so, remove the quotation marks.
|
|
383 */
|
|
384 void sqlite3DequoteExpr(Expr *p){
|
|
385 if( ExprHasAnyProperty(p, EP_Dequoted) ){
|
|
386 return;
|
|
387 }
|
|
388 ExprSetProperty(p, EP_Dequoted);
|
|
389 if( p->token.dyn==0 ){
|
|
390 sqlite3TokenCopy(&p->token, &p->token);
|
|
391 }
|
|
392 sqlite3Dequote((char*)p->token.z);
|
|
393 }
|
|
394
|
|
395
|
|
396 /*
|
|
397 ** The following group of routines make deep copies of expressions,
|
|
398 ** expression lists, ID lists, and select statements. The copies can
|
|
399 ** be deleted (by being passed to their respective ...Delete() routines)
|
|
400 ** without effecting the originals.
|
|
401 **
|
|
402 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
|
|
403 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
|
|
404 ** by subsequent calls to sqlite*ListAppend() routines.
|
|
405 **
|
|
406 ** Any tables that the SrcList might point to are not duplicated.
|
|
407 */
|
|
408 Expr *sqlite3ExprDup(Expr *p){
|
|
409 Expr *pNew;
|
|
410 if( p==0 ) return 0;
|
|
411 pNew = sqliteMallocRaw( sizeof(*p) );
|
|
412 if( pNew==0 ) return 0;
|
|
413 memcpy(pNew, p, sizeof(*pNew));
|
|
414 if( p->token.z!=0 ){
|
|
415 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
|
|
416 pNew->token.dyn = 1;
|
|
417 }else{
|
|
418 assert( pNew->token.z==0 );
|
|
419 }
|
|
420 pNew->span.z = 0;
|
|
421 pNew->pLeft = sqlite3ExprDup(p->pLeft);
|
|
422 pNew->pRight = sqlite3ExprDup(p->pRight);
|
|
423 pNew->pList = sqlite3ExprListDup(p->pList);
|
|
424 pNew->pSelect = sqlite3SelectDup(p->pSelect);
|
|
425 pNew->pTab = p->pTab;
|
|
426 return pNew;
|
|
427 }
|
|
428 void sqlite3TokenCopy(Token *pTo, Token *pFrom){
|
|
429 if( pTo->dyn ) sqliteFree((char*)pTo->z);
|
|
430 if( pFrom->z ){
|
|
431 pTo->n = pFrom->n;
|
|
432 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
|
|
433 pTo->dyn = 1;
|
|
434 }else{
|
|
435 pTo->z = 0;
|
|
436 }
|
|
437 }
|
|
438 ExprList *sqlite3ExprListDup(ExprList *p){
|
|
439 ExprList *pNew;
|
|
440 struct ExprList_item *pItem, *pOldItem;
|
|
441 int i;
|
|
442 if( p==0 ) return 0;
|
|
443 pNew = sqliteMalloc( sizeof(*pNew) );
|
|
444 if( pNew==0 ) return 0;
|
|
445 pNew->nExpr = pNew->nAlloc = p->nExpr;
|
|
446 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
|
|
447 if( pItem==0 ){
|
|
448 sqliteFree(pNew);
|
|
449 return 0;
|
|
450 }
|
|
451 pOldItem = p->a;
|
|
452 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
|
|
453 Expr *pNewExpr, *pOldExpr;
|
|
454 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
|
|
455 if( pOldExpr->span.z!=0 && pNewExpr ){
|
|
456 /* Always make a copy of the span for top-level expressions in the
|
|
457 ** expression list. The logic in SELECT processing that determines
|
|
458 ** the names of columns in the result set needs this information */
|
|
459 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
|
|
460 }
|
|
461 assert( pNewExpr==0 || pNewExpr->span.z!=0
|
|
462 || pOldExpr->span.z==0
|
|
463 || sqlite3MallocFailed() );
|
|
464 pItem->zName = sqliteStrDup(pOldItem->zName);
|
|
465 pItem->sortOrder = pOldItem->sortOrder;
|
|
466 pItem->isAgg = pOldItem->isAgg;
|
|
467 pItem->done = 0;
|
|
468 }
|
|
469 return pNew;
|
|
470 }
|
|
471
|
|
472 /*
|
|
473 ** If cursors, triggers, views and subqueries are all omitted from
|
|
474 ** the build, then none of the following routines, except for
|
|
475 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
|
|
476 ** called with a NULL argument.
|
|
477 */
|
|
478 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
|
|
479 || !defined(SQLITE_OMIT_SUBQUERY)
|
|
480 SrcList *sqlite3SrcListDup(SrcList *p){
|
|
481 SrcList *pNew;
|
|
482 int i;
|
|
483 int nByte;
|
|
484 if( p==0 ) return 0;
|
|
485 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
|
|
486 pNew = sqliteMallocRaw( nByte );
|
|
487 if( pNew==0 ) return 0;
|
|
488 pNew->nSrc = pNew->nAlloc = p->nSrc;
|
|
489 for(i=0; i<p->nSrc; i++){
|
|
490 struct SrcList_item *pNewItem = &pNew->a[i];
|
|
491 struct SrcList_item *pOldItem = &p->a[i];
|
|
492 Table *pTab;
|
|
493 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
|
|
494 pNewItem->zName = sqliteStrDup(pOldItem->zName);
|
|
495 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
|
|
496 pNewItem->jointype = pOldItem->jointype;
|
|
497 pNewItem->iCursor = pOldItem->iCursor;
|
|
498 pNewItem->isPopulated = pOldItem->isPopulated;
|
|
499 pTab = pNewItem->pTab = pOldItem->pTab;
|
|
500 if( pTab ){
|
|
501 pTab->nRef++;
|
|
502 }
|
|
503 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
|
|
504 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
|
|
505 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
|
|
506 pNewItem->colUsed = pOldItem->colUsed;
|
|
507 }
|
|
508 return pNew;
|
|
509 }
|
|
510 IdList *sqlite3IdListDup(IdList *p){
|
|
511 IdList *pNew;
|
|
512 int i;
|
|
513 if( p==0 ) return 0;
|
|
514 pNew = sqliteMallocRaw( sizeof(*pNew) );
|
|
515 if( pNew==0 ) return 0;
|
|
516 pNew->nId = pNew->nAlloc = p->nId;
|
|
517 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
|
|
518 if( pNew->a==0 ){
|
|
519 sqliteFree(pNew);
|
|
520 return 0;
|
|
521 }
|
|
522 for(i=0; i<p->nId; i++){
|
|
523 struct IdList_item *pNewItem = &pNew->a[i];
|
|
524 struct IdList_item *pOldItem = &p->a[i];
|
|
525 pNewItem->zName = sqliteStrDup(pOldItem->zName);
|
|
526 pNewItem->idx = pOldItem->idx;
|
|
527 }
|
|
528 return pNew;
|
|
529 }
|
|
530 Select *sqlite3SelectDup(Select *p){
|
|
531 Select *pNew;
|
|
532 if( p==0 ) return 0;
|
|
533 pNew = sqliteMallocRaw( sizeof(*p) );
|
|
534 if( pNew==0 ) return 0;
|
|
535 pNew->isDistinct = p->isDistinct;
|
|
536 pNew->pEList = sqlite3ExprListDup(p->pEList);
|
|
537 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
|
|
538 pNew->pWhere = sqlite3ExprDup(p->pWhere);
|
|
539 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
|
|
540 pNew->pHaving = sqlite3ExprDup(p->pHaving);
|
|
541 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
|
|
542 pNew->op = p->op;
|
|
543 pNew->pPrior = sqlite3SelectDup(p->pPrior);
|
|
544 pNew->pLimit = sqlite3ExprDup(p->pLimit);
|
|
545 pNew->pOffset = sqlite3ExprDup(p->pOffset);
|
|
546 pNew->iLimit = -1;
|
|
547 pNew->iOffset = -1;
|
|
548 pNew->isResolved = p->isResolved;
|
|
549 pNew->isAgg = p->isAgg;
|
|
550 pNew->usesVirt = 0;
|
|
551 pNew->disallowOrderBy = 0;
|
|
552 pNew->pRightmost = 0;
|
|
553 pNew->addrOpenVirt[0] = -1;
|
|
554 pNew->addrOpenVirt[1] = -1;
|
|
555 pNew->addrOpenVirt[2] = -1;
|
|
556 return pNew;
|
|
557 }
|
|
558 #else
|
|
559 Select *sqlite3SelectDup(Select *p){
|
|
560 assert( p==0 );
|
|
561 return 0;
|
|
562 }
|
|
563 #endif
|
|
564
|
|
565
|
|
566 /*
|
|
567 ** Add a new element to the end of an expression list. If pList is
|
|
568 ** initially NULL, then create a new expression list.
|
|
569 */
|
|
570 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
|
|
571 if( pList==0 ){
|
|
572 pList = sqliteMalloc( sizeof(ExprList) );
|
|
573 if( pList==0 ){
|
|
574 goto no_mem;
|
|
575 }
|
|
576 assert( pList->nAlloc==0 );
|
|
577 }
|
|
578 if( pList->nAlloc<=pList->nExpr ){
|
|
579 struct ExprList_item *a;
|
|
580 int n = pList->nAlloc*2 + 4;
|
|
581 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
|
|
582 if( a==0 ){
|
|
583 goto no_mem;
|
|
584 }
|
|
585 pList->a = a;
|
|
586 pList->nAlloc = n;
|
|
587 }
|
|
588 assert( pList->a!=0 );
|
|
589 if( pExpr || pName ){
|
|
590 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
|
|
591 memset(pItem, 0, sizeof(*pItem));
|
|
592 pItem->zName = sqlite3NameFromToken(pName);
|
|
593 pItem->pExpr = pExpr;
|
|
594 }
|
|
595 return pList;
|
|
596
|
|
597 no_mem:
|
|
598 /* Avoid leaking memory if malloc has failed. */
|
|
599 sqlite3ExprDelete(pExpr);
|
|
600 sqlite3ExprListDelete(pList);
|
|
601 return 0;
|
|
602 }
|
|
603
|
|
604 /*
|
|
605 ** Delete an entire expression list.
|
|
606 */
|
|
607 void sqlite3ExprListDelete(ExprList *pList){
|
|
608 int i;
|
|
609 struct ExprList_item *pItem;
|
|
610 if( pList==0 ) return;
|
|
611 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
|
|
612 assert( pList->nExpr<=pList->nAlloc );
|
|
613 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
|
|
614 sqlite3ExprDelete(pItem->pExpr);
|
|
615 sqliteFree(pItem->zName);
|
|
616 }
|
|
617 sqliteFree(pList->a);
|
|
618 sqliteFree(pList);
|
|
619 }
|
|
620
|
|
621 /*
|
|
622 ** Walk an expression tree. Call xFunc for each node visited.
|
|
623 **
|
|
624 ** The return value from xFunc determines whether the tree walk continues.
|
|
625 ** 0 means continue walking the tree. 1 means do not walk children
|
|
626 ** of the current node but continue with siblings. 2 means abandon
|
|
627 ** the tree walk completely.
|
|
628 **
|
|
629 ** The return value from this routine is 1 to abandon the tree walk
|
|
630 ** and 0 to continue.
|
|
631 **
|
|
632 ** NOTICE: This routine does *not* descend into subqueries.
|
|
633 */
|
|
634 static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
|
|
635 static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
|
|
636 int rc;
|
|
637 if( pExpr==0 ) return 0;
|
|
638 rc = (*xFunc)(pArg, pExpr);
|
|
639 if( rc==0 ){
|
|
640 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
|
|
641 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
|
|
642 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
|
|
643 }
|
|
644 return rc>1;
|
|
645 }
|
|
646
|
|
647 /*
|
|
648 ** Call walkExprTree() for every expression in list p.
|
|
649 */
|
|
650 static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
|
|
651 int i;
|
|
652 struct ExprList_item *pItem;
|
|
653 if( !p ) return 0;
|
|
654 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
|
|
655 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
|
|
656 }
|
|
657 return 0;
|
|
658 }
|
|
659
|
|
660 /*
|
|
661 ** Call walkExprTree() for every expression in Select p, not including
|
|
662 ** expressions that are part of sub-selects in any FROM clause or the LIMIT
|
|
663 ** or OFFSET expressions..
|
|
664 */
|
|
665 static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
|
|
666 walkExprList(p->pEList, xFunc, pArg);
|
|
667 walkExprTree(p->pWhere, xFunc, pArg);
|
|
668 walkExprList(p->pGroupBy, xFunc, pArg);
|
|
669 walkExprTree(p->pHaving, xFunc, pArg);
|
|
670 walkExprList(p->pOrderBy, xFunc, pArg);
|
|
671 return 0;
|
|
672 }
|
|
673
|
|
674
|
|
675 /*
|
|
676 ** This routine is designed as an xFunc for walkExprTree().
|
|
677 **
|
|
678 ** pArg is really a pointer to an integer. If we can tell by looking
|
|
679 ** at pExpr that the expression that contains pExpr is not a constant
|
|
680 ** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
|
|
681 ** If pExpr does does not disqualify the expression from being a constant
|
|
682 ** then do nothing.
|
|
683 **
|
|
684 ** After walking the whole tree, if no nodes are found that disqualify
|
|
685 ** the expression as constant, then we assume the whole expression
|
|
686 ** is constant. See sqlite3ExprIsConstant() for additional information.
|
|
687 */
|
|
688 static int exprNodeIsConstant(void *pArg, Expr *pExpr){
|
|
689 switch( pExpr->op ){
|
|
690 /* Consider functions to be constant if all their arguments are constant
|
|
691 ** and *pArg==2 */
|
|
692 case TK_FUNCTION:
|
|
693 if( *((int*)pArg)==2 ) return 0;
|
|
694 /* Fall through */
|
|
695 case TK_ID:
|
|
696 case TK_COLUMN:
|
|
697 case TK_DOT:
|
|
698 case TK_AGG_FUNCTION:
|
|
699 case TK_AGG_COLUMN:
|
|
700 #ifndef SQLITE_OMIT_SUBQUERY
|
|
701 case TK_SELECT:
|
|
702 case TK_EXISTS:
|
|
703 #endif
|
|
704 *((int*)pArg) = 0;
|
|
705 return 2;
|
|
706 case TK_IN:
|
|
707 if( pExpr->pSelect ){
|
|
708 *((int*)pArg) = 0;
|
|
709 return 2;
|
|
710 }
|
|
711 default:
|
|
712 return 0;
|
|
713 }
|
|
714 }
|
|
715
|
|
716 /*
|
|
717 ** Walk an expression tree. Return 1 if the expression is constant
|
|
718 ** and 0 if it involves variables or function calls.
|
|
719 **
|
|
720 ** For the purposes of this function, a double-quoted string (ex: "abc")
|
|
721 ** is considered a variable but a single-quoted string (ex: 'abc') is
|
|
722 ** a constant.
|
|
723 */
|
|
724 int sqlite3ExprIsConstant(Expr *p){
|
|
725 int isConst = 1;
|
|
726 walkExprTree(p, exprNodeIsConstant, &isConst);
|
|
727 return isConst;
|
|
728 }
|
|
729
|
|
730 /*
|
|
731 ** Walk an expression tree. Return 1 if the expression is constant
|
|
732 ** or a function call with constant arguments. Return and 0 if there
|
|
733 ** are any variables.
|
|
734 **
|
|
735 ** For the purposes of this function, a double-quoted string (ex: "abc")
|
|
736 ** is considered a variable but a single-quoted string (ex: 'abc') is
|
|
737 ** a constant.
|
|
738 */
|
|
739 int sqlite3ExprIsConstantOrFunction(Expr *p){
|
|
740 int isConst = 2;
|
|
741 walkExprTree(p, exprNodeIsConstant, &isConst);
|
|
742 return isConst!=0;
|
|
743 }
|
|
744
|
|
745 /*
|
|
746 ** If the expression p codes a constant integer that is small enough
|
|
747 ** to fit in a 32-bit integer, return 1 and put the value of the integer
|
|
748 ** in *pValue. If the expression is not an integer or if it is too big
|
|
749 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
|
|
750 */
|
|
751 int sqlite3ExprIsInteger(Expr *p, int *pValue){
|
|
752 switch( p->op ){
|
|
753 case TK_INTEGER: {
|
|
754 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
|
|
755 return 1;
|
|
756 }
|
|
757 break;
|
|
758 }
|
|
759 case TK_UPLUS: {
|
|
760 return sqlite3ExprIsInteger(p->pLeft, pValue);
|
|
761 }
|
|
762 case TK_UMINUS: {
|
|
763 int v;
|
|
764 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
|
|
765 *pValue = -v;
|
|
766 return 1;
|
|
767 }
|
|
768 break;
|
|
769 }
|
|
770 default: break;
|
|
771 }
|
|
772 return 0;
|
|
773 }
|
|
774
|
|
775 /*
|
|
776 ** Return TRUE if the given string is a row-id column name.
|
|
777 */
|
|
778 int sqlite3IsRowid(const char *z){
|
|
779 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
|
|
780 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
|
|
781 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
|
|
782 return 0;
|
|
783 }
|
|
784
|
|
785 /*
|
|
786 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
|
|
787 ** that name in the set of source tables in pSrcList and make the pExpr
|
|
788 ** expression node refer back to that source column. The following changes
|
|
789 ** are made to pExpr:
|
|
790 **
|
|
791 ** pExpr->iDb Set the index in db->aDb[] of the database holding
|
|
792 ** the table.
|
|
793 ** pExpr->iTable Set to the cursor number for the table obtained
|
|
794 ** from pSrcList.
|
|
795 ** pExpr->iColumn Set to the column number within the table.
|
|
796 ** pExpr->op Set to TK_COLUMN.
|
|
797 ** pExpr->pLeft Any expression this points to is deleted
|
|
798 ** pExpr->pRight Any expression this points to is deleted.
|
|
799 **
|
|
800 ** The pDbToken is the name of the database (the "X"). This value may be
|
|
801 ** NULL meaning that name is of the form Y.Z or Z. Any available database
|
|
802 ** can be used. The pTableToken is the name of the table (the "Y"). This
|
|
803 ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
|
|
804 ** means that the form of the name is Z and that columns from any table
|
|
805 ** can be used.
|
|
806 **
|
|
807 ** If the name cannot be resolved unambiguously, leave an error message
|
|
808 ** in pParse and return non-zero. Return zero on success.
|
|
809 */
|
|
810 static int lookupName(
|
|
811 Parse *pParse, /* The parsing context */
|
|
812 Token *pDbToken, /* Name of the database containing table, or NULL */
|
|
813 Token *pTableToken, /* Name of table containing column, or NULL */
|
|
814 Token *pColumnToken, /* Name of the column. */
|
|
815 NameContext *pNC, /* The name context used to resolve the name */
|
|
816 Expr *pExpr /* Make this EXPR node point to the selected column */
|
|
817 ){
|
|
818 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
|
|
819 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
|
|
820 char *zCol = 0; /* Name of the column. The "Z" */
|
|
821 int i, j; /* Loop counters */
|
|
822 int cnt = 0; /* Number of matching column names */
|
|
823 int cntTab = 0; /* Number of matching table names */
|
|
824 sqlite3 *db = pParse->db; /* The database */
|
|
825 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
|
|
826 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
|
|
827 NameContext *pTopNC = pNC; /* First namecontext in the list */
|
|
828
|
|
829 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
|
|
830 zDb = sqlite3NameFromToken(pDbToken);
|
|
831 zTab = sqlite3NameFromToken(pTableToken);
|
|
832 zCol = sqlite3NameFromToken(pColumnToken);
|
|
833 if( sqlite3MallocFailed() ){
|
|
834 goto lookupname_end;
|
|
835 }
|
|
836
|
|
837 pExpr->iTable = -1;
|
|
838 while( pNC && cnt==0 ){
|
|
839 ExprList *pEList;
|
|
840 SrcList *pSrcList = pNC->pSrcList;
|
|
841
|
|
842 if( pSrcList ){
|
|
843 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
|
|
844 Table *pTab;
|
|
845 int iDb;
|
|
846 Column *pCol;
|
|
847
|
|
848 pTab = pItem->pTab;
|
|
849 assert( pTab!=0 );
|
|
850 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
|
|
851 assert( pTab->nCol>0 );
|
|
852 if( zTab ){
|
|
853 if( pItem->zAlias ){
|
|
854 char *zTabName = pItem->zAlias;
|
|
855 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
|
|
856 }else{
|
|
857 char *zTabName = pTab->zName;
|
|
858 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
|
|
859 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
|
|
860 continue;
|
|
861 }
|
|
862 }
|
|
863 }
|
|
864 if( 0==(cntTab++) ){
|
|
865 pExpr->iTable = pItem->iCursor;
|
|
866 pExpr->pSchema = pTab->pSchema;
|
|
867 pMatch = pItem;
|
|
868 }
|
|
869 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
|
|
870 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
|
|
871 const char *zColl = pTab->aCol[j].zColl;
|
|
872 IdList *pUsing;
|
|
873 cnt++;
|
|
874 pExpr->iTable = pItem->iCursor;
|
|
875 pMatch = pItem;
|
|
876 pExpr->pSchema = pTab->pSchema;
|
|
877 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
|
|
878 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
|
|
879 pExpr->affinity = pTab->aCol[j].affinity;
|
|
880 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
|
|
881 if( pItem->jointype & JT_NATURAL ){
|
|
882 /* If this match occurred in the left table of a natural join,
|
|
883 ** then skip the right table to avoid a duplicate match */
|
|
884 pItem++;
|
|
885 i++;
|
|
886 }
|
|
887 if( (pUsing = pItem->pUsing)!=0 ){
|
|
888 /* If this match occurs on a column that is in the USING clause
|
|
889 ** of a join, skip the search of the right table of the join
|
|
890 ** to avoid a duplicate match there. */
|
|
891 int k;
|
|
892 for(k=0; k<pUsing->nId; k++){
|
|
893 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
|
|
894 pItem++;
|
|
895 i++;
|
|
896 break;
|
|
897 }
|
|
898 }
|
|
899 }
|
|
900 break;
|
|
901 }
|
|
902 }
|
|
903 }
|
|
904 }
|
|
905
|
|
906 #ifndef SQLITE_OMIT_TRIGGER
|
|
907 /* If we have not already resolved the name, then maybe
|
|
908 ** it is a new.* or old.* trigger argument reference
|
|
909 */
|
|
910 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
|
|
911 TriggerStack *pTriggerStack = pParse->trigStack;
|
|
912 Table *pTab = 0;
|
|
913 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
|
|
914 pExpr->iTable = pTriggerStack->newIdx;
|
|
915 assert( pTriggerStack->pTab );
|
|
916 pTab = pTriggerStack->pTab;
|
|
917 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
|
|
918 pExpr->iTable = pTriggerStack->oldIdx;
|
|
919 assert( pTriggerStack->pTab );
|
|
920 pTab = pTriggerStack->pTab;
|
|
921 }
|
|
922
|
|
923 if( pTab ){
|
|
924 int iCol;
|
|
925 Column *pCol = pTab->aCol;
|
|
926
|
|
927 pExpr->pSchema = pTab->pSchema;
|
|
928 cntTab++;
|
|
929 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
|
|
930 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
|
|
931 const char *zColl = pTab->aCol[iCol].zColl;
|
|
932 cnt++;
|
|
933 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
|
|
934 pExpr->affinity = pTab->aCol[iCol].affinity;
|
|
935 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
|
|
936 pExpr->pTab = pTab;
|
|
937 break;
|
|
938 }
|
|
939 }
|
|
940 }
|
|
941 }
|
|
942 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
|
|
943
|
|
944 /*
|
|
945 ** Perhaps the name is a reference to the ROWID
|
|
946 */
|
|
947 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
|
|
948 cnt = 1;
|
|
949 pExpr->iColumn = -1;
|
|
950 pExpr->affinity = SQLITE_AFF_INTEGER;
|
|
951 }
|
|
952
|
|
953 /*
|
|
954 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
|
|
955 ** might refer to an result-set alias. This happens, for example, when
|
|
956 ** we are resolving names in the WHERE clause of the following command:
|
|
957 **
|
|
958 ** SELECT a+b AS x FROM table WHERE x<10;
|
|
959 **
|
|
960 ** In cases like this, replace pExpr with a copy of the expression that
|
|
961 ** forms the result set entry ("a+b" in the example) and return immediately.
|
|
962 ** Note that the expression in the result set should have already been
|
|
963 ** resolved by the time the WHERE clause is resolved.
|
|
964 */
|
|
965 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
|
|
966 for(j=0; j<pEList->nExpr; j++){
|
|
967 char *zAs = pEList->a[j].zName;
|
|
968 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
|
|
969 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
|
|
970 pExpr->op = TK_AS;
|
|
971 pExpr->iColumn = j;
|
|
972 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
|
|
973 cnt = 1;
|
|
974 assert( zTab==0 && zDb==0 );
|
|
975 goto lookupname_end_2;
|
|
976 }
|
|
977 }
|
|
978 }
|
|
979
|
|
980 /* Advance to the next name context. The loop will exit when either
|
|
981 ** we have a match (cnt>0) or when we run out of name contexts.
|
|
982 */
|
|
983 if( cnt==0 ){
|
|
984 pNC = pNC->pNext;
|
|
985 }
|
|
986 }
|
|
987
|
|
988 /*
|
|
989 ** If X and Y are NULL (in other words if only the column name Z is
|
|
990 ** supplied) and the value of Z is enclosed in double-quotes, then
|
|
991 ** Z is a string literal if it doesn't match any column names. In that
|
|
992 ** case, we need to return right away and not make any changes to
|
|
993 ** pExpr.
|
|
994 **
|
|
995 ** Because no reference was made to outer contexts, the pNC->nRef
|
|
996 ** fields are not changed in any context.
|
|
997 */
|
|
998 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
|
|
999 sqliteFree(zCol);
|
|
1000 return 0;
|
|
1001 }
|
|
1002
|
|
1003 /*
|
|
1004 ** cnt==0 means there was not match. cnt>1 means there were two or
|
|
1005 ** more matches. Either way, we have an error.
|
|
1006 */
|
|
1007 if( cnt!=1 ){
|
|
1008 char *z = 0;
|
|
1009 char *zErr;
|
|
1010 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
|
|
1011 if( zDb ){
|
|
1012 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
|
|
1013 }else if( zTab ){
|
|
1014 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
|
|
1015 }else{
|
|
1016 z = sqliteStrDup(zCol);
|
|
1017 }
|
|
1018 sqlite3ErrorMsg(pParse, zErr, z);
|
|
1019 sqliteFree(z);
|
|
1020 pTopNC->nErr++;
|
|
1021 }
|
|
1022
|
|
1023 /* If a column from a table in pSrcList is referenced, then record
|
|
1024 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
|
|
1025 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
|
|
1026 ** column number is greater than the number of bits in the bitmask
|
|
1027 ** then set the high-order bit of the bitmask.
|
|
1028 */
|
|
1029 if( pExpr->iColumn>=0 && pMatch!=0 ){
|
|
1030 int n = pExpr->iColumn;
|
|
1031 if( n>=sizeof(Bitmask)*8 ){
|
|
1032 n = sizeof(Bitmask)*8-1;
|
|
1033 }
|
|
1034 assert( pMatch->iCursor==pExpr->iTable );
|
|
1035 pMatch->colUsed |= 1<<n;
|
|
1036 }
|
|
1037
|
|
1038 lookupname_end:
|
|
1039 /* Clean up and return
|
|
1040 */
|
|
1041 sqliteFree(zDb);
|
|
1042 sqliteFree(zTab);
|
|
1043 sqlite3ExprDelete(pExpr->pLeft);
|
|
1044 pExpr->pLeft = 0;
|
|
1045 sqlite3ExprDelete(pExpr->pRight);
|
|
1046 pExpr->pRight = 0;
|
|
1047 pExpr->op = TK_COLUMN;
|
|
1048 lookupname_end_2:
|
|
1049 sqliteFree(zCol);
|
|
1050 if( cnt==1 ){
|
|
1051 assert( pNC!=0 );
|
|
1052 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
|
|
1053 if( pMatch && !pMatch->pSelect ){
|
|
1054 pExpr->pTab = pMatch->pTab;
|
|
1055 }
|
|
1056 /* Increment the nRef value on all name contexts from TopNC up to
|
|
1057 ** the point where the name matched. */
|
|
1058 for(;;){
|
|
1059 assert( pTopNC!=0 );
|
|
1060 pTopNC->nRef++;
|
|
1061 if( pTopNC==pNC ) break;
|
|
1062 pTopNC = pTopNC->pNext;
|
|
1063 }
|
|
1064 return 0;
|
|
1065 } else {
|
|
1066 return 1;
|
|
1067 }
|
|
1068 }
|
|
1069
|
|
1070 /*
|
|
1071 ** This routine is designed as an xFunc for walkExprTree().
|
|
1072 **
|
|
1073 ** Resolve symbolic names into TK_COLUMN operators for the current
|
|
1074 ** node in the expression tree. Return 0 to continue the search down
|
|
1075 ** the tree or 2 to abort the tree walk.
|
|
1076 **
|
|
1077 ** This routine also does error checking and name resolution for
|
|
1078 ** function names. The operator for aggregate functions is changed
|
|
1079 ** to TK_AGG_FUNCTION.
|
|
1080 */
|
|
1081 static int nameResolverStep(void *pArg, Expr *pExpr){
|
|
1082 NameContext *pNC = (NameContext*)pArg;
|
|
1083 Parse *pParse;
|
|
1084
|
|
1085 if( pExpr==0 ) return 1;
|
|
1086 assert( pNC!=0 );
|
|
1087 pParse = pNC->pParse;
|
|
1088
|
|
1089 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
|
|
1090 ExprSetProperty(pExpr, EP_Resolved);
|
|
1091 #ifndef NDEBUG
|
|
1092 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
|
|
1093 SrcList *pSrcList = pNC->pSrcList;
|
|
1094 int i;
|
|
1095 for(i=0; i<pNC->pSrcList->nSrc; i++){
|
|
1096 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
|
|
1097 }
|
|
1098 }
|
|
1099 #endif
|
|
1100 switch( pExpr->op ){
|
|
1101 /* Double-quoted strings (ex: "abc") are used as identifiers if
|
|
1102 ** possible. Otherwise they remain as strings. Single-quoted
|
|
1103 ** strings (ex: 'abc') are always string literals.
|
|
1104 */
|
|
1105 case TK_STRING: {
|
|
1106 if( pExpr->token.z[0]=='\'' ) break;
|
|
1107 /* Fall thru into the TK_ID case if this is a double-quoted string */
|
|
1108 }
|
|
1109 /* A lone identifier is the name of a column.
|
|
1110 */
|
|
1111 case TK_ID: {
|
|
1112 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
|
|
1113 return 1;
|
|
1114 }
|
|
1115
|
|
1116 /* A table name and column name: ID.ID
|
|
1117 ** Or a database, table and column: ID.ID.ID
|
|
1118 */
|
|
1119 case TK_DOT: {
|
|
1120 Token *pColumn;
|
|
1121 Token *pTable;
|
|
1122 Token *pDb;
|
|
1123 Expr *pRight;
|
|
1124
|
|
1125 /* if( pSrcList==0 ) break; */
|
|
1126 pRight = pExpr->pRight;
|
|
1127 if( pRight->op==TK_ID ){
|
|
1128 pDb = 0;
|
|
1129 pTable = &pExpr->pLeft->token;
|
|
1130 pColumn = &pRight->token;
|
|
1131 }else{
|
|
1132 assert( pRight->op==TK_DOT );
|
|
1133 pDb = &pExpr->pLeft->token;
|
|
1134 pTable = &pRight->pLeft->token;
|
|
1135 pColumn = &pRight->pRight->token;
|
|
1136 }
|
|
1137 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
|
|
1138 return 1;
|
|
1139 }
|
|
1140
|
|
1141 /* Resolve function names
|
|
1142 */
|
|
1143 case TK_CONST_FUNC:
|
|
1144 case TK_FUNCTION: {
|
|
1145 ExprList *pList = pExpr->pList; /* The argument list */
|
|
1146 int n = pList ? pList->nExpr : 0; /* Number of arguments */
|
|
1147 int no_such_func = 0; /* True if no such function exists */
|
|
1148 int wrong_num_args = 0; /* True if wrong number of arguments */
|
|
1149 int is_agg = 0; /* True if is an aggregate function */
|
|
1150 int i;
|
|
1151 int nId; /* Number of characters in function name */
|
|
1152 const char *zId; /* The function name. */
|
|
1153 FuncDef *pDef; /* Information about the function */
|
|
1154 int enc = ENC(pParse->db); /* The database encoding */
|
|
1155
|
|
1156 zId = (char*)pExpr->token.z;
|
|
1157 nId = pExpr->token.n;
|
|
1158 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
|
|
1159 if( pDef==0 ){
|
|
1160 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
|
|
1161 if( pDef==0 ){
|
|
1162 no_such_func = 1;
|
|
1163 }else{
|
|
1164 wrong_num_args = 1;
|
|
1165 }
|
|
1166 }else{
|
|
1167 is_agg = pDef->xFunc==0;
|
|
1168 }
|
|
1169 if( is_agg && !pNC->allowAgg ){
|
|
1170 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
|
|
1171 pNC->nErr++;
|
|
1172 is_agg = 0;
|
|
1173 }else if( no_such_func ){
|
|
1174 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
|
|
1175 pNC->nErr++;
|
|
1176 }else if( wrong_num_args ){
|
|
1177 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
|
|
1178 nId, zId);
|
|
1179 pNC->nErr++;
|
|
1180 }
|
|
1181 if( is_agg ){
|
|
1182 pExpr->op = TK_AGG_FUNCTION;
|
|
1183 pNC->hasAgg = 1;
|
|
1184 }
|
|
1185 if( is_agg ) pNC->allowAgg = 0;
|
|
1186 for(i=0; pNC->nErr==0 && i<n; i++){
|
|
1187 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
|
|
1188 }
|
|
1189 if( is_agg ) pNC->allowAgg = 1;
|
|
1190 /* FIX ME: Compute pExpr->affinity based on the expected return
|
|
1191 ** type of the function
|
|
1192 */
|
|
1193 return is_agg;
|
|
1194 }
|
|
1195 #ifndef SQLITE_OMIT_SUBQUERY
|
|
1196 case TK_SELECT:
|
|
1197 case TK_EXISTS:
|
|
1198 #endif
|
|
1199 case TK_IN: {
|
|
1200 if( pExpr->pSelect ){
|
|
1201 int nRef = pNC->nRef;
|
|
1202 #ifndef SQLITE_OMIT_CHECK
|
|
1203 if( pNC->isCheck ){
|
|
1204 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
|
|
1205 }
|
|
1206 #endif
|
|
1207 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
|
|
1208 assert( pNC->nRef>=nRef );
|
|
1209 if( nRef!=pNC->nRef ){
|
|
1210 ExprSetProperty(pExpr, EP_VarSelect);
|
|
1211 }
|
|
1212 }
|
|
1213 break;
|
|
1214 }
|
|
1215 #ifndef SQLITE_OMIT_CHECK
|
|
1216 case TK_VARIABLE: {
|
|
1217 if( pNC->isCheck ){
|
|
1218 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
|
|
1219 }
|
|
1220 break;
|
|
1221 }
|
|
1222 #endif
|
|
1223 }
|
|
1224 return 0;
|
|
1225 }
|
|
1226
|
|
1227 /*
|
|
1228 ** This routine walks an expression tree and resolves references to
|
|
1229 ** table columns. Nodes of the form ID.ID or ID resolve into an
|
|
1230 ** index to the table in the table list and a column offset. The
|
|
1231 ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
|
|
1232 ** value is changed to the index of the referenced table in pTabList
|
|
1233 ** plus the "base" value. The base value will ultimately become the
|
|
1234 ** VDBE cursor number for a cursor that is pointing into the referenced
|
|
1235 ** table. The Expr.iColumn value is changed to the index of the column
|
|
1236 ** of the referenced table. The Expr.iColumn value for the special
|
|
1237 ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
|
|
1238 ** alias for ROWID.
|
|
1239 **
|
|
1240 ** Also resolve function names and check the functions for proper
|
|
1241 ** usage. Make sure all function names are recognized and all functions
|
|
1242 ** have the correct number of arguments. Leave an error message
|
|
1243 ** in pParse->zErrMsg if anything is amiss. Return the number of errors.
|
|
1244 **
|
|
1245 ** If the expression contains aggregate functions then set the EP_Agg
|
|
1246 ** property on the expression.
|
|
1247 */
|
|
1248 int sqlite3ExprResolveNames(
|
|
1249 NameContext *pNC, /* Namespace to resolve expressions in. */
|
|
1250 Expr *pExpr /* The expression to be analyzed. */
|
|
1251 ){
|
|
1252 int savedHasAgg;
|
|
1253 if( pExpr==0 ) return 0;
|
|
1254 savedHasAgg = pNC->hasAgg;
|
|
1255 pNC->hasAgg = 0;
|
|
1256 walkExprTree(pExpr, nameResolverStep, pNC);
|
|
1257 if( pNC->nErr>0 ){
|
|
1258 ExprSetProperty(pExpr, EP_Error);
|
|
1259 }
|
|
1260 if( pNC->hasAgg ){
|
|
1261 ExprSetProperty(pExpr, EP_Agg);
|
|
1262 }else if( savedHasAgg ){
|
|
1263 pNC->hasAgg = 1;
|
|
1264 }
|
|
1265 return ExprHasProperty(pExpr, EP_Error);
|
|
1266 }
|
|
1267
|
|
1268 /*
|
|
1269 ** A pointer instance of this structure is used to pass information
|
|
1270 ** through walkExprTree into codeSubqueryStep().
|
|
1271 */
|
|
1272 typedef struct QueryCoder QueryCoder;
|
|
1273 struct QueryCoder {
|
|
1274 Parse *pParse; /* The parsing context */
|
|
1275 NameContext *pNC; /* Namespace of first enclosing query */
|
|
1276 };
|
|
1277
|
|
1278
|
|
1279 /*
|
|
1280 ** Generate code for scalar subqueries used as an expression
|
|
1281 ** and IN operators. Examples:
|
|
1282 **
|
|
1283 ** (SELECT a FROM b) -- subquery
|
|
1284 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
|
|
1285 ** x IN (4,5,11) -- IN operator with list on right-hand side
|
|
1286 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
|
|
1287 **
|
|
1288 ** The pExpr parameter describes the expression that contains the IN
|
|
1289 ** operator or subquery.
|
|
1290 */
|
|
1291 #ifndef SQLITE_OMIT_SUBQUERY
|
|
1292 void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
|
|
1293 int testAddr = 0; /* One-time test address */
|
|
1294 Vdbe *v = sqlite3GetVdbe(pParse);
|
|
1295 if( v==0 ) return;
|
|
1296
|
|
1297 /* This code must be run in its entirety every time it is encountered
|
|
1298 ** if any of the following is true:
|
|
1299 **
|
|
1300 ** * The right-hand side is a correlated subquery
|
|
1301 ** * The right-hand side is an expression list containing variables
|
|
1302 ** * We are inside a trigger
|
|
1303 **
|
|
1304 ** If all of the above are false, then we can run this code just once
|
|
1305 ** save the results, and reuse the same result on subsequent invocations.
|
|
1306 */
|
|
1307 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
|
|
1308 int mem = pParse->nMem++;
|
|
1309 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
|
|
1310 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
|
|
1311 assert( testAddr>0 || sqlite3MallocFailed() );
|
|
1312 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
|
|
1313 }
|
|
1314
|
|
1315 switch( pExpr->op ){
|
|
1316 case TK_IN: {
|
|
1317 char affinity;
|
|
1318 KeyInfo keyInfo;
|
|
1319 int addr; /* Address of OP_OpenVirtual instruction */
|
|
1320
|
|
1321 affinity = sqlite3ExprAffinity(pExpr->pLeft);
|
|
1322
|
|
1323 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
|
|
1324 ** expression it is handled the same way. A virtual table is
|
|
1325 ** filled with single-field index keys representing the results
|
|
1326 ** from the SELECT or the <exprlist>.
|
|
1327 **
|
|
1328 ** If the 'x' expression is a column value, or the SELECT...
|
|
1329 ** statement returns a column value, then the affinity of that
|
|
1330 ** column is used to build the index keys. If both 'x' and the
|
|
1331 ** SELECT... statement are columns, then numeric affinity is used
|
|
1332 ** if either column has NUMERIC or INTEGER affinity. If neither
|
|
1333 ** 'x' nor the SELECT... statement are columns, then numeric affinity
|
|
1334 ** is used.
|
|
1335 */
|
|
1336 pExpr->iTable = pParse->nTab++;
|
|
1337 addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0);
|
|
1338 memset(&keyInfo, 0, sizeof(keyInfo));
|
|
1339 keyInfo.nField = 1;
|
|
1340 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
|
|
1341
|
|
1342 if( pExpr->pSelect ){
|
|
1343 /* Case 1: expr IN (SELECT ...)
|
|
1344 **
|
|
1345 ** Generate code to write the results of the select into the temporary
|
|
1346 ** table allocated and opened above.
|
|
1347 */
|
|
1348 int iParm = pExpr->iTable + (((int)affinity)<<16);
|
|
1349 ExprList *pEList;
|
|
1350 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
|
|
1351 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
|
|
1352 pEList = pExpr->pSelect->pEList;
|
|
1353 if( pEList && pEList->nExpr>0 ){
|
|
1354 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
|
|
1355 pEList->a[0].pExpr);
|
|
1356 }
|
|
1357 }else if( pExpr->pList ){
|
|
1358 /* Case 2: expr IN (exprlist)
|
|
1359 **
|
|
1360 ** For each expression, build an index key from the evaluation and
|
|
1361 ** store it in the temporary table. If <expr> is a column, then use
|
|
1362 ** that columns affinity when building index keys. If <expr> is not
|
|
1363 ** a column, use numeric affinity.
|
|
1364 */
|
|
1365 int i;
|
|
1366 ExprList *pList = pExpr->pList;
|
|
1367 struct ExprList_item *pItem;
|
|
1368
|
|
1369 if( !affinity ){
|
|
1370 affinity = SQLITE_AFF_NONE;
|
|
1371 }
|
|
1372 keyInfo.aColl[0] = pExpr->pLeft->pColl;
|
|
1373
|
|
1374 /* Loop through each expression in <exprlist>. */
|
|
1375 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
|
|
1376 Expr *pE2 = pItem->pExpr;
|
|
1377
|
|
1378 /* If the expression is not constant then we will need to
|
|
1379 ** disable the test that was generated above that makes sure
|
|
1380 ** this code only executes once. Because for a non-constant
|
|
1381 ** expression we need to rerun this code each time.
|
|
1382 */
|
|
1383 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
|
|
1384 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
|
|
1385 testAddr = 0;
|
|
1386 }
|
|
1387
|
|
1388 /* Evaluate the expression and insert it into the temp table */
|
|
1389 sqlite3ExprCode(pParse, pE2);
|
|
1390 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
|
|
1391 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
|
|
1392 }
|
|
1393 }
|
|
1394 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
|
|
1395 break;
|
|
1396 }
|
|
1397
|
|
1398 case TK_EXISTS:
|
|
1399 case TK_SELECT: {
|
|
1400 /* This has to be a scalar SELECT. Generate code to put the
|
|
1401 ** value of this select in a memory cell and record the number
|
|
1402 ** of the memory cell in iColumn.
|
|
1403 */
|
|
1404 static const Token one = { (u8*)"1", 0, 1 };
|
|
1405 Select *pSel;
|
|
1406 int iMem;
|
|
1407 int sop;
|
|
1408
|
|
1409 pExpr->iColumn = iMem = pParse->nMem++;
|
|
1410 pSel = pExpr->pSelect;
|
|
1411 if( pExpr->op==TK_SELECT ){
|
|
1412 sop = SRT_Mem;
|
|
1413 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
|
|
1414 VdbeComment((v, "# Init subquery result"));
|
|
1415 }else{
|
|
1416 sop = SRT_Exists;
|
|
1417 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
|
|
1418 VdbeComment((v, "# Init EXISTS result"));
|
|
1419 }
|
|
1420 sqlite3ExprDelete(pSel->pLimit);
|
|
1421 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
|
|
1422 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
|
|
1423 break;
|
|
1424 }
|
|
1425 }
|
|
1426
|
|
1427 if( testAddr ){
|
|
1428 sqlite3VdbeJumpHere(v, testAddr);
|
|
1429 }
|
|
1430 return;
|
|
1431 }
|
|
1432 #endif /* SQLITE_OMIT_SUBQUERY */
|
|
1433
|
|
1434 /*
|
|
1435 ** Generate an instruction that will put the integer describe by
|
|
1436 ** text z[0..n-1] on the stack.
|
|
1437 */
|
|
1438 static void codeInteger(Vdbe *v, const char *z, int n){
|
|
1439 int i;
|
|
1440 if( sqlite3GetInt32(z, &i) ){
|
|
1441 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
|
|
1442 }else if( sqlite3FitsIn64Bits(z) ){
|
|
1443 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
|
|
1444 }else{
|
|
1445 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
|
|
1446 }
|
|
1447 }
|
|
1448
|
|
1449 /*
|
|
1450 ** Generate code into the current Vdbe to evaluate the given
|
|
1451 ** expression and leave the result on the top of stack.
|
|
1452 **
|
|
1453 ** This code depends on the fact that certain token values (ex: TK_EQ)
|
|
1454 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
|
|
1455 ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
|
|
1456 ** the make process cause these values to align. Assert()s in the code
|
|
1457 ** below verify that the numbers are aligned correctly.
|
|
1458 */
|
|
1459 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
|
|
1460 Vdbe *v = pParse->pVdbe;
|
|
1461 int op;
|
|
1462 int stackChng = 1; /* Amount of change to stack depth */
|
|
1463
|
|
1464 if( v==0 ) return;
|
|
1465 if( pExpr==0 ){
|
|
1466 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
|
|
1467 return;
|
|
1468 }
|
|
1469 op = pExpr->op;
|
|
1470 switch( op ){
|
|
1471 case TK_AGG_COLUMN: {
|
|
1472 AggInfo *pAggInfo = pExpr->pAggInfo;
|
|
1473 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
|
|
1474 if( !pAggInfo->directMode ){
|
|
1475 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
|
|
1476 break;
|
|
1477 }else if( pAggInfo->useSortingIdx ){
|
|
1478 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
|
|
1479 pCol->iSorterColumn);
|
|
1480 break;
|
|
1481 }
|
|
1482 /* Otherwise, fall thru into the TK_COLUMN case */
|
|
1483 }
|
|
1484 case TK_COLUMN: {
|
|
1485 if( pExpr->iTable<0 ){
|
|
1486 /* This only happens when coding check constraints */
|
|
1487 assert( pParse->ckOffset>0 );
|
|
1488 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
|
|
1489 }else if( pExpr->iColumn>=0 ){
|
|
1490 Table *pTab = pExpr->pTab;
|
|
1491 int iCol = pExpr->iColumn;
|
|
1492 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, iCol);
|
|
1493 sqlite3ColumnDefault(v, pTab, iCol);
|
|
1494 #ifndef SQLITE_OMIT_FLOATING_POINT
|
|
1495 if( pTab && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
|
|
1496 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
|
|
1497 }
|
|
1498 #endif
|
|
1499 }else{
|
|
1500 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
|
|
1501 }
|
|
1502 break;
|
|
1503 }
|
|
1504 case TK_INTEGER: {
|
|
1505 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
|
|
1506 break;
|
|
1507 }
|
|
1508 case TK_FLOAT:
|
|
1509 case TK_STRING: {
|
|
1510 assert( TK_FLOAT==OP_Real );
|
|
1511 assert( TK_STRING==OP_String8 );
|
|
1512 sqlite3DequoteExpr(pExpr);
|
|
1513 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
|
|
1514 break;
|
|
1515 }
|
|
1516 case TK_NULL: {
|
|
1517 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
|
|
1518 break;
|
|
1519 }
|
|
1520 #ifndef SQLITE_OMIT_BLOB_LITERAL
|
|
1521 case TK_BLOB: {
|
|
1522 int n;
|
|
1523 const char *z;
|
|
1524 assert( TK_BLOB==OP_HexBlob );
|
|
1525 n = pExpr->token.n - 3;
|
|
1526 z = (char*)pExpr->token.z + 2;
|
|
1527 assert( n>=0 );
|
|
1528 if( n==0 ){
|
|
1529 z = "";
|
|
1530 }
|
|
1531 sqlite3VdbeOp3(v, op, 0, 0, z, n);
|
|
1532 break;
|
|
1533 }
|
|
1534 #endif
|
|
1535 case TK_VARIABLE: {
|
|
1536 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
|
|
1537 if( pExpr->token.n>1 ){
|
|
1538 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
|
|
1539 }
|
|
1540 break;
|
|
1541 }
|
|
1542 case TK_REGISTER: {
|
|
1543 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
|
|
1544 break;
|
|
1545 }
|
|
1546 #ifndef SQLITE_OMIT_CAST
|
|
1547 case TK_CAST: {
|
|
1548 /* Expressions of the form: CAST(pLeft AS token) */
|
|
1549 int aff, to_op;
|
|
1550 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
1551 aff = sqlite3AffinityType(&pExpr->token);
|
|
1552 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
|
|
1553 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
|
|
1554 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
|
|
1555 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
|
|
1556 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
|
|
1557 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
|
|
1558 sqlite3VdbeAddOp(v, to_op, 0, 0);
|
|
1559 stackChng = 0;
|
|
1560 break;
|
|
1561 }
|
|
1562 #endif /* SQLITE_OMIT_CAST */
|
|
1563 case TK_LT:
|
|
1564 case TK_LE:
|
|
1565 case TK_GT:
|
|
1566 case TK_GE:
|
|
1567 case TK_NE:
|
|
1568 case TK_EQ: {
|
|
1569 assert( TK_LT==OP_Lt );
|
|
1570 assert( TK_LE==OP_Le );
|
|
1571 assert( TK_GT==OP_Gt );
|
|
1572 assert( TK_GE==OP_Ge );
|
|
1573 assert( TK_EQ==OP_Eq );
|
|
1574 assert( TK_NE==OP_Ne );
|
|
1575 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
1576 sqlite3ExprCode(pParse, pExpr->pRight);
|
|
1577 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
|
|
1578 stackChng = -1;
|
|
1579 break;
|
|
1580 }
|
|
1581 case TK_AND:
|
|
1582 case TK_OR:
|
|
1583 case TK_PLUS:
|
|
1584 case TK_STAR:
|
|
1585 case TK_MINUS:
|
|
1586 case TK_REM:
|
|
1587 case TK_BITAND:
|
|
1588 case TK_BITOR:
|
|
1589 case TK_SLASH:
|
|
1590 case TK_LSHIFT:
|
|
1591 case TK_RSHIFT:
|
|
1592 case TK_CONCAT: {
|
|
1593 assert( TK_AND==OP_And );
|
|
1594 assert( TK_OR==OP_Or );
|
|
1595 assert( TK_PLUS==OP_Add );
|
|
1596 assert( TK_MINUS==OP_Subtract );
|
|
1597 assert( TK_REM==OP_Remainder );
|
|
1598 assert( TK_BITAND==OP_BitAnd );
|
|
1599 assert( TK_BITOR==OP_BitOr );
|
|
1600 assert( TK_SLASH==OP_Divide );
|
|
1601 assert( TK_LSHIFT==OP_ShiftLeft );
|
|
1602 assert( TK_RSHIFT==OP_ShiftRight );
|
|
1603 assert( TK_CONCAT==OP_Concat );
|
|
1604 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
1605 sqlite3ExprCode(pParse, pExpr->pRight);
|
|
1606 sqlite3VdbeAddOp(v, op, 0, 0);
|
|
1607 stackChng = -1;
|
|
1608 break;
|
|
1609 }
|
|
1610 case TK_UMINUS: {
|
|
1611 Expr *pLeft = pExpr->pLeft;
|
|
1612 assert( pLeft );
|
|
1613 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
|
|
1614 Token *p = &pLeft->token;
|
|
1615 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
|
|
1616 if( pLeft->op==TK_FLOAT ){
|
|
1617 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
|
|
1618 }else{
|
|
1619 codeInteger(v, z, p->n+1);
|
|
1620 }
|
|
1621 sqliteFree(z);
|
|
1622 break;
|
|
1623 }
|
|
1624 /* Fall through into TK_NOT */
|
|
1625 }
|
|
1626 case TK_BITNOT:
|
|
1627 case TK_NOT: {
|
|
1628 assert( TK_BITNOT==OP_BitNot );
|
|
1629 assert( TK_NOT==OP_Not );
|
|
1630 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
1631 sqlite3VdbeAddOp(v, op, 0, 0);
|
|
1632 stackChng = 0;
|
|
1633 break;
|
|
1634 }
|
|
1635 case TK_ISNULL:
|
|
1636 case TK_NOTNULL: {
|
|
1637 int dest;
|
|
1638 assert( TK_ISNULL==OP_IsNull );
|
|
1639 assert( TK_NOTNULL==OP_NotNull );
|
|
1640 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
|
|
1641 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
1642 dest = sqlite3VdbeCurrentAddr(v) + 2;
|
|
1643 sqlite3VdbeAddOp(v, op, 1, dest);
|
|
1644 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
|
|
1645 stackChng = 0;
|
|
1646 break;
|
|
1647 }
|
|
1648 case TK_AGG_FUNCTION: {
|
|
1649 AggInfo *pInfo = pExpr->pAggInfo;
|
|
1650 if( pInfo==0 ){
|
|
1651 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
|
|
1652 &pExpr->span);
|
|
1653 }else{
|
|
1654 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
|
|
1655 }
|
|
1656 break;
|
|
1657 }
|
|
1658 case TK_CONST_FUNC:
|
|
1659 case TK_FUNCTION: {
|
|
1660 ExprList *pList = pExpr->pList;
|
|
1661 int nExpr = pList ? pList->nExpr : 0;
|
|
1662 FuncDef *pDef;
|
|
1663 int nId;
|
|
1664 const char *zId;
|
|
1665 int constMask = 0;
|
|
1666 int i;
|
|
1667 u8 enc = ENC(pParse->db);
|
|
1668 CollSeq *pColl = 0;
|
|
1669 zId = (char*)pExpr->token.z;
|
|
1670 nId = pExpr->token.n;
|
|
1671 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
|
|
1672 assert( pDef!=0 );
|
|
1673 nExpr = sqlite3ExprCodeExprList(pParse, pList);
|
|
1674 for(i=0; i<nExpr && i<32; i++){
|
|
1675 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
|
|
1676 constMask |= (1<<i);
|
|
1677 }
|
|
1678 if( pDef->needCollSeq && !pColl ){
|
|
1679 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
|
|
1680 }
|
|
1681 }
|
|
1682 if( pDef->needCollSeq ){
|
|
1683 if( !pColl ) pColl = pParse->db->pDfltColl;
|
|
1684 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
|
|
1685 }
|
|
1686 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
|
|
1687 stackChng = 1-nExpr;
|
|
1688 break;
|
|
1689 }
|
|
1690 #ifndef SQLITE_OMIT_SUBQUERY
|
|
1691 case TK_EXISTS:
|
|
1692 case TK_SELECT: {
|
|
1693 if( pExpr->iColumn==0 ){
|
|
1694 sqlite3CodeSubselect(pParse, pExpr);
|
|
1695 }
|
|
1696 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
|
|
1697 VdbeComment((v, "# load subquery result"));
|
|
1698 break;
|
|
1699 }
|
|
1700 case TK_IN: {
|
|
1701 int addr;
|
|
1702 char affinity;
|
|
1703 int ckOffset = pParse->ckOffset;
|
|
1704 sqlite3CodeSubselect(pParse, pExpr);
|
|
1705
|
|
1706 /* Figure out the affinity to use to create a key from the results
|
|
1707 ** of the expression. affinityStr stores a static string suitable for
|
|
1708 ** P3 of OP_MakeRecord.
|
|
1709 */
|
|
1710 affinity = comparisonAffinity(pExpr);
|
|
1711
|
|
1712 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
|
|
1713 pParse->ckOffset = ckOffset+1;
|
|
1714
|
|
1715 /* Code the <expr> from "<expr> IN (...)". The temporary table
|
|
1716 ** pExpr->iTable contains the values that make up the (...) set.
|
|
1717 */
|
|
1718 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
1719 addr = sqlite3VdbeCurrentAddr(v);
|
|
1720 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
|
|
1721 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
|
|
1722 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
|
|
1723 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
|
|
1724 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
|
|
1725 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
|
|
1726 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
|
|
1727
|
|
1728 break;
|
|
1729 }
|
|
1730 #endif
|
|
1731 case TK_BETWEEN: {
|
|
1732 Expr *pLeft = pExpr->pLeft;
|
|
1733 struct ExprList_item *pLItem = pExpr->pList->a;
|
|
1734 Expr *pRight = pLItem->pExpr;
|
|
1735 sqlite3ExprCode(pParse, pLeft);
|
|
1736 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
|
|
1737 sqlite3ExprCode(pParse, pRight);
|
|
1738 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
|
|
1739 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
|
|
1740 pLItem++;
|
|
1741 pRight = pLItem->pExpr;
|
|
1742 sqlite3ExprCode(pParse, pRight);
|
|
1743 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
|
|
1744 sqlite3VdbeAddOp(v, OP_And, 0, 0);
|
|
1745 break;
|
|
1746 }
|
|
1747 case TK_UPLUS:
|
|
1748 case TK_AS: {
|
|
1749 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
1750 stackChng = 0;
|
|
1751 break;
|
|
1752 }
|
|
1753 case TK_CASE: {
|
|
1754 int expr_end_label;
|
|
1755 int jumpInst;
|
|
1756 int nExpr;
|
|
1757 int i;
|
|
1758 ExprList *pEList;
|
|
1759 struct ExprList_item *aListelem;
|
|
1760
|
|
1761 assert(pExpr->pList);
|
|
1762 assert((pExpr->pList->nExpr % 2) == 0);
|
|
1763 assert(pExpr->pList->nExpr > 0);
|
|
1764 pEList = pExpr->pList;
|
|
1765 aListelem = pEList->a;
|
|
1766 nExpr = pEList->nExpr;
|
|
1767 expr_end_label = sqlite3VdbeMakeLabel(v);
|
|
1768 if( pExpr->pLeft ){
|
|
1769 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
1770 }
|
|
1771 for(i=0; i<nExpr; i=i+2){
|
|
1772 sqlite3ExprCode(pParse, aListelem[i].pExpr);
|
|
1773 if( pExpr->pLeft ){
|
|
1774 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
|
|
1775 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
|
|
1776 OP_Ne, 0, 1);
|
|
1777 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
|
|
1778 }else{
|
|
1779 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
|
|
1780 }
|
|
1781 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
|
|
1782 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
|
|
1783 sqlite3VdbeJumpHere(v, jumpInst);
|
|
1784 }
|
|
1785 if( pExpr->pLeft ){
|
|
1786 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
|
|
1787 }
|
|
1788 if( pExpr->pRight ){
|
|
1789 sqlite3ExprCode(pParse, pExpr->pRight);
|
|
1790 }else{
|
|
1791 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
|
|
1792 }
|
|
1793 sqlite3VdbeResolveLabel(v, expr_end_label);
|
|
1794 break;
|
|
1795 }
|
|
1796 #ifndef SQLITE_OMIT_TRIGGER
|
|
1797 case TK_RAISE: {
|
|
1798 if( !pParse->trigStack ){
|
|
1799 sqlite3ErrorMsg(pParse,
|
|
1800 "RAISE() may only be used within a trigger-program");
|
|
1801 return;
|
|
1802 }
|
|
1803 if( pExpr->iColumn!=OE_Ignore ){
|
|
1804 assert( pExpr->iColumn==OE_Rollback ||
|
|
1805 pExpr->iColumn == OE_Abort ||
|
|
1806 pExpr->iColumn == OE_Fail );
|
|
1807 sqlite3DequoteExpr(pExpr);
|
|
1808 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
|
|
1809 (char*)pExpr->token.z, pExpr->token.n);
|
|
1810 } else {
|
|
1811 assert( pExpr->iColumn == OE_Ignore );
|
|
1812 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
|
|
1813 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
|
|
1814 VdbeComment((v, "# raise(IGNORE)"));
|
|
1815 }
|
|
1816 stackChng = 0;
|
|
1817 break;
|
|
1818 }
|
|
1819 #endif
|
|
1820 }
|
|
1821
|
|
1822 if( pParse->ckOffset ){
|
|
1823 pParse->ckOffset += stackChng;
|
|
1824 assert( pParse->ckOffset );
|
|
1825 }
|
|
1826 }
|
|
1827
|
|
1828 #ifndef SQLITE_OMIT_TRIGGER
|
|
1829 /*
|
|
1830 ** Generate code that evalutes the given expression and leaves the result
|
|
1831 ** on the stack. See also sqlite3ExprCode().
|
|
1832 **
|
|
1833 ** This routine might also cache the result and modify the pExpr tree
|
|
1834 ** so that it will make use of the cached result on subsequent evaluations
|
|
1835 ** rather than evaluate the whole expression again. Trivial expressions are
|
|
1836 ** not cached. If the expression is cached, its result is stored in a
|
|
1837 ** memory location.
|
|
1838 */
|
|
1839 void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
|
|
1840 Vdbe *v = pParse->pVdbe;
|
|
1841 int iMem;
|
|
1842 int addr1, addr2;
|
|
1843 if( v==0 ) return;
|
|
1844 addr1 = sqlite3VdbeCurrentAddr(v);
|
|
1845 sqlite3ExprCode(pParse, pExpr);
|
|
1846 addr2 = sqlite3VdbeCurrentAddr(v);
|
|
1847 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
|
|
1848 iMem = pExpr->iTable = pParse->nMem++;
|
|
1849 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
|
|
1850 pExpr->op = TK_REGISTER;
|
|
1851 }
|
|
1852 }
|
|
1853 #endif
|
|
1854
|
|
1855 /*
|
|
1856 ** Generate code that pushes the value of every element of the given
|
|
1857 ** expression list onto the stack.
|
|
1858 **
|
|
1859 ** Return the number of elements pushed onto the stack.
|
|
1860 */
|
|
1861 int sqlite3ExprCodeExprList(
|
|
1862 Parse *pParse, /* Parsing context */
|
|
1863 ExprList *pList /* The expression list to be coded */
|
|
1864 ){
|
|
1865 struct ExprList_item *pItem;
|
|
1866 int i, n;
|
|
1867 if( pList==0 ) return 0;
|
|
1868 n = pList->nExpr;
|
|
1869 for(pItem=pList->a, i=n; i>0; i--, pItem++){
|
|
1870 sqlite3ExprCode(pParse, pItem->pExpr);
|
|
1871 }
|
|
1872 return n;
|
|
1873 }
|
|
1874
|
|
1875 /*
|
|
1876 ** Generate code for a boolean expression such that a jump is made
|
|
1877 ** to the label "dest" if the expression is true but execution
|
|
1878 ** continues straight thru if the expression is false.
|
|
1879 **
|
|
1880 ** If the expression evaluates to NULL (neither true nor false), then
|
|
1881 ** take the jump if the jumpIfNull flag is true.
|
|
1882 **
|
|
1883 ** This code depends on the fact that certain token values (ex: TK_EQ)
|
|
1884 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
|
|
1885 ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
|
|
1886 ** the make process cause these values to align. Assert()s in the code
|
|
1887 ** below verify that the numbers are aligned correctly.
|
|
1888 */
|
|
1889 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
|
|
1890 Vdbe *v = pParse->pVdbe;
|
|
1891 int op = 0;
|
|
1892 int ckOffset = pParse->ckOffset;
|
|
1893 if( v==0 || pExpr==0 ) return;
|
|
1894 op = pExpr->op;
|
|
1895 switch( op ){
|
|
1896 case TK_AND: {
|
|
1897 int d2 = sqlite3VdbeMakeLabel(v);
|
|
1898 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
|
|
1899 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
|
|
1900 sqlite3VdbeResolveLabel(v, d2);
|
|
1901 break;
|
|
1902 }
|
|
1903 case TK_OR: {
|
|
1904 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
|
|
1905 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
|
|
1906 break;
|
|
1907 }
|
|
1908 case TK_NOT: {
|
|
1909 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
|
|
1910 break;
|
|
1911 }
|
|
1912 case TK_LT:
|
|
1913 case TK_LE:
|
|
1914 case TK_GT:
|
|
1915 case TK_GE:
|
|
1916 case TK_NE:
|
|
1917 case TK_EQ: {
|
|
1918 assert( TK_LT==OP_Lt );
|
|
1919 assert( TK_LE==OP_Le );
|
|
1920 assert( TK_GT==OP_Gt );
|
|
1921 assert( TK_GE==OP_Ge );
|
|
1922 assert( TK_EQ==OP_Eq );
|
|
1923 assert( TK_NE==OP_Ne );
|
|
1924 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
1925 sqlite3ExprCode(pParse, pExpr->pRight);
|
|
1926 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
|
|
1927 break;
|
|
1928 }
|
|
1929 case TK_ISNULL:
|
|
1930 case TK_NOTNULL: {
|
|
1931 assert( TK_ISNULL==OP_IsNull );
|
|
1932 assert( TK_NOTNULL==OP_NotNull );
|
|
1933 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
1934 sqlite3VdbeAddOp(v, op, 1, dest);
|
|
1935 break;
|
|
1936 }
|
|
1937 case TK_BETWEEN: {
|
|
1938 /* The expression "x BETWEEN y AND z" is implemented as:
|
|
1939 **
|
|
1940 ** 1 IF (x < y) GOTO 3
|
|
1941 ** 2 IF (x <= z) GOTO <dest>
|
|
1942 ** 3 ...
|
|
1943 */
|
|
1944 int addr;
|
|
1945 Expr *pLeft = pExpr->pLeft;
|
|
1946 Expr *pRight = pExpr->pList->a[0].pExpr;
|
|
1947 sqlite3ExprCode(pParse, pLeft);
|
|
1948 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
|
|
1949 sqlite3ExprCode(pParse, pRight);
|
|
1950 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
|
|
1951
|
|
1952 pRight = pExpr->pList->a[1].pExpr;
|
|
1953 sqlite3ExprCode(pParse, pRight);
|
|
1954 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
|
|
1955
|
|
1956 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
|
|
1957 sqlite3VdbeJumpHere(v, addr);
|
|
1958 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
|
|
1959 break;
|
|
1960 }
|
|
1961 default: {
|
|
1962 sqlite3ExprCode(pParse, pExpr);
|
|
1963 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
|
|
1964 break;
|
|
1965 }
|
|
1966 }
|
|
1967 pParse->ckOffset = ckOffset;
|
|
1968 }
|
|
1969
|
|
1970 /*
|
|
1971 ** Generate code for a boolean expression such that a jump is made
|
|
1972 ** to the label "dest" if the expression is false but execution
|
|
1973 ** continues straight thru if the expression is true.
|
|
1974 **
|
|
1975 ** If the expression evaluates to NULL (neither true nor false) then
|
|
1976 ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
|
|
1977 */
|
|
1978 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
|
|
1979 Vdbe *v = pParse->pVdbe;
|
|
1980 int op = 0;
|
|
1981 int ckOffset = pParse->ckOffset;
|
|
1982 if( v==0 || pExpr==0 ) return;
|
|
1983
|
|
1984 /* The value of pExpr->op and op are related as follows:
|
|
1985 **
|
|
1986 ** pExpr->op op
|
|
1987 ** --------- ----------
|
|
1988 ** TK_ISNULL OP_NotNull
|
|
1989 ** TK_NOTNULL OP_IsNull
|
|
1990 ** TK_NE OP_Eq
|
|
1991 ** TK_EQ OP_Ne
|
|
1992 ** TK_GT OP_Le
|
|
1993 ** TK_LE OP_Gt
|
|
1994 ** TK_GE OP_Lt
|
|
1995 ** TK_LT OP_Ge
|
|
1996 **
|
|
1997 ** For other values of pExpr->op, op is undefined and unused.
|
|
1998 ** The value of TK_ and OP_ constants are arranged such that we
|
|
1999 ** can compute the mapping above using the following expression.
|
|
2000 ** Assert()s verify that the computation is correct.
|
|
2001 */
|
|
2002 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
|
|
2003
|
|
2004 /* Verify correct alignment of TK_ and OP_ constants
|
|
2005 */
|
|
2006 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
|
|
2007 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
|
|
2008 assert( pExpr->op!=TK_NE || op==OP_Eq );
|
|
2009 assert( pExpr->op!=TK_EQ || op==OP_Ne );
|
|
2010 assert( pExpr->op!=TK_LT || op==OP_Ge );
|
|
2011 assert( pExpr->op!=TK_LE || op==OP_Gt );
|
|
2012 assert( pExpr->op!=TK_GT || op==OP_Le );
|
|
2013 assert( pExpr->op!=TK_GE || op==OP_Lt );
|
|
2014
|
|
2015 switch( pExpr->op ){
|
|
2016 case TK_AND: {
|
|
2017 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
|
|
2018 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
|
|
2019 break;
|
|
2020 }
|
|
2021 case TK_OR: {
|
|
2022 int d2 = sqlite3VdbeMakeLabel(v);
|
|
2023 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
|
|
2024 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
|
|
2025 sqlite3VdbeResolveLabel(v, d2);
|
|
2026 break;
|
|
2027 }
|
|
2028 case TK_NOT: {
|
|
2029 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
|
|
2030 break;
|
|
2031 }
|
|
2032 case TK_LT:
|
|
2033 case TK_LE:
|
|
2034 case TK_GT:
|
|
2035 case TK_GE:
|
|
2036 case TK_NE:
|
|
2037 case TK_EQ: {
|
|
2038 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
2039 sqlite3ExprCode(pParse, pExpr->pRight);
|
|
2040 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
|
|
2041 break;
|
|
2042 }
|
|
2043 case TK_ISNULL:
|
|
2044 case TK_NOTNULL: {
|
|
2045 sqlite3ExprCode(pParse, pExpr->pLeft);
|
|
2046 sqlite3VdbeAddOp(v, op, 1, dest);
|
|
2047 break;
|
|
2048 }
|
|
2049 case TK_BETWEEN: {
|
|
2050 /* The expression is "x BETWEEN y AND z". It is implemented as:
|
|
2051 **
|
|
2052 ** 1 IF (x >= y) GOTO 3
|
|
2053 ** 2 GOTO <dest>
|
|
2054 ** 3 IF (x > z) GOTO <dest>
|
|
2055 */
|
|
2056 int addr;
|
|
2057 Expr *pLeft = pExpr->pLeft;
|
|
2058 Expr *pRight = pExpr->pList->a[0].pExpr;
|
|
2059 sqlite3ExprCode(pParse, pLeft);
|
|
2060 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
|
|
2061 sqlite3ExprCode(pParse, pRight);
|
|
2062 addr = sqlite3VdbeCurrentAddr(v);
|
|
2063 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
|
|
2064
|
|
2065 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
|
|
2066 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
|
|
2067 pRight = pExpr->pList->a[1].pExpr;
|
|
2068 sqlite3ExprCode(pParse, pRight);
|
|
2069 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
|
|
2070 break;
|
|
2071 }
|
|
2072 default: {
|
|
2073 sqlite3ExprCode(pParse, pExpr);
|
|
2074 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
|
|
2075 break;
|
|
2076 }
|
|
2077 }
|
|
2078 pParse->ckOffset = ckOffset;
|
|
2079 }
|
|
2080
|
|
2081 /*
|
|
2082 ** Do a deep comparison of two expression trees. Return TRUE (non-zero)
|
|
2083 ** if they are identical and return FALSE if they differ in any way.
|
|
2084 */
|
|
2085 int sqlite3ExprCompare(Expr *pA, Expr *pB){
|
|
2086 int i;
|
|
2087 if( pA==0||pB==0 ){
|
|
2088 return pB==pA;
|
|
2089 }
|
|
2090 if( pA->op!=pB->op ) return 0;
|
|
2091 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
|
|
2092 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
|
|
2093 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
|
|
2094 if( pA->pList ){
|
|
2095 if( pB->pList==0 ) return 0;
|
|
2096 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
|
|
2097 for(i=0; i<pA->pList->nExpr; i++){
|
|
2098 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
|
|
2099 return 0;
|
|
2100 }
|
|
2101 }
|
|
2102 }else if( pB->pList ){
|
|
2103 return 0;
|
|
2104 }
|
|
2105 if( pA->pSelect || pB->pSelect ) return 0;
|
|
2106 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
|
|
2107 if( pA->token.z ){
|
|
2108 if( pB->token.z==0 ) return 0;
|
|
2109 if( pB->token.n!=pA->token.n ) return 0;
|
|
2110 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
|
|
2111 return 0;
|
|
2112 }
|
|
2113 }
|
|
2114 return 1;
|
|
2115 }
|
|
2116
|
|
2117
|
|
2118 /*
|
|
2119 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
|
|
2120 ** the new element. Return a negative number if malloc fails.
|
|
2121 */
|
|
2122 static int addAggInfoColumn(AggInfo *pInfo){
|
|
2123 int i;
|
|
2124 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
|
|
2125 if( i<0 ){
|
|
2126 return -1;
|
|
2127 }
|
|
2128 return i;
|
|
2129 }
|
|
2130
|
|
2131 /*
|
|
2132 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
|
|
2133 ** the new element. Return a negative number if malloc fails.
|
|
2134 */
|
|
2135 static int addAggInfoFunc(AggInfo *pInfo){
|
|
2136 int i;
|
|
2137 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
|
|
2138 if( i<0 ){
|
|
2139 return -1;
|
|
2140 }
|
|
2141 return i;
|
|
2142 }
|
|
2143
|
|
2144 /*
|
|
2145 ** This is an xFunc for walkExprTree() used to implement
|
|
2146 ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
|
|
2147 ** for additional information.
|
|
2148 **
|
|
2149 ** This routine analyzes the aggregate function at pExpr.
|
|
2150 */
|
|
2151 static int analyzeAggregate(void *pArg, Expr *pExpr){
|
|
2152 int i;
|
|
2153 NameContext *pNC = (NameContext *)pArg;
|
|
2154 Parse *pParse = pNC->pParse;
|
|
2155 SrcList *pSrcList = pNC->pSrcList;
|
|
2156 AggInfo *pAggInfo = pNC->pAggInfo;
|
|
2157
|
|
2158
|
|
2159 switch( pExpr->op ){
|
|
2160 case TK_COLUMN: {
|
|
2161 /* Check to see if the column is in one of the tables in the FROM
|
|
2162 ** clause of the aggregate query */
|
|
2163 if( pSrcList ){
|
|
2164 struct SrcList_item *pItem = pSrcList->a;
|
|
2165 for(i=0; i<pSrcList->nSrc; i++, pItem++){
|
|
2166 struct AggInfo_col *pCol;
|
|
2167 if( pExpr->iTable==pItem->iCursor ){
|
|
2168 /* If we reach this point, it means that pExpr refers to a table
|
|
2169 ** that is in the FROM clause of the aggregate query.
|
|
2170 **
|
|
2171 ** Make an entry for the column in pAggInfo->aCol[] if there
|
|
2172 ** is not an entry there already.
|
|
2173 */
|
|
2174 pCol = pAggInfo->aCol;
|
|
2175 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
|
|
2176 if( pCol->iTable==pExpr->iTable &&
|
|
2177 pCol->iColumn==pExpr->iColumn ){
|
|
2178 break;
|
|
2179 }
|
|
2180 }
|
|
2181 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
|
|
2182 pCol = &pAggInfo->aCol[i];
|
|
2183 pCol->iTable = pExpr->iTable;
|
|
2184 pCol->iColumn = pExpr->iColumn;
|
|
2185 pCol->iMem = pParse->nMem++;
|
|
2186 pCol->iSorterColumn = -1;
|
|
2187 pCol->pExpr = pExpr;
|
|
2188 if( pAggInfo->pGroupBy ){
|
|
2189 int j, n;
|
|
2190 ExprList *pGB = pAggInfo->pGroupBy;
|
|
2191 struct ExprList_item *pTerm = pGB->a;
|
|
2192 n = pGB->nExpr;
|
|
2193 for(j=0; j<n; j++, pTerm++){
|
|
2194 Expr *pE = pTerm->pExpr;
|
|
2195 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
|
|
2196 pE->iColumn==pExpr->iColumn ){
|
|
2197 pCol->iSorterColumn = j;
|
|
2198 break;
|
|
2199 }
|
|
2200 }
|
|
2201 }
|
|
2202 if( pCol->iSorterColumn<0 ){
|
|
2203 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
|
|
2204 }
|
|
2205 }
|
|
2206 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
|
|
2207 ** because it was there before or because we just created it).
|
|
2208 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
|
|
2209 ** pAggInfo->aCol[] entry.
|
|
2210 */
|
|
2211 pExpr->pAggInfo = pAggInfo;
|
|
2212 pExpr->op = TK_AGG_COLUMN;
|
|
2213 pExpr->iAgg = i;
|
|
2214 break;
|
|
2215 } /* endif pExpr->iTable==pItem->iCursor */
|
|
2216 } /* end loop over pSrcList */
|
|
2217 }
|
|
2218 return 1;
|
|
2219 }
|
|
2220 case TK_AGG_FUNCTION: {
|
|
2221 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
|
|
2222 ** to be ignored */
|
|
2223 if( pNC->nDepth==0 ){
|
|
2224 /* Check to see if pExpr is a duplicate of another aggregate
|
|
2225 ** function that is already in the pAggInfo structure
|
|
2226 */
|
|
2227 struct AggInfo_func *pItem = pAggInfo->aFunc;
|
|
2228 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
|
|
2229 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
|
|
2230 break;
|
|
2231 }
|
|
2232 }
|
|
2233 if( i>=pAggInfo->nFunc ){
|
|
2234 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
|
|
2235 */
|
|
2236 u8 enc = ENC(pParse->db);
|
|
2237 i = addAggInfoFunc(pAggInfo);
|
|
2238 if( i>=0 ){
|
|
2239 pItem = &pAggInfo->aFunc[i];
|
|
2240 pItem->pExpr = pExpr;
|
|
2241 pItem->iMem = pParse->nMem++;
|
|
2242 pItem->pFunc = sqlite3FindFunction(pParse->db,
|
|
2243 (char*)pExpr->token.z, pExpr->token.n,
|
|
2244 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
|
|
2245 if( pExpr->flags & EP_Distinct ){
|
|
2246 pItem->iDistinct = pParse->nTab++;
|
|
2247 }else{
|
|
2248 pItem->iDistinct = -1;
|
|
2249 }
|
|
2250 }
|
|
2251 }
|
|
2252 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
|
|
2253 */
|
|
2254 pExpr->iAgg = i;
|
|
2255 pExpr->pAggInfo = pAggInfo;
|
|
2256 return 1;
|
|
2257 }
|
|
2258 }
|
|
2259 }
|
|
2260
|
|
2261 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
|
|
2262 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
|
|
2263 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
|
|
2264 */
|
|
2265 if( pExpr->pSelect ){
|
|
2266 pNC->nDepth++;
|
|
2267 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
|
|
2268 pNC->nDepth--;
|
|
2269 }
|
|
2270 return 0;
|
|
2271 }
|
|
2272
|
|
2273 /*
|
|
2274 ** Analyze the given expression looking for aggregate functions and
|
|
2275 ** for variables that need to be added to the pParse->aAgg[] array.
|
|
2276 ** Make additional entries to the pParse->aAgg[] array as necessary.
|
|
2277 **
|
|
2278 ** This routine should only be called after the expression has been
|
|
2279 ** analyzed by sqlite3ExprResolveNames().
|
|
2280 **
|
|
2281 ** If errors are seen, leave an error message in zErrMsg and return
|
|
2282 ** the number of errors.
|
|
2283 */
|
|
2284 int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
|
|
2285 int nErr = pNC->pParse->nErr;
|
|
2286 walkExprTree(pExpr, analyzeAggregate, pNC);
|
|
2287 return pNC->pParse->nErr - nErr;
|
|
2288 }
|
|
2289
|
|
2290 /*
|
|
2291 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
|
|
2292 ** expression list. Return the number of errors.
|
|
2293 **
|
|
2294 ** If an error is found, the analysis is cut short.
|
|
2295 */
|
|
2296 int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
|
|
2297 struct ExprList_item *pItem;
|
|
2298 int i;
|
|
2299 int nErr = 0;
|
|
2300 if( pList ){
|
|
2301 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
|
|
2302 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
|
|
2303 }
|
|
2304 }
|
|
2305 return nErr;
|
|
2306 }
|