line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
** 2001 September 15 |
3
|
|
|
|
|
|
|
** |
4
|
|
|
|
|
|
|
** The author disclaims copyright to this source code. In place of |
5
|
|
|
|
|
|
|
** a legal notice, here is a blessing: |
6
|
|
|
|
|
|
|
** |
7
|
|
|
|
|
|
|
** May you do good and not evil. |
8
|
|
|
|
|
|
|
** May you find forgiveness for yourself and forgive others. |
9
|
|
|
|
|
|
|
** May you share freely, never taking more than you give. |
10
|
|
|
|
|
|
|
** |
11
|
|
|
|
|
|
|
************************************************************************* |
12
|
|
|
|
|
|
|
** This file contains C code routines that are called by the parser |
13
|
|
|
|
|
|
|
** to handle SELECT statements in SQLite. |
14
|
|
|
|
|
|
|
** |
15
|
|
|
|
|
|
|
** $Id: select.c,v 1.1.1.1 2004/08/08 15:03:58 matt Exp $ |
16
|
|
|
|
|
|
|
*/ |
17
|
|
|
|
|
|
|
#include "sqliteInt.h" |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
/* |
21
|
|
|
|
|
|
|
** Allocate a new Select structure and return a pointer to that |
22
|
|
|
|
|
|
|
** structure. |
23
|
|
|
|
|
|
|
*/ |
24
|
138
|
|
|
|
|
|
Select *sqliteSelectNew( |
25
|
|
|
|
|
|
|
ExprList *pEList, /* which columns to include in the result */ |
26
|
|
|
|
|
|
|
SrcList *pSrc, /* the FROM clause -- which tables to scan */ |
27
|
|
|
|
|
|
|
Expr *pWhere, /* the WHERE clause */ |
28
|
|
|
|
|
|
|
ExprList *pGroupBy, /* the GROUP BY clause */ |
29
|
|
|
|
|
|
|
Expr *pHaving, /* the HAVING clause */ |
30
|
|
|
|
|
|
|
ExprList *pOrderBy, /* the ORDER BY clause */ |
31
|
|
|
|
|
|
|
int isDistinct, /* true if the DISTINCT keyword is present */ |
32
|
|
|
|
|
|
|
int nLimit, /* LIMIT value. -1 means not used */ |
33
|
|
|
|
|
|
|
int nOffset /* OFFSET value. 0 means no offset */ |
34
|
|
|
|
|
|
|
){ |
35
|
|
|
|
|
|
|
Select *pNew; |
36
|
138
|
|
|
|
|
|
pNew = sqliteMalloc( sizeof(*pNew) ); |
37
|
138
|
50
|
|
|
|
|
if( pNew==0 ){ |
38
|
0
|
|
|
|
|
|
sqliteExprListDelete(pEList); |
39
|
0
|
|
|
|
|
|
sqliteSrcListDelete(pSrc); |
40
|
0
|
|
|
|
|
|
sqliteExprDelete(pWhere); |
41
|
0
|
|
|
|
|
|
sqliteExprListDelete(pGroupBy); |
42
|
0
|
|
|
|
|
|
sqliteExprDelete(pHaving); |
43
|
0
|
|
|
|
|
|
sqliteExprListDelete(pOrderBy); |
44
|
|
|
|
|
|
|
}else{ |
45
|
138
|
50
|
|
|
|
|
if( pEList==0 ){ |
46
|
0
|
|
|
|
|
|
pEList = sqliteExprListAppend(0, sqliteExpr(TK_ALL,0,0,0), 0); |
47
|
|
|
|
|
|
|
} |
48
|
138
|
|
|
|
|
|
pNew->pEList = pEList; |
49
|
138
|
|
|
|
|
|
pNew->pSrc = pSrc; |
50
|
138
|
|
|
|
|
|
pNew->pWhere = pWhere; |
51
|
138
|
|
|
|
|
|
pNew->pGroupBy = pGroupBy; |
52
|
138
|
|
|
|
|
|
pNew->pHaving = pHaving; |
53
|
138
|
|
|
|
|
|
pNew->pOrderBy = pOrderBy; |
54
|
138
|
|
|
|
|
|
pNew->isDistinct = isDistinct; |
55
|
138
|
|
|
|
|
|
pNew->op = TK_SELECT; |
56
|
138
|
|
|
|
|
|
pNew->nLimit = nLimit; |
57
|
138
|
|
|
|
|
|
pNew->nOffset = nOffset; |
58
|
138
|
|
|
|
|
|
pNew->iLimit = -1; |
59
|
138
|
|
|
|
|
|
pNew->iOffset = -1; |
60
|
|
|
|
|
|
|
} |
61
|
138
|
|
|
|
|
|
return pNew; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
/* |
65
|
|
|
|
|
|
|
** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the |
66
|
|
|
|
|
|
|
** type of join. Return an integer constant that expresses that type |
67
|
|
|
|
|
|
|
** in terms of the following bit values: |
68
|
|
|
|
|
|
|
** |
69
|
|
|
|
|
|
|
** JT_INNER |
70
|
|
|
|
|
|
|
** JT_OUTER |
71
|
|
|
|
|
|
|
** JT_NATURAL |
72
|
|
|
|
|
|
|
** JT_LEFT |
73
|
|
|
|
|
|
|
** JT_RIGHT |
74
|
|
|
|
|
|
|
** |
75
|
|
|
|
|
|
|
** A full outer join is the combination of JT_LEFT and JT_RIGHT. |
76
|
|
|
|
|
|
|
** |
77
|
|
|
|
|
|
|
** If an illegal or unsupported join type is seen, then still return |
78
|
|
|
|
|
|
|
** a join type, but put an error in the pParse structure. |
79
|
|
|
|
|
|
|
*/ |
80
|
0
|
|
|
|
|
|
int sqliteJoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ |
81
|
0
|
|
|
|
|
|
int jointype = 0; |
82
|
|
|
|
|
|
|
Token *apAll[3]; |
83
|
|
|
|
|
|
|
Token *p; |
84
|
|
|
|
|
|
|
static struct { |
85
|
|
|
|
|
|
|
const char *zKeyword; |
86
|
|
|
|
|
|
|
int nChar; |
87
|
|
|
|
|
|
|
int code; |
88
|
|
|
|
|
|
|
} keywords[] = { |
89
|
|
|
|
|
|
|
{ "natural", 7, JT_NATURAL }, |
90
|
|
|
|
|
|
|
{ "left", 4, JT_LEFT|JT_OUTER }, |
91
|
|
|
|
|
|
|
{ "right", 5, JT_RIGHT|JT_OUTER }, |
92
|
|
|
|
|
|
|
{ "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER }, |
93
|
|
|
|
|
|
|
{ "outer", 5, JT_OUTER }, |
94
|
|
|
|
|
|
|
{ "inner", 5, JT_INNER }, |
95
|
|
|
|
|
|
|
{ "cross", 5, JT_INNER }, |
96
|
|
|
|
|
|
|
}; |
97
|
|
|
|
|
|
|
int i, j; |
98
|
0
|
|
|
|
|
|
apAll[0] = pA; |
99
|
0
|
|
|
|
|
|
apAll[1] = pB; |
100
|
0
|
|
|
|
|
|
apAll[2] = pC; |
101
|
0
|
0
|
|
|
|
|
for(i=0; i<3 && apAll[i]; i++){ |
|
|
0
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
p = apAll[i]; |
103
|
0
|
0
|
|
|
|
|
for(j=0; j
|
104
|
0
|
0
|
|
|
|
|
if( p->n==keywords[j].nChar |
105
|
0
|
0
|
|
|
|
|
&& sqliteStrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){ |
106
|
0
|
|
|
|
|
|
jointype |= keywords[j].code; |
107
|
0
|
|
|
|
|
|
break; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
0
|
0
|
|
|
|
|
if( j>=sizeof(keywords)/sizeof(keywords[0]) ){ |
111
|
0
|
|
|
|
|
|
jointype |= JT_ERROR; |
112
|
0
|
|
|
|
|
|
break; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
0
|
0
|
|
|
|
|
if( |
116
|
0
|
0
|
|
|
|
|
(jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || |
117
|
0
|
|
|
|
|
|
(jointype & JT_ERROR)!=0 |
118
|
0
|
|
|
|
|
|
){ |
119
|
|
|
|
|
|
|
static Token dummy = { 0, 0 }; |
120
|
0
|
|
|
|
|
|
char *zSp1 = " ", *zSp2 = " "; |
121
|
0
|
0
|
|
|
|
|
if( pB==0 ){ pB = &dummy; zSp1 = 0; } |
122
|
0
|
0
|
|
|
|
|
if( pC==0 ){ pC = &dummy; zSp2 = 0; } |
123
|
0
|
|
|
|
|
|
sqliteSetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0, |
124
|
0
|
|
|
|
|
|
pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0); |
125
|
0
|
|
|
|
|
|
pParse->nErr++; |
126
|
0
|
|
|
|
|
|
jointype = JT_INNER; |
127
|
0
|
0
|
|
|
|
|
}else if( jointype & JT_RIGHT ){ |
128
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, |
129
|
|
|
|
|
|
|
"RIGHT and FULL OUTER JOINs are not currently supported"); |
130
|
0
|
|
|
|
|
|
jointype = JT_INNER; |
131
|
|
|
|
|
|
|
} |
132
|
0
|
|
|
|
|
|
return jointype; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
/* |
136
|
|
|
|
|
|
|
** Return the index of a column in a table. Return -1 if the column |
137
|
|
|
|
|
|
|
** is not contained in the table. |
138
|
|
|
|
|
|
|
*/ |
139
|
0
|
|
|
|
|
|
static int columnIndex(Table *pTab, const char *zCol){ |
140
|
|
|
|
|
|
|
int i; |
141
|
0
|
0
|
|
|
|
|
for(i=0; inCol; i++){ |
142
|
0
|
0
|
|
|
|
|
if( sqliteStrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; |
143
|
|
|
|
|
|
|
} |
144
|
0
|
|
|
|
|
|
return -1; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
/* |
148
|
|
|
|
|
|
|
** Add a term to the WHERE expression in *ppExpr that requires the |
149
|
|
|
|
|
|
|
** zCol column to be equal in the two tables pTab1 and pTab2. |
150
|
|
|
|
|
|
|
*/ |
151
|
0
|
|
|
|
|
|
static void addWhereTerm( |
152
|
|
|
|
|
|
|
const char *zCol, /* Name of the column */ |
153
|
|
|
|
|
|
|
const Table *pTab1, /* First table */ |
154
|
|
|
|
|
|
|
const Table *pTab2, /* Second table */ |
155
|
|
|
|
|
|
|
Expr **ppExpr /* Add the equality term to this expression */ |
156
|
|
|
|
|
|
|
){ |
157
|
|
|
|
|
|
|
Token dummy; |
158
|
|
|
|
|
|
|
Expr *pE1a, *pE1b, *pE1c; |
159
|
|
|
|
|
|
|
Expr *pE2a, *pE2b, *pE2c; |
160
|
|
|
|
|
|
|
Expr *pE; |
161
|
|
|
|
|
|
|
|
162
|
0
|
|
|
|
|
|
dummy.z = zCol; |
163
|
0
|
|
|
|
|
|
dummy.n = strlen(zCol); |
164
|
0
|
|
|
|
|
|
dummy.dyn = 0; |
165
|
0
|
|
|
|
|
|
pE1a = sqliteExpr(TK_ID, 0, 0, &dummy); |
166
|
0
|
|
|
|
|
|
pE2a = sqliteExpr(TK_ID, 0, 0, &dummy); |
167
|
0
|
|
|
|
|
|
dummy.z = pTab1->zName; |
168
|
0
|
|
|
|
|
|
dummy.n = strlen(dummy.z); |
169
|
0
|
|
|
|
|
|
pE1b = sqliteExpr(TK_ID, 0, 0, &dummy); |
170
|
0
|
|
|
|
|
|
dummy.z = pTab2->zName; |
171
|
0
|
|
|
|
|
|
dummy.n = strlen(dummy.z); |
172
|
0
|
|
|
|
|
|
pE2b = sqliteExpr(TK_ID, 0, 0, &dummy); |
173
|
0
|
|
|
|
|
|
pE1c = sqliteExpr(TK_DOT, pE1b, pE1a, 0); |
174
|
0
|
|
|
|
|
|
pE2c = sqliteExpr(TK_DOT, pE2b, pE2a, 0); |
175
|
0
|
|
|
|
|
|
pE = sqliteExpr(TK_EQ, pE1c, pE2c, 0); |
176
|
0
|
|
|
|
|
|
ExprSetProperty(pE, EP_FromJoin); |
177
|
0
|
0
|
|
|
|
|
if( *ppExpr ){ |
178
|
0
|
|
|
|
|
|
*ppExpr = sqliteExpr(TK_AND, *ppExpr, pE, 0); |
179
|
|
|
|
|
|
|
}else{ |
180
|
0
|
|
|
|
|
|
*ppExpr = pE; |
181
|
|
|
|
|
|
|
} |
182
|
0
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
/* |
185
|
|
|
|
|
|
|
** Set the EP_FromJoin property on all terms of the given expression. |
186
|
|
|
|
|
|
|
** |
187
|
|
|
|
|
|
|
** The EP_FromJoin property is used on terms of an expression to tell |
188
|
|
|
|
|
|
|
** the LEFT OUTER JOIN processing logic that this term is part of the |
189
|
|
|
|
|
|
|
** join restriction specified in the ON or USING clause and not a part |
190
|
|
|
|
|
|
|
** of the more general WHERE clause. These terms are moved over to the |
191
|
|
|
|
|
|
|
** WHERE clause during join processing but we need to remember that they |
192
|
|
|
|
|
|
|
** originated in the ON or USING clause. |
193
|
|
|
|
|
|
|
*/ |
194
|
0
|
|
|
|
|
|
static void setJoinExpr(Expr *p){ |
195
|
0
|
0
|
|
|
|
|
while( p ){ |
196
|
0
|
|
|
|
|
|
ExprSetProperty(p, EP_FromJoin); |
197
|
0
|
|
|
|
|
|
setJoinExpr(p->pLeft); |
198
|
0
|
|
|
|
|
|
p = p->pRight; |
199
|
|
|
|
|
|
|
} |
200
|
0
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
/* |
203
|
|
|
|
|
|
|
** This routine processes the join information for a SELECT statement. |
204
|
|
|
|
|
|
|
** ON and USING clauses are converted into extra terms of the WHERE clause. |
205
|
|
|
|
|
|
|
** NATURAL joins also create extra WHERE clause terms. |
206
|
|
|
|
|
|
|
** |
207
|
|
|
|
|
|
|
** This routine returns the number of errors encountered. |
208
|
|
|
|
|
|
|
*/ |
209
|
141
|
|
|
|
|
|
static int sqliteProcessJoin(Parse *pParse, Select *p){ |
210
|
|
|
|
|
|
|
SrcList *pSrc; |
211
|
|
|
|
|
|
|
int i, j; |
212
|
141
|
|
|
|
|
|
pSrc = p->pSrc; |
213
|
142
|
100
|
|
|
|
|
for(i=0; inSrc-1; i++){ |
214
|
1
|
|
|
|
|
|
struct SrcList_item *pTerm = &pSrc->a[i]; |
215
|
1
|
|
|
|
|
|
struct SrcList_item *pOther = &pSrc->a[i+1]; |
216
|
|
|
|
|
|
|
|
217
|
1
|
50
|
|
|
|
|
if( pTerm->pTab==0 || pOther->pTab==0 ) continue; |
|
|
50
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
/* When the NATURAL keyword is present, add WHERE clause terms for |
220
|
|
|
|
|
|
|
** every column that the two tables have in common. |
221
|
|
|
|
|
|
|
*/ |
222
|
1
|
50
|
|
|
|
|
if( pTerm->jointype & JT_NATURAL ){ |
223
|
|
|
|
|
|
|
Table *pTab; |
224
|
0
|
0
|
|
|
|
|
if( pTerm->pOn || pTerm->pUsing ){ |
|
|
0
|
|
|
|
|
|
225
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, "a NATURAL join may not have " |
226
|
|
|
|
|
|
|
"an ON or USING clause", 0); |
227
|
0
|
|
|
|
|
|
return 1; |
228
|
|
|
|
|
|
|
} |
229
|
0
|
|
|
|
|
|
pTab = pTerm->pTab; |
230
|
0
|
0
|
|
|
|
|
for(j=0; jnCol; j++){ |
231
|
0
|
0
|
|
|
|
|
if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){ |
232
|
0
|
|
|
|
|
|
addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere); |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
/* Disallow both ON and USING clauses in the same join |
238
|
|
|
|
|
|
|
*/ |
239
|
1
|
50
|
|
|
|
|
if( pTerm->pOn && pTerm->pUsing ){ |
|
|
0
|
|
|
|
|
|
240
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, "cannot have both ON and USING " |
241
|
|
|
|
|
|
|
"clauses in the same join"); |
242
|
0
|
|
|
|
|
|
return 1; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
/* Add the ON clause to the end of the WHERE clause, connected by |
246
|
|
|
|
|
|
|
** and AND operator. |
247
|
|
|
|
|
|
|
*/ |
248
|
1
|
50
|
|
|
|
|
if( pTerm->pOn ){ |
249
|
0
|
|
|
|
|
|
setJoinExpr(pTerm->pOn); |
250
|
0
|
0
|
|
|
|
|
if( p->pWhere==0 ){ |
251
|
0
|
|
|
|
|
|
p->pWhere = pTerm->pOn; |
252
|
|
|
|
|
|
|
}else{ |
253
|
0
|
|
|
|
|
|
p->pWhere = sqliteExpr(TK_AND, p->pWhere, pTerm->pOn, 0); |
254
|
|
|
|
|
|
|
} |
255
|
0
|
|
|
|
|
|
pTerm->pOn = 0; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
/* Create extra terms on the WHERE clause for each column named |
259
|
|
|
|
|
|
|
** in the USING clause. Example: If the two tables to be joined are |
260
|
|
|
|
|
|
|
** A and B and the USING clause names X, Y, and Z, then add this |
261
|
|
|
|
|
|
|
** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z |
262
|
|
|
|
|
|
|
** Report an error if any column mentioned in the USING clause is |
263
|
|
|
|
|
|
|
** not contained in both tables to be joined. |
264
|
|
|
|
|
|
|
*/ |
265
|
1
|
50
|
|
|
|
|
if( pTerm->pUsing ){ |
266
|
|
|
|
|
|
|
IdList *pList; |
267
|
|
|
|
|
|
|
int j; |
268
|
|
|
|
|
|
|
assert( inSrc-1 ); |
269
|
0
|
|
|
|
|
|
pList = pTerm->pUsing; |
270
|
0
|
0
|
|
|
|
|
for(j=0; jnId; j++){ |
271
|
0
|
|
|
|
|
|
if( columnIndex(pTerm->pTab, pList->a[j].zName)<0 || |
272
|
0
|
|
|
|
|
|
columnIndex(pOther->pTab, pList->a[j].zName)<0 ){ |
273
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, "cannot join using column %s - column " |
274
|
0
|
|
|
|
|
|
"not present in both tables", pList->a[j].zName); |
275
|
0
|
|
|
|
|
|
return 1; |
276
|
|
|
|
|
|
|
} |
277
|
0
|
|
|
|
|
|
addWhereTerm(pList->a[j].zName, pTerm->pTab, pOther->pTab, &p->pWhere); |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
} |
281
|
141
|
|
|
|
|
|
return 0; |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
/* |
285
|
|
|
|
|
|
|
** Delete the given Select structure and all of its substructures. |
286
|
|
|
|
|
|
|
*/ |
287
|
1272
|
|
|
|
|
|
void sqliteSelectDelete(Select *p){ |
288
|
1272
|
100
|
|
|
|
|
if( p==0 ) return; |
289
|
138
|
|
|
|
|
|
sqliteExprListDelete(p->pEList); |
290
|
138
|
|
|
|
|
|
sqliteSrcListDelete(p->pSrc); |
291
|
138
|
|
|
|
|
|
sqliteExprDelete(p->pWhere); |
292
|
138
|
|
|
|
|
|
sqliteExprListDelete(p->pGroupBy); |
293
|
138
|
|
|
|
|
|
sqliteExprDelete(p->pHaving); |
294
|
138
|
|
|
|
|
|
sqliteExprListDelete(p->pOrderBy); |
295
|
138
|
|
|
|
|
|
sqliteSelectDelete(p->pPrior); |
296
|
138
|
|
|
|
|
|
sqliteFree(p->zSelect); |
297
|
138
|
|
|
|
|
|
sqliteFree(p); |
298
|
|
|
|
|
|
|
} |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
/* |
301
|
|
|
|
|
|
|
** Delete the aggregate information from the parse structure. |
302
|
|
|
|
|
|
|
*/ |
303
|
270
|
|
|
|
|
|
static void sqliteAggregateInfoReset(Parse *pParse){ |
304
|
270
|
|
|
|
|
|
sqliteFree(pParse->aAgg); |
305
|
270
|
|
|
|
|
|
pParse->aAgg = 0; |
306
|
270
|
|
|
|
|
|
pParse->nAgg = 0; |
307
|
270
|
|
|
|
|
|
pParse->useAgg = 0; |
308
|
270
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
/* |
311
|
|
|
|
|
|
|
** Insert code into "v" that will push the record on the top of the |
312
|
|
|
|
|
|
|
** stack into the sorter. |
313
|
|
|
|
|
|
|
*/ |
314
|
5
|
|
|
|
|
|
static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){ |
315
|
|
|
|
|
|
|
char *zSortOrder; |
316
|
|
|
|
|
|
|
int i; |
317
|
5
|
|
|
|
|
|
zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 ); |
318
|
5
|
50
|
|
|
|
|
if( zSortOrder==0 ) return; |
319
|
16
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
320
|
11
|
|
|
|
|
|
int order = pOrderBy->a[i].sortOrder; |
321
|
|
|
|
|
|
|
int type; |
322
|
|
|
|
|
|
|
int c; |
323
|
11
|
50
|
|
|
|
|
if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){ |
324
|
0
|
|
|
|
|
|
type = SQLITE_SO_TEXT; |
325
|
11
|
50
|
|
|
|
|
}else if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_NUM ){ |
326
|
0
|
|
|
|
|
|
type = SQLITE_SO_NUM; |
327
|
11
|
50
|
|
|
|
|
}else if( pParse->db->file_format>=4 ){ |
328
|
11
|
|
|
|
|
|
type = sqliteExprType(pOrderBy->a[i].pExpr); |
329
|
|
|
|
|
|
|
}else{ |
330
|
0
|
|
|
|
|
|
type = SQLITE_SO_NUM; |
331
|
|
|
|
|
|
|
} |
332
|
11
|
100
|
|
|
|
|
if( (order & SQLITE_SO_DIRMASK)==SQLITE_SO_ASC ){ |
333
|
10
|
100
|
|
|
|
|
c = type==SQLITE_SO_TEXT ? 'A' : '+'; |
334
|
|
|
|
|
|
|
}else{ |
335
|
1
|
50
|
|
|
|
|
c = type==SQLITE_SO_TEXT ? 'D' : '-'; |
336
|
|
|
|
|
|
|
} |
337
|
11
|
|
|
|
|
|
zSortOrder[i] = c; |
338
|
11
|
|
|
|
|
|
sqliteExprCode(pParse, pOrderBy->a[i].pExpr); |
339
|
|
|
|
|
|
|
} |
340
|
5
|
|
|
|
|
|
zSortOrder[pOrderBy->nExpr] = 0; |
341
|
5
|
|
|
|
|
|
sqliteVdbeOp3(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, P3_DYNAMIC); |
342
|
5
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_SortPut, 0, 0); |
343
|
|
|
|
|
|
|
} |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
/* |
346
|
|
|
|
|
|
|
** This routine adds a P3 argument to the last VDBE opcode that was |
347
|
|
|
|
|
|
|
** inserted. The P3 argument added is a string suitable for the |
348
|
|
|
|
|
|
|
** OP_MakeKey or OP_MakeIdxKey opcodes. The string consists of |
349
|
|
|
|
|
|
|
** characters 't' or 'n' depending on whether or not the various |
350
|
|
|
|
|
|
|
** fields of the key to be generated should be treated as numeric |
351
|
|
|
|
|
|
|
** or as text. See the OP_MakeKey and OP_MakeIdxKey opcode |
352
|
|
|
|
|
|
|
** documentation for additional information about the P3 string. |
353
|
|
|
|
|
|
|
** See also the sqliteAddIdxKeyType() routine. |
354
|
|
|
|
|
|
|
*/ |
355
|
2
|
|
|
|
|
|
void sqliteAddKeyType(Vdbe *v, ExprList *pEList){ |
356
|
2
|
|
|
|
|
|
int nColumn = pEList->nExpr; |
357
|
2
|
|
|
|
|
|
char *zType = sqliteMalloc( nColumn+1 ); |
358
|
|
|
|
|
|
|
int i; |
359
|
2
|
50
|
|
|
|
|
if( zType==0 ) return; |
360
|
4
|
100
|
|
|
|
|
for(i=0; i
|
361
|
2
|
50
|
|
|
|
|
zType[i] = sqliteExprType(pEList->a[i].pExpr)==SQLITE_SO_NUM ? 'n' : 't'; |
362
|
|
|
|
|
|
|
} |
363
|
2
|
|
|
|
|
|
zType[i] = 0; |
364
|
2
|
|
|
|
|
|
sqliteVdbeChangeP3(v, -1, zType, P3_DYNAMIC); |
365
|
|
|
|
|
|
|
} |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
/* |
368
|
|
|
|
|
|
|
** Add code to implement the OFFSET and LIMIT |
369
|
|
|
|
|
|
|
*/ |
370
|
135
|
|
|
|
|
|
static void codeLimiter( |
371
|
|
|
|
|
|
|
Vdbe *v, /* Generate code into this VM */ |
372
|
|
|
|
|
|
|
Select *p, /* The SELECT statement being coded */ |
373
|
|
|
|
|
|
|
int iContinue, /* Jump here to skip the current record */ |
374
|
|
|
|
|
|
|
int iBreak, /* Jump here to end the loop */ |
375
|
|
|
|
|
|
|
int nPop /* Number of times to pop stack when jumping */ |
376
|
|
|
|
|
|
|
){ |
377
|
135
|
50
|
|
|
|
|
if( p->iOffset>=0 ){ |
378
|
0
|
|
|
|
|
|
int addr = sqliteVdbeCurrentAddr(v) + 2; |
379
|
0
|
0
|
|
|
|
|
if( nPop>0 ) addr++; |
380
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MemIncr, p->iOffset, addr); |
381
|
0
|
0
|
|
|
|
|
if( nPop>0 ){ |
382
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Pop, nPop, 0); |
383
|
|
|
|
|
|
|
} |
384
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Goto, 0, iContinue); |
385
|
|
|
|
|
|
|
} |
386
|
135
|
50
|
|
|
|
|
if( p->iLimit>=0 ){ |
387
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak); |
388
|
|
|
|
|
|
|
} |
389
|
135
|
|
|
|
|
|
} |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
/* |
392
|
|
|
|
|
|
|
** This routine generates the code for the inside of the inner loop |
393
|
|
|
|
|
|
|
** of a SELECT. |
394
|
|
|
|
|
|
|
** |
395
|
|
|
|
|
|
|
** If srcTab and nColumn are both zero, then the pEList expressions |
396
|
|
|
|
|
|
|
** are evaluated in order to get the data for this row. If nColumn>0 |
397
|
|
|
|
|
|
|
** then data is pulled from srcTab and pEList is used only to get the |
398
|
|
|
|
|
|
|
** datatypes for each column. |
399
|
|
|
|
|
|
|
*/ |
400
|
135
|
|
|
|
|
|
static int selectInnerLoop( |
401
|
|
|
|
|
|
|
Parse *pParse, /* The parser context */ |
402
|
|
|
|
|
|
|
Select *p, /* The complete select statement being coded */ |
403
|
|
|
|
|
|
|
ExprList *pEList, /* List of values being extracted */ |
404
|
|
|
|
|
|
|
int srcTab, /* Pull data from this table */ |
405
|
|
|
|
|
|
|
int nColumn, /* Number of columns in the source table */ |
406
|
|
|
|
|
|
|
ExprList *pOrderBy, /* If not NULL, sort results using this key */ |
407
|
|
|
|
|
|
|
int distinct, /* If >=0, make sure results are distinct */ |
408
|
|
|
|
|
|
|
int eDest, /* How to dispose of the results */ |
409
|
|
|
|
|
|
|
int iParm, /* An argument to the disposal method */ |
410
|
|
|
|
|
|
|
int iContinue, /* Jump here to continue with next row */ |
411
|
|
|
|
|
|
|
int iBreak /* Jump here to break out of the inner loop */ |
412
|
|
|
|
|
|
|
){ |
413
|
135
|
|
|
|
|
|
Vdbe *v = pParse->pVdbe; |
414
|
|
|
|
|
|
|
int i; |
415
|
|
|
|
|
|
|
int hasDistinct; /* True if the DISTINCT keyword is present */ |
416
|
|
|
|
|
|
|
|
417
|
135
|
50
|
|
|
|
|
if( v==0 ) return 0; |
418
|
|
|
|
|
|
|
assert( pEList!=0 ); |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
/* If there was a LIMIT clause on the SELECT statement, then do the check |
421
|
|
|
|
|
|
|
** to see if this row should be output. |
422
|
|
|
|
|
|
|
*/ |
423
|
135
|
50
|
|
|
|
|
hasDistinct = distinct>=0 && pEList && pEList->nExpr>0; |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
424
|
135
|
100
|
|
|
|
|
if( pOrderBy==0 && !hasDistinct ){ |
|
|
50
|
|
|
|
|
|
425
|
130
|
|
|
|
|
|
codeLimiter(v, p, iContinue, iBreak, 0); |
426
|
|
|
|
|
|
|
} |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
/* Pull the requested columns. |
429
|
|
|
|
|
|
|
*/ |
430
|
135
|
50
|
|
|
|
|
if( nColumn>0 ){ |
431
|
0
|
0
|
|
|
|
|
for(i=0; i
|
432
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Column, srcTab, i); |
433
|
|
|
|
|
|
|
} |
434
|
|
|
|
|
|
|
}else{ |
435
|
135
|
|
|
|
|
|
nColumn = pEList->nExpr; |
436
|
566
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
437
|
431
|
|
|
|
|
|
sqliteExprCode(pParse, pEList->a[i].pExpr); |
438
|
|
|
|
|
|
|
} |
439
|
|
|
|
|
|
|
} |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
/* If the DISTINCT keyword was present on the SELECT statement |
442
|
|
|
|
|
|
|
** and this row has been seen before, then do not make this row |
443
|
|
|
|
|
|
|
** part of the result. |
444
|
|
|
|
|
|
|
*/ |
445
|
135
|
50
|
|
|
|
|
if( hasDistinct ){ |
446
|
|
|
|
|
|
|
#if NULL_ALWAYS_DISTINCT |
447
|
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqliteVdbeCurrentAddr(v)+7); |
448
|
|
|
|
|
|
|
#endif |
449
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1); |
450
|
0
|
0
|
|
|
|
|
if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pEList); |
451
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Distinct, distinct, sqliteVdbeCurrentAddr(v)+3); |
452
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0); |
453
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Goto, 0, iContinue); |
454
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_String, 0, 0); |
455
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0); |
456
|
0
|
0
|
|
|
|
|
if( pOrderBy==0 ){ |
457
|
0
|
|
|
|
|
|
codeLimiter(v, p, iContinue, iBreak, nColumn); |
458
|
|
|
|
|
|
|
} |
459
|
|
|
|
|
|
|
} |
460
|
|
|
|
|
|
|
|
461
|
135
|
|
|
|
|
|
switch( eDest ){ |
462
|
|
|
|
|
|
|
/* In this mode, write each query result to the key of the temporary |
463
|
|
|
|
|
|
|
** table iParm. |
464
|
|
|
|
|
|
|
*/ |
465
|
|
|
|
|
|
|
case SRT_Union: { |
466
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); |
467
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_String, 0, 0); |
468
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0); |
469
|
0
|
|
|
|
|
|
break; |
470
|
|
|
|
|
|
|
} |
471
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
/* Store the result as data using a unique key. |
473
|
|
|
|
|
|
|
*/ |
474
|
|
|
|
|
|
|
case SRT_Table: |
475
|
|
|
|
|
|
|
case SRT_TempTable: { |
476
|
12
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
477
|
12
|
50
|
|
|
|
|
if( pOrderBy ){ |
478
|
0
|
|
|
|
|
|
pushOntoSorter(pParse, v, pOrderBy); |
479
|
|
|
|
|
|
|
}else{ |
480
|
12
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0); |
481
|
12
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Pull, 1, 0); |
482
|
12
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0); |
483
|
|
|
|
|
|
|
} |
484
|
12
|
|
|
|
|
|
break; |
485
|
|
|
|
|
|
|
} |
486
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
/* Construct a record from the query result, but instead of |
488
|
|
|
|
|
|
|
** saving that record, use it as a key to delete elements from |
489
|
|
|
|
|
|
|
** the temporary table iParm. |
490
|
|
|
|
|
|
|
*/ |
491
|
|
|
|
|
|
|
case SRT_Except: { |
492
|
|
|
|
|
|
|
int addr; |
493
|
0
|
|
|
|
|
|
addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); |
494
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3); |
495
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Delete, iParm, 0); |
496
|
0
|
|
|
|
|
|
break; |
497
|
|
|
|
|
|
|
} |
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
/* If we are creating a set for an "expr IN (SELECT ...)" construct, |
500
|
|
|
|
|
|
|
** then there should be a single item on the stack. Write this |
501
|
|
|
|
|
|
|
** item into the set table with bogus data. |
502
|
|
|
|
|
|
|
*/ |
503
|
|
|
|
|
|
|
case SRT_Set: { |
504
|
0
|
|
|
|
|
|
int addr1 = sqliteVdbeCurrentAddr(v); |
505
|
|
|
|
|
|
|
int addr2; |
506
|
|
|
|
|
|
|
assert( nColumn==1 ); |
507
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_NotNull, -1, addr1+3); |
508
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
509
|
0
|
|
|
|
|
|
addr2 = sqliteVdbeAddOp(v, OP_Goto, 0, 0); |
510
|
0
|
0
|
|
|
|
|
if( pOrderBy ){ |
511
|
0
|
|
|
|
|
|
pushOntoSorter(pParse, v, pOrderBy); |
512
|
|
|
|
|
|
|
}else{ |
513
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_String, 0, 0); |
514
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0); |
515
|
|
|
|
|
|
|
} |
516
|
0
|
|
|
|
|
|
sqliteVdbeChangeP2(v, addr2, sqliteVdbeCurrentAddr(v)); |
517
|
0
|
|
|
|
|
|
break; |
518
|
|
|
|
|
|
|
} |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
/* If this is a scalar select that is part of an expression, then |
521
|
|
|
|
|
|
|
** store the results in the appropriate memory cell and break out |
522
|
|
|
|
|
|
|
** of the scan loop. |
523
|
|
|
|
|
|
|
*/ |
524
|
|
|
|
|
|
|
case SRT_Mem: { |
525
|
|
|
|
|
|
|
assert( nColumn==1 ); |
526
|
0
|
0
|
|
|
|
|
if( pOrderBy ){ |
527
|
0
|
|
|
|
|
|
pushOntoSorter(pParse, v, pOrderBy); |
528
|
|
|
|
|
|
|
}else{ |
529
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); |
530
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Goto, 0, iBreak); |
531
|
|
|
|
|
|
|
} |
532
|
0
|
|
|
|
|
|
break; |
533
|
|
|
|
|
|
|
} |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
/* Send the data to the callback function. |
536
|
|
|
|
|
|
|
*/ |
537
|
|
|
|
|
|
|
case SRT_Callback: |
538
|
|
|
|
|
|
|
case SRT_Sorter: { |
539
|
123
|
100
|
|
|
|
|
if( pOrderBy ){ |
540
|
5
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0); |
541
|
5
|
|
|
|
|
|
pushOntoSorter(pParse, v, pOrderBy); |
542
|
|
|
|
|
|
|
}else{ |
543
|
|
|
|
|
|
|
assert( eDest==SRT_Callback ); |
544
|
118
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Callback, nColumn, 0); |
545
|
|
|
|
|
|
|
} |
546
|
123
|
|
|
|
|
|
break; |
547
|
|
|
|
|
|
|
} |
548
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
/* Invoke a subroutine to handle the results. The subroutine itself |
550
|
|
|
|
|
|
|
** is responsible for popping the results off of the stack. |
551
|
|
|
|
|
|
|
*/ |
552
|
|
|
|
|
|
|
case SRT_Subroutine: { |
553
|
0
|
0
|
|
|
|
|
if( pOrderBy ){ |
554
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
555
|
0
|
|
|
|
|
|
pushOntoSorter(pParse, v, pOrderBy); |
556
|
|
|
|
|
|
|
}else{ |
557
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Gosub, 0, iParm); |
558
|
|
|
|
|
|
|
} |
559
|
0
|
|
|
|
|
|
break; |
560
|
|
|
|
|
|
|
} |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
/* Discard the results. This is used for SELECT statements inside |
563
|
|
|
|
|
|
|
** the body of a TRIGGER. The purpose of such selects is to call |
564
|
|
|
|
|
|
|
** user-defined functions that have side effects. We do not care |
565
|
|
|
|
|
|
|
** about the actual results of the select. |
566
|
|
|
|
|
|
|
*/ |
567
|
|
|
|
|
|
|
default: { |
568
|
|
|
|
|
|
|
assert( eDest==SRT_Discard ); |
569
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Pop, nColumn, 0); |
570
|
0
|
|
|
|
|
|
break; |
571
|
|
|
|
|
|
|
} |
572
|
|
|
|
|
|
|
} |
573
|
135
|
|
|
|
|
|
return 0; |
574
|
|
|
|
|
|
|
} |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
/* |
577
|
|
|
|
|
|
|
** If the inner loop was generated using a non-null pOrderBy argument, |
578
|
|
|
|
|
|
|
** then the results were placed in a sorter. After the loop is terminated |
579
|
|
|
|
|
|
|
** we need to run the sorter and output the results. The following |
580
|
|
|
|
|
|
|
** routine generates the code needed to do that. |
581
|
|
|
|
|
|
|
*/ |
582
|
5
|
|
|
|
|
|
static void generateSortTail( |
583
|
|
|
|
|
|
|
Select *p, /* The SELECT statement */ |
584
|
|
|
|
|
|
|
Vdbe *v, /* Generate code into this VDBE */ |
585
|
|
|
|
|
|
|
int nColumn, /* Number of columns of data */ |
586
|
|
|
|
|
|
|
int eDest, /* Write the sorted results here */ |
587
|
|
|
|
|
|
|
int iParm /* Optional parameter associated with eDest */ |
588
|
|
|
|
|
|
|
){ |
589
|
5
|
|
|
|
|
|
int end1 = sqliteVdbeMakeLabel(v); |
590
|
5
|
|
|
|
|
|
int end2 = sqliteVdbeMakeLabel(v); |
591
|
|
|
|
|
|
|
int addr; |
592
|
5
|
50
|
|
|
|
|
if( eDest==SRT_Sorter ) return; |
593
|
5
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Sort, 0, 0); |
594
|
5
|
|
|
|
|
|
addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end1); |
595
|
5
|
|
|
|
|
|
codeLimiter(v, p, addr, end2, 1); |
596
|
5
|
|
|
|
|
|
switch( eDest ){ |
597
|
|
|
|
|
|
|
case SRT_Callback: { |
598
|
5
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0); |
599
|
5
|
|
|
|
|
|
break; |
600
|
|
|
|
|
|
|
} |
601
|
|
|
|
|
|
|
case SRT_Table: |
602
|
|
|
|
|
|
|
case SRT_TempTable: { |
603
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0); |
604
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Pull, 1, 0); |
605
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0); |
606
|
0
|
|
|
|
|
|
break; |
607
|
|
|
|
|
|
|
} |
608
|
|
|
|
|
|
|
case SRT_Set: { |
609
|
|
|
|
|
|
|
assert( nColumn==1 ); |
610
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3); |
611
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
612
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Goto, 0, sqliteVdbeCurrentAddr(v)+3); |
613
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_String, 0, 0); |
614
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0); |
615
|
0
|
|
|
|
|
|
break; |
616
|
|
|
|
|
|
|
} |
617
|
|
|
|
|
|
|
case SRT_Mem: { |
618
|
|
|
|
|
|
|
assert( nColumn==1 ); |
619
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); |
620
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Goto, 0, end1); |
621
|
0
|
|
|
|
|
|
break; |
622
|
|
|
|
|
|
|
} |
623
|
|
|
|
|
|
|
case SRT_Subroutine: { |
624
|
|
|
|
|
|
|
int i; |
625
|
0
|
0
|
|
|
|
|
for(i=0; i
|
626
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Column, -1-i, i); |
627
|
|
|
|
|
|
|
} |
628
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Gosub, 0, iParm); |
629
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
630
|
0
|
|
|
|
|
|
break; |
631
|
|
|
|
|
|
|
} |
632
|
|
|
|
|
|
|
default: { |
633
|
|
|
|
|
|
|
/* Do nothing */ |
634
|
0
|
|
|
|
|
|
break; |
635
|
|
|
|
|
|
|
} |
636
|
|
|
|
|
|
|
} |
637
|
5
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
638
|
5
|
|
|
|
|
|
sqliteVdbeResolveLabel(v, end2); |
639
|
5
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
640
|
5
|
|
|
|
|
|
sqliteVdbeResolveLabel(v, end1); |
641
|
5
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_SortReset, 0, 0); |
642
|
|
|
|
|
|
|
} |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
/* |
645
|
|
|
|
|
|
|
** Generate code that will tell the VDBE the datatypes of |
646
|
|
|
|
|
|
|
** columns in the result set. |
647
|
|
|
|
|
|
|
** |
648
|
|
|
|
|
|
|
** This routine only generates code if the "PRAGMA show_datatypes=on" |
649
|
|
|
|
|
|
|
** has been executed. The datatypes are reported out in the azCol |
650
|
|
|
|
|
|
|
** parameter to the callback function. The first N azCol[] entries |
651
|
|
|
|
|
|
|
** are the names of the columns, and the second N entries are the |
652
|
|
|
|
|
|
|
** datatypes for the columns. |
653
|
|
|
|
|
|
|
** |
654
|
|
|
|
|
|
|
** The "datatype" for a result that is a column of a type is the |
655
|
|
|
|
|
|
|
** datatype definition extracted from the CREATE TABLE statement. |
656
|
|
|
|
|
|
|
** The datatype for an expression is either TEXT or NUMERIC. The |
657
|
|
|
|
|
|
|
** datatype for a ROWID field is INTEGER. |
658
|
|
|
|
|
|
|
*/ |
659
|
123
|
|
|
|
|
|
static void generateColumnTypes( |
660
|
|
|
|
|
|
|
Parse *pParse, /* Parser context */ |
661
|
|
|
|
|
|
|
SrcList *pTabList, /* List of tables */ |
662
|
|
|
|
|
|
|
ExprList *pEList /* Expressions defining the result set */ |
663
|
|
|
|
|
|
|
){ |
664
|
123
|
|
|
|
|
|
Vdbe *v = pParse->pVdbe; |
665
|
|
|
|
|
|
|
int i, j; |
666
|
518
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
667
|
395
|
|
|
|
|
|
Expr *p = pEList->a[i].pExpr; |
668
|
395
|
|
|
|
|
|
char *zType = 0; |
669
|
395
|
50
|
|
|
|
|
if( p==0 ) continue; |
670
|
694
|
100
|
|
|
|
|
if( p->op==TK_COLUMN && pTabList ){ |
|
|
50
|
|
|
|
|
|
671
|
|
|
|
|
|
|
Table *pTab; |
672
|
299
|
|
|
|
|
|
int iCol = p->iColumn; |
673
|
300
|
50
|
|
|
|
|
for(j=0; jnSrc && pTabList->a[j].iCursor!=p->iTable; j++){} |
|
|
100
|
|
|
|
|
|
674
|
|
|
|
|
|
|
assert( jnSrc ); |
675
|
299
|
|
|
|
|
|
pTab = pTabList->a[j].pTab; |
676
|
299
|
50
|
|
|
|
|
if( iCol<0 ) iCol = pTab->iPKey; |
677
|
|
|
|
|
|
|
assert( iCol==-1 || (iCol>=0 && iColnCol) ); |
678
|
299
|
50
|
|
|
|
|
if( iCol<0 ){ |
679
|
0
|
|
|
|
|
|
zType = "INTEGER"; |
680
|
|
|
|
|
|
|
}else{ |
681
|
299
|
|
|
|
|
|
zType = pTab->aCol[iCol].zType; |
682
|
|
|
|
|
|
|
} |
683
|
|
|
|
|
|
|
}else{ |
684
|
96
|
100
|
|
|
|
|
if( sqliteExprType(p)==SQLITE_SO_TEXT ){ |
685
|
9
|
|
|
|
|
|
zType = "TEXT"; |
686
|
|
|
|
|
|
|
}else{ |
687
|
87
|
|
|
|
|
|
zType = "NUMERIC"; |
688
|
|
|
|
|
|
|
} |
689
|
|
|
|
|
|
|
} |
690
|
395
|
|
|
|
|
|
sqliteVdbeOp3(v, OP_ColumnName, i + pEList->nExpr, 0, zType, 0); |
691
|
|
|
|
|
|
|
} |
692
|
123
|
|
|
|
|
|
} |
693
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
/* |
695
|
|
|
|
|
|
|
** Generate code that will tell the VDBE the names of columns |
696
|
|
|
|
|
|
|
** in the result set. This information is used to provide the |
697
|
|
|
|
|
|
|
** azCol[] values in the callback. |
698
|
|
|
|
|
|
|
*/ |
699
|
123
|
|
|
|
|
|
static void generateColumnNames( |
700
|
|
|
|
|
|
|
Parse *pParse, /* Parser context */ |
701
|
|
|
|
|
|
|
SrcList *pTabList, /* List of tables */ |
702
|
|
|
|
|
|
|
ExprList *pEList /* Expressions defining the result set */ |
703
|
|
|
|
|
|
|
){ |
704
|
123
|
|
|
|
|
|
Vdbe *v = pParse->pVdbe; |
705
|
|
|
|
|
|
|
int i, j; |
706
|
123
|
|
|
|
|
|
sqlite *db = pParse->db; |
707
|
|
|
|
|
|
|
int fullNames, shortNames; |
708
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
assert( v!=0 ); |
710
|
123
|
50
|
|
|
|
|
if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return; |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
711
|
123
|
|
|
|
|
|
pParse->colNamesSet = 1; |
712
|
123
|
|
|
|
|
|
fullNames = (db->flags & SQLITE_FullColNames)!=0; |
713
|
123
|
|
|
|
|
|
shortNames = (db->flags & SQLITE_ShortColNames)!=0; |
714
|
518
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
715
|
|
|
|
|
|
|
Expr *p; |
716
|
395
|
|
|
|
|
|
int p2 = i==pEList->nExpr-1; |
717
|
395
|
|
|
|
|
|
p = pEList->a[i].pExpr; |
718
|
395
|
50
|
|
|
|
|
if( p==0 ) continue; |
719
|
395
|
100
|
|
|
|
|
if( pEList->a[i].zName ){ |
720
|
3
|
|
|
|
|
|
char *zName = pEList->a[i].zName; |
721
|
3
|
|
|
|
|
|
sqliteVdbeOp3(v, OP_ColumnName, i, p2, zName, 0); |
722
|
3
|
|
|
|
|
|
continue; |
723
|
|
|
|
|
|
|
} |
724
|
698
|
100
|
|
|
|
|
if( p->op==TK_COLUMN && pTabList ){ |
|
|
50
|
|
|
|
|
|
725
|
|
|
|
|
|
|
Table *pTab; |
726
|
|
|
|
|
|
|
char *zCol; |
727
|
306
|
|
|
|
|
|
int iCol = p->iColumn; |
728
|
306
|
50
|
|
|
|
|
for(j=0; jnSrc && pTabList->a[j].iCursor!=p->iTable; j++){} |
|
|
50
|
|
|
|
|
|
729
|
|
|
|
|
|
|
assert( jnSrc ); |
730
|
306
|
|
|
|
|
|
pTab = pTabList->a[j].pTab; |
731
|
306
|
50
|
|
|
|
|
if( iCol<0 ) iCol = pTab->iPKey; |
732
|
|
|
|
|
|
|
assert( iCol==-1 || (iCol>=0 && iColnCol) ); |
733
|
306
|
50
|
|
|
|
|
if( iCol<0 ){ |
734
|
0
|
|
|
|
|
|
zCol = "_ROWID_"; |
735
|
|
|
|
|
|
|
}else{ |
736
|
306
|
|
|
|
|
|
zCol = pTab->aCol[iCol].zName; |
737
|
|
|
|
|
|
|
} |
738
|
612
|
50
|
|
|
|
|
if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){ |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
739
|
306
|
|
|
|
|
|
int addr = sqliteVdbeOp3(v,OP_ColumnName, i, p2, p->span.z, p->span.n); |
740
|
306
|
|
|
|
|
|
sqliteVdbeCompressSpace(v, addr); |
741
|
0
|
0
|
|
|
|
|
}else if( fullNames || (!shortNames && pTabList->nSrc>1) ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
742
|
0
|
|
|
|
|
|
char *zName = 0; |
743
|
|
|
|
|
|
|
char *zTab; |
744
|
|
|
|
|
|
|
|
745
|
0
|
|
|
|
|
|
zTab = pTabList->a[j].zAlias; |
746
|
0
|
0
|
|
|
|
|
if( fullNames || zTab==0 ) zTab = pTab->zName; |
|
|
0
|
|
|
|
|
|
747
|
0
|
|
|
|
|
|
sqliteSetString(&zName, zTab, ".", zCol, 0); |
748
|
0
|
|
|
|
|
|
sqliteVdbeOp3(v, OP_ColumnName, i, p2, zName, P3_DYNAMIC); |
749
|
|
|
|
|
|
|
}else{ |
750
|
0
|
|
|
|
|
|
sqliteVdbeOp3(v, OP_ColumnName, i, p2, zCol, 0); |
751
|
|
|
|
|
|
|
} |
752
|
172
|
50
|
|
|
|
|
}else if( p->span.z && p->span.z[0] ){ |
|
|
50
|
|
|
|
|
|
753
|
86
|
|
|
|
|
|
int addr = sqliteVdbeOp3(v,OP_ColumnName, i, p2, p->span.z, p->span.n); |
754
|
86
|
|
|
|
|
|
sqliteVdbeCompressSpace(v, addr); |
755
|
|
|
|
|
|
|
}else{ |
756
|
|
|
|
|
|
|
char zName[30]; |
757
|
|
|
|
|
|
|
assert( p->op!=TK_COLUMN || pTabList==0 ); |
758
|
0
|
|
|
|
|
|
sprintf(zName, "column%d", i+1); |
759
|
0
|
|
|
|
|
|
sqliteVdbeOp3(v, OP_ColumnName, i, p2, zName, 0); |
760
|
|
|
|
|
|
|
} |
761
|
|
|
|
|
|
|
} |
762
|
|
|
|
|
|
|
} |
763
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
/* |
765
|
|
|
|
|
|
|
** Name of the connection operator, used for error messages. |
766
|
|
|
|
|
|
|
*/ |
767
|
0
|
|
|
|
|
|
static const char *selectOpName(int id){ |
768
|
|
|
|
|
|
|
char *z; |
769
|
0
|
|
|
|
|
|
switch( id ){ |
770
|
0
|
|
|
|
|
|
case TK_ALL: z = "UNION ALL"; break; |
771
|
0
|
|
|
|
|
|
case TK_INTERSECT: z = "INTERSECT"; break; |
772
|
0
|
|
|
|
|
|
case TK_EXCEPT: z = "EXCEPT"; break; |
773
|
0
|
|
|
|
|
|
default: z = "UNION"; break; |
774
|
|
|
|
|
|
|
} |
775
|
0
|
|
|
|
|
|
return z; |
776
|
|
|
|
|
|
|
} |
777
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
/* |
779
|
|
|
|
|
|
|
** Forward declaration |
780
|
|
|
|
|
|
|
*/ |
781
|
|
|
|
|
|
|
static int fillInColumnList(Parse*, Select*); |
782
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
/* |
784
|
|
|
|
|
|
|
** Given a SELECT statement, generate a Table structure that describes |
785
|
|
|
|
|
|
|
** the result set of that SELECT. |
786
|
|
|
|
|
|
|
*/ |
787
|
6
|
|
|
|
|
|
Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ |
788
|
|
|
|
|
|
|
Table *pTab; |
789
|
|
|
|
|
|
|
int i, j; |
790
|
|
|
|
|
|
|
ExprList *pEList; |
791
|
|
|
|
|
|
|
Column *aCol; |
792
|
|
|
|
|
|
|
|
793
|
6
|
50
|
|
|
|
|
if( fillInColumnList(pParse, pSelect) ){ |
794
|
0
|
|
|
|
|
|
return 0; |
795
|
|
|
|
|
|
|
} |
796
|
6
|
|
|
|
|
|
pTab = sqliteMalloc( sizeof(Table) ); |
797
|
6
|
50
|
|
|
|
|
if( pTab==0 ){ |
798
|
0
|
|
|
|
|
|
return 0; |
799
|
|
|
|
|
|
|
} |
800
|
6
|
50
|
|
|
|
|
pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; |
801
|
6
|
|
|
|
|
|
pEList = pSelect->pEList; |
802
|
6
|
|
|
|
|
|
pTab->nCol = pEList->nExpr; |
803
|
|
|
|
|
|
|
assert( pTab->nCol>0 ); |
804
|
6
|
|
|
|
|
|
pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); |
805
|
33
|
100
|
|
|
|
|
for(i=0; inCol; i++){ |
806
|
|
|
|
|
|
|
Expr *p, *pR; |
807
|
27
|
100
|
|
|
|
|
if( pEList->a[i].zName ){ |
808
|
24
|
|
|
|
|
|
aCol[i].zName = sqliteStrDup(pEList->a[i].zName); |
809
|
3
|
50
|
|
|
|
|
}else if( (p=pEList->a[i].pExpr)->op==TK_DOT |
810
|
0
|
0
|
|
|
|
|
&& (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
811
|
|
|
|
|
|
|
int cnt; |
812
|
0
|
|
|
|
|
|
sqliteSetNString(&aCol[i].zName, pR->token.z, pR->token.n, 0); |
813
|
0
|
0
|
|
|
|
|
for(j=cnt=0; j
|
814
|
0
|
0
|
|
|
|
|
if( sqliteStrICmp(aCol[j].zName, aCol[i].zName)==0 ){ |
815
|
|
|
|
|
|
|
int n; |
816
|
|
|
|
|
|
|
char zBuf[30]; |
817
|
0
|
|
|
|
|
|
sprintf(zBuf,"_%d",++cnt); |
818
|
0
|
|
|
|
|
|
n = strlen(zBuf); |
819
|
0
|
|
|
|
|
|
sqliteSetNString(&aCol[i].zName, pR->token.z, pR->token.n, zBuf, n,0); |
820
|
0
|
|
|
|
|
|
j = -1; |
821
|
|
|
|
|
|
|
} |
822
|
|
|
|
|
|
|
} |
823
|
3
|
50
|
|
|
|
|
}else if( p->span.z && p->span.z[0] ){ |
|
|
50
|
|
|
|
|
|
824
|
3
|
|
|
|
|
|
sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0); |
825
|
|
|
|
|
|
|
}else{ |
826
|
|
|
|
|
|
|
char zBuf[30]; |
827
|
0
|
|
|
|
|
|
sprintf(zBuf, "column%d", i+1); |
828
|
0
|
|
|
|
|
|
aCol[i].zName = sqliteStrDup(zBuf); |
829
|
|
|
|
|
|
|
} |
830
|
27
|
|
|
|
|
|
sqliteDequote(aCol[i].zName); |
831
|
|
|
|
|
|
|
} |
832
|
6
|
|
|
|
|
|
pTab->iPKey = -1; |
833
|
6
|
|
|
|
|
|
return pTab; |
834
|
|
|
|
|
|
|
} |
835
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
/* |
837
|
|
|
|
|
|
|
** For the given SELECT statement, do three things. |
838
|
|
|
|
|
|
|
** |
839
|
|
|
|
|
|
|
** (1) Fill in the pTabList->a[].pTab fields in the SrcList that |
840
|
|
|
|
|
|
|
** defines the set of tables that should be scanned. For views, |
841
|
|
|
|
|
|
|
** fill pTabList->a[].pSelect with a copy of the SELECT statement |
842
|
|
|
|
|
|
|
** that implements the view. A copy is made of the view's SELECT |
843
|
|
|
|
|
|
|
** statement so that we can freely modify or delete that statement |
844
|
|
|
|
|
|
|
** without worrying about messing up the presistent representation |
845
|
|
|
|
|
|
|
** of the view. |
846
|
|
|
|
|
|
|
** |
847
|
|
|
|
|
|
|
** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword |
848
|
|
|
|
|
|
|
** on joins and the ON and USING clause of joins. |
849
|
|
|
|
|
|
|
** |
850
|
|
|
|
|
|
|
** (3) Scan the list of columns in the result set (pEList) looking |
851
|
|
|
|
|
|
|
** for instances of the "*" operator or the TABLE.* operator. |
852
|
|
|
|
|
|
|
** If found, expand each "*" to be every column in every table |
853
|
|
|
|
|
|
|
** and TABLE.* to be every column in TABLE. |
854
|
|
|
|
|
|
|
** |
855
|
|
|
|
|
|
|
** Return 0 on success. If there are problems, leave an error message |
856
|
|
|
|
|
|
|
** in pParse and return non-zero. |
857
|
|
|
|
|
|
|
*/ |
858
|
144
|
|
|
|
|
|
static int fillInColumnList(Parse *pParse, Select *p){ |
859
|
|
|
|
|
|
|
int i, j, k, rc; |
860
|
|
|
|
|
|
|
SrcList *pTabList; |
861
|
|
|
|
|
|
|
ExprList *pEList; |
862
|
|
|
|
|
|
|
Table *pTab; |
863
|
|
|
|
|
|
|
|
864
|
144
|
50
|
|
|
|
|
if( p==0 || p->pSrc==0 ) return 1; |
|
|
50
|
|
|
|
|
|
865
|
144
|
|
|
|
|
|
pTabList = p->pSrc; |
866
|
144
|
|
|
|
|
|
pEList = p->pEList; |
867
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
/* Look up every table in the table list. |
869
|
|
|
|
|
|
|
*/ |
870
|
260
|
100
|
|
|
|
|
for(i=0; inSrc; i++){ |
871
|
119
|
100
|
|
|
|
|
if( pTabList->a[i].pTab ){ |
872
|
|
|
|
|
|
|
/* This routine has run before! No need to continue */ |
873
|
3
|
|
|
|
|
|
return 0; |
874
|
|
|
|
|
|
|
} |
875
|
116
|
100
|
|
|
|
|
if( pTabList->a[i].zName==0 ){ |
876
|
|
|
|
|
|
|
/* A sub-query in the FROM clause of a SELECT */ |
877
|
|
|
|
|
|
|
assert( pTabList->a[i].pSelect!=0 ); |
878
|
6
|
50
|
|
|
|
|
if( pTabList->a[i].zAlias==0 ){ |
879
|
|
|
|
|
|
|
char zFakeName[60]; |
880
|
6
|
|
|
|
|
|
sprintf(zFakeName, "sqlite_subquery_%p_", |
881
|
6
|
|
|
|
|
|
(void*)pTabList->a[i].pSelect); |
882
|
6
|
|
|
|
|
|
sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0); |
883
|
|
|
|
|
|
|
} |
884
|
6
|
|
|
|
|
|
pTabList->a[i].pTab = pTab = |
885
|
6
|
|
|
|
|
|
sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias, |
886
|
|
|
|
|
|
|
pTabList->a[i].pSelect); |
887
|
6
|
50
|
|
|
|
|
if( pTab==0 ){ |
888
|
0
|
|
|
|
|
|
return 1; |
889
|
|
|
|
|
|
|
} |
890
|
|
|
|
|
|
|
/* The isTransient flag indicates that the Table structure has been |
891
|
|
|
|
|
|
|
** dynamically allocated and may be freed at any time. In other words, |
892
|
|
|
|
|
|
|
** pTab is not pointing to a persistent table structure that defines |
893
|
|
|
|
|
|
|
** part of the schema. */ |
894
|
6
|
|
|
|
|
|
pTab->isTransient = 1; |
895
|
|
|
|
|
|
|
}else{ |
896
|
|
|
|
|
|
|
/* An ordinary table or view name in the FROM clause */ |
897
|
110
|
|
|
|
|
|
pTabList->a[i].pTab = pTab = |
898
|
110
|
|
|
|
|
|
sqliteLocateTable(pParse,pTabList->a[i].zName,pTabList->a[i].zDatabase); |
899
|
110
|
50
|
|
|
|
|
if( pTab==0 ){ |
900
|
0
|
|
|
|
|
|
return 1; |
901
|
|
|
|
|
|
|
} |
902
|
110
|
50
|
|
|
|
|
if( pTab->pSelect ){ |
903
|
|
|
|
|
|
|
/* We reach here if the named table is a really a view */ |
904
|
0
|
0
|
|
|
|
|
if( sqliteViewGetColumnNames(pParse, pTab) ){ |
905
|
0
|
|
|
|
|
|
return 1; |
906
|
|
|
|
|
|
|
} |
907
|
|
|
|
|
|
|
/* If pTabList->a[i].pSelect!=0 it means we are dealing with a |
908
|
|
|
|
|
|
|
** view within a view. The SELECT structure has already been |
909
|
|
|
|
|
|
|
** copied by the outer view so we can skip the copy step here |
910
|
|
|
|
|
|
|
** in the inner view. |
911
|
|
|
|
|
|
|
*/ |
912
|
0
|
0
|
|
|
|
|
if( pTabList->a[i].pSelect==0 ){ |
913
|
0
|
|
|
|
|
|
pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect); |
914
|
|
|
|
|
|
|
} |
915
|
|
|
|
|
|
|
} |
916
|
|
|
|
|
|
|
} |
917
|
|
|
|
|
|
|
} |
918
|
|
|
|
|
|
|
|
919
|
|
|
|
|
|
|
/* Process NATURAL keywords, and ON and USING clauses of joins. |
920
|
|
|
|
|
|
|
*/ |
921
|
141
|
50
|
|
|
|
|
if( sqliteProcessJoin(pParse, p) ) return 1; |
922
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
/* For every "*" that occurs in the column list, insert the names of |
924
|
|
|
|
|
|
|
** all columns in all tables. And for every TABLE.* insert the names |
925
|
|
|
|
|
|
|
** of all columns in TABLE. The parser inserted a special expression |
926
|
|
|
|
|
|
|
** with the TK_ALL operator for each "*" that it found in the column list. |
927
|
|
|
|
|
|
|
** The following code just has to locate the TK_ALL expressions and expand |
928
|
|
|
|
|
|
|
** each one to the list of all columns in all tables. |
929
|
|
|
|
|
|
|
** |
930
|
|
|
|
|
|
|
** The first loop just checks to see if there are any "*" operators |
931
|
|
|
|
|
|
|
** that need expanding. |
932
|
|
|
|
|
|
|
*/ |
933
|
524
|
100
|
|
|
|
|
for(k=0; knExpr; k++){ |
934
|
411
|
|
|
|
|
|
Expr *pE = pEList->a[k].pExpr; |
935
|
411
|
100
|
|
|
|
|
if( pE->op==TK_ALL ) break; |
936
|
384
|
100
|
|
|
|
|
if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
937
|
1
|
50
|
|
|
|
|
&& pE->pLeft && pE->pLeft->op==TK_ID ) break; |
|
|
50
|
|
|
|
|
|
938
|
|
|
|
|
|
|
} |
939
|
141
|
|
|
|
|
|
rc = 0; |
940
|
141
|
100
|
|
|
|
|
if( knExpr ){ |
941
|
|
|
|
|
|
|
/* |
942
|
|
|
|
|
|
|
** If we get here it means the result set contains one or more "*" |
943
|
|
|
|
|
|
|
** operators that need to be expanded. Loop through each expression |
944
|
|
|
|
|
|
|
** in the result set and expand them one by one. |
945
|
|
|
|
|
|
|
*/ |
946
|
28
|
|
|
|
|
|
struct ExprList_item *a = pEList->a; |
947
|
28
|
|
|
|
|
|
ExprList *pNew = 0; |
948
|
57
|
100
|
|
|
|
|
for(k=0; knExpr; k++){ |
949
|
29
|
|
|
|
|
|
Expr *pE = a[k].pExpr; |
950
|
29
|
100
|
|
|
|
|
if( pE->op!=TK_ALL && |
|
|
50
|
|
|
|
|
|
951
|
2
|
50
|
|
|
|
|
(pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ |
|
|
100
|
|
|
|
|
|
952
|
|
|
|
|
|
|
/* This particular expression does not need to be expanded. |
953
|
|
|
|
|
|
|
*/ |
954
|
1
|
|
|
|
|
|
pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0); |
955
|
1
|
|
|
|
|
|
pNew->a[pNew->nExpr-1].zName = a[k].zName; |
956
|
1
|
|
|
|
|
|
a[k].pExpr = 0; |
957
|
1
|
|
|
|
|
|
a[k].zName = 0; |
958
|
|
|
|
|
|
|
}else{ |
959
|
|
|
|
|
|
|
/* This expression is a "*" or a "TABLE.*" and needs to be |
960
|
|
|
|
|
|
|
** expanded. */ |
961
|
28
|
|
|
|
|
|
int tableSeen = 0; /* Set to 1 when TABLE matches */ |
962
|
|
|
|
|
|
|
char *zTName; /* text of name of TABLE */ |
963
|
28
|
100
|
|
|
|
|
if( pE->op==TK_DOT && pE->pLeft ){ |
|
|
50
|
|
|
|
|
|
964
|
1
|
|
|
|
|
|
zTName = sqliteTableNameFromToken(&pE->pLeft->token); |
965
|
|
|
|
|
|
|
}else{ |
966
|
27
|
|
|
|
|
|
zTName = 0; |
967
|
|
|
|
|
|
|
} |
968
|
56
|
100
|
|
|
|
|
for(i=0; inSrc; i++){ |
969
|
28
|
|
|
|
|
|
Table *pTab = pTabList->a[i].pTab; |
970
|
28
|
|
|
|
|
|
char *zTabName = pTabList->a[i].zAlias; |
971
|
28
|
100
|
|
|
|
|
if( zTabName==0 || zTabName[0]==0 ){ |
|
|
50
|
|
|
|
|
|
972
|
25
|
|
|
|
|
|
zTabName = pTab->zName; |
973
|
|
|
|
|
|
|
} |
974
|
29
|
100
|
|
|
|
|
if( zTName && (zTabName==0 || zTabName[0]==0 || |
|
|
50
|
|
|
|
|
|
975
|
1
|
|
|
|
|
|
sqliteStrICmp(zTName, zTabName)!=0) ){ |
976
|
0
|
|
|
|
|
|
continue; |
977
|
|
|
|
|
|
|
} |
978
|
28
|
|
|
|
|
|
tableSeen = 1; |
979
|
103
|
100
|
|
|
|
|
for(j=0; jnCol; j++){ |
980
|
|
|
|
|
|
|
Expr *pExpr, *pLeft, *pRight; |
981
|
75
|
|
|
|
|
|
char *zName = pTab->aCol[j].zName; |
982
|
|
|
|
|
|
|
|
983
|
75
|
50
|
|
|
|
|
if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 && |
984
|
0
|
|
|
|
|
|
columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){ |
985
|
|
|
|
|
|
|
/* In a NATURAL join, omit the join columns from the |
986
|
|
|
|
|
|
|
** table on the right */ |
987
|
0
|
|
|
|
|
|
continue; |
988
|
|
|
|
|
|
|
} |
989
|
75
|
50
|
|
|
|
|
if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){ |
|
|
0
|
|
|
|
|
|
990
|
|
|
|
|
|
|
/* In a join with a USING clause, omit columns in the |
991
|
|
|
|
|
|
|
** using clause from the table on the right. */ |
992
|
0
|
|
|
|
|
|
continue; |
993
|
|
|
|
|
|
|
} |
994
|
75
|
|
|
|
|
|
pRight = sqliteExpr(TK_ID, 0, 0, 0); |
995
|
75
|
50
|
|
|
|
|
if( pRight==0 ) break; |
996
|
75
|
|
|
|
|
|
pRight->token.z = zName; |
997
|
75
|
|
|
|
|
|
pRight->token.n = strlen(zName); |
998
|
75
|
|
|
|
|
|
pRight->token.dyn = 0; |
999
|
75
|
50
|
|
|
|
|
if( zTabName && pTabList->nSrc>1 ){ |
|
|
50
|
|
|
|
|
|
1000
|
0
|
|
|
|
|
|
pLeft = sqliteExpr(TK_ID, 0, 0, 0); |
1001
|
0
|
|
|
|
|
|
pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0); |
1002
|
0
|
0
|
|
|
|
|
if( pExpr==0 ) break; |
1003
|
0
|
|
|
|
|
|
pLeft->token.z = zTabName; |
1004
|
0
|
|
|
|
|
|
pLeft->token.n = strlen(zTabName); |
1005
|
0
|
|
|
|
|
|
pLeft->token.dyn = 0; |
1006
|
0
|
|
|
|
|
|
sqliteSetString((char**)&pExpr->span.z, zTabName, ".", zName, 0); |
1007
|
0
|
|
|
|
|
|
pExpr->span.n = strlen(pExpr->span.z); |
1008
|
0
|
|
|
|
|
|
pExpr->span.dyn = 1; |
1009
|
0
|
|
|
|
|
|
pExpr->token.z = 0; |
1010
|
0
|
|
|
|
|
|
pExpr->token.n = 0; |
1011
|
0
|
|
|
|
|
|
pExpr->token.dyn = 0; |
1012
|
|
|
|
|
|
|
}else{ |
1013
|
75
|
|
|
|
|
|
pExpr = pRight; |
1014
|
75
|
|
|
|
|
|
pExpr->span = pExpr->token; |
1015
|
|
|
|
|
|
|
} |
1016
|
75
|
|
|
|
|
|
pNew = sqliteExprListAppend(pNew, pExpr, 0); |
1017
|
|
|
|
|
|
|
} |
1018
|
|
|
|
|
|
|
} |
1019
|
28
|
50
|
|
|
|
|
if( !tableSeen ){ |
1020
|
0
|
0
|
|
|
|
|
if( zTName ){ |
1021
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, "no such table: %s", zTName); |
1022
|
|
|
|
|
|
|
}else{ |
1023
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, "no tables specified"); |
1024
|
|
|
|
|
|
|
} |
1025
|
0
|
|
|
|
|
|
rc = 1; |
1026
|
|
|
|
|
|
|
} |
1027
|
28
|
|
|
|
|
|
sqliteFree(zTName); |
1028
|
|
|
|
|
|
|
} |
1029
|
|
|
|
|
|
|
} |
1030
|
28
|
|
|
|
|
|
sqliteExprListDelete(pEList); |
1031
|
28
|
|
|
|
|
|
p->pEList = pNew; |
1032
|
|
|
|
|
|
|
} |
1033
|
141
|
|
|
|
|
|
return rc; |
1034
|
|
|
|
|
|
|
} |
1035
|
|
|
|
|
|
|
|
1036
|
|
|
|
|
|
|
/* |
1037
|
|
|
|
|
|
|
** This routine recursively unlinks the Select.pSrc.a[].pTab pointers |
1038
|
|
|
|
|
|
|
** in a select structure. It just sets the pointers to NULL. This |
1039
|
|
|
|
|
|
|
** routine is recursive in the sense that if the Select.pSrc.a[].pSelect |
1040
|
|
|
|
|
|
|
** pointer is not NULL, this routine is called recursively on that pointer. |
1041
|
|
|
|
|
|
|
** |
1042
|
|
|
|
|
|
|
** This routine is called on the Select structure that defines a |
1043
|
|
|
|
|
|
|
** VIEW in order to undo any bindings to tables. This is necessary |
1044
|
|
|
|
|
|
|
** because those tables might be DROPed by a subsequent SQL command. |
1045
|
|
|
|
|
|
|
** If the bindings are not removed, then the Select.pSrc->a[].pTab field |
1046
|
|
|
|
|
|
|
** will be left pointing to a deallocated Table structure after the |
1047
|
|
|
|
|
|
|
** DROP and a coredump will occur the next time the VIEW is used. |
1048
|
|
|
|
|
|
|
*/ |
1049
|
0
|
|
|
|
|
|
void sqliteSelectUnbind(Select *p){ |
1050
|
|
|
|
|
|
|
int i; |
1051
|
0
|
|
|
|
|
|
SrcList *pSrc = p->pSrc; |
1052
|
|
|
|
|
|
|
Table *pTab; |
1053
|
0
|
0
|
|
|
|
|
if( p==0 ) return; |
1054
|
0
|
0
|
|
|
|
|
for(i=0; inSrc; i++){ |
1055
|
0
|
0
|
|
|
|
|
if( (pTab = pSrc->a[i].pTab)!=0 ){ |
1056
|
0
|
0
|
|
|
|
|
if( pTab->isTransient ){ |
1057
|
0
|
|
|
|
|
|
sqliteDeleteTable(0, pTab); |
1058
|
|
|
|
|
|
|
} |
1059
|
0
|
|
|
|
|
|
pSrc->a[i].pTab = 0; |
1060
|
0
|
0
|
|
|
|
|
if( pSrc->a[i].pSelect ){ |
1061
|
0
|
|
|
|
|
|
sqliteSelectUnbind(pSrc->a[i].pSelect); |
1062
|
|
|
|
|
|
|
} |
1063
|
|
|
|
|
|
|
} |
1064
|
|
|
|
|
|
|
} |
1065
|
|
|
|
|
|
|
} |
1066
|
|
|
|
|
|
|
|
1067
|
|
|
|
|
|
|
/* |
1068
|
|
|
|
|
|
|
** This routine associates entries in an ORDER BY expression list with |
1069
|
|
|
|
|
|
|
** columns in a result. For each ORDER BY expression, the opcode of |
1070
|
|
|
|
|
|
|
** the top-level node is changed to TK_COLUMN and the iColumn value of |
1071
|
|
|
|
|
|
|
** the top-level node is filled in with column number and the iTable |
1072
|
|
|
|
|
|
|
** value of the top-level node is filled with iTable parameter. |
1073
|
|
|
|
|
|
|
** |
1074
|
|
|
|
|
|
|
** If there are prior SELECT clauses, they are processed first. A match |
1075
|
|
|
|
|
|
|
** in an earlier SELECT takes precedence over a later SELECT. |
1076
|
|
|
|
|
|
|
** |
1077
|
|
|
|
|
|
|
** Any entry that does not match is flagged as an error. The number |
1078
|
|
|
|
|
|
|
** of errors is returned. |
1079
|
|
|
|
|
|
|
** |
1080
|
|
|
|
|
|
|
** This routine does NOT correctly initialize the Expr.dataType field |
1081
|
|
|
|
|
|
|
** of the ORDER BY expressions. The multiSelectSortOrder() routine |
1082
|
|
|
|
|
|
|
** must be called to do that after the individual select statements |
1083
|
|
|
|
|
|
|
** have all been analyzed. This routine is unable to compute Expr.dataType |
1084
|
|
|
|
|
|
|
** because it must be called before the individual select statements |
1085
|
|
|
|
|
|
|
** have been analyzed. |
1086
|
|
|
|
|
|
|
*/ |
1087
|
0
|
|
|
|
|
|
static int matchOrderbyToColumn( |
1088
|
|
|
|
|
|
|
Parse *pParse, /* A place to leave error messages */ |
1089
|
|
|
|
|
|
|
Select *pSelect, /* Match to result columns of this SELECT */ |
1090
|
|
|
|
|
|
|
ExprList *pOrderBy, /* The ORDER BY values to match against columns */ |
1091
|
|
|
|
|
|
|
int iTable, /* Insert this value in iTable */ |
1092
|
|
|
|
|
|
|
int mustComplete /* If TRUE all ORDER BYs must match */ |
1093
|
|
|
|
|
|
|
){ |
1094
|
0
|
|
|
|
|
|
int nErr = 0; |
1095
|
|
|
|
|
|
|
int i, j; |
1096
|
|
|
|
|
|
|
ExprList *pEList; |
1097
|
|
|
|
|
|
|
|
1098
|
0
|
0
|
|
|
|
|
if( pSelect==0 || pOrderBy==0 ) return 1; |
|
|
0
|
|
|
|
|
|
1099
|
0
|
0
|
|
|
|
|
if( mustComplete ){ |
1100
|
0
|
0
|
|
|
|
|
for(i=0; inExpr; i++){ pOrderBy->a[i].done = 0; } |
1101
|
|
|
|
|
|
|
} |
1102
|
0
|
0
|
|
|
|
|
if( fillInColumnList(pParse, pSelect) ){ |
1103
|
0
|
|
|
|
|
|
return 1; |
1104
|
|
|
|
|
|
|
} |
1105
|
0
|
0
|
|
|
|
|
if( pSelect->pPrior ){ |
1106
|
0
|
0
|
|
|
|
|
if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ |
1107
|
0
|
|
|
|
|
|
return 1; |
1108
|
|
|
|
|
|
|
} |
1109
|
|
|
|
|
|
|
} |
1110
|
0
|
|
|
|
|
|
pEList = pSelect->pEList; |
1111
|
0
|
0
|
|
|
|
|
for(i=0; inExpr; i++){ |
1112
|
0
|
|
|
|
|
|
Expr *pE = pOrderBy->a[i].pExpr; |
1113
|
0
|
|
|
|
|
|
int iCol = -1; |
1114
|
0
|
0
|
|
|
|
|
if( pOrderBy->a[i].done ) continue; |
1115
|
0
|
0
|
|
|
|
|
if( sqliteExprIsInteger(pE, &iCol) ){ |
1116
|
0
|
0
|
|
|
|
|
if( iCol<=0 || iCol>pEList->nExpr ){ |
|
|
0
|
|
|
|
|
|
1117
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, |
1118
|
|
|
|
|
|
|
"ORDER BY position %d should be between 1 and %d", |
1119
|
|
|
|
|
|
|
iCol, pEList->nExpr); |
1120
|
0
|
|
|
|
|
|
nErr++; |
1121
|
0
|
|
|
|
|
|
break; |
1122
|
|
|
|
|
|
|
} |
1123
|
0
|
0
|
|
|
|
|
if( !mustComplete ) continue; |
1124
|
0
|
|
|
|
|
|
iCol--; |
1125
|
|
|
|
|
|
|
} |
1126
|
0
|
0
|
|
|
|
|
for(j=0; iCol<0 && jnExpr; j++){ |
|
|
0
|
|
|
|
|
|
1127
|
0
|
0
|
|
|
|
|
if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1128
|
|
|
|
|
|
|
char *zName, *zLabel; |
1129
|
0
|
|
|
|
|
|
zName = pEList->a[j].zName; |
1130
|
|
|
|
|
|
|
assert( pE->token.z ); |
1131
|
0
|
|
|
|
|
|
zLabel = sqliteStrNDup(pE->token.z, pE->token.n); |
1132
|
0
|
|
|
|
|
|
sqliteDequote(zLabel); |
1133
|
0
|
0
|
|
|
|
|
if( sqliteStrICmp(zName, zLabel)==0 ){ |
1134
|
0
|
|
|
|
|
|
iCol = j; |
1135
|
|
|
|
|
|
|
} |
1136
|
0
|
|
|
|
|
|
sqliteFree(zLabel); |
1137
|
|
|
|
|
|
|
} |
1138
|
0
|
0
|
|
|
|
|
if( iCol<0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){ |
|
|
0
|
|
|
|
|
|
1139
|
0
|
|
|
|
|
|
iCol = j; |
1140
|
|
|
|
|
|
|
} |
1141
|
|
|
|
|
|
|
} |
1142
|
0
|
0
|
|
|
|
|
if( iCol>=0 ){ |
1143
|
0
|
|
|
|
|
|
pE->op = TK_COLUMN; |
1144
|
0
|
|
|
|
|
|
pE->iColumn = iCol; |
1145
|
0
|
|
|
|
|
|
pE->iTable = iTable; |
1146
|
0
|
|
|
|
|
|
pOrderBy->a[i].done = 1; |
1147
|
|
|
|
|
|
|
} |
1148
|
0
|
0
|
|
|
|
|
if( iCol<0 && mustComplete ){ |
|
|
0
|
|
|
|
|
|
1149
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, |
1150
|
|
|
|
|
|
|
"ORDER BY term number %d does not match any result column", i+1); |
1151
|
0
|
|
|
|
|
|
nErr++; |
1152
|
0
|
|
|
|
|
|
break; |
1153
|
|
|
|
|
|
|
} |
1154
|
|
|
|
|
|
|
} |
1155
|
0
|
|
|
|
|
|
return nErr; |
1156
|
|
|
|
|
|
|
} |
1157
|
|
|
|
|
|
|
|
1158
|
|
|
|
|
|
|
/* |
1159
|
|
|
|
|
|
|
** Get a VDBE for the given parser context. Create a new one if necessary. |
1160
|
|
|
|
|
|
|
** If an error occurs, return NULL and leave a message in pParse. |
1161
|
|
|
|
|
|
|
*/ |
1162
|
1093
|
|
|
|
|
|
Vdbe *sqliteGetVdbe(Parse *pParse){ |
1163
|
1093
|
|
|
|
|
|
Vdbe *v = pParse->pVdbe; |
1164
|
1093
|
100
|
|
|
|
|
if( v==0 ){ |
1165
|
348
|
|
|
|
|
|
v = pParse->pVdbe = sqliteVdbeCreate(pParse->db); |
1166
|
|
|
|
|
|
|
} |
1167
|
1093
|
|
|
|
|
|
return v; |
1168
|
|
|
|
|
|
|
} |
1169
|
|
|
|
|
|
|
|
1170
|
|
|
|
|
|
|
/* |
1171
|
|
|
|
|
|
|
** This routine sets the Expr.dataType field on all elements of |
1172
|
|
|
|
|
|
|
** the pOrderBy expression list. The pOrderBy list will have been |
1173
|
|
|
|
|
|
|
** set up by matchOrderbyToColumn(). Hence each expression has |
1174
|
|
|
|
|
|
|
** a TK_COLUMN as its root node. The Expr.iColumn refers to a |
1175
|
|
|
|
|
|
|
** column in the result set. The datatype is set to SQLITE_SO_TEXT |
1176
|
|
|
|
|
|
|
** if the corresponding column in p and every SELECT to the left of |
1177
|
|
|
|
|
|
|
** p has a datatype of SQLITE_SO_TEXT. If the cooressponding column |
1178
|
|
|
|
|
|
|
** in p or any of the left SELECTs is SQLITE_SO_NUM, then the datatype |
1179
|
|
|
|
|
|
|
** of the order-by expression is set to SQLITE_SO_NUM. |
1180
|
|
|
|
|
|
|
** |
1181
|
|
|
|
|
|
|
** Examples: |
1182
|
|
|
|
|
|
|
** |
1183
|
|
|
|
|
|
|
** CREATE TABLE one(a INTEGER, b TEXT); |
1184
|
|
|
|
|
|
|
** CREATE TABLE two(c VARCHAR(5), d FLOAT); |
1185
|
|
|
|
|
|
|
** |
1186
|
|
|
|
|
|
|
** SELECT b, b FROM one UNION SELECT d, c FROM two ORDER BY 1, 2; |
1187
|
|
|
|
|
|
|
** |
1188
|
|
|
|
|
|
|
** The primary sort key will use SQLITE_SO_NUM because the "d" in |
1189
|
|
|
|
|
|
|
** the second SELECT is numeric. The 1st column of the first SELECT |
1190
|
|
|
|
|
|
|
** is text but that does not matter because a numeric always overrides |
1191
|
|
|
|
|
|
|
** a text. |
1192
|
|
|
|
|
|
|
** |
1193
|
|
|
|
|
|
|
** The secondary key will use the SQLITE_SO_TEXT sort order because |
1194
|
|
|
|
|
|
|
** both the (second) "b" in the first SELECT and the "c" in the second |
1195
|
|
|
|
|
|
|
** SELECT have a datatype of text. |
1196
|
|
|
|
|
|
|
*/ |
1197
|
0
|
|
|
|
|
|
static void multiSelectSortOrder(Select *p, ExprList *pOrderBy){ |
1198
|
|
|
|
|
|
|
int i; |
1199
|
|
|
|
|
|
|
ExprList *pEList; |
1200
|
0
|
0
|
|
|
|
|
if( pOrderBy==0 ) return; |
1201
|
0
|
0
|
|
|
|
|
if( p==0 ){ |
1202
|
0
|
0
|
|
|
|
|
for(i=0; inExpr; i++){ |
1203
|
0
|
|
|
|
|
|
pOrderBy->a[i].pExpr->dataType = SQLITE_SO_TEXT; |
1204
|
|
|
|
|
|
|
} |
1205
|
0
|
|
|
|
|
|
return; |
1206
|
|
|
|
|
|
|
} |
1207
|
0
|
|
|
|
|
|
multiSelectSortOrder(p->pPrior, pOrderBy); |
1208
|
0
|
|
|
|
|
|
pEList = p->pEList; |
1209
|
0
|
0
|
|
|
|
|
for(i=0; inExpr; i++){ |
1210
|
0
|
|
|
|
|
|
Expr *pE = pOrderBy->a[i].pExpr; |
1211
|
0
|
0
|
|
|
|
|
if( pE->dataType==SQLITE_SO_NUM ) continue; |
1212
|
|
|
|
|
|
|
assert( pE->iColumn>=0 ); |
1213
|
0
|
0
|
|
|
|
|
if( pEList->nExpr>pE->iColumn ){ |
1214
|
0
|
|
|
|
|
|
pE->dataType = sqliteExprType(pEList->a[pE->iColumn].pExpr); |
1215
|
|
|
|
|
|
|
} |
1216
|
|
|
|
|
|
|
} |
1217
|
|
|
|
|
|
|
} |
1218
|
|
|
|
|
|
|
|
1219
|
|
|
|
|
|
|
/* |
1220
|
|
|
|
|
|
|
** Compute the iLimit and iOffset fields of the SELECT based on the |
1221
|
|
|
|
|
|
|
** nLimit and nOffset fields. nLimit and nOffset hold the integers |
1222
|
|
|
|
|
|
|
** that appear in the original SQL statement after the LIMIT and OFFSET |
1223
|
|
|
|
|
|
|
** keywords. Or that hold -1 and 0 if those keywords are omitted. |
1224
|
|
|
|
|
|
|
** iLimit and iOffset are the integer memory register numbers for |
1225
|
|
|
|
|
|
|
** counters used to compute the limit and offset. If there is no |
1226
|
|
|
|
|
|
|
** limit and/or offset, then iLimit and iOffset are negative. |
1227
|
|
|
|
|
|
|
** |
1228
|
|
|
|
|
|
|
** This routine changes the values if iLimit and iOffset only if |
1229
|
|
|
|
|
|
|
** a limit or offset is defined by nLimit and nOffset. iLimit and |
1230
|
|
|
|
|
|
|
** iOffset should have been preset to appropriate default values |
1231
|
|
|
|
|
|
|
** (usually but not always -1) prior to calling this routine. |
1232
|
|
|
|
|
|
|
** Only if nLimit>=0 or nOffset>0 do the limit registers get |
1233
|
|
|
|
|
|
|
** redefined. The UNION ALL operator uses this property to force |
1234
|
|
|
|
|
|
|
** the reuse of the same limit and offset registers across multiple |
1235
|
|
|
|
|
|
|
** SELECT statements. |
1236
|
|
|
|
|
|
|
*/ |
1237
|
135
|
|
|
|
|
|
static void computeLimitRegisters(Parse *pParse, Select *p){ |
1238
|
|
|
|
|
|
|
/* |
1239
|
|
|
|
|
|
|
** If the comparison is p->nLimit>0 then "LIMIT 0" shows |
1240
|
|
|
|
|
|
|
** all rows. It is the same as no limit. If the comparision is |
1241
|
|
|
|
|
|
|
** p->nLimit>=0 then "LIMIT 0" show no rows at all. |
1242
|
|
|
|
|
|
|
** "LIMIT -1" always shows all rows. There is some |
1243
|
|
|
|
|
|
|
** contraversy about what the correct behavior should be. |
1244
|
|
|
|
|
|
|
** The current implementation interprets "LIMIT 0" to mean |
1245
|
|
|
|
|
|
|
** no rows. |
1246
|
|
|
|
|
|
|
*/ |
1247
|
135
|
50
|
|
|
|
|
if( p->nLimit>=0 ){ |
1248
|
0
|
|
|
|
|
|
int iMem = pParse->nMem++; |
1249
|
0
|
|
|
|
|
|
Vdbe *v = sqliteGetVdbe(pParse); |
1250
|
0
|
0
|
|
|
|
|
if( v==0 ) return; |
1251
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Integer, -p->nLimit, 0); |
1252
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MemStore, iMem, 1); |
1253
|
0
|
|
|
|
|
|
p->iLimit = iMem; |
1254
|
|
|
|
|
|
|
} |
1255
|
135
|
50
|
|
|
|
|
if( p->nOffset>0 ){ |
1256
|
0
|
|
|
|
|
|
int iMem = pParse->nMem++; |
1257
|
0
|
|
|
|
|
|
Vdbe *v = sqliteGetVdbe(pParse); |
1258
|
0
|
0
|
|
|
|
|
if( v==0 ) return; |
1259
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Integer, -p->nOffset, 0); |
1260
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MemStore, iMem, 1); |
1261
|
0
|
|
|
|
|
|
p->iOffset = iMem; |
1262
|
|
|
|
|
|
|
} |
1263
|
|
|
|
|
|
|
} |
1264
|
|
|
|
|
|
|
|
1265
|
|
|
|
|
|
|
/* |
1266
|
|
|
|
|
|
|
** This routine is called to process a query that is really the union |
1267
|
|
|
|
|
|
|
** or intersection of two or more separate queries. |
1268
|
|
|
|
|
|
|
** |
1269
|
|
|
|
|
|
|
** "p" points to the right-most of the two queries. the query on the |
1270
|
|
|
|
|
|
|
** left is p->pPrior. The left query could also be a compound query |
1271
|
|
|
|
|
|
|
** in which case this routine will be called recursively. |
1272
|
|
|
|
|
|
|
** |
1273
|
|
|
|
|
|
|
** The results of the total query are to be written into a destination |
1274
|
|
|
|
|
|
|
** of type eDest with parameter iParm. |
1275
|
|
|
|
|
|
|
** |
1276
|
|
|
|
|
|
|
** Example 1: Consider a three-way compound SQL statement. |
1277
|
|
|
|
|
|
|
** |
1278
|
|
|
|
|
|
|
** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 |
1279
|
|
|
|
|
|
|
** |
1280
|
|
|
|
|
|
|
** This statement is parsed up as follows: |
1281
|
|
|
|
|
|
|
** |
1282
|
|
|
|
|
|
|
** SELECT c FROM t3 |
1283
|
|
|
|
|
|
|
** | |
1284
|
|
|
|
|
|
|
** `-----> SELECT b FROM t2 |
1285
|
|
|
|
|
|
|
** | |
1286
|
|
|
|
|
|
|
** `------> SELECT a FROM t1 |
1287
|
|
|
|
|
|
|
** |
1288
|
|
|
|
|
|
|
** The arrows in the diagram above represent the Select.pPrior pointer. |
1289
|
|
|
|
|
|
|
** So if this routine is called with p equal to the t3 query, then |
1290
|
|
|
|
|
|
|
** pPrior will be the t2 query. p->op will be TK_UNION in this case. |
1291
|
|
|
|
|
|
|
** |
1292
|
|
|
|
|
|
|
** Notice that because of the way SQLite parses compound SELECTs, the |
1293
|
|
|
|
|
|
|
** individual selects always group from left to right. |
1294
|
|
|
|
|
|
|
*/ |
1295
|
9
|
|
|
|
|
|
static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ |
1296
|
|
|
|
|
|
|
int rc; /* Success code from a subroutine */ |
1297
|
|
|
|
|
|
|
Select *pPrior; /* Another SELECT immediately to our left */ |
1298
|
|
|
|
|
|
|
Vdbe *v; /* Generate code to this VDBE */ |
1299
|
|
|
|
|
|
|
|
1300
|
|
|
|
|
|
|
/* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only |
1301
|
|
|
|
|
|
|
** the last SELECT in the series may have an ORDER BY or LIMIT. |
1302
|
|
|
|
|
|
|
*/ |
1303
|
9
|
50
|
|
|
|
|
if( p==0 || p->pPrior==0 ) return 1; |
|
|
50
|
|
|
|
|
|
1304
|
9
|
|
|
|
|
|
pPrior = p->pPrior; |
1305
|
9
|
50
|
|
|
|
|
if( pPrior->pOrderBy ){ |
1306
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse,"ORDER BY clause should come after %s not before", |
1307
|
0
|
|
|
|
|
|
selectOpName(p->op)); |
1308
|
0
|
|
|
|
|
|
return 1; |
1309
|
|
|
|
|
|
|
} |
1310
|
9
|
50
|
|
|
|
|
if( pPrior->nLimit>=0 || pPrior->nOffset>0 ){ |
|
|
50
|
|
|
|
|
|
1311
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse,"LIMIT clause should come after %s not before", |
1312
|
0
|
|
|
|
|
|
selectOpName(p->op)); |
1313
|
0
|
|
|
|
|
|
return 1; |
1314
|
|
|
|
|
|
|
} |
1315
|
|
|
|
|
|
|
|
1316
|
|
|
|
|
|
|
/* Make sure we have a valid query engine. If not, create a new one. |
1317
|
|
|
|
|
|
|
*/ |
1318
|
9
|
|
|
|
|
|
v = sqliteGetVdbe(pParse); |
1319
|
9
|
50
|
|
|
|
|
if( v==0 ) return 1; |
1320
|
|
|
|
|
|
|
|
1321
|
|
|
|
|
|
|
/* Create the destination temporary table if necessary |
1322
|
|
|
|
|
|
|
*/ |
1323
|
9
|
100
|
|
|
|
|
if( eDest==SRT_TempTable ){ |
1324
|
3
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0); |
1325
|
3
|
|
|
|
|
|
eDest = SRT_Table; |
1326
|
|
|
|
|
|
|
} |
1327
|
|
|
|
|
|
|
|
1328
|
|
|
|
|
|
|
/* Generate code for the left and right SELECT statements. |
1329
|
|
|
|
|
|
|
*/ |
1330
|
9
|
|
|
|
|
|
switch( p->op ){ |
1331
|
|
|
|
|
|
|
case TK_ALL: { |
1332
|
9
|
50
|
|
|
|
|
if( p->pOrderBy==0 ){ |
1333
|
9
|
|
|
|
|
|
pPrior->nLimit = p->nLimit; |
1334
|
9
|
|
|
|
|
|
pPrior->nOffset = p->nOffset; |
1335
|
9
|
|
|
|
|
|
rc = sqliteSelect(pParse, pPrior, eDest, iParm, 0, 0, 0); |
1336
|
9
|
50
|
|
|
|
|
if( rc ) return rc; |
1337
|
9
|
|
|
|
|
|
p->pPrior = 0; |
1338
|
9
|
|
|
|
|
|
p->iLimit = pPrior->iLimit; |
1339
|
9
|
|
|
|
|
|
p->iOffset = pPrior->iOffset; |
1340
|
9
|
|
|
|
|
|
p->nLimit = -1; |
1341
|
9
|
|
|
|
|
|
p->nOffset = 0; |
1342
|
9
|
|
|
|
|
|
rc = sqliteSelect(pParse, p, eDest, iParm, 0, 0, 0); |
1343
|
9
|
|
|
|
|
|
p->pPrior = pPrior; |
1344
|
9
|
50
|
|
|
|
|
if( rc ) return rc; |
1345
|
9
|
|
|
|
|
|
break; |
1346
|
|
|
|
|
|
|
} |
1347
|
|
|
|
|
|
|
/* For UNION ALL ... ORDER BY fall through to the next case */ |
1348
|
|
|
|
|
|
|
} |
1349
|
|
|
|
|
|
|
case TK_EXCEPT: |
1350
|
|
|
|
|
|
|
case TK_UNION: { |
1351
|
|
|
|
|
|
|
int unionTab; /* Cursor number of the temporary table holding result */ |
1352
|
|
|
|
|
|
|
int op; /* One of the SRT_ operations to apply to self */ |
1353
|
|
|
|
|
|
|
int priorOp; /* The SRT_ operation to apply to prior selects */ |
1354
|
|
|
|
|
|
|
int nLimit, nOffset; /* Saved values of p->nLimit and p->nOffset */ |
1355
|
|
|
|
|
|
|
ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */ |
1356
|
|
|
|
|
|
|
|
1357
|
0
|
0
|
|
|
|
|
priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; |
1358
|
0
|
0
|
|
|
|
|
if( eDest==priorOp && p->pOrderBy==0 && p->nLimit<0 && p->nOffset==0 ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1359
|
|
|
|
|
|
|
/* We can reuse a temporary table generated by a SELECT to our |
1360
|
|
|
|
|
|
|
** right. |
1361
|
|
|
|
|
|
|
*/ |
1362
|
0
|
|
|
|
|
|
unionTab = iParm; |
1363
|
|
|
|
|
|
|
}else{ |
1364
|
|
|
|
|
|
|
/* We will need to create our own temporary table to hold the |
1365
|
|
|
|
|
|
|
** intermediate results. |
1366
|
|
|
|
|
|
|
*/ |
1367
|
0
|
|
|
|
|
|
unionTab = pParse->nTab++; |
1368
|
0
|
0
|
|
|
|
|
if( p->pOrderBy |
1369
|
0
|
0
|
|
|
|
|
&& matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ |
1370
|
0
|
|
|
|
|
|
return 1; |
1371
|
|
|
|
|
|
|
} |
1372
|
0
|
0
|
|
|
|
|
if( p->op!=TK_ALL ){ |
1373
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1); |
1374
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1); |
1375
|
|
|
|
|
|
|
}else{ |
1376
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0); |
1377
|
|
|
|
|
|
|
} |
1378
|
|
|
|
|
|
|
} |
1379
|
|
|
|
|
|
|
|
1380
|
|
|
|
|
|
|
/* Code the SELECT statements to our left |
1381
|
|
|
|
|
|
|
*/ |
1382
|
0
|
|
|
|
|
|
rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0); |
1383
|
0
|
0
|
|
|
|
|
if( rc ) return rc; |
1384
|
|
|
|
|
|
|
|
1385
|
|
|
|
|
|
|
/* Code the current SELECT statement |
1386
|
|
|
|
|
|
|
*/ |
1387
|
0
|
|
|
|
|
|
switch( p->op ){ |
1388
|
0
|
|
|
|
|
|
case TK_EXCEPT: op = SRT_Except; break; |
1389
|
0
|
|
|
|
|
|
case TK_UNION: op = SRT_Union; break; |
1390
|
0
|
|
|
|
|
|
case TK_ALL: op = SRT_Table; break; |
1391
|
|
|
|
|
|
|
} |
1392
|
0
|
|
|
|
|
|
p->pPrior = 0; |
1393
|
0
|
|
|
|
|
|
pOrderBy = p->pOrderBy; |
1394
|
0
|
|
|
|
|
|
p->pOrderBy = 0; |
1395
|
0
|
|
|
|
|
|
nLimit = p->nLimit; |
1396
|
0
|
|
|
|
|
|
p->nLimit = -1; |
1397
|
0
|
|
|
|
|
|
nOffset = p->nOffset; |
1398
|
0
|
|
|
|
|
|
p->nOffset = 0; |
1399
|
0
|
|
|
|
|
|
rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0); |
1400
|
0
|
|
|
|
|
|
p->pPrior = pPrior; |
1401
|
0
|
|
|
|
|
|
p->pOrderBy = pOrderBy; |
1402
|
0
|
|
|
|
|
|
p->nLimit = nLimit; |
1403
|
0
|
|
|
|
|
|
p->nOffset = nOffset; |
1404
|
0
|
0
|
|
|
|
|
if( rc ) return rc; |
1405
|
|
|
|
|
|
|
|
1406
|
|
|
|
|
|
|
/* Convert the data in the temporary table into whatever form |
1407
|
|
|
|
|
|
|
** it is that we currently need. |
1408
|
|
|
|
|
|
|
*/ |
1409
|
0
|
0
|
|
|
|
|
if( eDest!=priorOp || unionTab!=iParm ){ |
|
|
0
|
|
|
|
|
|
1410
|
|
|
|
|
|
|
int iCont, iBreak, iStart; |
1411
|
|
|
|
|
|
|
assert( p->pEList ); |
1412
|
0
|
0
|
|
|
|
|
if( eDest==SRT_Callback ){ |
1413
|
0
|
|
|
|
|
|
generateColumnNames(pParse, 0, p->pEList); |
1414
|
0
|
|
|
|
|
|
generateColumnTypes(pParse, p->pSrc, p->pEList); |
1415
|
|
|
|
|
|
|
} |
1416
|
0
|
|
|
|
|
|
iBreak = sqliteVdbeMakeLabel(v); |
1417
|
0
|
|
|
|
|
|
iCont = sqliteVdbeMakeLabel(v); |
1418
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak); |
1419
|
0
|
|
|
|
|
|
computeLimitRegisters(pParse, p); |
1420
|
0
|
|
|
|
|
|
iStart = sqliteVdbeCurrentAddr(v); |
1421
|
0
|
|
|
|
|
|
multiSelectSortOrder(p, p->pOrderBy); |
1422
|
0
|
|
|
|
|
|
rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, |
1423
|
|
|
|
|
|
|
p->pOrderBy, -1, eDest, iParm, |
1424
|
|
|
|
|
|
|
iCont, iBreak); |
1425
|
0
|
0
|
|
|
|
|
if( rc ) return 1; |
1426
|
0
|
|
|
|
|
|
sqliteVdbeResolveLabel(v, iCont); |
1427
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Next, unionTab, iStart); |
1428
|
0
|
|
|
|
|
|
sqliteVdbeResolveLabel(v, iBreak); |
1429
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Close, unionTab, 0); |
1430
|
0
|
0
|
|
|
|
|
if( p->pOrderBy ){ |
1431
|
0
|
|
|
|
|
|
generateSortTail(p, v, p->pEList->nExpr, eDest, iParm); |
1432
|
|
|
|
|
|
|
} |
1433
|
|
|
|
|
|
|
} |
1434
|
0
|
|
|
|
|
|
break; |
1435
|
|
|
|
|
|
|
} |
1436
|
|
|
|
|
|
|
case TK_INTERSECT: { |
1437
|
|
|
|
|
|
|
int tab1, tab2; |
1438
|
|
|
|
|
|
|
int iCont, iBreak, iStart; |
1439
|
|
|
|
|
|
|
int nLimit, nOffset; |
1440
|
|
|
|
|
|
|
|
1441
|
|
|
|
|
|
|
/* INTERSECT is different from the others since it requires |
1442
|
|
|
|
|
|
|
** two temporary tables. Hence it has its own case. Begin |
1443
|
|
|
|
|
|
|
** by allocating the tables we will need. |
1444
|
|
|
|
|
|
|
*/ |
1445
|
0
|
|
|
|
|
|
tab1 = pParse->nTab++; |
1446
|
0
|
|
|
|
|
|
tab2 = pParse->nTab++; |
1447
|
0
|
0
|
|
|
|
|
if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ |
|
|
0
|
|
|
|
|
|
1448
|
0
|
|
|
|
|
|
return 1; |
1449
|
|
|
|
|
|
|
} |
1450
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1); |
1451
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1); |
1452
|
|
|
|
|
|
|
|
1453
|
|
|
|
|
|
|
/* Code the SELECTs to our left into temporary table "tab1". |
1454
|
|
|
|
|
|
|
*/ |
1455
|
0
|
|
|
|
|
|
rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0); |
1456
|
0
|
0
|
|
|
|
|
if( rc ) return rc; |
1457
|
|
|
|
|
|
|
|
1458
|
|
|
|
|
|
|
/* Code the current SELECT into temporary table "tab2" |
1459
|
|
|
|
|
|
|
*/ |
1460
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1); |
1461
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1); |
1462
|
0
|
|
|
|
|
|
p->pPrior = 0; |
1463
|
0
|
|
|
|
|
|
nLimit = p->nLimit; |
1464
|
0
|
|
|
|
|
|
p->nLimit = -1; |
1465
|
0
|
|
|
|
|
|
nOffset = p->nOffset; |
1466
|
0
|
|
|
|
|
|
p->nOffset = 0; |
1467
|
0
|
|
|
|
|
|
rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0); |
1468
|
0
|
|
|
|
|
|
p->pPrior = pPrior; |
1469
|
0
|
|
|
|
|
|
p->nLimit = nLimit; |
1470
|
0
|
|
|
|
|
|
p->nOffset = nOffset; |
1471
|
0
|
0
|
|
|
|
|
if( rc ) return rc; |
1472
|
|
|
|
|
|
|
|
1473
|
|
|
|
|
|
|
/* Generate code to take the intersection of the two temporary |
1474
|
|
|
|
|
|
|
** tables. |
1475
|
|
|
|
|
|
|
*/ |
1476
|
|
|
|
|
|
|
assert( p->pEList ); |
1477
|
0
|
0
|
|
|
|
|
if( eDest==SRT_Callback ){ |
1478
|
0
|
|
|
|
|
|
generateColumnNames(pParse, 0, p->pEList); |
1479
|
0
|
|
|
|
|
|
generateColumnTypes(pParse, p->pSrc, p->pEList); |
1480
|
|
|
|
|
|
|
} |
1481
|
0
|
|
|
|
|
|
iBreak = sqliteVdbeMakeLabel(v); |
1482
|
0
|
|
|
|
|
|
iCont = sqliteVdbeMakeLabel(v); |
1483
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak); |
1484
|
0
|
|
|
|
|
|
computeLimitRegisters(pParse, p); |
1485
|
0
|
|
|
|
|
|
iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0); |
1486
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont); |
1487
|
0
|
|
|
|
|
|
multiSelectSortOrder(p, p->pOrderBy); |
1488
|
0
|
|
|
|
|
|
rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, |
1489
|
|
|
|
|
|
|
p->pOrderBy, -1, eDest, iParm, |
1490
|
|
|
|
|
|
|
iCont, iBreak); |
1491
|
0
|
0
|
|
|
|
|
if( rc ) return 1; |
1492
|
0
|
|
|
|
|
|
sqliteVdbeResolveLabel(v, iCont); |
1493
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Next, tab1, iStart); |
1494
|
0
|
|
|
|
|
|
sqliteVdbeResolveLabel(v, iBreak); |
1495
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Close, tab2, 0); |
1496
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Close, tab1, 0); |
1497
|
0
|
0
|
|
|
|
|
if( p->pOrderBy ){ |
1498
|
0
|
|
|
|
|
|
generateSortTail(p, v, p->pEList->nExpr, eDest, iParm); |
1499
|
|
|
|
|
|
|
} |
1500
|
0
|
|
|
|
|
|
break; |
1501
|
|
|
|
|
|
|
} |
1502
|
|
|
|
|
|
|
} |
1503
|
|
|
|
|
|
|
assert( p->pEList && pPrior->pEList ); |
1504
|
9
|
50
|
|
|
|
|
if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ |
1505
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, "SELECTs to the left and right of %s" |
1506
|
0
|
|
|
|
|
|
" do not have the same number of result columns", selectOpName(p->op)); |
1507
|
0
|
|
|
|
|
|
return 1; |
1508
|
|
|
|
|
|
|
} |
1509
|
9
|
|
|
|
|
|
return 0; |
1510
|
|
|
|
|
|
|
} |
1511
|
|
|
|
|
|
|
|
1512
|
|
|
|
|
|
|
/* |
1513
|
|
|
|
|
|
|
** Scan through the expression pExpr. Replace every reference to |
1514
|
|
|
|
|
|
|
** a column in table number iTable with a copy of the iColumn-th |
1515
|
|
|
|
|
|
|
** entry in pEList. (But leave references to the ROWID column |
1516
|
|
|
|
|
|
|
** unchanged.) |
1517
|
|
|
|
|
|
|
** |
1518
|
|
|
|
|
|
|
** This routine is part of the flattening procedure. A subquery |
1519
|
|
|
|
|
|
|
** whose result set is defined by pEList appears as entry in the |
1520
|
|
|
|
|
|
|
** FROM clause of a SELECT such that the VDBE cursor assigned to that |
1521
|
|
|
|
|
|
|
** FORM clause entry is iTable. This routine make the necessary |
1522
|
|
|
|
|
|
|
** changes to pExpr so that it refers directly to the source table |
1523
|
|
|
|
|
|
|
** of the subquery rather the result set of the subquery. |
1524
|
|
|
|
|
|
|
*/ |
1525
|
|
|
|
|
|
|
static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */ |
1526
|
48
|
|
|
|
|
|
static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ |
1527
|
48
|
100
|
|
|
|
|
if( pExpr==0 ) return; |
1528
|
36
|
100
|
|
|
|
|
if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ |
|
|
50
|
|
|
|
|
|
1529
|
60
|
50
|
|
|
|
|
if( pExpr->iColumn<0 ){ |
1530
|
0
|
|
|
|
|
|
pExpr->op = TK_NULL; |
1531
|
|
|
|
|
|
|
}else{ |
1532
|
|
|
|
|
|
|
Expr *pNew; |
1533
|
|
|
|
|
|
|
assert( pEList!=0 && pExpr->iColumnnExpr ); |
1534
|
|
|
|
|
|
|
assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); |
1535
|
30
|
|
|
|
|
|
pNew = pEList->a[pExpr->iColumn].pExpr; |
1536
|
|
|
|
|
|
|
assert( pNew!=0 ); |
1537
|
30
|
|
|
|
|
|
pExpr->op = pNew->op; |
1538
|
30
|
|
|
|
|
|
pExpr->dataType = pNew->dataType; |
1539
|
|
|
|
|
|
|
assert( pExpr->pLeft==0 ); |
1540
|
30
|
|
|
|
|
|
pExpr->pLeft = sqliteExprDup(pNew->pLeft); |
1541
|
|
|
|
|
|
|
assert( pExpr->pRight==0 ); |
1542
|
30
|
|
|
|
|
|
pExpr->pRight = sqliteExprDup(pNew->pRight); |
1543
|
|
|
|
|
|
|
assert( pExpr->pList==0 ); |
1544
|
30
|
|
|
|
|
|
pExpr->pList = sqliteExprListDup(pNew->pList); |
1545
|
30
|
|
|
|
|
|
pExpr->iTable = pNew->iTable; |
1546
|
30
|
|
|
|
|
|
pExpr->iColumn = pNew->iColumn; |
1547
|
30
|
|
|
|
|
|
pExpr->iAgg = pNew->iAgg; |
1548
|
30
|
|
|
|
|
|
sqliteTokenCopy(&pExpr->token, &pNew->token); |
1549
|
30
|
|
|
|
|
|
sqliteTokenCopy(&pExpr->span, &pNew->span); |
1550
|
|
|
|
|
|
|
} |
1551
|
|
|
|
|
|
|
}else{ |
1552
|
6
|
|
|
|
|
|
substExpr(pExpr->pLeft, iTable, pEList); |
1553
|
6
|
|
|
|
|
|
substExpr(pExpr->pRight, iTable, pEList); |
1554
|
6
|
|
|
|
|
|
substExprList(pExpr->pList, iTable, pEList); |
1555
|
|
|
|
|
|
|
} |
1556
|
|
|
|
|
|
|
} |
1557
|
|
|
|
|
|
|
static void |
1558
|
12
|
|
|
|
|
|
substExprList(ExprList *pList, int iTable, ExprList *pEList){ |
1559
|
|
|
|
|
|
|
int i; |
1560
|
12
|
100
|
|
|
|
|
if( pList==0 ) return; |
1561
|
42
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
1562
|
33
|
|
|
|
|
|
substExpr(pList->a[i].pExpr, iTable, pEList); |
1563
|
|
|
|
|
|
|
} |
1564
|
|
|
|
|
|
|
} |
1565
|
|
|
|
|
|
|
|
1566
|
|
|
|
|
|
|
/* |
1567
|
|
|
|
|
|
|
** This routine attempts to flatten subqueries in order to speed |
1568
|
|
|
|
|
|
|
** execution. It returns 1 if it makes changes and 0 if no flattening |
1569
|
|
|
|
|
|
|
** occurs. |
1570
|
|
|
|
|
|
|
** |
1571
|
|
|
|
|
|
|
** To understand the concept of flattening, consider the following |
1572
|
|
|
|
|
|
|
** query: |
1573
|
|
|
|
|
|
|
** |
1574
|
|
|
|
|
|
|
** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 |
1575
|
|
|
|
|
|
|
** |
1576
|
|
|
|
|
|
|
** The default way of implementing this query is to execute the |
1577
|
|
|
|
|
|
|
** subquery first and store the results in a temporary table, then |
1578
|
|
|
|
|
|
|
** run the outer query on that temporary table. This requires two |
1579
|
|
|
|
|
|
|
** passes over the data. Furthermore, because the temporary table |
1580
|
|
|
|
|
|
|
** has no indices, the WHERE clause on the outer query cannot be |
1581
|
|
|
|
|
|
|
** optimized. |
1582
|
|
|
|
|
|
|
** |
1583
|
|
|
|
|
|
|
** This routine attempts to rewrite queries such as the above into |
1584
|
|
|
|
|
|
|
** a single flat select, like this: |
1585
|
|
|
|
|
|
|
** |
1586
|
|
|
|
|
|
|
** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 |
1587
|
|
|
|
|
|
|
** |
1588
|
|
|
|
|
|
|
** The code generated for this simpification gives the same result |
1589
|
|
|
|
|
|
|
** but only has to scan the data once. And because indices might |
1590
|
|
|
|
|
|
|
** exist on the table t1, a complete scan of the data might be |
1591
|
|
|
|
|
|
|
** avoided. |
1592
|
|
|
|
|
|
|
** |
1593
|
|
|
|
|
|
|
** Flattening is only attempted if all of the following are true: |
1594
|
|
|
|
|
|
|
** |
1595
|
|
|
|
|
|
|
** (1) The subquery and the outer query do not both use aggregates. |
1596
|
|
|
|
|
|
|
** |
1597
|
|
|
|
|
|
|
** (2) The subquery is not an aggregate or the outer query is not a join. |
1598
|
|
|
|
|
|
|
** |
1599
|
|
|
|
|
|
|
** (3) The subquery is not the right operand of a left outer join, or |
1600
|
|
|
|
|
|
|
** the subquery is not itself a join. (Ticket #306) |
1601
|
|
|
|
|
|
|
** |
1602
|
|
|
|
|
|
|
** (4) The subquery is not DISTINCT or the outer query is not a join. |
1603
|
|
|
|
|
|
|
** |
1604
|
|
|
|
|
|
|
** (5) The subquery is not DISTINCT or the outer query does not use |
1605
|
|
|
|
|
|
|
** aggregates. |
1606
|
|
|
|
|
|
|
** |
1607
|
|
|
|
|
|
|
** (6) The subquery does not use aggregates or the outer query is not |
1608
|
|
|
|
|
|
|
** DISTINCT. |
1609
|
|
|
|
|
|
|
** |
1610
|
|
|
|
|
|
|
** (7) The subquery has a FROM clause. |
1611
|
|
|
|
|
|
|
** |
1612
|
|
|
|
|
|
|
** (8) The subquery does not use LIMIT or the outer query is not a join. |
1613
|
|
|
|
|
|
|
** |
1614
|
|
|
|
|
|
|
** (9) The subquery does not use LIMIT or the outer query does not use |
1615
|
|
|
|
|
|
|
** aggregates. |
1616
|
|
|
|
|
|
|
** |
1617
|
|
|
|
|
|
|
** (10) The subquery does not use aggregates or the outer query does not |
1618
|
|
|
|
|
|
|
** use LIMIT. |
1619
|
|
|
|
|
|
|
** |
1620
|
|
|
|
|
|
|
** (11) The subquery and the outer query do not both have ORDER BY clauses. |
1621
|
|
|
|
|
|
|
** |
1622
|
|
|
|
|
|
|
** (12) The subquery is not the right term of a LEFT OUTER JOIN or the |
1623
|
|
|
|
|
|
|
** subquery has no WHERE clause. (added by ticket #350) |
1624
|
|
|
|
|
|
|
** |
1625
|
|
|
|
|
|
|
** In this routine, the "p" parameter is a pointer to the outer query. |
1626
|
|
|
|
|
|
|
** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query |
1627
|
|
|
|
|
|
|
** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. |
1628
|
|
|
|
|
|
|
** |
1629
|
|
|
|
|
|
|
** If flattening is not attempted, this routine is a no-op and returns 0. |
1630
|
|
|
|
|
|
|
** If flattening is attempted this routine returns 1. |
1631
|
|
|
|
|
|
|
** |
1632
|
|
|
|
|
|
|
** All of the expression analysis must occur on both the outer query and |
1633
|
|
|
|
|
|
|
** the subquery before this routine runs. |
1634
|
|
|
|
|
|
|
*/ |
1635
|
3
|
|
|
|
|
|
static int flattenSubquery( |
1636
|
|
|
|
|
|
|
Parse *pParse, /* The parsing context */ |
1637
|
|
|
|
|
|
|
Select *p, /* The parent or outer SELECT statement */ |
1638
|
|
|
|
|
|
|
int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ |
1639
|
|
|
|
|
|
|
int isAgg, /* True if outer SELECT uses aggregate functions */ |
1640
|
|
|
|
|
|
|
int subqueryIsAgg /* True if the subquery uses aggregate functions */ |
1641
|
|
|
|
|
|
|
){ |
1642
|
|
|
|
|
|
|
Select *pSub; /* The inner query or "subquery" */ |
1643
|
|
|
|
|
|
|
SrcList *pSrc; /* The FROM clause of the outer query */ |
1644
|
|
|
|
|
|
|
SrcList *pSubSrc; /* The FROM clause of the subquery */ |
1645
|
|
|
|
|
|
|
ExprList *pList; /* The result set of the outer query */ |
1646
|
|
|
|
|
|
|
int iParent; /* VDBE cursor number of the pSub result set temp table */ |
1647
|
|
|
|
|
|
|
int i; |
1648
|
|
|
|
|
|
|
Expr *pWhere; |
1649
|
|
|
|
|
|
|
|
1650
|
|
|
|
|
|
|
/* Check to see if flattening is permitted. Return 0 if not. |
1651
|
|
|
|
|
|
|
*/ |
1652
|
3
|
50
|
|
|
|
|
if( p==0 ) return 0; |
1653
|
3
|
|
|
|
|
|
pSrc = p->pSrc; |
1654
|
|
|
|
|
|
|
assert( pSrc && iFrom>=0 && iFromnSrc ); |
1655
|
3
|
|
|
|
|
|
pSub = pSrc->a[iFrom].pSelect; |
1656
|
|
|
|
|
|
|
assert( pSub!=0 ); |
1657
|
3
|
50
|
|
|
|
|
if( isAgg && subqueryIsAgg ) return 0; |
|
|
0
|
|
|
|
|
|
1658
|
3
|
50
|
|
|
|
|
if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; |
|
|
0
|
|
|
|
|
|
1659
|
3
|
|
|
|
|
|
pSubSrc = pSub->pSrc; |
1660
|
|
|
|
|
|
|
assert( pSubSrc ); |
1661
|
3
|
50
|
|
|
|
|
if( pSubSrc->nSrc==0 ) return 0; |
1662
|
3
|
50
|
|
|
|
|
if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){ |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1663
|
0
|
|
|
|
|
|
return 0; |
1664
|
|
|
|
|
|
|
} |
1665
|
3
|
50
|
|
|
|
|
if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0; |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1666
|
3
|
50
|
|
|
|
|
if( p->pOrderBy && pSub->pOrderBy ) return 0; |
|
|
50
|
|
|
|
|
|
1667
|
|
|
|
|
|
|
|
1668
|
|
|
|
|
|
|
/* Restriction 3: If the subquery is a join, make sure the subquery is |
1669
|
|
|
|
|
|
|
** not used as the right operand of an outer join. Examples of why this |
1670
|
|
|
|
|
|
|
** is not allowed: |
1671
|
|
|
|
|
|
|
** |
1672
|
|
|
|
|
|
|
** t1 LEFT OUTER JOIN (t2 JOIN t3) |
1673
|
|
|
|
|
|
|
** |
1674
|
|
|
|
|
|
|
** If we flatten the above, we would get |
1675
|
|
|
|
|
|
|
** |
1676
|
|
|
|
|
|
|
** (t1 LEFT OUTER JOIN t2) JOIN t3 |
1677
|
|
|
|
|
|
|
** |
1678
|
|
|
|
|
|
|
** which is not at all the same thing. |
1679
|
|
|
|
|
|
|
*/ |
1680
|
3
|
50
|
|
|
|
|
if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1681
|
0
|
|
|
|
|
|
return 0; |
1682
|
|
|
|
|
|
|
} |
1683
|
|
|
|
|
|
|
|
1684
|
|
|
|
|
|
|
/* Restriction 12: If the subquery is the right operand of a left outer |
1685
|
|
|
|
|
|
|
** join, make sure the subquery has no WHERE clause. |
1686
|
|
|
|
|
|
|
** An examples of why this is not allowed: |
1687
|
|
|
|
|
|
|
** |
1688
|
|
|
|
|
|
|
** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) |
1689
|
|
|
|
|
|
|
** |
1690
|
|
|
|
|
|
|
** If we flatten the above, we would get |
1691
|
|
|
|
|
|
|
** |
1692
|
|
|
|
|
|
|
** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 |
1693
|
|
|
|
|
|
|
** |
1694
|
|
|
|
|
|
|
** But the t2.x>0 test will always fail on a NULL row of t2, which |
1695
|
|
|
|
|
|
|
** effectively converts the OUTER JOIN into an INNER JOIN. |
1696
|
|
|
|
|
|
|
*/ |
1697
|
3
|
50
|
|
|
|
|
if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 |
|
|
0
|
|
|
|
|
|
1698
|
0
|
0
|
|
|
|
|
&& pSub->pWhere!=0 ){ |
1699
|
0
|
|
|
|
|
|
return 0; |
1700
|
|
|
|
|
|
|
} |
1701
|
|
|
|
|
|
|
|
1702
|
|
|
|
|
|
|
/* If we reach this point, it means flattening is permitted for the |
1703
|
|
|
|
|
|
|
** iFrom-th entry of the FROM clause in the outer query. |
1704
|
|
|
|
|
|
|
*/ |
1705
|
|
|
|
|
|
|
|
1706
|
|
|
|
|
|
|
/* Move all of the FROM elements of the subquery into the |
1707
|
|
|
|
|
|
|
** the FROM clause of the outer query. Before doing this, remember |
1708
|
|
|
|
|
|
|
** the cursor number for the original outer query FROM element in |
1709
|
|
|
|
|
|
|
** iParent. The iParent cursor will never be used. Subsequent code |
1710
|
|
|
|
|
|
|
** will scan expressions looking for iParent references and replace |
1711
|
|
|
|
|
|
|
** those references with expressions that resolve to the subquery FROM |
1712
|
|
|
|
|
|
|
** elements we are now copying in. |
1713
|
|
|
|
|
|
|
*/ |
1714
|
3
|
|
|
|
|
|
iParent = pSrc->a[iFrom].iCursor; |
1715
|
|
|
|
|
|
|
{ |
1716
|
3
|
|
|
|
|
|
int nSubSrc = pSubSrc->nSrc; |
1717
|
3
|
|
|
|
|
|
int jointype = pSrc->a[iFrom].jointype; |
1718
|
|
|
|
|
|
|
|
1719
|
3
|
50
|
|
|
|
|
if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){ |
|
|
50
|
|
|
|
|
|
1720
|
3
|
|
|
|
|
|
sqliteDeleteTable(0, pSrc->a[iFrom].pTab); |
1721
|
|
|
|
|
|
|
} |
1722
|
3
|
|
|
|
|
|
sqliteFree(pSrc->a[iFrom].zDatabase); |
1723
|
3
|
|
|
|
|
|
sqliteFree(pSrc->a[iFrom].zName); |
1724
|
3
|
|
|
|
|
|
sqliteFree(pSrc->a[iFrom].zAlias); |
1725
|
3
|
50
|
|
|
|
|
if( nSubSrc>1 ){ |
1726
|
0
|
|
|
|
|
|
int extra = nSubSrc - 1; |
1727
|
0
|
0
|
|
|
|
|
for(i=1; i
|
1728
|
0
|
|
|
|
|
|
pSrc = sqliteSrcListAppend(pSrc, 0, 0); |
1729
|
|
|
|
|
|
|
} |
1730
|
0
|
|
|
|
|
|
p->pSrc = pSrc; |
1731
|
0
|
0
|
|
|
|
|
for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){ |
1732
|
0
|
|
|
|
|
|
pSrc->a[i] = pSrc->a[i-extra]; |
1733
|
|
|
|
|
|
|
} |
1734
|
|
|
|
|
|
|
} |
1735
|
6
|
100
|
|
|
|
|
for(i=0; i
|
1736
|
3
|
|
|
|
|
|
pSrc->a[i+iFrom] = pSubSrc->a[i]; |
1737
|
3
|
|
|
|
|
|
memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); |
1738
|
|
|
|
|
|
|
} |
1739
|
3
|
|
|
|
|
|
pSrc->a[iFrom+nSubSrc-1].jointype = jointype; |
1740
|
|
|
|
|
|
|
} |
1741
|
|
|
|
|
|
|
|
1742
|
|
|
|
|
|
|
/* Now begin substituting subquery result set expressions for |
1743
|
|
|
|
|
|
|
** references to the iParent in the outer query. |
1744
|
|
|
|
|
|
|
** |
1745
|
|
|
|
|
|
|
** Example: |
1746
|
|
|
|
|
|
|
** |
1747
|
|
|
|
|
|
|
** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; |
1748
|
|
|
|
|
|
|
** \ \_____________ subquery __________/ / |
1749
|
|
|
|
|
|
|
** \_____________________ outer query ______________________________/ |
1750
|
|
|
|
|
|
|
** |
1751
|
|
|
|
|
|
|
** We look at every expression in the outer query and every place we see |
1752
|
|
|
|
|
|
|
** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". |
1753
|
|
|
|
|
|
|
*/ |
1754
|
3
|
|
|
|
|
|
substExprList(p->pEList, iParent, pSub->pEList); |
1755
|
3
|
|
|
|
|
|
pList = p->pEList; |
1756
|
21
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
1757
|
|
|
|
|
|
|
Expr *pExpr; |
1758
|
18
|
50
|
|
|
|
|
if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){ |
|
|
50
|
|
|
|
|
|
1759
|
18
|
|
|
|
|
|
pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n); |
1760
|
|
|
|
|
|
|
} |
1761
|
|
|
|
|
|
|
} |
1762
|
3
|
50
|
|
|
|
|
if( isAgg ){ |
1763
|
0
|
|
|
|
|
|
substExprList(p->pGroupBy, iParent, pSub->pEList); |
1764
|
0
|
|
|
|
|
|
substExpr(p->pHaving, iParent, pSub->pEList); |
1765
|
|
|
|
|
|
|
} |
1766
|
3
|
50
|
|
|
|
|
if( pSub->pOrderBy ){ |
1767
|
|
|
|
|
|
|
assert( p->pOrderBy==0 ); |
1768
|
0
|
|
|
|
|
|
p->pOrderBy = pSub->pOrderBy; |
1769
|
0
|
|
|
|
|
|
pSub->pOrderBy = 0; |
1770
|
3
|
50
|
|
|
|
|
}else if( p->pOrderBy ){ |
1771
|
3
|
|
|
|
|
|
substExprList(p->pOrderBy, iParent, pSub->pEList); |
1772
|
|
|
|
|
|
|
} |
1773
|
3
|
50
|
|
|
|
|
if( pSub->pWhere ){ |
1774
|
0
|
|
|
|
|
|
pWhere = sqliteExprDup(pSub->pWhere); |
1775
|
|
|
|
|
|
|
}else{ |
1776
|
3
|
|
|
|
|
|
pWhere = 0; |
1777
|
|
|
|
|
|
|
} |
1778
|
3
|
50
|
|
|
|
|
if( subqueryIsAgg ){ |
1779
|
|
|
|
|
|
|
assert( p->pHaving==0 ); |
1780
|
0
|
|
|
|
|
|
p->pHaving = p->pWhere; |
1781
|
0
|
|
|
|
|
|
p->pWhere = pWhere; |
1782
|
0
|
|
|
|
|
|
substExpr(p->pHaving, iParent, pSub->pEList); |
1783
|
0
|
0
|
|
|
|
|
if( pSub->pHaving ){ |
1784
|
0
|
|
|
|
|
|
Expr *pHaving = sqliteExprDup(pSub->pHaving); |
1785
|
0
|
0
|
|
|
|
|
if( p->pHaving ){ |
1786
|
0
|
|
|
|
|
|
p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0); |
1787
|
|
|
|
|
|
|
}else{ |
1788
|
0
|
|
|
|
|
|
p->pHaving = pHaving; |
1789
|
|
|
|
|
|
|
} |
1790
|
|
|
|
|
|
|
} |
1791
|
|
|
|
|
|
|
assert( p->pGroupBy==0 ); |
1792
|
0
|
|
|
|
|
|
p->pGroupBy = sqliteExprListDup(pSub->pGroupBy); |
1793
|
3
|
50
|
|
|
|
|
}else if( p->pWhere==0 ){ |
1794
|
0
|
|
|
|
|
|
p->pWhere = pWhere; |
1795
|
|
|
|
|
|
|
}else{ |
1796
|
3
|
|
|
|
|
|
substExpr(p->pWhere, iParent, pSub->pEList); |
1797
|
3
|
50
|
|
|
|
|
if( pWhere ){ |
1798
|
0
|
|
|
|
|
|
p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0); |
1799
|
|
|
|
|
|
|
} |
1800
|
|
|
|
|
|
|
} |
1801
|
|
|
|
|
|
|
|
1802
|
|
|
|
|
|
|
/* The flattened query is distinct if either the inner or the |
1803
|
|
|
|
|
|
|
** outer query is distinct. |
1804
|
|
|
|
|
|
|
*/ |
1805
|
3
|
50
|
|
|
|
|
p->isDistinct = p->isDistinct || pSub->isDistinct; |
|
|
50
|
|
|
|
|
|
1806
|
|
|
|
|
|
|
|
1807
|
|
|
|
|
|
|
/* Transfer the limit expression from the subquery to the outer |
1808
|
|
|
|
|
|
|
** query. |
1809
|
|
|
|
|
|
|
*/ |
1810
|
3
|
50
|
|
|
|
|
if( pSub->nLimit>=0 ){ |
1811
|
0
|
0
|
|
|
|
|
if( p->nLimit<0 ){ |
1812
|
0
|
|
|
|
|
|
p->nLimit = pSub->nLimit; |
1813
|
0
|
0
|
|
|
|
|
}else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){ |
1814
|
0
|
|
|
|
|
|
p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset; |
1815
|
|
|
|
|
|
|
} |
1816
|
|
|
|
|
|
|
} |
1817
|
3
|
|
|
|
|
|
p->nOffset += pSub->nOffset; |
1818
|
|
|
|
|
|
|
|
1819
|
|
|
|
|
|
|
/* Finially, delete what is left of the subquery and return |
1820
|
|
|
|
|
|
|
** success. |
1821
|
|
|
|
|
|
|
*/ |
1822
|
3
|
|
|
|
|
|
sqliteSelectDelete(pSub); |
1823
|
3
|
|
|
|
|
|
return 1; |
1824
|
|
|
|
|
|
|
} |
1825
|
|
|
|
|
|
|
|
1826
|
|
|
|
|
|
|
/* |
1827
|
|
|
|
|
|
|
** Analyze the SELECT statement passed in as an argument to see if it |
1828
|
|
|
|
|
|
|
** is a simple min() or max() query. If it is and this query can be |
1829
|
|
|
|
|
|
|
** satisfied using a single seek to the beginning or end of an index, |
1830
|
|
|
|
|
|
|
** then generate the code for this SELECT and return 1. If this is not a |
1831
|
|
|
|
|
|
|
** simple min() or max() query, then return 0; |
1832
|
|
|
|
|
|
|
** |
1833
|
|
|
|
|
|
|
** A simply min() or max() query looks like this: |
1834
|
|
|
|
|
|
|
** |
1835
|
|
|
|
|
|
|
** SELECT min(a) FROM table; |
1836
|
|
|
|
|
|
|
** SELECT max(a) FROM table; |
1837
|
|
|
|
|
|
|
** |
1838
|
|
|
|
|
|
|
** The query may have only a single table in its FROM argument. There |
1839
|
|
|
|
|
|
|
** can be no GROUP BY or HAVING or WHERE clauses. The result set must |
1840
|
|
|
|
|
|
|
** be the min() or max() of a single column of the table. The column |
1841
|
|
|
|
|
|
|
** in the min() or max() function must be indexed. |
1842
|
|
|
|
|
|
|
** |
1843
|
|
|
|
|
|
|
** The parameters to this routine are the same as for sqliteSelect(). |
1844
|
|
|
|
|
|
|
** See the header comment on that routine for additional information. |
1845
|
|
|
|
|
|
|
*/ |
1846
|
138
|
|
|
|
|
|
static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ |
1847
|
|
|
|
|
|
|
Expr *pExpr; |
1848
|
|
|
|
|
|
|
int iCol; |
1849
|
|
|
|
|
|
|
Table *pTab; |
1850
|
|
|
|
|
|
|
Index *pIdx; |
1851
|
|
|
|
|
|
|
int base; |
1852
|
|
|
|
|
|
|
Vdbe *v; |
1853
|
|
|
|
|
|
|
int seekOp; |
1854
|
|
|
|
|
|
|
int cont; |
1855
|
|
|
|
|
|
|
ExprList *pEList, *pList, eList; |
1856
|
|
|
|
|
|
|
struct ExprList_item eListItem; |
1857
|
|
|
|
|
|
|
SrcList *pSrc; |
1858
|
|
|
|
|
|
|
|
1859
|
|
|
|
|
|
|
|
1860
|
|
|
|
|
|
|
/* Check to see if this query is a simple min() or max() query. Return |
1861
|
|
|
|
|
|
|
** zero if it is not. |
1862
|
|
|
|
|
|
|
*/ |
1863
|
138
|
100
|
|
|
|
|
if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
1864
|
110
|
|
|
|
|
|
pSrc = p->pSrc; |
1865
|
110
|
100
|
|
|
|
|
if( pSrc->nSrc!=1 ) return 0; |
1866
|
87
|
|
|
|
|
|
pEList = p->pEList; |
1867
|
87
|
100
|
|
|
|
|
if( pEList->nExpr!=1 ) return 0; |
1868
|
14
|
|
|
|
|
|
pExpr = pEList->a[0].pExpr; |
1869
|
14
|
100
|
|
|
|
|
if( pExpr->op!=TK_AGG_FUNCTION ) return 0; |
1870
|
13
|
|
|
|
|
|
pList = pExpr->pList; |
1871
|
13
|
50
|
|
|
|
|
if( pList==0 || pList->nExpr!=1 ) return 0; |
|
|
100
|
|
|
|
|
|
1872
|
4
|
50
|
|
|
|
|
if( pExpr->token.n!=3 ) return 0; |
1873
|
0
|
0
|
|
|
|
|
if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){ |
1874
|
0
|
|
|
|
|
|
seekOp = OP_Rewind; |
1875
|
0
|
0
|
|
|
|
|
}else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){ |
1876
|
0
|
|
|
|
|
|
seekOp = OP_Last; |
1877
|
|
|
|
|
|
|
}else{ |
1878
|
0
|
|
|
|
|
|
return 0; |
1879
|
|
|
|
|
|
|
} |
1880
|
0
|
|
|
|
|
|
pExpr = pList->a[0].pExpr; |
1881
|
0
|
0
|
|
|
|
|
if( pExpr->op!=TK_COLUMN ) return 0; |
1882
|
0
|
|
|
|
|
|
iCol = pExpr->iColumn; |
1883
|
0
|
|
|
|
|
|
pTab = pSrc->a[0].pTab; |
1884
|
|
|
|
|
|
|
|
1885
|
|
|
|
|
|
|
/* If we get to here, it means the query is of the correct form. |
1886
|
|
|
|
|
|
|
** Check to make sure we have an index and make pIdx point to the |
1887
|
|
|
|
|
|
|
** appropriate index. If the min() or max() is on an INTEGER PRIMARY |
1888
|
|
|
|
|
|
|
** key column, no index is necessary so set pIdx to NULL. If no |
1889
|
|
|
|
|
|
|
** usable index is found, return 0. |
1890
|
|
|
|
|
|
|
*/ |
1891
|
0
|
0
|
|
|
|
|
if( iCol<0 ){ |
1892
|
0
|
|
|
|
|
|
pIdx = 0; |
1893
|
|
|
|
|
|
|
}else{ |
1894
|
0
|
0
|
|
|
|
|
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
1895
|
|
|
|
|
|
|
assert( pIdx->nColumn>=1 ); |
1896
|
0
|
0
|
|
|
|
|
if( pIdx->aiColumn[0]==iCol ) break; |
1897
|
|
|
|
|
|
|
} |
1898
|
0
|
0
|
|
|
|
|
if( pIdx==0 ) return 0; |
1899
|
|
|
|
|
|
|
} |
1900
|
|
|
|
|
|
|
|
1901
|
|
|
|
|
|
|
/* Identify column types if we will be using the callback. This |
1902
|
|
|
|
|
|
|
** step is skipped if the output is going to a table or a memory cell. |
1903
|
|
|
|
|
|
|
** The column names have already been generated in the calling function. |
1904
|
|
|
|
|
|
|
*/ |
1905
|
0
|
|
|
|
|
|
v = sqliteGetVdbe(pParse); |
1906
|
0
|
0
|
|
|
|
|
if( v==0 ) return 0; |
1907
|
0
|
0
|
|
|
|
|
if( eDest==SRT_Callback ){ |
1908
|
0
|
|
|
|
|
|
generateColumnTypes(pParse, p->pSrc, p->pEList); |
1909
|
|
|
|
|
|
|
} |
1910
|
|
|
|
|
|
|
|
1911
|
|
|
|
|
|
|
/* If the output is destined for a temporary table, open that table. |
1912
|
|
|
|
|
|
|
*/ |
1913
|
0
|
0
|
|
|
|
|
if( eDest==SRT_TempTable ){ |
1914
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0); |
1915
|
|
|
|
|
|
|
} |
1916
|
|
|
|
|
|
|
|
1917
|
|
|
|
|
|
|
/* Generating code to find the min or the max. Basically all we have |
1918
|
|
|
|
|
|
|
** to do is find the first or the last entry in the chosen index. If |
1919
|
|
|
|
|
|
|
** the min() or max() is on the INTEGER PRIMARY KEY, then find the first |
1920
|
|
|
|
|
|
|
** or last entry in the main table. |
1921
|
|
|
|
|
|
|
*/ |
1922
|
0
|
|
|
|
|
|
sqliteCodeVerifySchema(pParse, pTab->iDb); |
1923
|
0
|
|
|
|
|
|
base = pSrc->a[0].iCursor; |
1924
|
0
|
|
|
|
|
|
computeLimitRegisters(pParse, p); |
1925
|
0
|
0
|
|
|
|
|
if( pSrc->a[0].pSelect==0 ){ |
1926
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
1927
|
0
|
|
|
|
|
|
sqliteVdbeOp3(v, OP_OpenRead, base, pTab->tnum, pTab->zName, 0); |
1928
|
|
|
|
|
|
|
} |
1929
|
0
|
|
|
|
|
|
cont = sqliteVdbeMakeLabel(v); |
1930
|
0
|
0
|
|
|
|
|
if( pIdx==0 ){ |
1931
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, seekOp, base, 0); |
1932
|
|
|
|
|
|
|
}else{ |
1933
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0); |
1934
|
0
|
|
|
|
|
|
sqliteVdbeOp3(v, OP_OpenRead, base+1, pIdx->tnum, pIdx->zName, P3_STATIC); |
1935
|
0
|
0
|
|
|
|
|
if( seekOp==OP_Rewind ){ |
1936
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_String, 0, 0); |
1937
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MakeKey, 1, 0); |
1938
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
1939
|
0
|
|
|
|
|
|
seekOp = OP_MoveTo; |
1940
|
|
|
|
|
|
|
} |
1941
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, seekOp, base+1, 0); |
1942
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0); |
1943
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Close, base+1, 0); |
1944
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
1945
|
|
|
|
|
|
|
} |
1946
|
0
|
|
|
|
|
|
eList.nExpr = 1; |
1947
|
0
|
|
|
|
|
|
memset(&eListItem, 0, sizeof(eListItem)); |
1948
|
0
|
|
|
|
|
|
eList.a = &eListItem; |
1949
|
0
|
|
|
|
|
|
eList.a[0].pExpr = pExpr; |
1950
|
0
|
|
|
|
|
|
selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont); |
1951
|
0
|
|
|
|
|
|
sqliteVdbeResolveLabel(v, cont); |
1952
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Close, base, 0); |
1953
|
|
|
|
|
|
|
|
1954
|
138
|
|
|
|
|
|
return 1; |
1955
|
|
|
|
|
|
|
} |
1956
|
|
|
|
|
|
|
|
1957
|
|
|
|
|
|
|
/* |
1958
|
|
|
|
|
|
|
** Generate code for the given SELECT statement. |
1959
|
|
|
|
|
|
|
** |
1960
|
|
|
|
|
|
|
** The results are distributed in various ways depending on the |
1961
|
|
|
|
|
|
|
** value of eDest and iParm. |
1962
|
|
|
|
|
|
|
** |
1963
|
|
|
|
|
|
|
** eDest Value Result |
1964
|
|
|
|
|
|
|
** ------------ ------------------------------------------- |
1965
|
|
|
|
|
|
|
** SRT_Callback Invoke the callback for each row of the result. |
1966
|
|
|
|
|
|
|
** |
1967
|
|
|
|
|
|
|
** SRT_Mem Store first result in memory cell iParm |
1968
|
|
|
|
|
|
|
** |
1969
|
|
|
|
|
|
|
** SRT_Set Store results as keys of a table with cursor iParm |
1970
|
|
|
|
|
|
|
** |
1971
|
|
|
|
|
|
|
** SRT_Union Store results as a key in a temporary table iParm |
1972
|
|
|
|
|
|
|
** |
1973
|
|
|
|
|
|
|
** SRT_Except Remove results from the temporary table iParm. |
1974
|
|
|
|
|
|
|
** |
1975
|
|
|
|
|
|
|
** SRT_Table Store results in temporary table iParm |
1976
|
|
|
|
|
|
|
** |
1977
|
|
|
|
|
|
|
** The table above is incomplete. Additional eDist value have be added |
1978
|
|
|
|
|
|
|
** since this comment was written. See the selectInnerLoop() function for |
1979
|
|
|
|
|
|
|
** a complete listing of the allowed values of eDest and their meanings. |
1980
|
|
|
|
|
|
|
** |
1981
|
|
|
|
|
|
|
** This routine returns the number of errors. If any errors are |
1982
|
|
|
|
|
|
|
** encountered, then an appropriate error message is left in |
1983
|
|
|
|
|
|
|
** pParse->zErrMsg. |
1984
|
|
|
|
|
|
|
** |
1985
|
|
|
|
|
|
|
** This routine does NOT free the Select structure passed in. The |
1986
|
|
|
|
|
|
|
** calling function needs to do that. |
1987
|
|
|
|
|
|
|
** |
1988
|
|
|
|
|
|
|
** The pParent, parentTab, and *pParentAgg fields are filled in if this |
1989
|
|
|
|
|
|
|
** SELECT is a subquery. This routine may try to combine this SELECT |
1990
|
|
|
|
|
|
|
** with its parent to form a single flat query. In so doing, it might |
1991
|
|
|
|
|
|
|
** change the parent query from a non-aggregate to an aggregate query. |
1992
|
|
|
|
|
|
|
** For that reason, the pParentAgg flag is passed as a pointer, so it |
1993
|
|
|
|
|
|
|
** can be changed. |
1994
|
|
|
|
|
|
|
** |
1995
|
|
|
|
|
|
|
** Example 1: The meaning of the pParent parameter. |
1996
|
|
|
|
|
|
|
** |
1997
|
|
|
|
|
|
|
** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3; |
1998
|
|
|
|
|
|
|
** \ \_______ subquery _______/ / |
1999
|
|
|
|
|
|
|
** \ / |
2000
|
|
|
|
|
|
|
** \____________________ outer query ___________________/ |
2001
|
|
|
|
|
|
|
** |
2002
|
|
|
|
|
|
|
** This routine is called for the outer query first. For that call, |
2003
|
|
|
|
|
|
|
** pParent will be NULL. During the processing of the outer query, this |
2004
|
|
|
|
|
|
|
** routine is called recursively to handle the subquery. For the recursive |
2005
|
|
|
|
|
|
|
** call, pParent will point to the outer query. Because the subquery is |
2006
|
|
|
|
|
|
|
** the second element in a three-way join, the parentTab parameter will |
2007
|
|
|
|
|
|
|
** be 1 (the 2nd value of a 0-indexed array.) |
2008
|
|
|
|
|
|
|
*/ |
2009
|
147
|
|
|
|
|
|
int sqliteSelect( |
2010
|
|
|
|
|
|
|
Parse *pParse, /* The parser context */ |
2011
|
|
|
|
|
|
|
Select *p, /* The SELECT statement being coded. */ |
2012
|
|
|
|
|
|
|
int eDest, /* How to dispose of the results */ |
2013
|
|
|
|
|
|
|
int iParm, /* A parameter used by the eDest disposal method */ |
2014
|
|
|
|
|
|
|
Select *pParent, /* Another SELECT for which this is a sub-query */ |
2015
|
|
|
|
|
|
|
int parentTab, /* Index in pParent->pSrc of this query */ |
2016
|
|
|
|
|
|
|
int *pParentAgg /* True if pParent uses aggregate functions */ |
2017
|
|
|
|
|
|
|
){ |
2018
|
|
|
|
|
|
|
int i; |
2019
|
|
|
|
|
|
|
WhereInfo *pWInfo; |
2020
|
|
|
|
|
|
|
Vdbe *v; |
2021
|
147
|
|
|
|
|
|
int isAgg = 0; /* True for select lists like "count(*)" */ |
2022
|
|
|
|
|
|
|
ExprList *pEList; /* List of columns to extract. */ |
2023
|
|
|
|
|
|
|
SrcList *pTabList; /* List of tables to select from */ |
2024
|
|
|
|
|
|
|
Expr *pWhere; /* The WHERE clause. May be NULL */ |
2025
|
|
|
|
|
|
|
ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ |
2026
|
|
|
|
|
|
|
ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ |
2027
|
|
|
|
|
|
|
Expr *pHaving; /* The HAVING clause. May be NULL */ |
2028
|
|
|
|
|
|
|
int isDistinct; /* True if the DISTINCT keyword is present */ |
2029
|
|
|
|
|
|
|
int distinct; /* Table to use for the distinct set */ |
2030
|
147
|
|
|
|
|
|
int rc = 1; /* Value to return from this function */ |
2031
|
|
|
|
|
|
|
|
2032
|
147
|
50
|
|
|
|
|
if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1; |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
2033
|
147
|
50
|
|
|
|
|
if( sqliteAuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; |
2034
|
|
|
|
|
|
|
|
2035
|
|
|
|
|
|
|
/* If there is are a sequence of queries, do the earlier ones first. |
2036
|
|
|
|
|
|
|
*/ |
2037
|
147
|
100
|
|
|
|
|
if( p->pPrior ){ |
2038
|
9
|
|
|
|
|
|
return multiSelect(pParse, p, eDest, iParm); |
2039
|
|
|
|
|
|
|
} |
2040
|
|
|
|
|
|
|
|
2041
|
|
|
|
|
|
|
/* Make local copies of the parameters for this query. |
2042
|
|
|
|
|
|
|
*/ |
2043
|
138
|
|
|
|
|
|
pTabList = p->pSrc; |
2044
|
138
|
|
|
|
|
|
pWhere = p->pWhere; |
2045
|
138
|
|
|
|
|
|
pOrderBy = p->pOrderBy; |
2046
|
138
|
|
|
|
|
|
pGroupBy = p->pGroupBy; |
2047
|
138
|
|
|
|
|
|
pHaving = p->pHaving; |
2048
|
138
|
|
|
|
|
|
isDistinct = p->isDistinct; |
2049
|
|
|
|
|
|
|
|
2050
|
|
|
|
|
|
|
/* Allocate VDBE cursors for each table in the FROM clause |
2051
|
|
|
|
|
|
|
*/ |
2052
|
138
|
|
|
|
|
|
sqliteSrcListAssignCursors(pParse, pTabList); |
2053
|
|
|
|
|
|
|
|
2054
|
|
|
|
|
|
|
/* |
2055
|
|
|
|
|
|
|
** Do not even attempt to generate any code if we have already seen |
2056
|
|
|
|
|
|
|
** errors before this routine starts. |
2057
|
|
|
|
|
|
|
*/ |
2058
|
138
|
50
|
|
|
|
|
if( pParse->nErr>0 ) goto select_end; |
2059
|
|
|
|
|
|
|
|
2060
|
|
|
|
|
|
|
/* Expand any "*" terms in the result set. (For example the "*" in |
2061
|
|
|
|
|
|
|
** "SELECT * FROM t1") The fillInColumnlist() routine also does some |
2062
|
|
|
|
|
|
|
** other housekeeping - see the header comment for details. |
2063
|
|
|
|
|
|
|
*/ |
2064
|
138
|
50
|
|
|
|
|
if( fillInColumnList(pParse, p) ){ |
2065
|
0
|
|
|
|
|
|
goto select_end; |
2066
|
|
|
|
|
|
|
} |
2067
|
138
|
|
|
|
|
|
pWhere = p->pWhere; |
2068
|
138
|
|
|
|
|
|
pEList = p->pEList; |
2069
|
138
|
50
|
|
|
|
|
if( pEList==0 ) goto select_end; |
2070
|
|
|
|
|
|
|
|
2071
|
|
|
|
|
|
|
/* If writing to memory or generating a set |
2072
|
|
|
|
|
|
|
** only a single column may be output. |
2073
|
|
|
|
|
|
|
*/ |
2074
|
138
|
50
|
|
|
|
|
if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
2075
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, "only a single result allowed for " |
2076
|
|
|
|
|
|
|
"a SELECT that is part of an expression"); |
2077
|
0
|
|
|
|
|
|
goto select_end; |
2078
|
|
|
|
|
|
|
} |
2079
|
|
|
|
|
|
|
|
2080
|
|
|
|
|
|
|
/* ORDER BY is ignored for some destinations. |
2081
|
|
|
|
|
|
|
*/ |
2082
|
138
|
50
|
|
|
|
|
switch( eDest ){ |
2083
|
|
|
|
|
|
|
case SRT_Union: |
2084
|
|
|
|
|
|
|
case SRT_Except: |
2085
|
|
|
|
|
|
|
case SRT_Discard: |
2086
|
0
|
|
|
|
|
|
pOrderBy = 0; |
2087
|
0
|
|
|
|
|
|
break; |
2088
|
|
|
|
|
|
|
default: |
2089
|
138
|
|
|
|
|
|
break; |
2090
|
|
|
|
|
|
|
} |
2091
|
|
|
|
|
|
|
|
2092
|
|
|
|
|
|
|
/* At this point, we should have allocated all the cursors that we |
2093
|
|
|
|
|
|
|
** need to handle subquerys and temporary tables. |
2094
|
|
|
|
|
|
|
** |
2095
|
|
|
|
|
|
|
** Resolve the column names and do a semantics check on all the expressions. |
2096
|
|
|
|
|
|
|
*/ |
2097
|
587
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
2098
|
449
|
50
|
|
|
|
|
if( sqliteExprResolveIds(pParse, pTabList, 0, pEList->a[i].pExpr) ){ |
2099
|
0
|
|
|
|
|
|
goto select_end; |
2100
|
|
|
|
|
|
|
} |
2101
|
449
|
50
|
|
|
|
|
if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ |
2102
|
0
|
|
|
|
|
|
goto select_end; |
2103
|
|
|
|
|
|
|
} |
2104
|
|
|
|
|
|
|
} |
2105
|
138
|
100
|
|
|
|
|
if( pWhere ){ |
2106
|
27
|
50
|
|
|
|
|
if( sqliteExprResolveIds(pParse, pTabList, pEList, pWhere) ){ |
2107
|
0
|
|
|
|
|
|
goto select_end; |
2108
|
|
|
|
|
|
|
} |
2109
|
27
|
50
|
|
|
|
|
if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
2110
|
0
|
|
|
|
|
|
goto select_end; |
2111
|
|
|
|
|
|
|
} |
2112
|
|
|
|
|
|
|
} |
2113
|
138
|
50
|
|
|
|
|
if( pHaving ){ |
2114
|
0
|
0
|
|
|
|
|
if( pGroupBy==0 ){ |
2115
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); |
2116
|
0
|
|
|
|
|
|
goto select_end; |
2117
|
|
|
|
|
|
|
} |
2118
|
0
|
0
|
|
|
|
|
if( sqliteExprResolveIds(pParse, pTabList, pEList, pHaving) ){ |
2119
|
0
|
|
|
|
|
|
goto select_end; |
2120
|
|
|
|
|
|
|
} |
2121
|
0
|
0
|
|
|
|
|
if( sqliteExprCheck(pParse, pHaving, 1, &isAgg) ){ |
2122
|
0
|
|
|
|
|
|
goto select_end; |
2123
|
|
|
|
|
|
|
} |
2124
|
|
|
|
|
|
|
} |
2125
|
138
|
100
|
|
|
|
|
if( pOrderBy ){ |
2126
|
16
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
2127
|
|
|
|
|
|
|
int iCol; |
2128
|
11
|
|
|
|
|
|
Expr *pE = pOrderBy->a[i].pExpr; |
2129
|
11
|
50
|
|
|
|
|
if( sqliteExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
2130
|
0
|
|
|
|
|
|
sqliteExprDelete(pE); |
2131
|
0
|
|
|
|
|
|
pE = pOrderBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr); |
2132
|
|
|
|
|
|
|
} |
2133
|
11
|
50
|
|
|
|
|
if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){ |
2134
|
0
|
|
|
|
|
|
goto select_end; |
2135
|
|
|
|
|
|
|
} |
2136
|
11
|
50
|
|
|
|
|
if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
2137
|
0
|
|
|
|
|
|
goto select_end; |
2138
|
|
|
|
|
|
|
} |
2139
|
11
|
50
|
|
|
|
|
if( sqliteExprIsConstant(pE) ){ |
2140
|
0
|
0
|
|
|
|
|
if( sqliteExprIsInteger(pE, &iCol)==0 ){ |
2141
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, |
2142
|
|
|
|
|
|
|
"ORDER BY terms must not be non-integer constants"); |
2143
|
0
|
|
|
|
|
|
goto select_end; |
2144
|
0
|
0
|
|
|
|
|
}else if( iCol<=0 || iCol>pEList->nExpr ){ |
|
|
0
|
|
|
|
|
|
2145
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, |
2146
|
|
|
|
|
|
|
"ORDER BY column number %d out of range - should be " |
2147
|
|
|
|
|
|
|
"between 1 and %d", iCol, pEList->nExpr); |
2148
|
0
|
|
|
|
|
|
goto select_end; |
2149
|
|
|
|
|
|
|
} |
2150
|
|
|
|
|
|
|
} |
2151
|
|
|
|
|
|
|
} |
2152
|
|
|
|
|
|
|
} |
2153
|
138
|
100
|
|
|
|
|
if( pGroupBy ){ |
2154
|
4
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
2155
|
|
|
|
|
|
|
int iCol; |
2156
|
2
|
|
|
|
|
|
Expr *pE = pGroupBy->a[i].pExpr; |
2157
|
2
|
50
|
|
|
|
|
if( sqliteExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
2158
|
0
|
|
|
|
|
|
sqliteExprDelete(pE); |
2159
|
0
|
|
|
|
|
|
pE = pGroupBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr); |
2160
|
|
|
|
|
|
|
} |
2161
|
2
|
50
|
|
|
|
|
if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){ |
2162
|
0
|
|
|
|
|
|
goto select_end; |
2163
|
|
|
|
|
|
|
} |
2164
|
2
|
50
|
|
|
|
|
if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
2165
|
0
|
|
|
|
|
|
goto select_end; |
2166
|
|
|
|
|
|
|
} |
2167
|
2
|
50
|
|
|
|
|
if( sqliteExprIsConstant(pE) ){ |
2168
|
0
|
0
|
|
|
|
|
if( sqliteExprIsInteger(pE, &iCol)==0 ){ |
2169
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, |
2170
|
|
|
|
|
|
|
"GROUP BY terms must not be non-integer constants"); |
2171
|
0
|
|
|
|
|
|
goto select_end; |
2172
|
0
|
0
|
|
|
|
|
}else if( iCol<=0 || iCol>pEList->nExpr ){ |
|
|
0
|
|
|
|
|
|
2173
|
0
|
|
|
|
|
|
sqliteErrorMsg(pParse, |
2174
|
|
|
|
|
|
|
"GROUP BY column number %d out of range - should be " |
2175
|
|
|
|
|
|
|
"between 1 and %d", iCol, pEList->nExpr); |
2176
|
0
|
|
|
|
|
|
goto select_end; |
2177
|
|
|
|
|
|
|
} |
2178
|
|
|
|
|
|
|
} |
2179
|
|
|
|
|
|
|
} |
2180
|
|
|
|
|
|
|
} |
2181
|
|
|
|
|
|
|
|
2182
|
|
|
|
|
|
|
/* Begin generating code. |
2183
|
|
|
|
|
|
|
*/ |
2184
|
138
|
|
|
|
|
|
v = sqliteGetVdbe(pParse); |
2185
|
138
|
50
|
|
|
|
|
if( v==0 ) goto select_end; |
2186
|
|
|
|
|
|
|
|
2187
|
|
|
|
|
|
|
/* Identify column names if we will be using them in a callback. This |
2188
|
|
|
|
|
|
|
** step is skipped if the output is going to some other destination. |
2189
|
|
|
|
|
|
|
*/ |
2190
|
138
|
100
|
|
|
|
|
if( eDest==SRT_Callback ){ |
2191
|
123
|
|
|
|
|
|
generateColumnNames(pParse, pTabList, pEList); |
2192
|
|
|
|
|
|
|
} |
2193
|
|
|
|
|
|
|
|
2194
|
|
|
|
|
|
|
/* Generate code for all sub-queries in the FROM clause |
2195
|
|
|
|
|
|
|
*/ |
2196
|
254
|
100
|
|
|
|
|
for(i=0; inSrc; i++){ |
2197
|
|
|
|
|
|
|
const char *zSavedAuthContext; |
2198
|
|
|
|
|
|
|
int needRestoreContext; |
2199
|
|
|
|
|
|
|
|
2200
|
116
|
100
|
|
|
|
|
if( pTabList->a[i].pSelect==0 ) continue; |
2201
|
6
|
50
|
|
|
|
|
if( pTabList->a[i].zName!=0 ){ |
2202
|
0
|
|
|
|
|
|
zSavedAuthContext = pParse->zAuthContext; |
2203
|
0
|
|
|
|
|
|
pParse->zAuthContext = pTabList->a[i].zName; |
2204
|
0
|
|
|
|
|
|
needRestoreContext = 1; |
2205
|
|
|
|
|
|
|
}else{ |
2206
|
6
|
|
|
|
|
|
needRestoreContext = 0; |
2207
|
|
|
|
|
|
|
} |
2208
|
6
|
|
|
|
|
|
sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, |
2209
|
|
|
|
|
|
|
pTabList->a[i].iCursor, p, i, &isAgg); |
2210
|
6
|
50
|
|
|
|
|
if( needRestoreContext ){ |
2211
|
0
|
|
|
|
|
|
pParse->zAuthContext = zSavedAuthContext; |
2212
|
|
|
|
|
|
|
} |
2213
|
6
|
|
|
|
|
|
pTabList = p->pSrc; |
2214
|
6
|
|
|
|
|
|
pWhere = p->pWhere; |
2215
|
6
|
50
|
|
|
|
|
if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){ |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
2216
|
6
|
|
|
|
|
|
pOrderBy = p->pOrderBy; |
2217
|
|
|
|
|
|
|
} |
2218
|
6
|
|
|
|
|
|
pGroupBy = p->pGroupBy; |
2219
|
6
|
|
|
|
|
|
pHaving = p->pHaving; |
2220
|
6
|
|
|
|
|
|
isDistinct = p->isDistinct; |
2221
|
|
|
|
|
|
|
} |
2222
|
|
|
|
|
|
|
|
2223
|
|
|
|
|
|
|
/* Check for the special case of a min() or max() function by itself |
2224
|
|
|
|
|
|
|
** in the result set. |
2225
|
|
|
|
|
|
|
*/ |
2226
|
138
|
50
|
|
|
|
|
if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ |
2227
|
0
|
|
|
|
|
|
rc = 0; |
2228
|
0
|
|
|
|
|
|
goto select_end; |
2229
|
|
|
|
|
|
|
} |
2230
|
|
|
|
|
|
|
|
2231
|
|
|
|
|
|
|
/* Check to see if this is a subquery that can be "flattened" into its parent. |
2232
|
|
|
|
|
|
|
** If flattening is a possiblity, do so and return immediately. |
2233
|
|
|
|
|
|
|
*/ |
2234
|
141
|
100
|
|
|
|
|
if( pParent && pParentAgg && |
2235
|
3
|
|
|
|
|
|
flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){ |
2236
|
3
|
50
|
|
|
|
|
if( isAgg ) *pParentAgg = 1; |
2237
|
3
|
|
|
|
|
|
return rc; |
2238
|
|
|
|
|
|
|
} |
2239
|
|
|
|
|
|
|
|
2240
|
|
|
|
|
|
|
/* Set the limiter. |
2241
|
|
|
|
|
|
|
*/ |
2242
|
135
|
|
|
|
|
|
computeLimitRegisters(pParse, p); |
2243
|
|
|
|
|
|
|
|
2244
|
|
|
|
|
|
|
/* Identify column types if we will be using a callback. This |
2245
|
|
|
|
|
|
|
** step is skipped if the output is going to a destination other |
2246
|
|
|
|
|
|
|
** than a callback. |
2247
|
|
|
|
|
|
|
** |
2248
|
|
|
|
|
|
|
** We have to do this separately from the creation of column names |
2249
|
|
|
|
|
|
|
** above because if the pTabList contains views then they will not |
2250
|
|
|
|
|
|
|
** have been resolved and we will not know the column types until |
2251
|
|
|
|
|
|
|
** now. |
2252
|
|
|
|
|
|
|
*/ |
2253
|
135
|
100
|
|
|
|
|
if( eDest==SRT_Callback ){ |
2254
|
123
|
|
|
|
|
|
generateColumnTypes(pParse, pTabList, pEList); |
2255
|
|
|
|
|
|
|
} |
2256
|
|
|
|
|
|
|
|
2257
|
|
|
|
|
|
|
/* If the output is destined for a temporary table, open that table. |
2258
|
|
|
|
|
|
|
*/ |
2259
|
135
|
50
|
|
|
|
|
if( eDest==SRT_TempTable ){ |
2260
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0); |
2261
|
|
|
|
|
|
|
} |
2262
|
|
|
|
|
|
|
|
2263
|
|
|
|
|
|
|
/* Do an analysis of aggregate expressions. |
2264
|
|
|
|
|
|
|
*/ |
2265
|
135
|
|
|
|
|
|
sqliteAggregateInfoReset(pParse); |
2266
|
135
|
100
|
|
|
|
|
if( isAgg || pGroupBy ){ |
|
|
50
|
|
|
|
|
|
2267
|
|
|
|
|
|
|
assert( pParse->nAgg==0 ); |
2268
|
15
|
|
|
|
|
|
isAgg = 1; |
2269
|
32
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
2270
|
17
|
50
|
|
|
|
|
if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ |
2271
|
0
|
|
|
|
|
|
goto select_end; |
2272
|
|
|
|
|
|
|
} |
2273
|
|
|
|
|
|
|
} |
2274
|
15
|
100
|
|
|
|
|
if( pGroupBy ){ |
2275
|
4
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
2276
|
2
|
50
|
|
|
|
|
if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ |
2277
|
0
|
|
|
|
|
|
goto select_end; |
2278
|
|
|
|
|
|
|
} |
2279
|
|
|
|
|
|
|
} |
2280
|
|
|
|
|
|
|
} |
2281
|
15
|
50
|
|
|
|
|
if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){ |
|
|
0
|
|
|
|
|
|
2282
|
0
|
|
|
|
|
|
goto select_end; |
2283
|
|
|
|
|
|
|
} |
2284
|
15
|
100
|
|
|
|
|
if( pOrderBy ){ |
2285
|
2
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
2286
|
1
|
50
|
|
|
|
|
if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){ |
2287
|
0
|
|
|
|
|
|
goto select_end; |
2288
|
|
|
|
|
|
|
} |
2289
|
|
|
|
|
|
|
} |
2290
|
|
|
|
|
|
|
} |
2291
|
|
|
|
|
|
|
} |
2292
|
|
|
|
|
|
|
|
2293
|
|
|
|
|
|
|
/* Reset the aggregator |
2294
|
|
|
|
|
|
|
*/ |
2295
|
135
|
100
|
|
|
|
|
if( isAgg ){ |
2296
|
15
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg); |
2297
|
33
|
100
|
|
|
|
|
for(i=0; inAgg; i++){ |
2298
|
|
|
|
|
|
|
FuncDef *pFunc; |
2299
|
18
|
100
|
|
|
|
|
if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){ |
|
|
50
|
|
|
|
|
|
2300
|
15
|
|
|
|
|
|
sqliteVdbeOp3(v, OP_AggInit, 0, i, (char*)pFunc, P3_POINTER); |
2301
|
|
|
|
|
|
|
} |
2302
|
|
|
|
|
|
|
} |
2303
|
15
|
100
|
|
|
|
|
if( pGroupBy==0 ){ |
2304
|
13
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_String, 0, 0); |
2305
|
13
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_AggFocus, 0, 0); |
2306
|
|
|
|
|
|
|
} |
2307
|
|
|
|
|
|
|
} |
2308
|
|
|
|
|
|
|
|
2309
|
|
|
|
|
|
|
/* Initialize the memory cell to NULL |
2310
|
|
|
|
|
|
|
*/ |
2311
|
135
|
50
|
|
|
|
|
if( eDest==SRT_Mem ){ |
2312
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_String, 0, 0); |
2313
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); |
2314
|
|
|
|
|
|
|
} |
2315
|
|
|
|
|
|
|
|
2316
|
|
|
|
|
|
|
/* Open a temporary table to use for the distinct set. |
2317
|
|
|
|
|
|
|
*/ |
2318
|
135
|
50
|
|
|
|
|
if( isDistinct ){ |
2319
|
0
|
|
|
|
|
|
distinct = pParse->nTab++; |
2320
|
0
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1); |
2321
|
|
|
|
|
|
|
}else{ |
2322
|
135
|
|
|
|
|
|
distinct = -1; |
2323
|
|
|
|
|
|
|
} |
2324
|
|
|
|
|
|
|
|
2325
|
|
|
|
|
|
|
/* Begin the database scan |
2326
|
|
|
|
|
|
|
*/ |
2327
|
135
|
100
|
|
|
|
|
pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0, |
2328
|
|
|
|
|
|
|
pGroupBy ? 0 : &pOrderBy); |
2329
|
135
|
50
|
|
|
|
|
if( pWInfo==0 ) goto select_end; |
2330
|
|
|
|
|
|
|
|
2331
|
|
|
|
|
|
|
/* Use the standard inner loop if we are not dealing with |
2332
|
|
|
|
|
|
|
** aggregates |
2333
|
|
|
|
|
|
|
*/ |
2334
|
135
|
100
|
|
|
|
|
if( !isAgg ){ |
2335
|
120
|
50
|
|
|
|
|
if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, |
2336
|
|
|
|
|
|
|
iParm, pWInfo->iContinue, pWInfo->iBreak) ){ |
2337
|
0
|
|
|
|
|
|
goto select_end; |
2338
|
|
|
|
|
|
|
} |
2339
|
|
|
|
|
|
|
} |
2340
|
|
|
|
|
|
|
|
2341
|
|
|
|
|
|
|
/* If we are dealing with aggregates, then do the special aggregate |
2342
|
|
|
|
|
|
|
** processing. |
2343
|
|
|
|
|
|
|
*/ |
2344
|
|
|
|
|
|
|
else{ |
2345
|
|
|
|
|
|
|
AggExpr *pAgg; |
2346
|
15
|
100
|
|
|
|
|
if( pGroupBy ){ |
2347
|
|
|
|
|
|
|
int lbl1; |
2348
|
4
|
100
|
|
|
|
|
for(i=0; inExpr; i++){ |
2349
|
2
|
|
|
|
|
|
sqliteExprCode(pParse, pGroupBy->a[i].pExpr); |
2350
|
|
|
|
|
|
|
} |
2351
|
2
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0); |
2352
|
2
|
50
|
|
|
|
|
if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pGroupBy); |
2353
|
2
|
|
|
|
|
|
lbl1 = sqliteVdbeMakeLabel(v); |
2354
|
2
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1); |
2355
|
7
|
100
|
|
|
|
|
for(i=0, pAgg=pParse->aAgg; inAgg; i++, pAgg++){ |
2356
|
5
|
100
|
|
|
|
|
if( pAgg->isAgg ) continue; |
2357
|
3
|
|
|
|
|
|
sqliteExprCode(pParse, pAgg->pExpr); |
2358
|
3
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_AggSet, 0, i); |
2359
|
|
|
|
|
|
|
} |
2360
|
2
|
|
|
|
|
|
sqliteVdbeResolveLabel(v, lbl1); |
2361
|
|
|
|
|
|
|
} |
2362
|
33
|
100
|
|
|
|
|
for(i=0, pAgg=pParse->aAgg; inAgg; i++, pAgg++){ |
2363
|
|
|
|
|
|
|
Expr *pE; |
2364
|
|
|
|
|
|
|
int nExpr; |
2365
|
|
|
|
|
|
|
FuncDef *pDef; |
2366
|
18
|
100
|
|
|
|
|
if( !pAgg->isAgg ) continue; |
2367
|
|
|
|
|
|
|
assert( pAgg->pFunc!=0 ); |
2368
|
|
|
|
|
|
|
assert( pAgg->pFunc->xStep!=0 ); |
2369
|
15
|
|
|
|
|
|
pDef = pAgg->pFunc; |
2370
|
15
|
|
|
|
|
|
pE = pAgg->pExpr; |
2371
|
|
|
|
|
|
|
assert( pE!=0 ); |
2372
|
|
|
|
|
|
|
assert( pE->op==TK_AGG_FUNCTION ); |
2373
|
15
|
|
|
|
|
|
nExpr = sqliteExprCodeExprList(pParse, pE->pList, pDef->includeTypes); |
2374
|
15
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Integer, i, 0); |
2375
|
15
|
|
|
|
|
|
sqliteVdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_POINTER); |
2376
|
|
|
|
|
|
|
} |
2377
|
|
|
|
|
|
|
} |
2378
|
|
|
|
|
|
|
|
2379
|
|
|
|
|
|
|
/* End the database scan loop. |
2380
|
|
|
|
|
|
|
*/ |
2381
|
135
|
|
|
|
|
|
sqliteWhereEnd(pWInfo); |
2382
|
|
|
|
|
|
|
|
2383
|
|
|
|
|
|
|
/* If we are processing aggregates, we need to set up a second loop |
2384
|
|
|
|
|
|
|
** over all of the aggregate values and process them. |
2385
|
|
|
|
|
|
|
*/ |
2386
|
135
|
100
|
|
|
|
|
if( isAgg ){ |
2387
|
15
|
|
|
|
|
|
int endagg = sqliteVdbeMakeLabel(v); |
2388
|
|
|
|
|
|
|
int startagg; |
2389
|
15
|
|
|
|
|
|
startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg); |
2390
|
15
|
|
|
|
|
|
pParse->useAgg = 1; |
2391
|
15
|
50
|
|
|
|
|
if( pHaving ){ |
2392
|
0
|
|
|
|
|
|
sqliteExprIfFalse(pParse, pHaving, startagg, 1); |
2393
|
|
|
|
|
|
|
} |
2394
|
15
|
50
|
|
|
|
|
if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, |
2395
|
|
|
|
|
|
|
iParm, startagg, endagg) ){ |
2396
|
0
|
|
|
|
|
|
goto select_end; |
2397
|
|
|
|
|
|
|
} |
2398
|
15
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Goto, 0, startagg); |
2399
|
15
|
|
|
|
|
|
sqliteVdbeResolveLabel(v, endagg); |
2400
|
15
|
|
|
|
|
|
sqliteVdbeAddOp(v, OP_Noop, 0, 0); |
2401
|
15
|
|
|
|
|
|
pParse->useAgg = 0; |
2402
|
|
|
|
|
|
|
} |
2403
|
|
|
|
|
|
|
|
2404
|
|
|
|
|
|
|
/* If there is an ORDER BY clause, then we need to sort the results |
2405
|
|
|
|
|
|
|
** and send them to the callback one by one. |
2406
|
|
|
|
|
|
|
*/ |
2407
|
135
|
100
|
|
|
|
|
if( pOrderBy ){ |
2408
|
5
|
|
|
|
|
|
generateSortTail(p, v, pEList->nExpr, eDest, iParm); |
2409
|
|
|
|
|
|
|
} |
2410
|
|
|
|
|
|
|
|
2411
|
|
|
|
|
|
|
/* If this was a subquery, we have now converted the subquery into a |
2412
|
|
|
|
|
|
|
** temporary table. So delete the subquery structure from the parent |
2413
|
|
|
|
|
|
|
** to prevent this subquery from being evaluated again and to force the |
2414
|
|
|
|
|
|
|
** the use of the temporary table. |
2415
|
|
|
|
|
|
|
*/ |
2416
|
135
|
50
|
|
|
|
|
if( pParent ){ |
2417
|
|
|
|
|
|
|
assert( pParent->pSrc->nSrc>parentTab ); |
2418
|
|
|
|
|
|
|
assert( pParent->pSrc->a[parentTab].pSelect==p ); |
2419
|
0
|
|
|
|
|
|
sqliteSelectDelete(p); |
2420
|
0
|
|
|
|
|
|
pParent->pSrc->a[parentTab].pSelect = 0; |
2421
|
|
|
|
|
|
|
} |
2422
|
|
|
|
|
|
|
|
2423
|
|
|
|
|
|
|
/* The SELECT was successfully coded. Set the return code to 0 |
2424
|
|
|
|
|
|
|
** to indicate no errors. |
2425
|
|
|
|
|
|
|
*/ |
2426
|
135
|
|
|
|
|
|
rc = 0; |
2427
|
|
|
|
|
|
|
|
2428
|
|
|
|
|
|
|
/* Control jumps to here if an error is encountered above, or upon |
2429
|
|
|
|
|
|
|
** successful coding of the SELECT. |
2430
|
|
|
|
|
|
|
*/ |
2431
|
|
|
|
|
|
|
select_end: |
2432
|
135
|
|
|
|
|
|
sqliteAggregateInfoReset(pParse); |
2433
|
147
|
|
|
|
|
|
return rc; |
2434
|
|
|
|
|
|
|
} |