|
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.399 2008/10/11 16:47:36 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_SELECT ){ |
|
39 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); |
|
40 } |
|
41 #ifndef SQLITE_OMIT_CAST |
|
42 if( op==TK_CAST ){ |
|
43 return sqlite3AffinityType(&pExpr->token); |
|
44 } |
|
45 #endif |
|
46 if( (op==TK_COLUMN || op==TK_REGISTER) && pExpr->pTab!=0 ){ |
|
47 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally |
|
48 ** a TK_COLUMN but was previously evaluated and cached in a register */ |
|
49 int j = pExpr->iColumn; |
|
50 if( j<0 ) return SQLITE_AFF_INTEGER; |
|
51 assert( pExpr->pTab && j<pExpr->pTab->nCol ); |
|
52 return pExpr->pTab->aCol[j].affinity; |
|
53 } |
|
54 return pExpr->affinity; |
|
55 } |
|
56 |
|
57 /* |
|
58 ** Set the collating sequence for expression pExpr to be the collating |
|
59 ** sequence named by pToken. Return a pointer to the revised expression. |
|
60 ** The collating sequence is marked as "explicit" using the EP_ExpCollate |
|
61 ** flag. An explicit collating sequence will override implicit |
|
62 ** collating sequences. |
|
63 */ |
|
64 Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pCollName){ |
|
65 char *zColl = 0; /* Dequoted name of collation sequence */ |
|
66 CollSeq *pColl; |
|
67 sqlite3 *db = pParse->db; |
|
68 zColl = sqlite3NameFromToken(db, pCollName); |
|
69 if( pExpr && zColl ){ |
|
70 pColl = sqlite3LocateCollSeq(pParse, zColl, -1); |
|
71 if( pColl ){ |
|
72 pExpr->pColl = pColl; |
|
73 pExpr->flags |= EP_ExpCollate; |
|
74 } |
|
75 } |
|
76 sqlite3DbFree(db, zColl); |
|
77 return pExpr; |
|
78 } |
|
79 |
|
80 /* |
|
81 ** Return the default collation sequence for the expression pExpr. If |
|
82 ** there is no default collation type, return 0. |
|
83 */ |
|
84 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ |
|
85 CollSeq *pColl = 0; |
|
86 Expr *p = pExpr; |
|
87 while( p ){ |
|
88 int op; |
|
89 pColl = p->pColl; |
|
90 if( pColl ) break; |
|
91 op = p->op; |
|
92 if( (op==TK_COLUMN || op==TK_REGISTER) && p->pTab!=0 ){ |
|
93 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally |
|
94 ** a TK_COLUMN but was previously evaluated and cached in a register */ |
|
95 const char *zColl; |
|
96 int j = p->iColumn; |
|
97 if( j>=0 ){ |
|
98 sqlite3 *db = pParse->db; |
|
99 zColl = p->pTab->aCol[j].zColl; |
|
100 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, -1, 0); |
|
101 pExpr->pColl = pColl; |
|
102 } |
|
103 break; |
|
104 } |
|
105 if( op!=TK_CAST && op!=TK_UPLUS ){ |
|
106 break; |
|
107 } |
|
108 p = p->pLeft; |
|
109 } |
|
110 if( sqlite3CheckCollSeq(pParse, pColl) ){ |
|
111 pColl = 0; |
|
112 } |
|
113 return pColl; |
|
114 } |
|
115 |
|
116 /* |
|
117 ** pExpr is an operand of a comparison operator. aff2 is the |
|
118 ** type affinity of the other operand. This routine returns the |
|
119 ** type affinity that should be used for the comparison operator. |
|
120 */ |
|
121 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ |
|
122 char aff1 = sqlite3ExprAffinity(pExpr); |
|
123 if( aff1 && aff2 ){ |
|
124 /* Both sides of the comparison are columns. If one has numeric |
|
125 ** affinity, use that. Otherwise use no affinity. |
|
126 */ |
|
127 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ |
|
128 return SQLITE_AFF_NUMERIC; |
|
129 }else{ |
|
130 return SQLITE_AFF_NONE; |
|
131 } |
|
132 }else if( !aff1 && !aff2 ){ |
|
133 /* Neither side of the comparison is a column. Compare the |
|
134 ** results directly. |
|
135 */ |
|
136 return SQLITE_AFF_NONE; |
|
137 }else{ |
|
138 /* One side is a column, the other is not. Use the columns affinity. */ |
|
139 assert( aff1==0 || aff2==0 ); |
|
140 return (aff1 + aff2); |
|
141 } |
|
142 } |
|
143 |
|
144 /* |
|
145 ** pExpr is a comparison operator. Return the type affinity that should |
|
146 ** be applied to both operands prior to doing the comparison. |
|
147 */ |
|
148 static char comparisonAffinity(Expr *pExpr){ |
|
149 char aff; |
|
150 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || |
|
151 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || |
|
152 pExpr->op==TK_NE ); |
|
153 assert( pExpr->pLeft ); |
|
154 aff = sqlite3ExprAffinity(pExpr->pLeft); |
|
155 if( pExpr->pRight ){ |
|
156 aff = sqlite3CompareAffinity(pExpr->pRight, aff); |
|
157 } |
|
158 else if( pExpr->pSelect ){ |
|
159 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); |
|
160 } |
|
161 else if( !aff ){ |
|
162 aff = SQLITE_AFF_NONE; |
|
163 } |
|
164 return aff; |
|
165 } |
|
166 |
|
167 /* |
|
168 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. |
|
169 ** idx_affinity is the affinity of an indexed column. Return true |
|
170 ** if the index with affinity idx_affinity may be used to implement |
|
171 ** the comparison in pExpr. |
|
172 */ |
|
173 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ |
|
174 char aff = comparisonAffinity(pExpr); |
|
175 switch( aff ){ |
|
176 case SQLITE_AFF_NONE: |
|
177 return 1; |
|
178 case SQLITE_AFF_TEXT: |
|
179 return idx_affinity==SQLITE_AFF_TEXT; |
|
180 default: |
|
181 return sqlite3IsNumericAffinity(idx_affinity); |
|
182 } |
|
183 } |
|
184 |
|
185 /* |
|
186 ** Return the P5 value that should be used for a binary comparison |
|
187 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. |
|
188 */ |
|
189 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ |
|
190 u8 aff = (char)sqlite3ExprAffinity(pExpr2); |
|
191 aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull; |
|
192 return aff; |
|
193 } |
|
194 |
|
195 /* |
|
196 ** Return a pointer to the collation sequence that should be used by |
|
197 ** a binary comparison operator comparing pLeft and pRight. |
|
198 ** |
|
199 ** If the left hand expression has a collating sequence type, then it is |
|
200 ** used. Otherwise the collation sequence for the right hand expression |
|
201 ** is used, or the default (BINARY) if neither expression has a collating |
|
202 ** type. |
|
203 ** |
|
204 ** Argument pRight (but not pLeft) may be a null pointer. In this case, |
|
205 ** it is not considered. |
|
206 */ |
|
207 CollSeq *sqlite3BinaryCompareCollSeq( |
|
208 Parse *pParse, |
|
209 Expr *pLeft, |
|
210 Expr *pRight |
|
211 ){ |
|
212 CollSeq *pColl; |
|
213 assert( pLeft ); |
|
214 if( pLeft->flags & EP_ExpCollate ){ |
|
215 assert( pLeft->pColl ); |
|
216 pColl = pLeft->pColl; |
|
217 }else if( pRight && pRight->flags & EP_ExpCollate ){ |
|
218 assert( pRight->pColl ); |
|
219 pColl = pRight->pColl; |
|
220 }else{ |
|
221 pColl = sqlite3ExprCollSeq(pParse, pLeft); |
|
222 if( !pColl ){ |
|
223 pColl = sqlite3ExprCollSeq(pParse, pRight); |
|
224 } |
|
225 } |
|
226 return pColl; |
|
227 } |
|
228 |
|
229 /* |
|
230 ** Generate the operands for a comparison operation. Before |
|
231 ** generating the code for each operand, set the EP_AnyAff |
|
232 ** flag on the expression so that it will be able to used a |
|
233 ** cached column value that has previously undergone an |
|
234 ** affinity change. |
|
235 */ |
|
236 static void codeCompareOperands( |
|
237 Parse *pParse, /* Parsing and code generating context */ |
|
238 Expr *pLeft, /* The left operand */ |
|
239 int *pRegLeft, /* Register where left operand is stored */ |
|
240 int *pFreeLeft, /* Free this register when done */ |
|
241 Expr *pRight, /* The right operand */ |
|
242 int *pRegRight, /* Register where right operand is stored */ |
|
243 int *pFreeRight /* Write temp register for right operand there */ |
|
244 ){ |
|
245 while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft; |
|
246 pLeft->flags |= EP_AnyAff; |
|
247 *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft); |
|
248 while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft; |
|
249 pRight->flags |= EP_AnyAff; |
|
250 *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight); |
|
251 } |
|
252 |
|
253 /* |
|
254 ** Generate code for a comparison operator. |
|
255 */ |
|
256 static int codeCompare( |
|
257 Parse *pParse, /* The parsing (and code generating) context */ |
|
258 Expr *pLeft, /* The left operand */ |
|
259 Expr *pRight, /* The right operand */ |
|
260 int opcode, /* The comparison opcode */ |
|
261 int in1, int in2, /* Register holding operands */ |
|
262 int dest, /* Jump here if true. */ |
|
263 int jumpIfNull /* If true, jump if either operand is NULL */ |
|
264 ){ |
|
265 int p5; |
|
266 int addr; |
|
267 CollSeq *p4; |
|
268 |
|
269 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); |
|
270 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); |
|
271 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, |
|
272 (void*)p4, P4_COLLSEQ); |
|
273 sqlite3VdbeChangeP5(pParse->pVdbe, p5); |
|
274 if( (p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_NONE ){ |
|
275 sqlite3ExprCacheAffinityChange(pParse, in1, 1); |
|
276 sqlite3ExprCacheAffinityChange(pParse, in2, 1); |
|
277 } |
|
278 return addr; |
|
279 } |
|
280 |
|
281 #if SQLITE_MAX_EXPR_DEPTH>0 |
|
282 /* |
|
283 ** Check that argument nHeight is less than or equal to the maximum |
|
284 ** expression depth allowed. If it is not, leave an error message in |
|
285 ** pParse. |
|
286 */ |
|
287 int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ |
|
288 int rc = SQLITE_OK; |
|
289 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; |
|
290 if( nHeight>mxHeight ){ |
|
291 sqlite3ErrorMsg(pParse, |
|
292 "Expression tree is too large (maximum depth %d)", mxHeight |
|
293 ); |
|
294 rc = SQLITE_ERROR; |
|
295 } |
|
296 return rc; |
|
297 } |
|
298 |
|
299 /* The following three functions, heightOfExpr(), heightOfExprList() |
|
300 ** and heightOfSelect(), are used to determine the maximum height |
|
301 ** of any expression tree referenced by the structure passed as the |
|
302 ** first argument. |
|
303 ** |
|
304 ** If this maximum height is greater than the current value pointed |
|
305 ** to by pnHeight, the second parameter, then set *pnHeight to that |
|
306 ** value. |
|
307 */ |
|
308 static void heightOfExpr(Expr *p, int *pnHeight){ |
|
309 if( p ){ |
|
310 if( p->nHeight>*pnHeight ){ |
|
311 *pnHeight = p->nHeight; |
|
312 } |
|
313 } |
|
314 } |
|
315 static void heightOfExprList(ExprList *p, int *pnHeight){ |
|
316 if( p ){ |
|
317 int i; |
|
318 for(i=0; i<p->nExpr; i++){ |
|
319 heightOfExpr(p->a[i].pExpr, pnHeight); |
|
320 } |
|
321 } |
|
322 } |
|
323 static void heightOfSelect(Select *p, int *pnHeight){ |
|
324 if( p ){ |
|
325 heightOfExpr(p->pWhere, pnHeight); |
|
326 heightOfExpr(p->pHaving, pnHeight); |
|
327 heightOfExpr(p->pLimit, pnHeight); |
|
328 heightOfExpr(p->pOffset, pnHeight); |
|
329 heightOfExprList(p->pEList, pnHeight); |
|
330 heightOfExprList(p->pGroupBy, pnHeight); |
|
331 heightOfExprList(p->pOrderBy, pnHeight); |
|
332 heightOfSelect(p->pPrior, pnHeight); |
|
333 } |
|
334 } |
|
335 |
|
336 /* |
|
337 ** Set the Expr.nHeight variable in the structure passed as an |
|
338 ** argument. An expression with no children, Expr.pList or |
|
339 ** Expr.pSelect member has a height of 1. Any other expression |
|
340 ** has a height equal to the maximum height of any other |
|
341 ** referenced Expr plus one. |
|
342 */ |
|
343 static void exprSetHeight(Expr *p){ |
|
344 int nHeight = 0; |
|
345 heightOfExpr(p->pLeft, &nHeight); |
|
346 heightOfExpr(p->pRight, &nHeight); |
|
347 heightOfExprList(p->pList, &nHeight); |
|
348 heightOfSelect(p->pSelect, &nHeight); |
|
349 p->nHeight = nHeight + 1; |
|
350 } |
|
351 |
|
352 /* |
|
353 ** Set the Expr.nHeight variable using the exprSetHeight() function. If |
|
354 ** the height is greater than the maximum allowed expression depth, |
|
355 ** leave an error in pParse. |
|
356 */ |
|
357 void sqlite3ExprSetHeight(Parse *pParse, Expr *p){ |
|
358 exprSetHeight(p); |
|
359 sqlite3ExprCheckHeight(pParse, p->nHeight); |
|
360 } |
|
361 |
|
362 /* |
|
363 ** Return the maximum height of any expression tree referenced |
|
364 ** by the select statement passed as an argument. |
|
365 */ |
|
366 int sqlite3SelectExprHeight(Select *p){ |
|
367 int nHeight = 0; |
|
368 heightOfSelect(p, &nHeight); |
|
369 return nHeight; |
|
370 } |
|
371 #else |
|
372 #define exprSetHeight(y) |
|
373 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ |
|
374 |
|
375 /* |
|
376 ** Construct a new expression node and return a pointer to it. Memory |
|
377 ** for this node is obtained from sqlite3_malloc(). The calling function |
|
378 ** is responsible for making sure the node eventually gets freed. |
|
379 */ |
|
380 Expr *sqlite3Expr( |
|
381 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ |
|
382 int op, /* Expression opcode */ |
|
383 Expr *pLeft, /* Left operand */ |
|
384 Expr *pRight, /* Right operand */ |
|
385 const Token *pToken /* Argument token */ |
|
386 ){ |
|
387 Expr *pNew; |
|
388 pNew = sqlite3DbMallocZero(db, sizeof(Expr)); |
|
389 if( pNew==0 ){ |
|
390 /* When malloc fails, delete pLeft and pRight. Expressions passed to |
|
391 ** this function must always be allocated with sqlite3Expr() for this |
|
392 ** reason. |
|
393 */ |
|
394 sqlite3ExprDelete(db, pLeft); |
|
395 sqlite3ExprDelete(db, pRight); |
|
396 return 0; |
|
397 } |
|
398 pNew->op = op; |
|
399 pNew->pLeft = pLeft; |
|
400 pNew->pRight = pRight; |
|
401 pNew->iAgg = -1; |
|
402 pNew->span.z = (u8*)""; |
|
403 if( pToken ){ |
|
404 assert( pToken->dyn==0 ); |
|
405 pNew->span = pNew->token = *pToken; |
|
406 }else if( pLeft ){ |
|
407 if( pRight ){ |
|
408 if( pRight->span.dyn==0 && pLeft->span.dyn==0 ){ |
|
409 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); |
|
410 } |
|
411 if( pRight->flags & EP_ExpCollate ){ |
|
412 pNew->flags |= EP_ExpCollate; |
|
413 pNew->pColl = pRight->pColl; |
|
414 } |
|
415 } |
|
416 if( pLeft->flags & EP_ExpCollate ){ |
|
417 pNew->flags |= EP_ExpCollate; |
|
418 pNew->pColl = pLeft->pColl; |
|
419 } |
|
420 } |
|
421 |
|
422 exprSetHeight(pNew); |
|
423 return pNew; |
|
424 } |
|
425 |
|
426 /* |
|
427 ** Works like sqlite3Expr() except that it takes an extra Parse* |
|
428 ** argument and notifies the associated connection object if malloc fails. |
|
429 */ |
|
430 Expr *sqlite3PExpr( |
|
431 Parse *pParse, /* Parsing context */ |
|
432 int op, /* Expression opcode */ |
|
433 Expr *pLeft, /* Left operand */ |
|
434 Expr *pRight, /* Right operand */ |
|
435 const Token *pToken /* Argument token */ |
|
436 ){ |
|
437 Expr *p = sqlite3Expr(pParse->db, op, pLeft, pRight, pToken); |
|
438 if( p ){ |
|
439 sqlite3ExprCheckHeight(pParse, p->nHeight); |
|
440 } |
|
441 return p; |
|
442 } |
|
443 |
|
444 /* |
|
445 ** When doing a nested parse, you can include terms in an expression |
|
446 ** that look like this: #1 #2 ... These terms refer to registers |
|
447 ** in the virtual machine. #N is the N-th register. |
|
448 ** |
|
449 ** This routine is called by the parser to deal with on of those terms. |
|
450 ** It immediately generates code to store the value in a memory location. |
|
451 ** The returns an expression that will code to extract the value from |
|
452 ** that memory location as needed. |
|
453 */ |
|
454 Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ |
|
455 Vdbe *v = pParse->pVdbe; |
|
456 Expr *p; |
|
457 if( pParse->nested==0 ){ |
|
458 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); |
|
459 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); |
|
460 } |
|
461 if( v==0 ) return 0; |
|
462 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken); |
|
463 if( p==0 ){ |
|
464 return 0; /* Malloc failed */ |
|
465 } |
|
466 p->iTable = atoi((char*)&pToken->z[1]); |
|
467 return p; |
|
468 } |
|
469 |
|
470 /* |
|
471 ** Join two expressions using an AND operator. If either expression is |
|
472 ** NULL, then just return the other expression. |
|
473 */ |
|
474 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ |
|
475 if( pLeft==0 ){ |
|
476 return pRight; |
|
477 }else if( pRight==0 ){ |
|
478 return pLeft; |
|
479 }else{ |
|
480 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0); |
|
481 } |
|
482 } |
|
483 |
|
484 /* |
|
485 ** Set the Expr.span field of the given expression to span all |
|
486 ** text between the two given tokens. Both tokens must be pointing |
|
487 ** at the same string. |
|
488 */ |
|
489 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
|
490 assert( pRight!=0 ); |
|
491 assert( pLeft!=0 ); |
|
492 if( pExpr ){ |
|
493 pExpr->span.z = pLeft->z; |
|
494 pExpr->span.n = pRight->n + (pRight->z - pLeft->z); |
|
495 } |
|
496 } |
|
497 |
|
498 /* |
|
499 ** Construct a new expression node for a function with multiple |
|
500 ** arguments. |
|
501 */ |
|
502 Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ |
|
503 Expr *pNew; |
|
504 sqlite3 *db = pParse->db; |
|
505 assert( pToken ); |
|
506 pNew = sqlite3DbMallocZero(db, sizeof(Expr) ); |
|
507 if( pNew==0 ){ |
|
508 sqlite3ExprListDelete(db, pList); /* Avoid leaking memory when malloc fails */ |
|
509 return 0; |
|
510 } |
|
511 pNew->op = TK_FUNCTION; |
|
512 pNew->pList = pList; |
|
513 assert( pToken->dyn==0 ); |
|
514 pNew->token = *pToken; |
|
515 pNew->span = pNew->token; |
|
516 |
|
517 sqlite3ExprSetHeight(pParse, pNew); |
|
518 return pNew; |
|
519 } |
|
520 |
|
521 /* |
|
522 ** Assign a variable number to an expression that encodes a wildcard |
|
523 ** in the original SQL statement. |
|
524 ** |
|
525 ** Wildcards consisting of a single "?" are assigned the next sequential |
|
526 ** variable number. |
|
527 ** |
|
528 ** Wildcards of the form "?nnn" are assigned the number "nnn". We make |
|
529 ** sure "nnn" is not too be to avoid a denial of service attack when |
|
530 ** the SQL statement comes from an external source. |
|
531 ** |
|
532 ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number |
|
533 ** as the previous instance of the same wildcard. Or if this is the first |
|
534 ** instance of the wildcard, the next sequenial variable number is |
|
535 ** assigned. |
|
536 */ |
|
537 void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ |
|
538 Token *pToken; |
|
539 sqlite3 *db = pParse->db; |
|
540 |
|
541 if( pExpr==0 ) return; |
|
542 pToken = &pExpr->token; |
|
543 assert( pToken->n>=1 ); |
|
544 assert( pToken->z!=0 ); |
|
545 assert( pToken->z[0]!=0 ); |
|
546 if( pToken->n==1 ){ |
|
547 /* Wildcard of the form "?". Assign the next variable number */ |
|
548 pExpr->iTable = ++pParse->nVar; |
|
549 }else if( pToken->z[0]=='?' ){ |
|
550 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
|
551 ** use it as the variable number */ |
|
552 int i; |
|
553 pExpr->iTable = i = atoi((char*)&pToken->z[1]); |
|
554 testcase( i==0 ); |
|
555 testcase( i==1 ); |
|
556 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); |
|
557 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
|
558 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
|
559 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
|
560 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); |
|
561 } |
|
562 if( i>pParse->nVar ){ |
|
563 pParse->nVar = i; |
|
564 } |
|
565 }else{ |
|
566 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable |
|
567 ** number as the prior appearance of the same name, or if the name |
|
568 ** has never appeared before, reuse the same variable number |
|
569 */ |
|
570 int i, n; |
|
571 n = pToken->n; |
|
572 for(i=0; i<pParse->nVarExpr; i++){ |
|
573 Expr *pE; |
|
574 if( (pE = pParse->apVarExpr[i])!=0 |
|
575 && pE->token.n==n |
|
576 && memcmp(pE->token.z, pToken->z, n)==0 ){ |
|
577 pExpr->iTable = pE->iTable; |
|
578 break; |
|
579 } |
|
580 } |
|
581 if( i>=pParse->nVarExpr ){ |
|
582 pExpr->iTable = ++pParse->nVar; |
|
583 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ |
|
584 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; |
|
585 pParse->apVarExpr = |
|
586 sqlite3DbReallocOrFree( |
|
587 db, |
|
588 pParse->apVarExpr, |
|
589 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) |
|
590 ); |
|
591 } |
|
592 if( !db->mallocFailed ){ |
|
593 assert( pParse->apVarExpr!=0 ); |
|
594 pParse->apVarExpr[pParse->nVarExpr++] = pExpr; |
|
595 } |
|
596 } |
|
597 } |
|
598 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
|
599 sqlite3ErrorMsg(pParse, "too many SQL variables"); |
|
600 } |
|
601 } |
|
602 |
|
603 /* |
|
604 ** Clear an expression structure without deleting the structure itself. |
|
605 ** Substructure is deleted. |
|
606 */ |
|
607 void sqlite3ExprClear(sqlite3 *db, Expr *p){ |
|
608 if( p->span.dyn ) sqlite3DbFree(db, (char*)p->span.z); |
|
609 if( p->token.dyn ) sqlite3DbFree(db, (char*)p->token.z); |
|
610 sqlite3ExprDelete(db, p->pLeft); |
|
611 sqlite3ExprDelete(db, p->pRight); |
|
612 sqlite3ExprListDelete(db, p->pList); |
|
613 sqlite3SelectDelete(db, p->pSelect); |
|
614 } |
|
615 |
|
616 /* |
|
617 ** Recursively delete an expression tree. |
|
618 */ |
|
619 void sqlite3ExprDelete(sqlite3 *db, Expr *p){ |
|
620 if( p==0 ) return; |
|
621 sqlite3ExprClear(db, p); |
|
622 sqlite3DbFree(db, p); |
|
623 } |
|
624 |
|
625 /* |
|
626 ** The Expr.token field might be a string literal that is quoted. |
|
627 ** If so, remove the quotation marks. |
|
628 */ |
|
629 void sqlite3DequoteExpr(sqlite3 *db, Expr *p){ |
|
630 if( ExprHasAnyProperty(p, EP_Dequoted) ){ |
|
631 return; |
|
632 } |
|
633 ExprSetProperty(p, EP_Dequoted); |
|
634 if( p->token.dyn==0 ){ |
|
635 sqlite3TokenCopy(db, &p->token, &p->token); |
|
636 } |
|
637 sqlite3Dequote((char*)p->token.z); |
|
638 } |
|
639 |
|
640 /* |
|
641 ** The following group of routines make deep copies of expressions, |
|
642 ** expression lists, ID lists, and select statements. The copies can |
|
643 ** be deleted (by being passed to their respective ...Delete() routines) |
|
644 ** without effecting the originals. |
|
645 ** |
|
646 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), |
|
647 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded |
|
648 ** by subsequent calls to sqlite*ListAppend() routines. |
|
649 ** |
|
650 ** Any tables that the SrcList might point to are not duplicated. |
|
651 */ |
|
652 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){ |
|
653 Expr *pNew; |
|
654 if( p==0 ) return 0; |
|
655 pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); |
|
656 if( pNew==0 ) return 0; |
|
657 memcpy(pNew, p, sizeof(*pNew)); |
|
658 if( p->token.z!=0 ){ |
|
659 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n); |
|
660 pNew->token.dyn = 1; |
|
661 }else{ |
|
662 assert( pNew->token.z==0 ); |
|
663 } |
|
664 pNew->span.z = 0; |
|
665 pNew->pLeft = sqlite3ExprDup(db, p->pLeft); |
|
666 pNew->pRight = sqlite3ExprDup(db, p->pRight); |
|
667 pNew->pList = sqlite3ExprListDup(db, p->pList); |
|
668 pNew->pSelect = sqlite3SelectDup(db, p->pSelect); |
|
669 return pNew; |
|
670 } |
|
671 void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){ |
|
672 if( pTo->dyn ) sqlite3DbFree(db, (char*)pTo->z); |
|
673 if( pFrom->z ){ |
|
674 pTo->n = pFrom->n; |
|
675 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n); |
|
676 pTo->dyn = 1; |
|
677 }else{ |
|
678 pTo->z = 0; |
|
679 } |
|
680 } |
|
681 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){ |
|
682 ExprList *pNew; |
|
683 struct ExprList_item *pItem, *pOldItem; |
|
684 int i; |
|
685 if( p==0 ) return 0; |
|
686 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); |
|
687 if( pNew==0 ) return 0; |
|
688 pNew->iECursor = 0; |
|
689 pNew->nExpr = pNew->nAlloc = p->nExpr; |
|
690 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) ); |
|
691 if( pItem==0 ){ |
|
692 sqlite3DbFree(db, pNew); |
|
693 return 0; |
|
694 } |
|
695 pOldItem = p->a; |
|
696 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ |
|
697 Expr *pNewExpr, *pOldExpr; |
|
698 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr); |
|
699 if( pOldExpr->span.z!=0 && pNewExpr ){ |
|
700 /* Always make a copy of the span for top-level expressions in the |
|
701 ** expression list. The logic in SELECT processing that determines |
|
702 ** the names of columns in the result set needs this information */ |
|
703 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span); |
|
704 } |
|
705 assert( pNewExpr==0 || pNewExpr->span.z!=0 |
|
706 || pOldExpr->span.z==0 |
|
707 || db->mallocFailed ); |
|
708 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
|
709 pItem->sortOrder = pOldItem->sortOrder; |
|
710 pItem->done = 0; |
|
711 pItem->iCol = pOldItem->iCol; |
|
712 pItem->iAlias = pOldItem->iAlias; |
|
713 } |
|
714 return pNew; |
|
715 } |
|
716 |
|
717 /* |
|
718 ** If cursors, triggers, views and subqueries are all omitted from |
|
719 ** the build, then none of the following routines, except for |
|
720 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes |
|
721 ** called with a NULL argument. |
|
722 */ |
|
723 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ |
|
724 || !defined(SQLITE_OMIT_SUBQUERY) |
|
725 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){ |
|
726 SrcList *pNew; |
|
727 int i; |
|
728 int nByte; |
|
729 if( p==0 ) return 0; |
|
730 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); |
|
731 pNew = sqlite3DbMallocRaw(db, nByte ); |
|
732 if( pNew==0 ) return 0; |
|
733 pNew->nSrc = pNew->nAlloc = p->nSrc; |
|
734 for(i=0; i<p->nSrc; i++){ |
|
735 struct SrcList_item *pNewItem = &pNew->a[i]; |
|
736 struct SrcList_item *pOldItem = &p->a[i]; |
|
737 Table *pTab; |
|
738 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); |
|
739 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
|
740 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); |
|
741 pNewItem->jointype = pOldItem->jointype; |
|
742 pNewItem->iCursor = pOldItem->iCursor; |
|
743 pNewItem->isPopulated = pOldItem->isPopulated; |
|
744 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex); |
|
745 pNewItem->notIndexed = pOldItem->notIndexed; |
|
746 pNewItem->pIndex = pOldItem->pIndex; |
|
747 pTab = pNewItem->pTab = pOldItem->pTab; |
|
748 if( pTab ){ |
|
749 pTab->nRef++; |
|
750 } |
|
751 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect); |
|
752 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn); |
|
753 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); |
|
754 pNewItem->colUsed = pOldItem->colUsed; |
|
755 } |
|
756 return pNew; |
|
757 } |
|
758 IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ |
|
759 IdList *pNew; |
|
760 int i; |
|
761 if( p==0 ) return 0; |
|
762 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); |
|
763 if( pNew==0 ) return 0; |
|
764 pNew->nId = pNew->nAlloc = p->nId; |
|
765 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); |
|
766 if( pNew->a==0 ){ |
|
767 sqlite3DbFree(db, pNew); |
|
768 return 0; |
|
769 } |
|
770 for(i=0; i<p->nId; i++){ |
|
771 struct IdList_item *pNewItem = &pNew->a[i]; |
|
772 struct IdList_item *pOldItem = &p->a[i]; |
|
773 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
|
774 pNewItem->idx = pOldItem->idx; |
|
775 } |
|
776 return pNew; |
|
777 } |
|
778 Select *sqlite3SelectDup(sqlite3 *db, Select *p){ |
|
779 Select *pNew; |
|
780 if( p==0 ) return 0; |
|
781 pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); |
|
782 if( pNew==0 ) return 0; |
|
783 pNew->pEList = sqlite3ExprListDup(db, p->pEList); |
|
784 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc); |
|
785 pNew->pWhere = sqlite3ExprDup(db, p->pWhere); |
|
786 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy); |
|
787 pNew->pHaving = sqlite3ExprDup(db, p->pHaving); |
|
788 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy); |
|
789 pNew->op = p->op; |
|
790 pNew->pPrior = sqlite3SelectDup(db, p->pPrior); |
|
791 pNew->pLimit = sqlite3ExprDup(db, p->pLimit); |
|
792 pNew->pOffset = sqlite3ExprDup(db, p->pOffset); |
|
793 pNew->iLimit = 0; |
|
794 pNew->iOffset = 0; |
|
795 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; |
|
796 pNew->pRightmost = 0; |
|
797 pNew->addrOpenEphm[0] = -1; |
|
798 pNew->addrOpenEphm[1] = -1; |
|
799 pNew->addrOpenEphm[2] = -1; |
|
800 return pNew; |
|
801 } |
|
802 #else |
|
803 Select *sqlite3SelectDup(sqlite3 *db, Select *p){ |
|
804 assert( p==0 ); |
|
805 return 0; |
|
806 } |
|
807 #endif |
|
808 |
|
809 |
|
810 /* |
|
811 ** Add a new element to the end of an expression list. If pList is |
|
812 ** initially NULL, then create a new expression list. |
|
813 */ |
|
814 ExprList *sqlite3ExprListAppend( |
|
815 Parse *pParse, /* Parsing context */ |
|
816 ExprList *pList, /* List to which to append. Might be NULL */ |
|
817 Expr *pExpr, /* Expression to be appended */ |
|
818 Token *pName /* AS keyword for the expression */ |
|
819 ){ |
|
820 sqlite3 *db = pParse->db; |
|
821 if( pList==0 ){ |
|
822 pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); |
|
823 if( pList==0 ){ |
|
824 goto no_mem; |
|
825 } |
|
826 assert( pList->nAlloc==0 ); |
|
827 } |
|
828 if( pList->nAlloc<=pList->nExpr ){ |
|
829 struct ExprList_item *a; |
|
830 int n = pList->nAlloc*2 + 4; |
|
831 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0])); |
|
832 if( a==0 ){ |
|
833 goto no_mem; |
|
834 } |
|
835 pList->a = a; |
|
836 pList->nAlloc = n; |
|
837 } |
|
838 assert( pList->a!=0 ); |
|
839 if( pExpr || pName ){ |
|
840 struct ExprList_item *pItem = &pList->a[pList->nExpr++]; |
|
841 memset(pItem, 0, sizeof(*pItem)); |
|
842 pItem->zName = sqlite3NameFromToken(db, pName); |
|
843 pItem->pExpr = pExpr; |
|
844 pItem->iAlias = 0; |
|
845 } |
|
846 return pList; |
|
847 |
|
848 no_mem: |
|
849 /* Avoid leaking memory if malloc has failed. */ |
|
850 sqlite3ExprDelete(db, pExpr); |
|
851 sqlite3ExprListDelete(db, pList); |
|
852 return 0; |
|
853 } |
|
854 |
|
855 /* |
|
856 ** If the expression list pEList contains more than iLimit elements, |
|
857 ** leave an error message in pParse. |
|
858 */ |
|
859 void sqlite3ExprListCheckLength( |
|
860 Parse *pParse, |
|
861 ExprList *pEList, |
|
862 const char *zObject |
|
863 ){ |
|
864 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; |
|
865 testcase( pEList && pEList->nExpr==mx ); |
|
866 testcase( pEList && pEList->nExpr==mx+1 ); |
|
867 if( pEList && pEList->nExpr>mx ){ |
|
868 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); |
|
869 } |
|
870 } |
|
871 |
|
872 /* |
|
873 ** Delete an entire expression list. |
|
874 */ |
|
875 void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ |
|
876 int i; |
|
877 struct ExprList_item *pItem; |
|
878 if( pList==0 ) return; |
|
879 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); |
|
880 assert( pList->nExpr<=pList->nAlloc ); |
|
881 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
|
882 sqlite3ExprDelete(db, pItem->pExpr); |
|
883 sqlite3DbFree(db, pItem->zName); |
|
884 } |
|
885 sqlite3DbFree(db, pList->a); |
|
886 sqlite3DbFree(db, pList); |
|
887 } |
|
888 |
|
889 /* |
|
890 ** These routines are Walker callbacks. Walker.u.pi is a pointer |
|
891 ** to an integer. These routines are checking an expression to see |
|
892 ** if it is a constant. Set *Walker.u.pi to 0 if the expression is |
|
893 ** not constant. |
|
894 ** |
|
895 ** These callback routines are used to implement the following: |
|
896 ** |
|
897 ** sqlite3ExprIsConstant() |
|
898 ** sqlite3ExprIsConstantNotJoin() |
|
899 ** sqlite3ExprIsConstantOrFunction() |
|
900 ** |
|
901 */ |
|
902 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
|
903 |
|
904 /* If pWalker->u.i is 3 then any term of the expression that comes from |
|
905 ** the ON or USING clauses of a join disqualifies the expression |
|
906 ** from being considered constant. */ |
|
907 if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){ |
|
908 pWalker->u.i = 0; |
|
909 return WRC_Abort; |
|
910 } |
|
911 |
|
912 switch( pExpr->op ){ |
|
913 /* Consider functions to be constant if all their arguments are constant |
|
914 ** and pWalker->u.i==2 */ |
|
915 case TK_FUNCTION: |
|
916 if( pWalker->u.i==2 ) return 0; |
|
917 /* Fall through */ |
|
918 case TK_ID: |
|
919 case TK_COLUMN: |
|
920 case TK_DOT: |
|
921 case TK_AGG_FUNCTION: |
|
922 case TK_AGG_COLUMN: |
|
923 #ifndef SQLITE_OMIT_SUBQUERY |
|
924 case TK_SELECT: |
|
925 case TK_EXISTS: |
|
926 testcase( pExpr->op==TK_SELECT ); |
|
927 testcase( pExpr->op==TK_EXISTS ); |
|
928 #endif |
|
929 testcase( pExpr->op==TK_ID ); |
|
930 testcase( pExpr->op==TK_COLUMN ); |
|
931 testcase( pExpr->op==TK_DOT ); |
|
932 testcase( pExpr->op==TK_AGG_FUNCTION ); |
|
933 testcase( pExpr->op==TK_AGG_COLUMN ); |
|
934 pWalker->u.i = 0; |
|
935 return WRC_Abort; |
|
936 default: |
|
937 return WRC_Continue; |
|
938 } |
|
939 } |
|
940 static int selectNodeIsConstant(Walker *pWalker, Select *pSelect){ |
|
941 pWalker->u.i = 0; |
|
942 return WRC_Abort; |
|
943 } |
|
944 static int exprIsConst(Expr *p, int initFlag){ |
|
945 Walker w; |
|
946 w.u.i = initFlag; |
|
947 w.xExprCallback = exprNodeIsConstant; |
|
948 w.xSelectCallback = selectNodeIsConstant; |
|
949 sqlite3WalkExpr(&w, p); |
|
950 return w.u.i; |
|
951 } |
|
952 |
|
953 /* |
|
954 ** Walk an expression tree. Return 1 if the expression is constant |
|
955 ** and 0 if it involves variables or function calls. |
|
956 ** |
|
957 ** For the purposes of this function, a double-quoted string (ex: "abc") |
|
958 ** is considered a variable but a single-quoted string (ex: 'abc') is |
|
959 ** a constant. |
|
960 */ |
|
961 int sqlite3ExprIsConstant(Expr *p){ |
|
962 return exprIsConst(p, 1); |
|
963 } |
|
964 |
|
965 /* |
|
966 ** Walk an expression tree. Return 1 if the expression is constant |
|
967 ** that does no originate from the ON or USING clauses of a join. |
|
968 ** Return 0 if it involves variables or function calls or terms from |
|
969 ** an ON or USING clause. |
|
970 */ |
|
971 int sqlite3ExprIsConstantNotJoin(Expr *p){ |
|
972 return exprIsConst(p, 3); |
|
973 } |
|
974 |
|
975 /* |
|
976 ** Walk an expression tree. Return 1 if the expression is constant |
|
977 ** or a function call with constant arguments. Return and 0 if there |
|
978 ** are any variables. |
|
979 ** |
|
980 ** For the purposes of this function, a double-quoted string (ex: "abc") |
|
981 ** is considered a variable but a single-quoted string (ex: 'abc') is |
|
982 ** a constant. |
|
983 */ |
|
984 int sqlite3ExprIsConstantOrFunction(Expr *p){ |
|
985 return exprIsConst(p, 2); |
|
986 } |
|
987 |
|
988 /* |
|
989 ** If the expression p codes a constant integer that is small enough |
|
990 ** to fit in a 32-bit integer, return 1 and put the value of the integer |
|
991 ** in *pValue. If the expression is not an integer or if it is too big |
|
992 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. |
|
993 */ |
|
994 int sqlite3ExprIsInteger(Expr *p, int *pValue){ |
|
995 int rc = 0; |
|
996 if( p->flags & EP_IntValue ){ |
|
997 *pValue = p->iTable; |
|
998 return 1; |
|
999 } |
|
1000 switch( p->op ){ |
|
1001 case TK_INTEGER: { |
|
1002 rc = sqlite3GetInt32((char*)p->token.z, pValue); |
|
1003 break; |
|
1004 } |
|
1005 case TK_UPLUS: { |
|
1006 rc = sqlite3ExprIsInteger(p->pLeft, pValue); |
|
1007 break; |
|
1008 } |
|
1009 case TK_UMINUS: { |
|
1010 int v; |
|
1011 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ |
|
1012 *pValue = -v; |
|
1013 rc = 1; |
|
1014 } |
|
1015 break; |
|
1016 } |
|
1017 default: break; |
|
1018 } |
|
1019 if( rc ){ |
|
1020 p->op = TK_INTEGER; |
|
1021 p->flags |= EP_IntValue; |
|
1022 p->iTable = *pValue; |
|
1023 } |
|
1024 return rc; |
|
1025 } |
|
1026 |
|
1027 /* |
|
1028 ** Return TRUE if the given string is a row-id column name. |
|
1029 */ |
|
1030 int sqlite3IsRowid(const char *z){ |
|
1031 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; |
|
1032 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; |
|
1033 if( sqlite3StrICmp(z, "OID")==0 ) return 1; |
|
1034 return 0; |
|
1035 } |
|
1036 |
|
1037 #ifdef SQLITE_TEST |
|
1038 int sqlite3_enable_in_opt = 1; |
|
1039 #else |
|
1040 #define sqlite3_enable_in_opt 1 |
|
1041 #endif |
|
1042 |
|
1043 /* |
|
1044 ** Return true if the IN operator optimization is enabled and |
|
1045 ** the SELECT statement p exists and is of the |
|
1046 ** simple form: |
|
1047 ** |
|
1048 ** SELECT <column> FROM <table> |
|
1049 ** |
|
1050 ** If this is the case, it may be possible to use an existing table |
|
1051 ** or index instead of generating an epheremal table. |
|
1052 */ |
|
1053 #ifndef SQLITE_OMIT_SUBQUERY |
|
1054 static int isCandidateForInOpt(Select *p){ |
|
1055 SrcList *pSrc; |
|
1056 ExprList *pEList; |
|
1057 Table *pTab; |
|
1058 if( !sqlite3_enable_in_opt ) return 0; /* IN optimization must be enabled */ |
|
1059 if( p==0 ) return 0; /* right-hand side of IN is SELECT */ |
|
1060 if( p->pPrior ) return 0; /* Not a compound SELECT */ |
|
1061 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ |
|
1062 return 0; /* No DISTINCT keyword and no aggregate functions */ |
|
1063 } |
|
1064 if( p->pGroupBy ) return 0; /* Has no GROUP BY clause */ |
|
1065 if( p->pLimit ) return 0; /* Has no LIMIT clause */ |
|
1066 if( p->pOffset ) return 0; |
|
1067 if( p->pWhere ) return 0; /* Has no WHERE clause */ |
|
1068 pSrc = p->pSrc; |
|
1069 if( pSrc==0 ) return 0; /* A single table in the FROM clause */ |
|
1070 if( pSrc->nSrc!=1 ) return 0; |
|
1071 if( pSrc->a[0].pSelect ) return 0; /* FROM clause is not a subquery */ |
|
1072 pTab = pSrc->a[0].pTab; |
|
1073 if( pTab==0 ) return 0; |
|
1074 if( pTab->pSelect ) return 0; /* FROM clause is not a view */ |
|
1075 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ |
|
1076 pEList = p->pEList; |
|
1077 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */ |
|
1078 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */ |
|
1079 return 1; |
|
1080 } |
|
1081 #endif /* SQLITE_OMIT_SUBQUERY */ |
|
1082 |
|
1083 /* |
|
1084 ** This function is used by the implementation of the IN (...) operator. |
|
1085 ** It's job is to find or create a b-tree structure that may be used |
|
1086 ** either to test for membership of the (...) set or to iterate through |
|
1087 ** its members, skipping duplicates. |
|
1088 ** |
|
1089 ** The cursor opened on the structure (database table, database index |
|
1090 ** or ephermal table) is stored in pX->iTable before this function returns. |
|
1091 ** The returned value indicates the structure type, as follows: |
|
1092 ** |
|
1093 ** IN_INDEX_ROWID - The cursor was opened on a database table. |
|
1094 ** IN_INDEX_INDEX - The cursor was opened on a database index. |
|
1095 ** IN_INDEX_EPH - The cursor was opened on a specially created and |
|
1096 ** populated epheremal table. |
|
1097 ** |
|
1098 ** An existing structure may only be used if the SELECT is of the simple |
|
1099 ** form: |
|
1100 ** |
|
1101 ** SELECT <column> FROM <table> |
|
1102 ** |
|
1103 ** If prNotFound parameter is 0, then the structure will be used to iterate |
|
1104 ** through the set members, skipping any duplicates. In this case an |
|
1105 ** epheremal table must be used unless the selected <column> is guaranteed |
|
1106 ** to be unique - either because it is an INTEGER PRIMARY KEY or it |
|
1107 ** is unique by virtue of a constraint or implicit index. |
|
1108 ** |
|
1109 ** If the prNotFound parameter is not 0, then the structure will be used |
|
1110 ** for fast set membership tests. In this case an epheremal table must |
|
1111 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can |
|
1112 ** be found with <column> as its left-most column. |
|
1113 ** |
|
1114 ** When the structure is being used for set membership tests, the user |
|
1115 ** needs to know whether or not the structure contains an SQL NULL |
|
1116 ** value in order to correctly evaluate expressions like "X IN (Y, Z)". |
|
1117 ** If there is a chance that the structure may contain a NULL value at |
|
1118 ** runtime, then a register is allocated and the register number written |
|
1119 ** to *prNotFound. If there is no chance that the structure contains a |
|
1120 ** NULL value, then *prNotFound is left unchanged. |
|
1121 ** |
|
1122 ** If a register is allocated and its location stored in *prNotFound, then |
|
1123 ** its initial value is NULL. If the structure does not remain constant |
|
1124 ** for the duration of the query (i.e. the set is a correlated sub-select), |
|
1125 ** the value of the allocated register is reset to NULL each time the |
|
1126 ** structure is repopulated. This allows the caller to use vdbe code |
|
1127 ** equivalent to the following: |
|
1128 ** |
|
1129 ** if( register==NULL ){ |
|
1130 ** has_null = <test if data structure contains null> |
|
1131 ** register = 1 |
|
1132 ** } |
|
1133 ** |
|
1134 ** in order to avoid running the <test if data structure contains null> |
|
1135 ** test more often than is necessary. |
|
1136 */ |
|
1137 #ifndef SQLITE_OMIT_SUBQUERY |
|
1138 int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){ |
|
1139 Select *p; |
|
1140 int eType = 0; |
|
1141 int iTab = pParse->nTab++; |
|
1142 int mustBeUnique = !prNotFound; |
|
1143 |
|
1144 /* The follwing if(...) expression is true if the SELECT is of the |
|
1145 ** simple form: |
|
1146 ** |
|
1147 ** SELECT <column> FROM <table> |
|
1148 ** |
|
1149 ** If this is the case, it may be possible to use an existing table |
|
1150 ** or index instead of generating an epheremal table. |
|
1151 */ |
|
1152 p = pX->pSelect; |
|
1153 if( isCandidateForInOpt(p) ){ |
|
1154 sqlite3 *db = pParse->db; |
|
1155 Index *pIdx; |
|
1156 Expr *pExpr = p->pEList->a[0].pExpr; |
|
1157 int iCol = pExpr->iColumn; |
|
1158 Vdbe *v = sqlite3GetVdbe(pParse); |
|
1159 |
|
1160 /* This function is only called from two places. In both cases the vdbe |
|
1161 ** has already been allocated. So assume sqlite3GetVdbe() is always |
|
1162 ** successful here. |
|
1163 */ |
|
1164 assert(v); |
|
1165 if( iCol<0 ){ |
|
1166 int iMem = ++pParse->nMem; |
|
1167 int iAddr; |
|
1168 Table *pTab = p->pSrc->a[0].pTab; |
|
1169 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
|
1170 sqlite3VdbeUsesBtree(v, iDb); |
|
1171 |
|
1172 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); |
|
1173 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); |
|
1174 |
|
1175 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
|
1176 eType = IN_INDEX_ROWID; |
|
1177 |
|
1178 sqlite3VdbeJumpHere(v, iAddr); |
|
1179 }else{ |
|
1180 /* The collation sequence used by the comparison. If an index is to |
|
1181 ** be used in place of a temp-table, it must be ordered according |
|
1182 ** to this collation sequence. |
|
1183 */ |
|
1184 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); |
|
1185 |
|
1186 /* Check that the affinity that will be used to perform the |
|
1187 ** comparison is the same as the affinity of the column. If |
|
1188 ** it is not, it is not possible to use any index. |
|
1189 */ |
|
1190 Table *pTab = p->pSrc->a[0].pTab; |
|
1191 char aff = comparisonAffinity(pX); |
|
1192 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE); |
|
1193 |
|
1194 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ |
|
1195 if( (pIdx->aiColumn[0]==iCol) |
|
1196 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0)) |
|
1197 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None)) |
|
1198 ){ |
|
1199 int iDb; |
|
1200 int iMem = ++pParse->nMem; |
|
1201 int iAddr; |
|
1202 char *pKey; |
|
1203 |
|
1204 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx); |
|
1205 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema); |
|
1206 sqlite3VdbeUsesBtree(v, iDb); |
|
1207 |
|
1208 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); |
|
1209 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); |
|
1210 |
|
1211 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn); |
|
1212 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb, |
|
1213 pKey,P4_KEYINFO_HANDOFF); |
|
1214 VdbeComment((v, "%s", pIdx->zName)); |
|
1215 eType = IN_INDEX_INDEX; |
|
1216 |
|
1217 sqlite3VdbeJumpHere(v, iAddr); |
|
1218 if( prNotFound && !pTab->aCol[iCol].notNull ){ |
|
1219 *prNotFound = ++pParse->nMem; |
|
1220 } |
|
1221 } |
|
1222 } |
|
1223 } |
|
1224 } |
|
1225 |
|
1226 if( eType==0 ){ |
|
1227 int rMayHaveNull = 0; |
|
1228 eType = IN_INDEX_EPH; |
|
1229 if( prNotFound ){ |
|
1230 *prNotFound = rMayHaveNull = ++pParse->nMem; |
|
1231 }else if( pX->pLeft->iColumn<0 && pX->pSelect==0 ){ |
|
1232 eType = IN_INDEX_ROWID; |
|
1233 } |
|
1234 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); |
|
1235 }else{ |
|
1236 pX->iTable = iTab; |
|
1237 } |
|
1238 return eType; |
|
1239 } |
|
1240 #endif |
|
1241 |
|
1242 /* |
|
1243 ** Generate code for scalar subqueries used as an expression |
|
1244 ** and IN operators. Examples: |
|
1245 ** |
|
1246 ** (SELECT a FROM b) -- subquery |
|
1247 ** EXISTS (SELECT a FROM b) -- EXISTS subquery |
|
1248 ** x IN (4,5,11) -- IN operator with list on right-hand side |
|
1249 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right |
|
1250 ** |
|
1251 ** The pExpr parameter describes the expression that contains the IN |
|
1252 ** operator or subquery. |
|
1253 ** |
|
1254 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed |
|
1255 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference |
|
1256 ** to some integer key column of a table B-Tree. In this case, use an |
|
1257 ** intkey B-Tree to store the set of IN(...) values instead of the usual |
|
1258 ** (slower) variable length keys B-Tree. |
|
1259 */ |
|
1260 #ifndef SQLITE_OMIT_SUBQUERY |
|
1261 void sqlite3CodeSubselect( |
|
1262 Parse *pParse, |
|
1263 Expr *pExpr, |
|
1264 int rMayHaveNull, |
|
1265 int isRowid |
|
1266 ){ |
|
1267 int testAddr = 0; /* One-time test address */ |
|
1268 Vdbe *v = sqlite3GetVdbe(pParse); |
|
1269 if( v==0 ) return; |
|
1270 |
|
1271 |
|
1272 /* This code must be run in its entirety every time it is encountered |
|
1273 ** if any of the following is true: |
|
1274 ** |
|
1275 ** * The right-hand side is a correlated subquery |
|
1276 ** * The right-hand side is an expression list containing variables |
|
1277 ** * We are inside a trigger |
|
1278 ** |
|
1279 ** If all of the above are false, then we can run this code just once |
|
1280 ** save the results, and reuse the same result on subsequent invocations. |
|
1281 */ |
|
1282 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ |
|
1283 int mem = ++pParse->nMem; |
|
1284 sqlite3VdbeAddOp1(v, OP_If, mem); |
|
1285 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem); |
|
1286 assert( testAddr>0 || pParse->db->mallocFailed ); |
|
1287 } |
|
1288 |
|
1289 switch( pExpr->op ){ |
|
1290 case TK_IN: { |
|
1291 char affinity; |
|
1292 KeyInfo keyInfo; |
|
1293 int addr; /* Address of OP_OpenEphemeral instruction */ |
|
1294 Expr *pLeft = pExpr->pLeft; |
|
1295 |
|
1296 if( rMayHaveNull ){ |
|
1297 sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull); |
|
1298 } |
|
1299 |
|
1300 affinity = sqlite3ExprAffinity(pLeft); |
|
1301 |
|
1302 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' |
|
1303 ** expression it is handled the same way. A virtual table is |
|
1304 ** filled with single-field index keys representing the results |
|
1305 ** from the SELECT or the <exprlist>. |
|
1306 ** |
|
1307 ** If the 'x' expression is a column value, or the SELECT... |
|
1308 ** statement returns a column value, then the affinity of that |
|
1309 ** column is used to build the index keys. If both 'x' and the |
|
1310 ** SELECT... statement are columns, then numeric affinity is used |
|
1311 ** if either column has NUMERIC or INTEGER affinity. If neither |
|
1312 ** 'x' nor the SELECT... statement are columns, then numeric affinity |
|
1313 ** is used. |
|
1314 */ |
|
1315 pExpr->iTable = pParse->nTab++; |
|
1316 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); |
|
1317 memset(&keyInfo, 0, sizeof(keyInfo)); |
|
1318 keyInfo.nField = 1; |
|
1319 |
|
1320 if( pExpr->pSelect ){ |
|
1321 /* Case 1: expr IN (SELECT ...) |
|
1322 ** |
|
1323 ** Generate code to write the results of the select into the temporary |
|
1324 ** table allocated and opened above. |
|
1325 */ |
|
1326 SelectDest dest; |
|
1327 ExprList *pEList; |
|
1328 |
|
1329 assert( !isRowid ); |
|
1330 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); |
|
1331 dest.affinity = (int)affinity; |
|
1332 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); |
|
1333 if( sqlite3Select(pParse, pExpr->pSelect, &dest) ){ |
|
1334 return; |
|
1335 } |
|
1336 pEList = pExpr->pSelect->pEList; |
|
1337 if( pEList && pEList->nExpr>0 ){ |
|
1338 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, |
|
1339 pEList->a[0].pExpr); |
|
1340 } |
|
1341 }else if( pExpr->pList ){ |
|
1342 /* Case 2: expr IN (exprlist) |
|
1343 ** |
|
1344 ** For each expression, build an index key from the evaluation and |
|
1345 ** store it in the temporary table. If <expr> is a column, then use |
|
1346 ** that columns affinity when building index keys. If <expr> is not |
|
1347 ** a column, use numeric affinity. |
|
1348 */ |
|
1349 int i; |
|
1350 ExprList *pList = pExpr->pList; |
|
1351 struct ExprList_item *pItem; |
|
1352 int r1, r2, r3; |
|
1353 |
|
1354 if( !affinity ){ |
|
1355 affinity = SQLITE_AFF_NONE; |
|
1356 } |
|
1357 keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
|
1358 |
|
1359 /* Loop through each expression in <exprlist>. */ |
|
1360 r1 = sqlite3GetTempReg(pParse); |
|
1361 r2 = sqlite3GetTempReg(pParse); |
|
1362 sqlite3VdbeAddOp2(v, OP_Null, 0, r2); |
|
1363 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ |
|
1364 Expr *pE2 = pItem->pExpr; |
|
1365 |
|
1366 /* If the expression is not constant then we will need to |
|
1367 ** disable the test that was generated above that makes sure |
|
1368 ** this code only executes once. Because for a non-constant |
|
1369 ** expression we need to rerun this code each time. |
|
1370 */ |
|
1371 if( testAddr && !sqlite3ExprIsConstant(pE2) ){ |
|
1372 sqlite3VdbeChangeToNoop(v, testAddr-1, 2); |
|
1373 testAddr = 0; |
|
1374 } |
|
1375 |
|
1376 /* Evaluate the expression and insert it into the temp table */ |
|
1377 pParse->disableColCache++; |
|
1378 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); |
|
1379 assert( pParse->disableColCache>0 ); |
|
1380 pParse->disableColCache--; |
|
1381 |
|
1382 if( isRowid ){ |
|
1383 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, sqlite3VdbeCurrentAddr(v)+2); |
|
1384 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); |
|
1385 }else{ |
|
1386 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); |
|
1387 sqlite3ExprCacheAffinityChange(pParse, r3, 1); |
|
1388 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); |
|
1389 } |
|
1390 } |
|
1391 sqlite3ReleaseTempReg(pParse, r1); |
|
1392 sqlite3ReleaseTempReg(pParse, r2); |
|
1393 } |
|
1394 if( !isRowid ){ |
|
1395 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO); |
|
1396 } |
|
1397 break; |
|
1398 } |
|
1399 |
|
1400 case TK_EXISTS: |
|
1401 case TK_SELECT: { |
|
1402 /* This has to be a scalar SELECT. Generate code to put the |
|
1403 ** value of this select in a memory cell and record the number |
|
1404 ** of the memory cell in iColumn. |
|
1405 */ |
|
1406 static const Token one = { (u8*)"1", 0, 1 }; |
|
1407 Select *pSel; |
|
1408 SelectDest dest; |
|
1409 |
|
1410 pSel = pExpr->pSelect; |
|
1411 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); |
|
1412 if( pExpr->op==TK_SELECT ){ |
|
1413 dest.eDest = SRT_Mem; |
|
1414 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm); |
|
1415 VdbeComment((v, "Init subquery result")); |
|
1416 }else{ |
|
1417 dest.eDest = SRT_Exists; |
|
1418 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm); |
|
1419 VdbeComment((v, "Init EXISTS result")); |
|
1420 } |
|
1421 sqlite3ExprDelete(pParse->db, pSel->pLimit); |
|
1422 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one); |
|
1423 if( sqlite3Select(pParse, pSel, &dest) ){ |
|
1424 return; |
|
1425 } |
|
1426 pExpr->iColumn = dest.iParm; |
|
1427 break; |
|
1428 } |
|
1429 } |
|
1430 |
|
1431 if( testAddr ){ |
|
1432 sqlite3VdbeJumpHere(v, testAddr-1); |
|
1433 } |
|
1434 |
|
1435 return; |
|
1436 } |
|
1437 #endif /* SQLITE_OMIT_SUBQUERY */ |
|
1438 |
|
1439 /* |
|
1440 ** Duplicate an 8-byte value |
|
1441 */ |
|
1442 static char *dup8bytes(Vdbe *v, const char *in){ |
|
1443 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); |
|
1444 if( out ){ |
|
1445 memcpy(out, in, 8); |
|
1446 } |
|
1447 return out; |
|
1448 } |
|
1449 |
|
1450 /* |
|
1451 ** Generate an instruction that will put the floating point |
|
1452 ** value described by z[0..n-1] into register iMem. |
|
1453 ** |
|
1454 ** The z[] string will probably not be zero-terminated. But the |
|
1455 ** z[n] character is guaranteed to be something that does not look |
|
1456 ** like the continuation of the number. |
|
1457 */ |
|
1458 static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){ |
|
1459 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); |
|
1460 if( z ){ |
|
1461 double value; |
|
1462 char *zV; |
|
1463 assert( !isdigit(z[n]) ); |
|
1464 sqlite3AtoF(z, &value); |
|
1465 if( sqlite3IsNaN(value) ){ |
|
1466 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem); |
|
1467 }else{ |
|
1468 if( negateFlag ) value = -value; |
|
1469 zV = dup8bytes(v, (char*)&value); |
|
1470 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); |
|
1471 } |
|
1472 } |
|
1473 } |
|
1474 |
|
1475 |
|
1476 /* |
|
1477 ** Generate an instruction that will put the integer describe by |
|
1478 ** text z[0..n-1] into register iMem. |
|
1479 ** |
|
1480 ** The z[] string will probably not be zero-terminated. But the |
|
1481 ** z[n] character is guaranteed to be something that does not look |
|
1482 ** like the continuation of the number. |
|
1483 */ |
|
1484 static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){ |
|
1485 const char *z; |
|
1486 if( pExpr->flags & EP_IntValue ){ |
|
1487 int i = pExpr->iTable; |
|
1488 if( negFlag ) i = -i; |
|
1489 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); |
|
1490 }else if( (z = (char*)pExpr->token.z)!=0 ){ |
|
1491 int i; |
|
1492 int n = pExpr->token.n; |
|
1493 assert( !isdigit(z[n]) ); |
|
1494 if( sqlite3GetInt32(z, &i) ){ |
|
1495 if( negFlag ) i = -i; |
|
1496 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); |
|
1497 }else if( sqlite3FitsIn64Bits(z, negFlag) ){ |
|
1498 i64 value; |
|
1499 char *zV; |
|
1500 sqlite3Atoi64(z, &value); |
|
1501 if( negFlag ) value = -value; |
|
1502 zV = dup8bytes(v, (char*)&value); |
|
1503 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); |
|
1504 }else{ |
|
1505 codeReal(v, z, n, negFlag, iMem); |
|
1506 } |
|
1507 } |
|
1508 } |
|
1509 |
|
1510 |
|
1511 /* |
|
1512 ** Generate code that will extract the iColumn-th column from |
|
1513 ** table pTab and store the column value in a register. An effort |
|
1514 ** is made to store the column value in register iReg, but this is |
|
1515 ** not guaranteed. The location of the column value is returned. |
|
1516 ** |
|
1517 ** There must be an open cursor to pTab in iTable when this routine |
|
1518 ** is called. If iColumn<0 then code is generated that extracts the rowid. |
|
1519 ** |
|
1520 ** This routine might attempt to reuse the value of the column that |
|
1521 ** has already been loaded into a register. The value will always |
|
1522 ** be used if it has not undergone any affinity changes. But if |
|
1523 ** an affinity change has occurred, then the cached value will only be |
|
1524 ** used if allowAffChng is true. |
|
1525 */ |
|
1526 int sqlite3ExprCodeGetColumn( |
|
1527 Parse *pParse, /* Parsing and code generating context */ |
|
1528 Table *pTab, /* Description of the table we are reading from */ |
|
1529 int iColumn, /* Index of the table column */ |
|
1530 int iTable, /* The cursor pointing to the table */ |
|
1531 int iReg, /* Store results here */ |
|
1532 int allowAffChng /* True if prior affinity changes are OK */ |
|
1533 ){ |
|
1534 Vdbe *v = pParse->pVdbe; |
|
1535 int i; |
|
1536 struct yColCache *p; |
|
1537 |
|
1538 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ |
|
1539 if( p->iTable==iTable && p->iColumn==iColumn |
|
1540 && (!p->affChange || allowAffChng) ){ |
|
1541 #if 0 |
|
1542 sqlite3VdbeAddOp0(v, OP_Noop); |
|
1543 VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg)); |
|
1544 #endif |
|
1545 return p->iReg; |
|
1546 } |
|
1547 } |
|
1548 assert( v!=0 ); |
|
1549 if( iColumn<0 ){ |
|
1550 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid; |
|
1551 sqlite3VdbeAddOp2(v, op, iTable, iReg); |
|
1552 }else if( pTab==0 ){ |
|
1553 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg); |
|
1554 }else{ |
|
1555 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; |
|
1556 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg); |
|
1557 sqlite3ColumnDefault(v, pTab, iColumn); |
|
1558 #ifndef SQLITE_OMIT_FLOATING_POINT |
|
1559 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){ |
|
1560 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
|
1561 } |
|
1562 #endif |
|
1563 } |
|
1564 if( pParse->disableColCache==0 ){ |
|
1565 i = pParse->iColCache; |
|
1566 p = &pParse->aColCache[i]; |
|
1567 p->iTable = iTable; |
|
1568 p->iColumn = iColumn; |
|
1569 p->iReg = iReg; |
|
1570 p->affChange = 0; |
|
1571 i++; |
|
1572 if( i>=ArraySize(pParse->aColCache) ) i = 0; |
|
1573 if( i>pParse->nColCache ) pParse->nColCache = i; |
|
1574 pParse->iColCache = i; |
|
1575 } |
|
1576 return iReg; |
|
1577 } |
|
1578 |
|
1579 /* |
|
1580 ** Clear all column cache entries associated with the vdbe |
|
1581 ** cursor with cursor number iTable. |
|
1582 */ |
|
1583 void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){ |
|
1584 if( iTable<0 ){ |
|
1585 pParse->nColCache = 0; |
|
1586 pParse->iColCache = 0; |
|
1587 }else{ |
|
1588 int i; |
|
1589 for(i=0; i<pParse->nColCache; i++){ |
|
1590 if( pParse->aColCache[i].iTable==iTable ){ |
|
1591 testcase( i==pParse->nColCache-1 ); |
|
1592 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache]; |
|
1593 pParse->iColCache = pParse->nColCache; |
|
1594 } |
|
1595 } |
|
1596 } |
|
1597 } |
|
1598 |
|
1599 /* |
|
1600 ** Record the fact that an affinity change has occurred on iCount |
|
1601 ** registers starting with iStart. |
|
1602 */ |
|
1603 void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ |
|
1604 int iEnd = iStart + iCount - 1; |
|
1605 int i; |
|
1606 for(i=0; i<pParse->nColCache; i++){ |
|
1607 int r = pParse->aColCache[i].iReg; |
|
1608 if( r>=iStart && r<=iEnd ){ |
|
1609 pParse->aColCache[i].affChange = 1; |
|
1610 } |
|
1611 } |
|
1612 } |
|
1613 |
|
1614 /* |
|
1615 ** Generate code to move content from registers iFrom...iFrom+nReg-1 |
|
1616 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. |
|
1617 */ |
|
1618 void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ |
|
1619 int i; |
|
1620 if( iFrom==iTo ) return; |
|
1621 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); |
|
1622 for(i=0; i<pParse->nColCache; i++){ |
|
1623 int x = pParse->aColCache[i].iReg; |
|
1624 if( x>=iFrom && x<iFrom+nReg ){ |
|
1625 pParse->aColCache[i].iReg += iTo-iFrom; |
|
1626 } |
|
1627 } |
|
1628 } |
|
1629 |
|
1630 /* |
|
1631 ** Generate code to copy content from registers iFrom...iFrom+nReg-1 |
|
1632 ** over to iTo..iTo+nReg-1. |
|
1633 */ |
|
1634 void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){ |
|
1635 int i; |
|
1636 if( iFrom==iTo ) return; |
|
1637 for(i=0; i<nReg; i++){ |
|
1638 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i); |
|
1639 } |
|
1640 } |
|
1641 |
|
1642 /* |
|
1643 ** Return true if any register in the range iFrom..iTo (inclusive) |
|
1644 ** is used as part of the column cache. |
|
1645 */ |
|
1646 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ |
|
1647 int i; |
|
1648 for(i=0; i<pParse->nColCache; i++){ |
|
1649 int r = pParse->aColCache[i].iReg; |
|
1650 if( r>=iFrom && r<=iTo ) return 1; |
|
1651 } |
|
1652 return 0; |
|
1653 } |
|
1654 |
|
1655 /* |
|
1656 ** Theres is a value in register iCurrent. We ultimately want |
|
1657 ** the value to be in register iTarget. It might be that |
|
1658 ** iCurrent and iTarget are the same register. |
|
1659 ** |
|
1660 ** We are going to modify the value, so we need to make sure it |
|
1661 ** is not a cached register. If iCurrent is a cached register, |
|
1662 ** then try to move the value over to iTarget. If iTarget is a |
|
1663 ** cached register, then clear the corresponding cache line. |
|
1664 ** |
|
1665 ** Return the register that the value ends up in. |
|
1666 */ |
|
1667 int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){ |
|
1668 int i; |
|
1669 assert( pParse->pVdbe!=0 ); |
|
1670 if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){ |
|
1671 return iCurrent; |
|
1672 } |
|
1673 if( iCurrent!=iTarget ){ |
|
1674 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget); |
|
1675 } |
|
1676 for(i=0; i<pParse->nColCache; i++){ |
|
1677 if( pParse->aColCache[i].iReg==iTarget ){ |
|
1678 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache]; |
|
1679 pParse->iColCache = pParse->nColCache; |
|
1680 } |
|
1681 } |
|
1682 return iTarget; |
|
1683 } |
|
1684 |
|
1685 /* |
|
1686 ** If the last instruction coded is an ephemeral copy of any of |
|
1687 ** the registers in the nReg registers beginning with iReg, then |
|
1688 ** convert the last instruction from OP_SCopy to OP_Copy. |
|
1689 */ |
|
1690 void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){ |
|
1691 int addr; |
|
1692 VdbeOp *pOp; |
|
1693 Vdbe *v; |
|
1694 |
|
1695 v = pParse->pVdbe; |
|
1696 addr = sqlite3VdbeCurrentAddr(v); |
|
1697 pOp = sqlite3VdbeGetOp(v, addr-1); |
|
1698 assert( pOp || pParse->db->mallocFailed ); |
|
1699 if( pOp && pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){ |
|
1700 pOp->opcode = OP_Copy; |
|
1701 } |
|
1702 } |
|
1703 |
|
1704 /* |
|
1705 ** Generate code to store the value of the iAlias-th alias in register |
|
1706 ** target. The first time this is called, pExpr is evaluated to compute |
|
1707 ** the value of the alias. The value is stored in an auxiliary register |
|
1708 ** and the number of that register is returned. On subsequent calls, |
|
1709 ** the register number is returned without generating any code. |
|
1710 ** |
|
1711 ** Note that in order for this to work, code must be generated in the |
|
1712 ** same order that it is executed. |
|
1713 ** |
|
1714 ** Aliases are numbered starting with 1. So iAlias is in the range |
|
1715 ** of 1 to pParse->nAlias inclusive. |
|
1716 ** |
|
1717 ** pParse->aAlias[iAlias-1] records the register number where the value |
|
1718 ** of the iAlias-th alias is stored. If zero, that means that the |
|
1719 ** alias has not yet been computed. |
|
1720 */ |
|
1721 static int codeAlias(Parse *pParse, int iAlias, Expr *pExpr, int target){ |
|
1722 sqlite3 *db = pParse->db; |
|
1723 int iReg; |
|
1724 if( pParse->nAliasAlloc<pParse->nAlias ){ |
|
1725 pParse->aAlias = sqlite3DbReallocOrFree(db, pParse->aAlias, |
|
1726 sizeof(pParse->aAlias[0])*pParse->nAlias ); |
|
1727 testcase( db->mallocFailed && pParse->nAliasAlloc>0 ); |
|
1728 if( db->mallocFailed ) return 0; |
|
1729 memset(&pParse->aAlias[pParse->nAliasAlloc], 0, |
|
1730 (pParse->nAlias-pParse->nAliasAlloc)*sizeof(pParse->aAlias[0])); |
|
1731 pParse->nAliasAlloc = pParse->nAlias; |
|
1732 } |
|
1733 assert( iAlias>0 && iAlias<=pParse->nAlias ); |
|
1734 iReg = pParse->aAlias[iAlias-1]; |
|
1735 if( iReg==0 ){ |
|
1736 if( pParse->disableColCache ){ |
|
1737 iReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
|
1738 }else{ |
|
1739 iReg = ++pParse->nMem; |
|
1740 sqlite3ExprCode(pParse, pExpr, iReg); |
|
1741 pParse->aAlias[iAlias-1] = iReg; |
|
1742 } |
|
1743 } |
|
1744 return iReg; |
|
1745 } |
|
1746 |
|
1747 /* |
|
1748 ** Generate code into the current Vdbe to evaluate the given |
|
1749 ** expression. Attempt to store the results in register "target". |
|
1750 ** Return the register where results are stored. |
|
1751 ** |
|
1752 ** With this routine, there is no guarantee that results will |
|
1753 ** be stored in target. The result might be stored in some other |
|
1754 ** register if it is convenient to do so. The calling function |
|
1755 ** must check the return code and move the results to the desired |
|
1756 ** register. |
|
1757 */ |
|
1758 int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ |
|
1759 Vdbe *v = pParse->pVdbe; /* The VM under construction */ |
|
1760 int op; /* The opcode being coded */ |
|
1761 int inReg = target; /* Results stored in register inReg */ |
|
1762 int regFree1 = 0; /* If non-zero free this temporary register */ |
|
1763 int regFree2 = 0; /* If non-zero free this temporary register */ |
|
1764 int r1, r2, r3, r4; /* Various register numbers */ |
|
1765 sqlite3 *db; |
|
1766 |
|
1767 db = pParse->db; |
|
1768 assert( v!=0 || db->mallocFailed ); |
|
1769 assert( target>0 && target<=pParse->nMem ); |
|
1770 if( v==0 ) return 0; |
|
1771 |
|
1772 if( pExpr==0 ){ |
|
1773 op = TK_NULL; |
|
1774 }else{ |
|
1775 op = pExpr->op; |
|
1776 } |
|
1777 switch( op ){ |
|
1778 case TK_AGG_COLUMN: { |
|
1779 AggInfo *pAggInfo = pExpr->pAggInfo; |
|
1780 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; |
|
1781 if( !pAggInfo->directMode ){ |
|
1782 assert( pCol->iMem>0 ); |
|
1783 inReg = pCol->iMem; |
|
1784 break; |
|
1785 }else if( pAggInfo->useSortingIdx ){ |
|
1786 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, |
|
1787 pCol->iSorterColumn, target); |
|
1788 break; |
|
1789 } |
|
1790 /* Otherwise, fall thru into the TK_COLUMN case */ |
|
1791 } |
|
1792 case TK_COLUMN: { |
|
1793 if( pExpr->iTable<0 ){ |
|
1794 /* This only happens when coding check constraints */ |
|
1795 assert( pParse->ckBase>0 ); |
|
1796 inReg = pExpr->iColumn + pParse->ckBase; |
|
1797 }else{ |
|
1798 testcase( (pExpr->flags & EP_AnyAff)!=0 ); |
|
1799 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, |
|
1800 pExpr->iColumn, pExpr->iTable, target, |
|
1801 pExpr->flags & EP_AnyAff); |
|
1802 } |
|
1803 break; |
|
1804 } |
|
1805 case TK_INTEGER: { |
|
1806 codeInteger(v, pExpr, 0, target); |
|
1807 break; |
|
1808 } |
|
1809 case TK_FLOAT: { |
|
1810 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); |
|
1811 break; |
|
1812 } |
|
1813 case TK_STRING: { |
|
1814 sqlite3DequoteExpr(db, pExpr); |
|
1815 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0, |
|
1816 (char*)pExpr->token.z, pExpr->token.n); |
|
1817 break; |
|
1818 } |
|
1819 case TK_NULL: { |
|
1820 sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
|
1821 break; |
|
1822 } |
|
1823 #ifndef SQLITE_OMIT_BLOB_LITERAL |
|
1824 case TK_BLOB: { |
|
1825 int n; |
|
1826 const char *z; |
|
1827 char *zBlob; |
|
1828 assert( pExpr->token.n>=3 ); |
|
1829 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' ); |
|
1830 assert( pExpr->token.z[1]=='\'' ); |
|
1831 assert( pExpr->token.z[pExpr->token.n-1]=='\'' ); |
|
1832 n = pExpr->token.n - 3; |
|
1833 z = (char*)pExpr->token.z + 2; |
|
1834 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); |
|
1835 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); |
|
1836 break; |
|
1837 } |
|
1838 #endif |
|
1839 case TK_VARIABLE: { |
|
1840 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target); |
|
1841 if( pExpr->token.n>1 ){ |
|
1842 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n); |
|
1843 } |
|
1844 break; |
|
1845 } |
|
1846 case TK_REGISTER: { |
|
1847 inReg = pExpr->iTable; |
|
1848 break; |
|
1849 } |
|
1850 case TK_AS: { |
|
1851 inReg = codeAlias(pParse, pExpr->iTable, pExpr->pLeft, target); |
|
1852 break; |
|
1853 } |
|
1854 #ifndef SQLITE_OMIT_CAST |
|
1855 case TK_CAST: { |
|
1856 /* Expressions of the form: CAST(pLeft AS token) */ |
|
1857 int aff, to_op; |
|
1858 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
|
1859 aff = sqlite3AffinityType(&pExpr->token); |
|
1860 to_op = aff - SQLITE_AFF_TEXT + OP_ToText; |
|
1861 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); |
|
1862 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); |
|
1863 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); |
|
1864 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); |
|
1865 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); |
|
1866 testcase( to_op==OP_ToText ); |
|
1867 testcase( to_op==OP_ToBlob ); |
|
1868 testcase( to_op==OP_ToNumeric ); |
|
1869 testcase( to_op==OP_ToInt ); |
|
1870 testcase( to_op==OP_ToReal ); |
|
1871 if( inReg!=target ){ |
|
1872 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); |
|
1873 inReg = target; |
|
1874 } |
|
1875 sqlite3VdbeAddOp1(v, to_op, inReg); |
|
1876 testcase( usedAsColumnCache(pParse, inReg, inReg) ); |
|
1877 sqlite3ExprCacheAffinityChange(pParse, inReg, 1); |
|
1878 break; |
|
1879 } |
|
1880 #endif /* SQLITE_OMIT_CAST */ |
|
1881 case TK_LT: |
|
1882 case TK_LE: |
|
1883 case TK_GT: |
|
1884 case TK_GE: |
|
1885 case TK_NE: |
|
1886 case TK_EQ: { |
|
1887 assert( TK_LT==OP_Lt ); |
|
1888 assert( TK_LE==OP_Le ); |
|
1889 assert( TK_GT==OP_Gt ); |
|
1890 assert( TK_GE==OP_Ge ); |
|
1891 assert( TK_EQ==OP_Eq ); |
|
1892 assert( TK_NE==OP_Ne ); |
|
1893 testcase( op==TK_LT ); |
|
1894 testcase( op==TK_LE ); |
|
1895 testcase( op==TK_GT ); |
|
1896 testcase( op==TK_GE ); |
|
1897 testcase( op==TK_EQ ); |
|
1898 testcase( op==TK_NE ); |
|
1899 codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, |
|
1900 pExpr->pRight, &r2, ®Free2); |
|
1901 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
|
1902 r1, r2, inReg, SQLITE_STOREP2); |
|
1903 testcase( regFree1==0 ); |
|
1904 testcase( regFree2==0 ); |
|
1905 break; |
|
1906 } |
|
1907 case TK_AND: |
|
1908 case TK_OR: |
|
1909 case TK_PLUS: |
|
1910 case TK_STAR: |
|
1911 case TK_MINUS: |
|
1912 case TK_REM: |
|
1913 case TK_BITAND: |
|
1914 case TK_BITOR: |
|
1915 case TK_SLASH: |
|
1916 case TK_LSHIFT: |
|
1917 case TK_RSHIFT: |
|
1918 case TK_CONCAT: { |
|
1919 assert( TK_AND==OP_And ); |
|
1920 assert( TK_OR==OP_Or ); |
|
1921 assert( TK_PLUS==OP_Add ); |
|
1922 assert( TK_MINUS==OP_Subtract ); |
|
1923 assert( TK_REM==OP_Remainder ); |
|
1924 assert( TK_BITAND==OP_BitAnd ); |
|
1925 assert( TK_BITOR==OP_BitOr ); |
|
1926 assert( TK_SLASH==OP_Divide ); |
|
1927 assert( TK_LSHIFT==OP_ShiftLeft ); |
|
1928 assert( TK_RSHIFT==OP_ShiftRight ); |
|
1929 assert( TK_CONCAT==OP_Concat ); |
|
1930 testcase( op==TK_AND ); |
|
1931 testcase( op==TK_OR ); |
|
1932 testcase( op==TK_PLUS ); |
|
1933 testcase( op==TK_MINUS ); |
|
1934 testcase( op==TK_REM ); |
|
1935 testcase( op==TK_BITAND ); |
|
1936 testcase( op==TK_BITOR ); |
|
1937 testcase( op==TK_SLASH ); |
|
1938 testcase( op==TK_LSHIFT ); |
|
1939 testcase( op==TK_RSHIFT ); |
|
1940 testcase( op==TK_CONCAT ); |
|
1941 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
|
1942 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
|
1943 sqlite3VdbeAddOp3(v, op, r2, r1, target); |
|
1944 testcase( regFree1==0 ); |
|
1945 testcase( regFree2==0 ); |
|
1946 break; |
|
1947 } |
|
1948 case TK_UMINUS: { |
|
1949 Expr *pLeft = pExpr->pLeft; |
|
1950 assert( pLeft ); |
|
1951 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ |
|
1952 if( pLeft->op==TK_FLOAT ){ |
|
1953 codeReal(v, (char*)pLeft->token.z, pLeft->token.n, 1, target); |
|
1954 }else{ |
|
1955 codeInteger(v, pLeft, 1, target); |
|
1956 } |
|
1957 }else{ |
|
1958 regFree1 = r1 = sqlite3GetTempReg(pParse); |
|
1959 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1); |
|
1960 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); |
|
1961 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); |
|
1962 testcase( regFree2==0 ); |
|
1963 } |
|
1964 inReg = target; |
|
1965 break; |
|
1966 } |
|
1967 case TK_BITNOT: |
|
1968 case TK_NOT: { |
|
1969 assert( TK_BITNOT==OP_BitNot ); |
|
1970 assert( TK_NOT==OP_Not ); |
|
1971 testcase( op==TK_BITNOT ); |
|
1972 testcase( op==TK_NOT ); |
|
1973 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
|
1974 testcase( regFree1==0 ); |
|
1975 inReg = target; |
|
1976 sqlite3VdbeAddOp2(v, op, r1, inReg); |
|
1977 break; |
|
1978 } |
|
1979 case TK_ISNULL: |
|
1980 case TK_NOTNULL: { |
|
1981 int addr; |
|
1982 assert( TK_ISNULL==OP_IsNull ); |
|
1983 assert( TK_NOTNULL==OP_NotNull ); |
|
1984 testcase( op==TK_ISNULL ); |
|
1985 testcase( op==TK_NOTNULL ); |
|
1986 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
|
1987 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
|
1988 testcase( regFree1==0 ); |
|
1989 addr = sqlite3VdbeAddOp1(v, op, r1); |
|
1990 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); |
|
1991 sqlite3VdbeJumpHere(v, addr); |
|
1992 break; |
|
1993 } |
|
1994 case TK_AGG_FUNCTION: { |
|
1995 AggInfo *pInfo = pExpr->pAggInfo; |
|
1996 if( pInfo==0 ){ |
|
1997 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T", |
|
1998 &pExpr->span); |
|
1999 }else{ |
|
2000 inReg = pInfo->aFunc[pExpr->iAgg].iMem; |
|
2001 } |
|
2002 break; |
|
2003 } |
|
2004 case TK_CONST_FUNC: |
|
2005 case TK_FUNCTION: { |
|
2006 ExprList *pList = pExpr->pList; |
|
2007 int nExpr = pList ? pList->nExpr : 0; |
|
2008 FuncDef *pDef; |
|
2009 int nId; |
|
2010 const char *zId; |
|
2011 int constMask = 0; |
|
2012 int i; |
|
2013 u8 enc = ENC(db); |
|
2014 CollSeq *pColl = 0; |
|
2015 |
|
2016 testcase( op==TK_CONST_FUNC ); |
|
2017 testcase( op==TK_FUNCTION ); |
|
2018 zId = (char*)pExpr->token.z; |
|
2019 nId = pExpr->token.n; |
|
2020 pDef = sqlite3FindFunction(db, zId, nId, nExpr, enc, 0); |
|
2021 assert( pDef!=0 ); |
|
2022 if( pList ){ |
|
2023 nExpr = pList->nExpr; |
|
2024 r1 = sqlite3GetTempRange(pParse, nExpr); |
|
2025 sqlite3ExprCodeExprList(pParse, pList, r1, 1); |
|
2026 }else{ |
|
2027 nExpr = r1 = 0; |
|
2028 } |
|
2029 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
2030 /* Possibly overload the function if the first argument is |
|
2031 ** a virtual table column. |
|
2032 ** |
|
2033 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the |
|
2034 ** second argument, not the first, as the argument to test to |
|
2035 ** see if it is a column in a virtual table. This is done because |
|
2036 ** the left operand of infix functions (the operand we want to |
|
2037 ** control overloading) ends up as the second argument to the |
|
2038 ** function. The expression "A glob B" is equivalent to |
|
2039 ** "glob(B,A). We want to use the A in "A glob B" to test |
|
2040 ** for function overloading. But we use the B term in "glob(B,A)". |
|
2041 */ |
|
2042 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){ |
|
2043 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr); |
|
2044 }else if( nExpr>0 ){ |
|
2045 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr); |
|
2046 } |
|
2047 #endif |
|
2048 for(i=0; i<nExpr && i<32; i++){ |
|
2049 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ |
|
2050 constMask |= (1<<i); |
|
2051 } |
|
2052 if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ |
|
2053 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
|
2054 } |
|
2055 } |
|
2056 if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){ |
|
2057 if( !pColl ) pColl = db->pDfltColl; |
|
2058 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); |
|
2059 } |
|
2060 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, |
|
2061 (char*)pDef, P4_FUNCDEF); |
|
2062 sqlite3VdbeChangeP5(v, nExpr); |
|
2063 if( nExpr ){ |
|
2064 sqlite3ReleaseTempRange(pParse, r1, nExpr); |
|
2065 } |
|
2066 sqlite3ExprCacheAffinityChange(pParse, r1, nExpr); |
|
2067 break; |
|
2068 } |
|
2069 #ifndef SQLITE_OMIT_SUBQUERY |
|
2070 case TK_EXISTS: |
|
2071 case TK_SELECT: { |
|
2072 testcase( op==TK_EXISTS ); |
|
2073 testcase( op==TK_SELECT ); |
|
2074 if( pExpr->iColumn==0 ){ |
|
2075 sqlite3CodeSubselect(pParse, pExpr, 0, 0); |
|
2076 } |
|
2077 inReg = pExpr->iColumn; |
|
2078 break; |
|
2079 } |
|
2080 case TK_IN: { |
|
2081 int rNotFound = 0; |
|
2082 int rMayHaveNull = 0; |
|
2083 int j2, j3, j4, j5; |
|
2084 char affinity; |
|
2085 int eType; |
|
2086 |
|
2087 VdbeNoopComment((v, "begin IN expr r%d", target)); |
|
2088 eType = sqlite3FindInIndex(pParse, pExpr, &rMayHaveNull); |
|
2089 if( rMayHaveNull ){ |
|
2090 rNotFound = ++pParse->nMem; |
|
2091 } |
|
2092 |
|
2093 /* Figure out the affinity to use to create a key from the results |
|
2094 ** of the expression. affinityStr stores a static string suitable for |
|
2095 ** P4 of OP_MakeRecord. |
|
2096 */ |
|
2097 affinity = comparisonAffinity(pExpr); |
|
2098 |
|
2099 |
|
2100 /* Code the <expr> from "<expr> IN (...)". The temporary table |
|
2101 ** pExpr->iTable contains the values that make up the (...) set. |
|
2102 */ |
|
2103 pParse->disableColCache++; |
|
2104 sqlite3ExprCode(pParse, pExpr->pLeft, target); |
|
2105 pParse->disableColCache--; |
|
2106 j2 = sqlite3VdbeAddOp1(v, OP_IsNull, target); |
|
2107 if( eType==IN_INDEX_ROWID ){ |
|
2108 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, target); |
|
2109 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, target); |
|
2110 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
|
2111 j5 = sqlite3VdbeAddOp0(v, OP_Goto); |
|
2112 sqlite3VdbeJumpHere(v, j3); |
|
2113 sqlite3VdbeJumpHere(v, j4); |
|
2114 sqlite3VdbeAddOp2(v, OP_Integer, 0, target); |
|
2115 }else{ |
|
2116 r2 = regFree2 = sqlite3GetTempReg(pParse); |
|
2117 |
|
2118 /* Create a record and test for set membership. If the set contains |
|
2119 ** the value, then jump to the end of the test code. The target |
|
2120 ** register still contains the true (1) value written to it earlier. |
|
2121 */ |
|
2122 sqlite3VdbeAddOp4(v, OP_MakeRecord, target, 1, r2, &affinity, 1); |
|
2123 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
|
2124 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2); |
|
2125 |
|
2126 /* If the set membership test fails, then the result of the |
|
2127 ** "x IN (...)" expression must be either 0 or NULL. If the set |
|
2128 ** contains no NULL values, then the result is 0. If the set |
|
2129 ** contains one or more NULL values, then the result of the |
|
2130 ** expression is also NULL. |
|
2131 */ |
|
2132 if( rNotFound==0 ){ |
|
2133 /* This branch runs if it is known at compile time (now) that |
|
2134 ** the set contains no NULL values. This happens as the result |
|
2135 ** of a "NOT NULL" constraint in the database schema. No need |
|
2136 ** to test the data structure at runtime in this case. |
|
2137 */ |
|
2138 sqlite3VdbeAddOp2(v, OP_Integer, 0, target); |
|
2139 }else{ |
|
2140 /* This block populates the rNotFound register with either NULL |
|
2141 ** or 0 (an integer value). If the data structure contains one |
|
2142 ** or more NULLs, then set rNotFound to NULL. Otherwise, set it |
|
2143 ** to 0. If register rMayHaveNull is already set to some value |
|
2144 ** other than NULL, then the test has already been run and |
|
2145 ** rNotFound is already populated. |
|
2146 */ |
|
2147 static const char nullRecord[] = { 0x02, 0x00 }; |
|
2148 j3 = sqlite3VdbeAddOp1(v, OP_NotNull, rMayHaveNull); |
|
2149 sqlite3VdbeAddOp2(v, OP_Null, 0, rNotFound); |
|
2150 sqlite3VdbeAddOp4(v, OP_Blob, 2, rMayHaveNull, 0, |
|
2151 nullRecord, P4_STATIC); |
|
2152 j4 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, rMayHaveNull); |
|
2153 sqlite3VdbeAddOp2(v, OP_Integer, 0, rNotFound); |
|
2154 sqlite3VdbeJumpHere(v, j4); |
|
2155 sqlite3VdbeJumpHere(v, j3); |
|
2156 |
|
2157 /* Copy the value of register rNotFound (which is either NULL or 0) |
|
2158 ** into the target register. This will be the result of the |
|
2159 ** expression. |
|
2160 */ |
|
2161 sqlite3VdbeAddOp2(v, OP_Copy, rNotFound, target); |
|
2162 } |
|
2163 } |
|
2164 sqlite3VdbeJumpHere(v, j2); |
|
2165 sqlite3VdbeJumpHere(v, j5); |
|
2166 VdbeComment((v, "end IN expr r%d", target)); |
|
2167 break; |
|
2168 } |
|
2169 #endif |
|
2170 /* |
|
2171 ** x BETWEEN y AND z |
|
2172 ** |
|
2173 ** This is equivalent to |
|
2174 ** |
|
2175 ** x>=y AND x<=z |
|
2176 ** |
|
2177 ** X is stored in pExpr->pLeft. |
|
2178 ** Y is stored in pExpr->pList->a[0].pExpr. |
|
2179 ** Z is stored in pExpr->pList->a[1].pExpr. |
|
2180 */ |
|
2181 case TK_BETWEEN: { |
|
2182 Expr *pLeft = pExpr->pLeft; |
|
2183 struct ExprList_item *pLItem = pExpr->pList->a; |
|
2184 Expr *pRight = pLItem->pExpr; |
|
2185 |
|
2186 codeCompareOperands(pParse, pLeft, &r1, ®Free1, |
|
2187 pRight, &r2, ®Free2); |
|
2188 testcase( regFree1==0 ); |
|
2189 testcase( regFree2==0 ); |
|
2190 r3 = sqlite3GetTempReg(pParse); |
|
2191 r4 = sqlite3GetTempReg(pParse); |
|
2192 codeCompare(pParse, pLeft, pRight, OP_Ge, |
|
2193 r1, r2, r3, SQLITE_STOREP2); |
|
2194 pLItem++; |
|
2195 pRight = pLItem->pExpr; |
|
2196 sqlite3ReleaseTempReg(pParse, regFree2); |
|
2197 r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); |
|
2198 testcase( regFree2==0 ); |
|
2199 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); |
|
2200 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); |
|
2201 sqlite3ReleaseTempReg(pParse, r3); |
|
2202 sqlite3ReleaseTempReg(pParse, r4); |
|
2203 break; |
|
2204 } |
|
2205 case TK_UPLUS: { |
|
2206 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
|
2207 break; |
|
2208 } |
|
2209 |
|
2210 /* |
|
2211 ** Form A: |
|
2212 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
|
2213 ** |
|
2214 ** Form B: |
|
2215 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
|
2216 ** |
|
2217 ** Form A is can be transformed into the equivalent form B as follows: |
|
2218 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... |
|
2219 ** WHEN x=eN THEN rN ELSE y END |
|
2220 ** |
|
2221 ** X (if it exists) is in pExpr->pLeft. |
|
2222 ** Y is in pExpr->pRight. The Y is also optional. If there is no |
|
2223 ** ELSE clause and no other term matches, then the result of the |
|
2224 ** exprssion is NULL. |
|
2225 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. |
|
2226 ** |
|
2227 ** The result of the expression is the Ri for the first matching Ei, |
|
2228 ** or if there is no matching Ei, the ELSE term Y, or if there is |
|
2229 ** no ELSE term, NULL. |
|
2230 */ |
|
2231 case TK_CASE: { |
|
2232 int endLabel; /* GOTO label for end of CASE stmt */ |
|
2233 int nextCase; /* GOTO label for next WHEN clause */ |
|
2234 int nExpr; /* 2x number of WHEN terms */ |
|
2235 int i; /* Loop counter */ |
|
2236 ExprList *pEList; /* List of WHEN terms */ |
|
2237 struct ExprList_item *aListelem; /* Array of WHEN terms */ |
|
2238 Expr opCompare; /* The X==Ei expression */ |
|
2239 Expr cacheX; /* Cached expression X */ |
|
2240 Expr *pX; /* The X expression */ |
|
2241 Expr *pTest; /* X==Ei (form A) or just Ei (form B) */ |
|
2242 |
|
2243 assert(pExpr->pList); |
|
2244 assert((pExpr->pList->nExpr % 2) == 0); |
|
2245 assert(pExpr->pList->nExpr > 0); |
|
2246 pEList = pExpr->pList; |
|
2247 aListelem = pEList->a; |
|
2248 nExpr = pEList->nExpr; |
|
2249 endLabel = sqlite3VdbeMakeLabel(v); |
|
2250 if( (pX = pExpr->pLeft)!=0 ){ |
|
2251 cacheX = *pX; |
|
2252 testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER ); |
|
2253 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, ®Free1); |
|
2254 testcase( regFree1==0 ); |
|
2255 cacheX.op = TK_REGISTER; |
|
2256 opCompare.op = TK_EQ; |
|
2257 opCompare.pLeft = &cacheX; |
|
2258 pTest = &opCompare; |
|
2259 } |
|
2260 pParse->disableColCache++; |
|
2261 for(i=0; i<nExpr; i=i+2){ |
|
2262 if( pX ){ |
|
2263 opCompare.pRight = aListelem[i].pExpr; |
|
2264 }else{ |
|
2265 pTest = aListelem[i].pExpr; |
|
2266 } |
|
2267 nextCase = sqlite3VdbeMakeLabel(v); |
|
2268 testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER ); |
|
2269 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); |
|
2270 testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); |
|
2271 testcase( aListelem[i+1].pExpr->op==TK_REGISTER ); |
|
2272 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); |
|
2273 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); |
|
2274 sqlite3VdbeResolveLabel(v, nextCase); |
|
2275 } |
|
2276 if( pExpr->pRight ){ |
|
2277 sqlite3ExprCode(pParse, pExpr->pRight, target); |
|
2278 }else{ |
|
2279 sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
|
2280 } |
|
2281 sqlite3VdbeResolveLabel(v, endLabel); |
|
2282 assert( pParse->disableColCache>0 ); |
|
2283 pParse->disableColCache--; |
|
2284 break; |
|
2285 } |
|
2286 #ifndef SQLITE_OMIT_TRIGGER |
|
2287 case TK_RAISE: { |
|
2288 if( !pParse->trigStack ){ |
|
2289 sqlite3ErrorMsg(pParse, |
|
2290 "RAISE() may only be used within a trigger-program"); |
|
2291 return 0; |
|
2292 } |
|
2293 if( pExpr->iColumn!=OE_Ignore ){ |
|
2294 assert( pExpr->iColumn==OE_Rollback || |
|
2295 pExpr->iColumn == OE_Abort || |
|
2296 pExpr->iColumn == OE_Fail ); |
|
2297 sqlite3DequoteExpr(db, pExpr); |
|
2298 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0, |
|
2299 (char*)pExpr->token.z, pExpr->token.n); |
|
2300 } else { |
|
2301 assert( pExpr->iColumn == OE_Ignore ); |
|
2302 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0); |
|
2303 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump); |
|
2304 VdbeComment((v, "raise(IGNORE)")); |
|
2305 } |
|
2306 break; |
|
2307 } |
|
2308 #endif |
|
2309 } |
|
2310 sqlite3ReleaseTempReg(pParse, regFree1); |
|
2311 sqlite3ReleaseTempReg(pParse, regFree2); |
|
2312 return inReg; |
|
2313 } |
|
2314 |
|
2315 /* |
|
2316 ** Generate code to evaluate an expression and store the results |
|
2317 ** into a register. Return the register number where the results |
|
2318 ** are stored. |
|
2319 ** |
|
2320 ** If the register is a temporary register that can be deallocated, |
|
2321 ** then write its number into *pReg. If the result register is not |
|
2322 ** a temporary, then set *pReg to zero. |
|
2323 */ |
|
2324 int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ |
|
2325 int r1 = sqlite3GetTempReg(pParse); |
|
2326 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
|
2327 if( r2==r1 ){ |
|
2328 *pReg = r1; |
|
2329 }else{ |
|
2330 sqlite3ReleaseTempReg(pParse, r1); |
|
2331 *pReg = 0; |
|
2332 } |
|
2333 return r2; |
|
2334 } |
|
2335 |
|
2336 /* |
|
2337 ** Generate code that will evaluate expression pExpr and store the |
|
2338 ** results in register target. The results are guaranteed to appear |
|
2339 ** in register target. |
|
2340 */ |
|
2341 int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ |
|
2342 int inReg; |
|
2343 |
|
2344 assert( target>0 && target<=pParse->nMem ); |
|
2345 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
|
2346 assert( pParse->pVdbe || pParse->db->mallocFailed ); |
|
2347 if( inReg!=target && pParse->pVdbe ){ |
|
2348 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); |
|
2349 } |
|
2350 return target; |
|
2351 } |
|
2352 |
|
2353 /* |
|
2354 ** Generate code that evalutes the given expression and puts the result |
|
2355 ** in register target. |
|
2356 ** |
|
2357 ** Also make a copy of the expression results into another "cache" register |
|
2358 ** and modify the expression so that the next time it is evaluated, |
|
2359 ** the result is a copy of the cache register. |
|
2360 ** |
|
2361 ** This routine is used for expressions that are used multiple |
|
2362 ** times. They are evaluated once and the results of the expression |
|
2363 ** are reused. |
|
2364 */ |
|
2365 int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ |
|
2366 Vdbe *v = pParse->pVdbe; |
|
2367 int inReg; |
|
2368 inReg = sqlite3ExprCode(pParse, pExpr, target); |
|
2369 assert( target>0 ); |
|
2370 if( pExpr->op!=TK_REGISTER ){ |
|
2371 int iMem; |
|
2372 iMem = ++pParse->nMem; |
|
2373 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); |
|
2374 pExpr->iTable = iMem; |
|
2375 pExpr->op = TK_REGISTER; |
|
2376 } |
|
2377 return inReg; |
|
2378 } |
|
2379 |
|
2380 /* |
|
2381 ** Return TRUE if pExpr is an constant expression that is appropriate |
|
2382 ** for factoring out of a loop. Appropriate expressions are: |
|
2383 ** |
|
2384 ** * Any expression that evaluates to two or more opcodes. |
|
2385 ** |
|
2386 ** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, |
|
2387 ** or OP_Variable that does not need to be placed in a |
|
2388 ** specific register. |
|
2389 ** |
|
2390 ** There is no point in factoring out single-instruction constant |
|
2391 ** expressions that need to be placed in a particular register. |
|
2392 ** We could factor them out, but then we would end up adding an |
|
2393 ** OP_SCopy instruction to move the value into the correct register |
|
2394 ** later. We might as well just use the original instruction and |
|
2395 ** avoid the OP_SCopy. |
|
2396 */ |
|
2397 static int isAppropriateForFactoring(Expr *p){ |
|
2398 if( !sqlite3ExprIsConstantNotJoin(p) ){ |
|
2399 return 0; /* Only constant expressions are appropriate for factoring */ |
|
2400 } |
|
2401 if( (p->flags & EP_FixedDest)==0 ){ |
|
2402 return 1; /* Any constant without a fixed destination is appropriate */ |
|
2403 } |
|
2404 while( p->op==TK_UPLUS ) p = p->pLeft; |
|
2405 switch( p->op ){ |
|
2406 #ifndef SQLITE_OMIT_BLOB_LITERAL |
|
2407 case TK_BLOB: |
|
2408 #endif |
|
2409 case TK_VARIABLE: |
|
2410 case TK_INTEGER: |
|
2411 case TK_FLOAT: |
|
2412 case TK_NULL: |
|
2413 case TK_STRING: { |
|
2414 testcase( p->op==TK_BLOB ); |
|
2415 testcase( p->op==TK_VARIABLE ); |
|
2416 testcase( p->op==TK_INTEGER ); |
|
2417 testcase( p->op==TK_FLOAT ); |
|
2418 testcase( p->op==TK_NULL ); |
|
2419 testcase( p->op==TK_STRING ); |
|
2420 /* Single-instruction constants with a fixed destination are |
|
2421 ** better done in-line. If we factor them, they will just end |
|
2422 ** up generating an OP_SCopy to move the value to the destination |
|
2423 ** register. */ |
|
2424 return 0; |
|
2425 } |
|
2426 case TK_UMINUS: { |
|
2427 if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){ |
|
2428 return 0; |
|
2429 } |
|
2430 break; |
|
2431 } |
|
2432 default: { |
|
2433 break; |
|
2434 } |
|
2435 } |
|
2436 return 1; |
|
2437 } |
|
2438 |
|
2439 /* |
|
2440 ** If pExpr is a constant expression that is appropriate for |
|
2441 ** factoring out of a loop, then evaluate the expression |
|
2442 ** into a register and convert the expression into a TK_REGISTER |
|
2443 ** expression. |
|
2444 */ |
|
2445 static int evalConstExpr(Walker *pWalker, Expr *pExpr){ |
|
2446 Parse *pParse = pWalker->pParse; |
|
2447 switch( pExpr->op ){ |
|
2448 case TK_REGISTER: { |
|
2449 return 1; |
|
2450 } |
|
2451 case TK_FUNCTION: |
|
2452 case TK_AGG_FUNCTION: |
|
2453 case TK_CONST_FUNC: { |
|
2454 /* The arguments to a function have a fixed destination. |
|
2455 ** Mark them this way to avoid generated unneeded OP_SCopy |
|
2456 ** instructions. |
|
2457 */ |
|
2458 ExprList *pList = pExpr->pList; |
|
2459 if( pList ){ |
|
2460 int i = pList->nExpr; |
|
2461 struct ExprList_item *pItem = pList->a; |
|
2462 for(; i>0; i--, pItem++){ |
|
2463 if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest; |
|
2464 } |
|
2465 } |
|
2466 break; |
|
2467 } |
|
2468 } |
|
2469 if( isAppropriateForFactoring(pExpr) ){ |
|
2470 int r1 = ++pParse->nMem; |
|
2471 int r2; |
|
2472 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
|
2473 if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1); |
|
2474 pExpr->op = TK_REGISTER; |
|
2475 pExpr->iTable = r2; |
|
2476 return WRC_Prune; |
|
2477 } |
|
2478 return WRC_Continue; |
|
2479 } |
|
2480 |
|
2481 /* |
|
2482 ** Preevaluate constant subexpressions within pExpr and store the |
|
2483 ** results in registers. Modify pExpr so that the constant subexpresions |
|
2484 ** are TK_REGISTER opcodes that refer to the precomputed values. |
|
2485 */ |
|
2486 void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ |
|
2487 Walker w; |
|
2488 w.xExprCallback = evalConstExpr; |
|
2489 w.xSelectCallback = 0; |
|
2490 w.pParse = pParse; |
|
2491 sqlite3WalkExpr(&w, pExpr); |
|
2492 } |
|
2493 |
|
2494 |
|
2495 /* |
|
2496 ** Generate code that pushes the value of every element of the given |
|
2497 ** expression list into a sequence of registers beginning at target. |
|
2498 ** |
|
2499 ** Return the number of elements evaluated. |
|
2500 */ |
|
2501 int sqlite3ExprCodeExprList( |
|
2502 Parse *pParse, /* Parsing context */ |
|
2503 ExprList *pList, /* The expression list to be coded */ |
|
2504 int target, /* Where to write results */ |
|
2505 int doHardCopy /* Make a hard copy of every element */ |
|
2506 ){ |
|
2507 struct ExprList_item *pItem; |
|
2508 int i, n; |
|
2509 assert( pList!=0 ); |
|
2510 assert( target>0 ); |
|
2511 n = pList->nExpr; |
|
2512 for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
|
2513 if( pItem->iAlias ){ |
|
2514 int iReg = codeAlias(pParse, pItem->iAlias, pItem->pExpr, target); |
|
2515 Vdbe *v = sqlite3GetVdbe(pParse); |
|
2516 if( iReg!=target+i ){ |
|
2517 sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target+i); |
|
2518 } |
|
2519 }else{ |
|
2520 sqlite3ExprCode(pParse, pItem->pExpr, target+i); |
|
2521 } |
|
2522 if( doHardCopy ){ |
|
2523 sqlite3ExprHardCopy(pParse, target, n); |
|
2524 } |
|
2525 } |
|
2526 return n; |
|
2527 } |
|
2528 |
|
2529 /* |
|
2530 ** Generate code for a boolean expression such that a jump is made |
|
2531 ** to the label "dest" if the expression is true but execution |
|
2532 ** continues straight thru if the expression is false. |
|
2533 ** |
|
2534 ** If the expression evaluates to NULL (neither true nor false), then |
|
2535 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. |
|
2536 ** |
|
2537 ** This code depends on the fact that certain token values (ex: TK_EQ) |
|
2538 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding |
|
2539 ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in |
|
2540 ** the make process cause these values to align. Assert()s in the code |
|
2541 ** below verify that the numbers are aligned correctly. |
|
2542 */ |
|
2543 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
|
2544 Vdbe *v = pParse->pVdbe; |
|
2545 int op = 0; |
|
2546 int regFree1 = 0; |
|
2547 int regFree2 = 0; |
|
2548 int r1, r2; |
|
2549 |
|
2550 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
|
2551 if( v==0 || pExpr==0 ) return; |
|
2552 op = pExpr->op; |
|
2553 switch( op ){ |
|
2554 case TK_AND: { |
|
2555 int d2 = sqlite3VdbeMakeLabel(v); |
|
2556 testcase( jumpIfNull==0 ); |
|
2557 testcase( pParse->disableColCache==0 ); |
|
2558 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); |
|
2559 pParse->disableColCache++; |
|
2560 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
|
2561 assert( pParse->disableColCache>0 ); |
|
2562 pParse->disableColCache--; |
|
2563 sqlite3VdbeResolveLabel(v, d2); |
|
2564 break; |
|
2565 } |
|
2566 case TK_OR: { |
|
2567 testcase( jumpIfNull==0 ); |
|
2568 testcase( pParse->disableColCache==0 ); |
|
2569 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
|
2570 pParse->disableColCache++; |
|
2571 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
|
2572 assert( pParse->disableColCache>0 ); |
|
2573 pParse->disableColCache--; |
|
2574 break; |
|
2575 } |
|
2576 case TK_NOT: { |
|
2577 testcase( jumpIfNull==0 ); |
|
2578 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
|
2579 break; |
|
2580 } |
|
2581 case TK_LT: |
|
2582 case TK_LE: |
|
2583 case TK_GT: |
|
2584 case TK_GE: |
|
2585 case TK_NE: |
|
2586 case TK_EQ: { |
|
2587 assert( TK_LT==OP_Lt ); |
|
2588 assert( TK_LE==OP_Le ); |
|
2589 assert( TK_GT==OP_Gt ); |
|
2590 assert( TK_GE==OP_Ge ); |
|
2591 assert( TK_EQ==OP_Eq ); |
|
2592 assert( TK_NE==OP_Ne ); |
|
2593 testcase( op==TK_LT ); |
|
2594 testcase( op==TK_LE ); |
|
2595 testcase( op==TK_GT ); |
|
2596 testcase( op==TK_GE ); |
|
2597 testcase( op==TK_EQ ); |
|
2598 testcase( op==TK_NE ); |
|
2599 testcase( jumpIfNull==0 ); |
|
2600 codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, |
|
2601 pExpr->pRight, &r2, ®Free2); |
|
2602 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
|
2603 r1, r2, dest, jumpIfNull); |
|
2604 testcase( regFree1==0 ); |
|
2605 testcase( regFree2==0 ); |
|
2606 break; |
|
2607 } |
|
2608 case TK_ISNULL: |
|
2609 case TK_NOTNULL: { |
|
2610 assert( TK_ISNULL==OP_IsNull ); |
|
2611 assert( TK_NOTNULL==OP_NotNull ); |
|
2612 testcase( op==TK_ISNULL ); |
|
2613 testcase( op==TK_NOTNULL ); |
|
2614 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
|
2615 sqlite3VdbeAddOp2(v, op, r1, dest); |
|
2616 testcase( regFree1==0 ); |
|
2617 break; |
|
2618 } |
|
2619 case TK_BETWEEN: { |
|
2620 /* x BETWEEN y AND z |
|
2621 ** |
|
2622 ** Is equivalent to |
|
2623 ** |
|
2624 ** x>=y AND x<=z |
|
2625 ** |
|
2626 ** Code it as such, taking care to do the common subexpression |
|
2627 ** elementation of x. |
|
2628 */ |
|
2629 Expr exprAnd; |
|
2630 Expr compLeft; |
|
2631 Expr compRight; |
|
2632 Expr exprX; |
|
2633 |
|
2634 exprX = *pExpr->pLeft; |
|
2635 exprAnd.op = TK_AND; |
|
2636 exprAnd.pLeft = &compLeft; |
|
2637 exprAnd.pRight = &compRight; |
|
2638 compLeft.op = TK_GE; |
|
2639 compLeft.pLeft = &exprX; |
|
2640 compLeft.pRight = pExpr->pList->a[0].pExpr; |
|
2641 compRight.op = TK_LE; |
|
2642 compRight.pLeft = &exprX; |
|
2643 compRight.pRight = pExpr->pList->a[1].pExpr; |
|
2644 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); |
|
2645 testcase( regFree1==0 ); |
|
2646 exprX.op = TK_REGISTER; |
|
2647 testcase( jumpIfNull==0 ); |
|
2648 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); |
|
2649 break; |
|
2650 } |
|
2651 default: { |
|
2652 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
|
2653 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); |
|
2654 testcase( regFree1==0 ); |
|
2655 testcase( jumpIfNull==0 ); |
|
2656 break; |
|
2657 } |
|
2658 } |
|
2659 sqlite3ReleaseTempReg(pParse, regFree1); |
|
2660 sqlite3ReleaseTempReg(pParse, regFree2); |
|
2661 } |
|
2662 |
|
2663 /* |
|
2664 ** Generate code for a boolean expression such that a jump is made |
|
2665 ** to the label "dest" if the expression is false but execution |
|
2666 ** continues straight thru if the expression is true. |
|
2667 ** |
|
2668 ** If the expression evaluates to NULL (neither true nor false) then |
|
2669 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull |
|
2670 ** is 0. |
|
2671 */ |
|
2672 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
|
2673 Vdbe *v = pParse->pVdbe; |
|
2674 int op = 0; |
|
2675 int regFree1 = 0; |
|
2676 int regFree2 = 0; |
|
2677 int r1, r2; |
|
2678 |
|
2679 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
|
2680 if( v==0 || pExpr==0 ) return; |
|
2681 |
|
2682 /* The value of pExpr->op and op are related as follows: |
|
2683 ** |
|
2684 ** pExpr->op op |
|
2685 ** --------- ---------- |
|
2686 ** TK_ISNULL OP_NotNull |
|
2687 ** TK_NOTNULL OP_IsNull |
|
2688 ** TK_NE OP_Eq |
|
2689 ** TK_EQ OP_Ne |
|
2690 ** TK_GT OP_Le |
|
2691 ** TK_LE OP_Gt |
|
2692 ** TK_GE OP_Lt |
|
2693 ** TK_LT OP_Ge |
|
2694 ** |
|
2695 ** For other values of pExpr->op, op is undefined and unused. |
|
2696 ** The value of TK_ and OP_ constants are arranged such that we |
|
2697 ** can compute the mapping above using the following expression. |
|
2698 ** Assert()s verify that the computation is correct. |
|
2699 */ |
|
2700 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); |
|
2701 |
|
2702 /* Verify correct alignment of TK_ and OP_ constants |
|
2703 */ |
|
2704 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); |
|
2705 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); |
|
2706 assert( pExpr->op!=TK_NE || op==OP_Eq ); |
|
2707 assert( pExpr->op!=TK_EQ || op==OP_Ne ); |
|
2708 assert( pExpr->op!=TK_LT || op==OP_Ge ); |
|
2709 assert( pExpr->op!=TK_LE || op==OP_Gt ); |
|
2710 assert( pExpr->op!=TK_GT || op==OP_Le ); |
|
2711 assert( pExpr->op!=TK_GE || op==OP_Lt ); |
|
2712 |
|
2713 switch( pExpr->op ){ |
|
2714 case TK_AND: { |
|
2715 testcase( jumpIfNull==0 ); |
|
2716 testcase( pParse->disableColCache==0 ); |
|
2717 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
|
2718 pParse->disableColCache++; |
|
2719 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
|
2720 assert( pParse->disableColCache>0 ); |
|
2721 pParse->disableColCache--; |
|
2722 break; |
|
2723 } |
|
2724 case TK_OR: { |
|
2725 int d2 = sqlite3VdbeMakeLabel(v); |
|
2726 testcase( jumpIfNull==0 ); |
|
2727 testcase( pParse->disableColCache==0 ); |
|
2728 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); |
|
2729 pParse->disableColCache++; |
|
2730 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
|
2731 assert( pParse->disableColCache>0 ); |
|
2732 pParse->disableColCache--; |
|
2733 sqlite3VdbeResolveLabel(v, d2); |
|
2734 break; |
|
2735 } |
|
2736 case TK_NOT: { |
|
2737 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
|
2738 break; |
|
2739 } |
|
2740 case TK_LT: |
|
2741 case TK_LE: |
|
2742 case TK_GT: |
|
2743 case TK_GE: |
|
2744 case TK_NE: |
|
2745 case TK_EQ: { |
|
2746 testcase( op==TK_LT ); |
|
2747 testcase( op==TK_LE ); |
|
2748 testcase( op==TK_GT ); |
|
2749 testcase( op==TK_GE ); |
|
2750 testcase( op==TK_EQ ); |
|
2751 testcase( op==TK_NE ); |
|
2752 testcase( jumpIfNull==0 ); |
|
2753 codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, |
|
2754 pExpr->pRight, &r2, ®Free2); |
|
2755 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
|
2756 r1, r2, dest, jumpIfNull); |
|
2757 testcase( regFree1==0 ); |
|
2758 testcase( regFree2==0 ); |
|
2759 break; |
|
2760 } |
|
2761 case TK_ISNULL: |
|
2762 case TK_NOTNULL: { |
|
2763 testcase( op==TK_ISNULL ); |
|
2764 testcase( op==TK_NOTNULL ); |
|
2765 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
|
2766 sqlite3VdbeAddOp2(v, op, r1, dest); |
|
2767 testcase( regFree1==0 ); |
|
2768 break; |
|
2769 } |
|
2770 case TK_BETWEEN: { |
|
2771 /* x BETWEEN y AND z |
|
2772 ** |
|
2773 ** Is equivalent to |
|
2774 ** |
|
2775 ** x>=y AND x<=z |
|
2776 ** |
|
2777 ** Code it as such, taking care to do the common subexpression |
|
2778 ** elementation of x. |
|
2779 */ |
|
2780 Expr exprAnd; |
|
2781 Expr compLeft; |
|
2782 Expr compRight; |
|
2783 Expr exprX; |
|
2784 |
|
2785 exprX = *pExpr->pLeft; |
|
2786 exprAnd.op = TK_AND; |
|
2787 exprAnd.pLeft = &compLeft; |
|
2788 exprAnd.pRight = &compRight; |
|
2789 compLeft.op = TK_GE; |
|
2790 compLeft.pLeft = &exprX; |
|
2791 compLeft.pRight = pExpr->pList->a[0].pExpr; |
|
2792 compRight.op = TK_LE; |
|
2793 compRight.pLeft = &exprX; |
|
2794 compRight.pRight = pExpr->pList->a[1].pExpr; |
|
2795 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); |
|
2796 testcase( regFree1==0 ); |
|
2797 exprX.op = TK_REGISTER; |
|
2798 testcase( jumpIfNull==0 ); |
|
2799 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); |
|
2800 break; |
|
2801 } |
|
2802 default: { |
|
2803 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
|
2804 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); |
|
2805 testcase( regFree1==0 ); |
|
2806 testcase( jumpIfNull==0 ); |
|
2807 break; |
|
2808 } |
|
2809 } |
|
2810 sqlite3ReleaseTempReg(pParse, regFree1); |
|
2811 sqlite3ReleaseTempReg(pParse, regFree2); |
|
2812 } |
|
2813 |
|
2814 /* |
|
2815 ** Do a deep comparison of two expression trees. Return TRUE (non-zero) |
|
2816 ** if they are identical and return FALSE if they differ in any way. |
|
2817 ** |
|
2818 ** Sometimes this routine will return FALSE even if the two expressions |
|
2819 ** really are equivalent. If we cannot prove that the expressions are |
|
2820 ** identical, we return FALSE just to be safe. So if this routine |
|
2821 ** returns false, then you do not really know for certain if the two |
|
2822 ** expressions are the same. But if you get a TRUE return, then you |
|
2823 ** can be sure the expressions are the same. In the places where |
|
2824 ** this routine is used, it does not hurt to get an extra FALSE - that |
|
2825 ** just might result in some slightly slower code. But returning |
|
2826 ** an incorrect TRUE could lead to a malfunction. |
|
2827 */ |
|
2828 int sqlite3ExprCompare(Expr *pA, Expr *pB){ |
|
2829 int i; |
|
2830 if( pA==0||pB==0 ){ |
|
2831 return pB==pA; |
|
2832 } |
|
2833 if( pA->op!=pB->op ) return 0; |
|
2834 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; |
|
2835 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; |
|
2836 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; |
|
2837 if( pA->pList ){ |
|
2838 if( pB->pList==0 ) return 0; |
|
2839 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; |
|
2840 for(i=0; i<pA->pList->nExpr; i++){ |
|
2841 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ |
|
2842 return 0; |
|
2843 } |
|
2844 } |
|
2845 }else if( pB->pList ){ |
|
2846 return 0; |
|
2847 } |
|
2848 if( pA->pSelect || pB->pSelect ) return 0; |
|
2849 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; |
|
2850 if( pA->op!=TK_COLUMN && pA->token.z ){ |
|
2851 if( pB->token.z==0 ) return 0; |
|
2852 if( pB->token.n!=pA->token.n ) return 0; |
|
2853 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){ |
|
2854 return 0; |
|
2855 } |
|
2856 } |
|
2857 return 1; |
|
2858 } |
|
2859 |
|
2860 |
|
2861 /* |
|
2862 ** Add a new element to the pAggInfo->aCol[] array. Return the index of |
|
2863 ** the new element. Return a negative number if malloc fails. |
|
2864 */ |
|
2865 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ |
|
2866 int i; |
|
2867 pInfo->aCol = sqlite3ArrayAllocate( |
|
2868 db, |
|
2869 pInfo->aCol, |
|
2870 sizeof(pInfo->aCol[0]), |
|
2871 3, |
|
2872 &pInfo->nColumn, |
|
2873 &pInfo->nColumnAlloc, |
|
2874 &i |
|
2875 ); |
|
2876 return i; |
|
2877 } |
|
2878 |
|
2879 /* |
|
2880 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of |
|
2881 ** the new element. Return a negative number if malloc fails. |
|
2882 */ |
|
2883 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ |
|
2884 int i; |
|
2885 pInfo->aFunc = sqlite3ArrayAllocate( |
|
2886 db, |
|
2887 pInfo->aFunc, |
|
2888 sizeof(pInfo->aFunc[0]), |
|
2889 3, |
|
2890 &pInfo->nFunc, |
|
2891 &pInfo->nFuncAlloc, |
|
2892 &i |
|
2893 ); |
|
2894 return i; |
|
2895 } |
|
2896 |
|
2897 /* |
|
2898 ** This is the xExprCallback for a tree walker. It is used to |
|
2899 ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates |
|
2900 ** for additional information. |
|
2901 */ |
|
2902 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ |
|
2903 int i; |
|
2904 NameContext *pNC = pWalker->u.pNC; |
|
2905 Parse *pParse = pNC->pParse; |
|
2906 SrcList *pSrcList = pNC->pSrcList; |
|
2907 AggInfo *pAggInfo = pNC->pAggInfo; |
|
2908 |
|
2909 switch( pExpr->op ){ |
|
2910 case TK_AGG_COLUMN: |
|
2911 case TK_COLUMN: { |
|
2912 testcase( pExpr->op==TK_AGG_COLUMN ); |
|
2913 testcase( pExpr->op==TK_COLUMN ); |
|
2914 /* Check to see if the column is in one of the tables in the FROM |
|
2915 ** clause of the aggregate query */ |
|
2916 if( pSrcList ){ |
|
2917 struct SrcList_item *pItem = pSrcList->a; |
|
2918 for(i=0; i<pSrcList->nSrc; i++, pItem++){ |
|
2919 struct AggInfo_col *pCol; |
|
2920 if( pExpr->iTable==pItem->iCursor ){ |
|
2921 /* If we reach this point, it means that pExpr refers to a table |
|
2922 ** that is in the FROM clause of the aggregate query. |
|
2923 ** |
|
2924 ** Make an entry for the column in pAggInfo->aCol[] if there |
|
2925 ** is not an entry there already. |
|
2926 */ |
|
2927 int k; |
|
2928 pCol = pAggInfo->aCol; |
|
2929 for(k=0; k<pAggInfo->nColumn; k++, pCol++){ |
|
2930 if( pCol->iTable==pExpr->iTable && |
|
2931 pCol->iColumn==pExpr->iColumn ){ |
|
2932 break; |
|
2933 } |
|
2934 } |
|
2935 if( (k>=pAggInfo->nColumn) |
|
2936 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 |
|
2937 ){ |
|
2938 pCol = &pAggInfo->aCol[k]; |
|
2939 pCol->pTab = pExpr->pTab; |
|
2940 pCol->iTable = pExpr->iTable; |
|
2941 pCol->iColumn = pExpr->iColumn; |
|
2942 pCol->iMem = ++pParse->nMem; |
|
2943 pCol->iSorterColumn = -1; |
|
2944 pCol->pExpr = pExpr; |
|
2945 if( pAggInfo->pGroupBy ){ |
|
2946 int j, n; |
|
2947 ExprList *pGB = pAggInfo->pGroupBy; |
|
2948 struct ExprList_item *pTerm = pGB->a; |
|
2949 n = pGB->nExpr; |
|
2950 for(j=0; j<n; j++, pTerm++){ |
|
2951 Expr *pE = pTerm->pExpr; |
|
2952 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && |
|
2953 pE->iColumn==pExpr->iColumn ){ |
|
2954 pCol->iSorterColumn = j; |
|
2955 break; |
|
2956 } |
|
2957 } |
|
2958 } |
|
2959 if( pCol->iSorterColumn<0 ){ |
|
2960 pCol->iSorterColumn = pAggInfo->nSortingColumn++; |
|
2961 } |
|
2962 } |
|
2963 /* There is now an entry for pExpr in pAggInfo->aCol[] (either |
|
2964 ** because it was there before or because we just created it). |
|
2965 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that |
|
2966 ** pAggInfo->aCol[] entry. |
|
2967 */ |
|
2968 pExpr->pAggInfo = pAggInfo; |
|
2969 pExpr->op = TK_AGG_COLUMN; |
|
2970 pExpr->iAgg = k; |
|
2971 break; |
|
2972 } /* endif pExpr->iTable==pItem->iCursor */ |
|
2973 } /* end loop over pSrcList */ |
|
2974 } |
|
2975 return WRC_Prune; |
|
2976 } |
|
2977 case TK_AGG_FUNCTION: { |
|
2978 /* The pNC->nDepth==0 test causes aggregate functions in subqueries |
|
2979 ** to be ignored */ |
|
2980 if( pNC->nDepth==0 ){ |
|
2981 /* Check to see if pExpr is a duplicate of another aggregate |
|
2982 ** function that is already in the pAggInfo structure |
|
2983 */ |
|
2984 struct AggInfo_func *pItem = pAggInfo->aFunc; |
|
2985 for(i=0; i<pAggInfo->nFunc; i++, pItem++){ |
|
2986 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ |
|
2987 break; |
|
2988 } |
|
2989 } |
|
2990 if( i>=pAggInfo->nFunc ){ |
|
2991 /* pExpr is original. Make a new entry in pAggInfo->aFunc[] |
|
2992 */ |
|
2993 u8 enc = ENC(pParse->db); |
|
2994 i = addAggInfoFunc(pParse->db, pAggInfo); |
|
2995 if( i>=0 ){ |
|
2996 pItem = &pAggInfo->aFunc[i]; |
|
2997 pItem->pExpr = pExpr; |
|
2998 pItem->iMem = ++pParse->nMem; |
|
2999 pItem->pFunc = sqlite3FindFunction(pParse->db, |
|
3000 (char*)pExpr->token.z, pExpr->token.n, |
|
3001 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); |
|
3002 if( pExpr->flags & EP_Distinct ){ |
|
3003 pItem->iDistinct = pParse->nTab++; |
|
3004 }else{ |
|
3005 pItem->iDistinct = -1; |
|
3006 } |
|
3007 } |
|
3008 } |
|
3009 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry |
|
3010 */ |
|
3011 pExpr->iAgg = i; |
|
3012 pExpr->pAggInfo = pAggInfo; |
|
3013 return WRC_Prune; |
|
3014 } |
|
3015 } |
|
3016 } |
|
3017 return WRC_Continue; |
|
3018 } |
|
3019 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ |
|
3020 NameContext *pNC = pWalker->u.pNC; |
|
3021 if( pNC->nDepth==0 ){ |
|
3022 pNC->nDepth++; |
|
3023 sqlite3WalkSelect(pWalker, pSelect); |
|
3024 pNC->nDepth--; |
|
3025 return WRC_Prune; |
|
3026 }else{ |
|
3027 return WRC_Continue; |
|
3028 } |
|
3029 } |
|
3030 |
|
3031 /* |
|
3032 ** Analyze the given expression looking for aggregate functions and |
|
3033 ** for variables that need to be added to the pParse->aAgg[] array. |
|
3034 ** Make additional entries to the pParse->aAgg[] array as necessary. |
|
3035 ** |
|
3036 ** This routine should only be called after the expression has been |
|
3037 ** analyzed by sqlite3ResolveExprNames(). |
|
3038 */ |
|
3039 void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ |
|
3040 Walker w; |
|
3041 w.xExprCallback = analyzeAggregate; |
|
3042 w.xSelectCallback = analyzeAggregatesInSelect; |
|
3043 w.u.pNC = pNC; |
|
3044 sqlite3WalkExpr(&w, pExpr); |
|
3045 } |
|
3046 |
|
3047 /* |
|
3048 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an |
|
3049 ** expression list. Return the number of errors. |
|
3050 ** |
|
3051 ** If an error is found, the analysis is cut short. |
|
3052 */ |
|
3053 void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ |
|
3054 struct ExprList_item *pItem; |
|
3055 int i; |
|
3056 if( pList ){ |
|
3057 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
|
3058 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); |
|
3059 } |
|
3060 } |
|
3061 } |
|
3062 |
|
3063 /* |
|
3064 ** Allocate or deallocate temporary use registers during code generation. |
|
3065 */ |
|
3066 int sqlite3GetTempReg(Parse *pParse){ |
|
3067 if( pParse->nTempReg==0 ){ |
|
3068 return ++pParse->nMem; |
|
3069 } |
|
3070 return pParse->aTempReg[--pParse->nTempReg]; |
|
3071 } |
|
3072 void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ |
|
3073 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ |
|
3074 sqlite3ExprWritableRegister(pParse, iReg, iReg); |
|
3075 pParse->aTempReg[pParse->nTempReg++] = iReg; |
|
3076 } |
|
3077 } |
|
3078 |
|
3079 /* |
|
3080 ** Allocate or deallocate a block of nReg consecutive registers |
|
3081 */ |
|
3082 int sqlite3GetTempRange(Parse *pParse, int nReg){ |
|
3083 int i, n; |
|
3084 i = pParse->iRangeReg; |
|
3085 n = pParse->nRangeReg; |
|
3086 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){ |
|
3087 pParse->iRangeReg += nReg; |
|
3088 pParse->nRangeReg -= nReg; |
|
3089 }else{ |
|
3090 i = pParse->nMem+1; |
|
3091 pParse->nMem += nReg; |
|
3092 } |
|
3093 return i; |
|
3094 } |
|
3095 void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ |
|
3096 if( nReg>pParse->nRangeReg ){ |
|
3097 pParse->nRangeReg = nReg; |
|
3098 pParse->iRangeReg = iReg; |
|
3099 } |
|
3100 } |