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