4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This file contains C code routines that are called by the parser
13 ** to handle SELECT statements in SQLite.
17 #include "sqliteInt.h"
21 ** Allocate a new Select structure and return a pointer to that
24 Select
*sqlite3SelectNew(
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 Expr
*pLimit
, /* LIMIT value. NULL means not used */
33 Expr
*pOffset
/* OFFSET value. NULL means no offset */
36 pNew
= sqliteMalloc( sizeof(*pNew
) );
37 assert( !pOffset
|| pLimit
); /* Can't have OFFSET without LIMIT. */
39 sqlite3ExprListDelete(pEList
);
40 sqlite3SrcListDelete(pSrc
);
41 sqlite3ExprDelete(pWhere
);
42 sqlite3ExprListDelete(pGroupBy
);
43 sqlite3ExprDelete(pHaving
);
44 sqlite3ExprListDelete(pOrderBy
);
45 sqlite3ExprDelete(pLimit
);
46 sqlite3ExprDelete(pOffset
);
49 pEList
= sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL
,0,0,0), 0);
51 pNew
->pEList
= pEList
;
53 pNew
->pWhere
= pWhere
;
54 pNew
->pGroupBy
= pGroupBy
;
55 pNew
->pHaving
= pHaving
;
56 pNew
->pOrderBy
= pOrderBy
;
57 pNew
->isDistinct
= isDistinct
;
59 pNew
->pLimit
= pLimit
;
60 pNew
->pOffset
= pOffset
;
68 ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
69 ** type of join. Return an integer constant that expresses that type
70 ** in terms of the following bit values:
78 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
80 ** If an illegal or unsupported join type is seen, then still return
81 ** a join type, but put an error in the pParse structure.
83 int sqlite3JoinType(Parse
*pParse
, Token
*pA
, Token
*pB
, Token
*pC
){
92 { "natural", 7, JT_NATURAL
},
93 { "left", 4, JT_LEFT
|JT_OUTER
},
94 { "right", 5, JT_RIGHT
|JT_OUTER
},
95 { "full", 4, JT_LEFT
|JT_RIGHT
|JT_OUTER
},
96 { "outer", 5, JT_OUTER
},
97 { "inner", 5, JT_INNER
},
98 { "cross", 5, JT_INNER
},
104 for(i
=0; i
<3 && apAll
[i
]; i
++){
106 for(j
=0; j
<sizeof(keywords
)/sizeof(keywords
[0]); j
++){
107 if( p
->n
==keywords
[j
].nChar
108 && sqlite3StrNICmp(p
->z
, keywords
[j
].zKeyword
, p
->n
)==0 ){
109 jointype
|= keywords
[j
].code
;
113 if( j
>=sizeof(keywords
)/sizeof(keywords
[0]) ){
114 jointype
|= JT_ERROR
;
119 (jointype
& (JT_INNER
|JT_OUTER
))==(JT_INNER
|JT_OUTER
) ||
120 (jointype
& JT_ERROR
)!=0
122 const char *zSp1
= " ";
123 const char *zSp2
= " ";
124 if( pB
==0 ){ zSp1
++; }
125 if( pC
==0 ){ zSp2
++; }
126 sqlite3ErrorMsg(pParse
, "unknown or unsupported join type: "
127 "%T%s%T%s%T", pA
, zSp1
, pB
, zSp2
, pC
);
129 }else if( jointype
& JT_RIGHT
){
130 sqlite3ErrorMsg(pParse
,
131 "RIGHT and FULL OUTER JOINs are not currently supported");
138 ** Return the index of a column in a table. Return -1 if the column
139 ** is not contained in the table.
141 static int columnIndex(Table
*pTab
, const char *zCol
){
143 for(i
=0; i
<pTab
->nCol
; i
++){
144 if( sqlite3StrICmp(pTab
->aCol
[i
].zName
, zCol
)==0 ) return i
;
150 ** Set the value of a token to a '\000'-terminated string.
152 static void setToken(Token
*p
, const char *z
){
160 ** Add a term to the WHERE expression in *ppExpr that requires the
161 ** zCol column to be equal in the two tables pTab1 and pTab2.
163 static void addWhereTerm(
164 const char *zCol
, /* Name of the column */
165 const Table
*pTab1
, /* First table */
166 const char *zAlias1
, /* Alias for first table. May be NULL */
167 const Table
*pTab2
, /* Second table */
168 const char *zAlias2
, /* Alias for second table. May be NULL */
169 Expr
**ppExpr
/* Add the equality term to this expression */
172 Expr
*pE1a
, *pE1b
, *pE1c
;
173 Expr
*pE2a
, *pE2b
, *pE2c
;
176 setToken(&dummy
, zCol
);
177 pE1a
= sqlite3Expr(TK_ID
, 0, 0, &dummy
);
178 pE2a
= sqlite3Expr(TK_ID
, 0, 0, &dummy
);
180 zAlias1
= pTab1
->zName
;
182 setToken(&dummy
, zAlias1
);
183 pE1b
= sqlite3Expr(TK_ID
, 0, 0, &dummy
);
185 zAlias2
= pTab2
->zName
;
187 setToken(&dummy
, zAlias2
);
188 pE2b
= sqlite3Expr(TK_ID
, 0, 0, &dummy
);
189 pE1c
= sqlite3Expr(TK_DOT
, pE1b
, pE1a
, 0);
190 pE2c
= sqlite3Expr(TK_DOT
, pE2b
, pE2a
, 0);
191 pE
= sqlite3Expr(TK_EQ
, pE1c
, pE2c
, 0);
192 ExprSetProperty(pE
, EP_FromJoin
);
193 *ppExpr
= sqlite3ExprAnd(*ppExpr
, pE
);
197 ** Set the EP_FromJoin property on all terms of the given expression.
199 ** The EP_FromJoin property is used on terms of an expression to tell
200 ** the LEFT OUTER JOIN processing logic that this term is part of the
201 ** join restriction specified in the ON or USING clause and not a part
202 ** of the more general WHERE clause. These terms are moved over to the
203 ** WHERE clause during join processing but we need to remember that they
204 ** originated in the ON or USING clause.
206 static void setJoinExpr(Expr
*p
){
208 ExprSetProperty(p
, EP_FromJoin
);
209 setJoinExpr(p
->pLeft
);
215 ** This routine processes the join information for a SELECT statement.
216 ** ON and USING clauses are converted into extra terms of the WHERE clause.
217 ** NATURAL joins also create extra WHERE clause terms.
219 ** The terms of a FROM clause are contained in the Select.pSrc structure.
220 ** The left most table is the first entry in Select.pSrc. The right-most
221 ** table is the last entry. The join operator is held in the entry to
222 ** the left. Thus entry 0 contains the join operator for the join between
223 ** entries 0 and 1. Any ON or USING clauses associated with the join are
224 ** also attached to the left entry.
226 ** This routine returns the number of errors encountered.
228 static int sqliteProcessJoin(Parse
*pParse
, Select
*p
){
229 SrcList
*pSrc
; /* All tables in the FROM clause */
230 int i
, j
; /* Loop counters */
231 struct SrcList_item
*pLeft
; /* Left table being joined */
232 struct SrcList_item
*pRight
; /* Right table being joined */
237 for(i
=0; i
<pSrc
->nSrc
-1; i
++, pRight
++, pLeft
++){
238 Table
*pLeftTab
= pLeft
->pTab
;
239 Table
*pRightTab
= pRight
->pTab
;
241 if( pLeftTab
==0 || pRightTab
==0 ) continue;
243 /* When the NATURAL keyword is present, add WHERE clause terms for
244 ** every column that the two tables have in common.
246 if( pLeft
->jointype
& JT_NATURAL
){
247 if( pLeft
->pOn
|| pLeft
->pUsing
){
248 sqlite3ErrorMsg(pParse
, "a NATURAL join may not have "
249 "an ON or USING clause", 0);
252 for(j
=0; j
<pLeftTab
->nCol
; j
++){
253 char *zName
= pLeftTab
->aCol
[j
].zName
;
254 if( columnIndex(pRightTab
, zName
)>=0 ){
255 addWhereTerm(zName
, pLeftTab
, pLeft
->zAlias
,
256 pRightTab
, pRight
->zAlias
, &p
->pWhere
);
261 /* Disallow both ON and USING clauses in the same join
263 if( pLeft
->pOn
&& pLeft
->pUsing
){
264 sqlite3ErrorMsg(pParse
, "cannot have both ON and USING "
265 "clauses in the same join");
269 /* Add the ON clause to the end of the WHERE clause, connected by
273 setJoinExpr(pLeft
->pOn
);
274 p
->pWhere
= sqlite3ExprAnd(p
->pWhere
, pLeft
->pOn
);
278 /* Create extra terms on the WHERE clause for each column named
279 ** in the USING clause. Example: If the two tables to be joined are
280 ** A and B and the USING clause names X, Y, and Z, then add this
281 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
282 ** Report an error if any column mentioned in the USING clause is
283 ** not contained in both tables to be joined.
286 IdList
*pList
= pLeft
->pUsing
;
287 for(j
=0; j
<pList
->nId
; j
++){
288 char *zName
= pList
->a
[j
].zName
;
289 if( columnIndex(pLeftTab
, zName
)<0 || columnIndex(pRightTab
, zName
)<0 ){
290 sqlite3ErrorMsg(pParse
, "cannot join using column %s - column "
291 "not present in both tables", zName
);
294 addWhereTerm(zName
, pLeftTab
, pLeft
->zAlias
,
295 pRightTab
, pRight
->zAlias
, &p
->pWhere
);
303 ** Delete the given Select structure and all of its substructures.
305 void sqlite3SelectDelete(Select
*p
){
307 sqlite3ExprListDelete(p
->pEList
);
308 sqlite3SrcListDelete(p
->pSrc
);
309 sqlite3ExprDelete(p
->pWhere
);
310 sqlite3ExprListDelete(p
->pGroupBy
);
311 sqlite3ExprDelete(p
->pHaving
);
312 sqlite3ExprListDelete(p
->pOrderBy
);
313 sqlite3SelectDelete(p
->pPrior
);
314 sqlite3ExprDelete(p
->pLimit
);
315 sqlite3ExprDelete(p
->pOffset
);
320 ** Insert code into "v" that will push the record on the top of the
321 ** stack into the sorter.
323 static void pushOntoSorter(Parse
*pParse
, Vdbe
*v
, ExprList
*pOrderBy
){
325 for(i
=0; i
<pOrderBy
->nExpr
; i
++){
326 sqlite3ExprCode(pParse
, pOrderBy
->a
[i
].pExpr
);
328 sqlite3VdbeAddOp(v
, OP_MakeRecord
, pOrderBy
->nExpr
, 0);
329 sqlite3VdbeAddOp(v
, OP_SortInsert
, 0, 0);
333 ** Add code to implement the OFFSET and LIMIT
335 static void codeLimiter(
336 Vdbe
*v
, /* Generate code into this VM */
337 Select
*p
, /* The SELECT statement being coded */
338 int iContinue
, /* Jump here to skip the current record */
339 int iBreak
, /* Jump here to end the loop */
340 int nPop
/* Number of times to pop stack when jumping */
343 int addr
= sqlite3VdbeCurrentAddr(v
) + 3;
345 sqlite3VdbeAddOp(v
, OP_MemIncr
, p
->iOffset
, 0);
346 sqlite3VdbeAddOp(v
, OP_IfMemPos
, p
->iOffset
, addr
);
348 sqlite3VdbeAddOp(v
, OP_Pop
, nPop
, 0);
350 sqlite3VdbeAddOp(v
, OP_Goto
, 0, iContinue
);
351 VdbeComment((v
, "# skip OFFSET records"));
354 sqlite3VdbeAddOp(v
, OP_MemIncr
, p
->iLimit
, iBreak
);
355 VdbeComment((v
, "# exit when LIMIT reached"));
360 ** This routine generates the code for the inside of the inner loop
363 ** If srcTab and nColumn are both zero, then the pEList expressions
364 ** are evaluated in order to get the data for this row. If nColumn>0
365 ** then data is pulled from srcTab and pEList is used only to get the
366 ** datatypes for each column.
368 static int selectInnerLoop(
369 Parse
*pParse
, /* The parser context */
370 Select
*p
, /* The complete select statement being coded */
371 ExprList
*pEList
, /* List of values being extracted */
372 int srcTab
, /* Pull data from this table */
373 int nColumn
, /* Number of columns in the source table */
374 ExprList
*pOrderBy
, /* If not NULL, sort results using this key */
375 int distinct
, /* If >=0, make sure results are distinct */
376 int eDest
, /* How to dispose of the results */
377 int iParm
, /* An argument to the disposal method */
378 int iContinue
, /* Jump here to continue with next row */
379 int iBreak
, /* Jump here to break out of the inner loop */
380 char *aff
/* affinity string if eDest is SRT_Union */
382 Vdbe
*v
= pParse
->pVdbe
;
384 int hasDistinct
; /* True if the DISTINCT keyword is present */
389 /* If there was a LIMIT clause on the SELECT statement, then do the check
390 ** to see if this row should be output.
392 hasDistinct
= distinct
>=0 && pEList
&& pEList
->nExpr
>0;
393 if( pOrderBy
==0 && !hasDistinct
){
394 codeLimiter(v
, p
, iContinue
, iBreak
, 0);
397 /* Pull the requested columns.
400 for(i
=0; i
<nColumn
; i
++){
401 sqlite3VdbeAddOp(v
, OP_Column
, srcTab
, i
);
404 nColumn
= pEList
->nExpr
;
405 for(i
=0; i
<pEList
->nExpr
; i
++){
406 sqlite3ExprCode(pParse
, pEList
->a
[i
].pExpr
);
410 /* If the DISTINCT keyword was present on the SELECT statement
411 ** and this row has been seen before, then do not make this row
412 ** part of the result.
415 #if NULL_ALWAYS_DISTINCT
416 sqlite3VdbeAddOp(v
, OP_IsNull
, -pEList
->nExpr
, sqlite3VdbeCurrentAddr(v
)+7);
418 /* Deliberately leave the affinity string off of the following
420 sqlite3VdbeAddOp(v
, OP_MakeRecord
, pEList
->nExpr
* -1, 0);
421 sqlite3VdbeAddOp(v
, OP_Distinct
, distinct
, sqlite3VdbeCurrentAddr(v
)+3);
422 sqlite3VdbeAddOp(v
, OP_Pop
, pEList
->nExpr
+1, 0);
423 sqlite3VdbeAddOp(v
, OP_Goto
, 0, iContinue
);
424 VdbeComment((v
, "# skip indistinct records"));
425 sqlite3VdbeAddOp(v
, OP_IdxInsert
, distinct
, 0);
427 codeLimiter(v
, p
, iContinue
, iBreak
, nColumn
);
432 #ifndef SQLITE_OMIT_COMPOUND_SELECT
433 /* In this mode, write each query result to the key of the temporary
437 sqlite3VdbeAddOp(v
, OP_MakeRecord
, nColumn
, NULL_ALWAYS_DISTINCT
);
438 sqlite3VdbeChangeP3(v
, -1, aff
, P3_STATIC
);
439 sqlite3VdbeAddOp(v
, OP_IdxInsert
, iParm
, 0);
443 /* Construct a record from the query result, but instead of
444 ** saving that record, use it as a key to delete elements from
445 ** the temporary table iParm.
449 addr
= sqlite3VdbeAddOp(v
, OP_MakeRecord
, nColumn
, NULL_ALWAYS_DISTINCT
);
450 sqlite3VdbeChangeP3(v
, -1, aff
, P3_STATIC
);
451 sqlite3VdbeAddOp(v
, OP_NotFound
, iParm
, addr
+3);
452 sqlite3VdbeAddOp(v
, OP_Delete
, iParm
, 0);
457 /* Store the result as data using a unique key.
460 case SRT_TempTable
: {
461 sqlite3VdbeAddOp(v
, OP_MakeRecord
, nColumn
, 0);
463 pushOntoSorter(pParse
, v
, pOrderBy
);
465 sqlite3VdbeAddOp(v
, OP_NewRowid
, iParm
, 0);
466 sqlite3VdbeAddOp(v
, OP_Pull
, 1, 0);
467 sqlite3VdbeAddOp(v
, OP_Insert
, iParm
, 0);
472 #ifndef SQLITE_OMIT_SUBQUERY
473 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
474 ** then there should be a single item on the stack. Write this
475 ** item into the set table with bogus data.
478 int addr1
= sqlite3VdbeCurrentAddr(v
);
481 assert( nColumn
==1 );
482 sqlite3VdbeAddOp(v
, OP_NotNull
, -1, addr1
+3);
483 sqlite3VdbeAddOp(v
, OP_Pop
, 1, 0);
484 addr2
= sqlite3VdbeAddOp(v
, OP_Goto
, 0, 0);
486 pushOntoSorter(pParse
, v
, pOrderBy
);
488 char aff
= (iParm
>>16)&0xFF;
489 aff
= sqlite3CompareAffinity(pEList
->a
[0].pExpr
, aff
);
490 sqlite3VdbeOp3(v
, OP_MakeRecord
, 1, 0, &aff
, 1);
491 sqlite3VdbeAddOp(v
, OP_IdxInsert
, (iParm
&0x0000FFFF), 0);
493 sqlite3VdbeChangeP2(v
, addr2
, sqlite3VdbeCurrentAddr(v
));
497 /* If this is a scalar select that is part of an expression, then
498 ** store the results in the appropriate memory cell and break out
503 assert( nColumn
==1 );
505 pushOntoSorter(pParse
, v
, pOrderBy
);
507 sqlite3VdbeAddOp(v
, OP_MemStore
, iParm
, 1);
508 sqlite3VdbeAddOp(v
, OP_Goto
, 0, iBreak
);
512 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
514 /* Send the data to the callback function.
519 sqlite3VdbeAddOp(v
, OP_MakeRecord
, nColumn
, 0);
520 pushOntoSorter(pParse
, v
, pOrderBy
);
522 assert( eDest
==SRT_Callback
);
523 sqlite3VdbeAddOp(v
, OP_Callback
, nColumn
, 0);
528 /* Invoke a subroutine to handle the results. The subroutine itself
529 ** is responsible for popping the results off of the stack.
531 case SRT_Subroutine
: {
533 sqlite3VdbeAddOp(v
, OP_MakeRecord
, nColumn
, 0);
534 pushOntoSorter(pParse
, v
, pOrderBy
);
536 sqlite3VdbeAddOp(v
, OP_Gosub
, 0, iParm
);
541 #if !defined(SQLITE_OMIT_TRIGGER)
542 /* Discard the results. This is used for SELECT statements inside
543 ** the body of a TRIGGER. The purpose of such selects is to call
544 ** user-defined functions that have side effects. We do not care
545 ** about the actual results of the select.
548 assert( eDest
==SRT_Discard
);
549 sqlite3VdbeAddOp(v
, OP_Pop
, nColumn
, 0);
558 ** If the inner loop was generated using a non-null pOrderBy argument,
559 ** then the results were placed in a sorter. After the loop is terminated
560 ** we need to run the sorter and output the results. The following
561 ** routine generates the code needed to do that.
563 static void generateSortTail(
564 Parse
*pParse
, /* The parsing context */
565 Select
*p
, /* The SELECT statement */
566 Vdbe
*v
, /* Generate code into this VDBE */
567 int nColumn
, /* Number of columns of data */
568 int eDest
, /* Write the sorted results here */
569 int iParm
/* Optional parameter associated with eDest */
571 int end1
= sqlite3VdbeMakeLabel(v
);
572 int end2
= sqlite3VdbeMakeLabel(v
);
577 sqlite3
*db
= pParse
->db
;
579 if( eDest
==SRT_Sorter
) return;
580 pOrderBy
= p
->pOrderBy
;
581 nCol
= pOrderBy
->nExpr
;
582 pInfo
= sqliteMalloc( sizeof(*pInfo
) + nCol
*(sizeof(CollSeq
*)+1) );
583 if( pInfo
==0 ) return;
584 pInfo
->aSortOrder
= (char*)&pInfo
->aColl
[nCol
];
585 pInfo
->nField
= nCol
;
586 for(i
=0; i
<nCol
; i
++){
587 /* If a collation sequence was specified explicity, then it
588 ** is stored in pOrderBy->a[i].zName. Otherwise, use the default
589 ** collation type for the expression.
591 pInfo
->aColl
[i
] = sqlite3ExprCollSeq(pParse
, pOrderBy
->a
[i
].pExpr
);
592 if( !pInfo
->aColl
[i
] ){
593 pInfo
->aColl
[i
] = db
->pDfltColl
;
595 pInfo
->aSortOrder
[i
] = pOrderBy
->a
[i
].sortOrder
;
597 sqlite3VdbeOp3(v
, OP_Sort
, 0, 0, (char*)pInfo
, P3_KEYINFO_HANDOFF
);
598 addr
= sqlite3VdbeAddOp(v
, OP_SortNext
, 0, end1
);
599 codeLimiter(v
, p
, addr
, end2
, 1);
602 case SRT_TempTable
: {
603 sqlite3VdbeAddOp(v
, OP_NewRowid
, iParm
, 0);
604 sqlite3VdbeAddOp(v
, OP_Pull
, 1, 0);
605 sqlite3VdbeAddOp(v
, OP_Insert
, iParm
, 0);
608 #ifndef SQLITE_OMIT_SUBQUERY
610 assert( nColumn
==1 );
611 sqlite3VdbeAddOp(v
, OP_NotNull
, -1, sqlite3VdbeCurrentAddr(v
)+3);
612 sqlite3VdbeAddOp(v
, OP_Pop
, 1, 0);
613 sqlite3VdbeAddOp(v
, OP_Goto
, 0, sqlite3VdbeCurrentAddr(v
)+3);
614 sqlite3VdbeOp3(v
, OP_MakeRecord
, 1, 0, "n", P3_STATIC
);
615 sqlite3VdbeAddOp(v
, OP_IdxInsert
, (iParm
&0x0000FFFF), 0);
620 assert( nColumn
==1 );
621 sqlite3VdbeAddOp(v
, OP_MemStore
, iParm
, 1);
622 sqlite3VdbeAddOp(v
, OP_Goto
, 0, end1
);
627 case SRT_Subroutine
: {
629 sqlite3VdbeAddOp(v
, OP_Integer
, p
->pEList
->nExpr
, 0);
630 sqlite3VdbeAddOp(v
, OP_Pull
, 1, 0);
631 for(i
=0; i
<nColumn
; i
++){
632 sqlite3VdbeAddOp(v
, OP_Column
, -1-i
, i
);
634 if( eDest
==SRT_Callback
){
635 sqlite3VdbeAddOp(v
, OP_Callback
, nColumn
, 0);
637 sqlite3VdbeAddOp(v
, OP_Gosub
, 0, iParm
);
639 sqlite3VdbeAddOp(v
, OP_Pop
, 2, 0);
647 sqlite3VdbeAddOp(v
, OP_Goto
, 0, addr
);
648 sqlite3VdbeResolveLabel(v
, end2
);
649 sqlite3VdbeAddOp(v
, OP_Pop
, 1, 0);
650 sqlite3VdbeResolveLabel(v
, end1
);
651 sqlite3VdbeAddOp(v
, OP_SortReset
, 0, 0);
655 ** Return a pointer to a string containing the 'declaration type' of the
656 ** expression pExpr. The string may be treated as static by the caller.
658 ** If the declaration type is the exact datatype definition extracted from
659 ** the original CREATE TABLE statement if the expression is a column.
661 ** The declaration type for an expression is either TEXT, NUMERIC or ANY.
662 ** The declaration type for a ROWID field is INTEGER.
664 static const char *columnType(NameContext
*pNC
, Expr
*pExpr
){
667 if( pExpr
==0 || pNC
->pSrcList
==0 ) return 0;
669 /* The TK_AS operator can only occur in ORDER BY, GROUP BY, HAVING,
670 ** and LIMIT clauses. But pExpr originates in the result set of a
671 ** SELECT. So pExpr can never contain an AS operator.
673 assert( pExpr
->op
!=TK_AS
);
678 int iCol
= pExpr
->iColumn
;
679 while( pNC
&& !pTab
){
680 SrcList
*pTabList
= pNC
->pSrcList
;
681 for(j
=0;j
<pTabList
->nSrc
&& pTabList
->a
[j
].iCursor
!=pExpr
->iTable
;j
++);
682 if( j
<pTabList
->nSrc
){
683 pTab
= pTabList
->a
[j
].pTab
;
690 ** This can occurs if you have something like "SELECT new.x;" inside
691 ** a trigger. In other words, if you reference the special "new"
692 ** table in the result set of a select. We do not have a good way
693 ** to find the actual table type, so call it "TEXT". This is really
694 ** something of a bug, but I do not know how to fix it.
696 ** This code does not produce the correct answer - it just prevents
697 ** a segfault. See ticket #1229.
703 if( iCol
<0 ) iCol
= pTab
->iPKey
;
704 assert( iCol
==-1 || (iCol
>=0 && iCol
<pTab
->nCol
) );
708 zType
= pTab
->aCol
[iCol
].zType
;
712 #ifndef SQLITE_OMIT_SUBQUERY
715 Select
*pS
= pExpr
->pSelect
;
716 sNC
.pSrcList
= pExpr
->pSelect
->pSrc
;
718 zType
= columnType(&sNC
, pS
->pEList
->a
[0].pExpr
);
730 ** Generate code that will tell the VDBE the declaration types of columns
731 ** in the result set.
733 static void generateColumnTypes(
734 Parse
*pParse
, /* Parser context */
735 SrcList
*pTabList
, /* List of tables */
736 ExprList
*pEList
/* Expressions defining the result set */
738 Vdbe
*v
= pParse
->pVdbe
;
741 sNC
.pSrcList
= pTabList
;
742 for(i
=0; i
<pEList
->nExpr
; i
++){
743 Expr
*p
= pEList
->a
[i
].pExpr
;
744 const char *zType
= columnType(&sNC
, p
);
745 if( zType
==0 ) continue;
746 /* The vdbe must make it's own copy of the column-type, in case the
747 ** schema is reset before this virtual machine is deleted.
749 sqlite3VdbeSetColName(v
, i
+pEList
->nExpr
, zType
, strlen(zType
));
754 ** Generate code that will tell the VDBE the names of columns
755 ** in the result set. This information is used to provide the
756 ** azCol[] values in the callback.
758 static void generateColumnNames(
759 Parse
*pParse
, /* Parser context */
760 SrcList
*pTabList
, /* List of tables */
761 ExprList
*pEList
/* Expressions defining the result set */
763 Vdbe
*v
= pParse
->pVdbe
;
765 sqlite3
*db
= pParse
->db
;
766 int fullNames
, shortNames
;
768 #ifndef SQLITE_OMIT_EXPLAIN
769 /* If this is an EXPLAIN, skip this step */
770 if( pParse
->explain
){
776 if( pParse
->colNamesSet
|| v
==0 || sqlite3_malloc_failed
) return;
777 pParse
->colNamesSet
= 1;
778 fullNames
= (db
->flags
& SQLITE_FullColNames
)!=0;
779 shortNames
= (db
->flags
& SQLITE_ShortColNames
)!=0;
780 sqlite3VdbeSetNumCols(v
, pEList
->nExpr
);
781 for(i
=0; i
<pEList
->nExpr
; i
++){
783 p
= pEList
->a
[i
].pExpr
;
785 if( pEList
->a
[i
].zName
){
786 char *zName
= pEList
->a
[i
].zName
;
787 sqlite3VdbeSetColName(v
, i
, zName
, strlen(zName
));
790 if( p
->op
==TK_COLUMN
&& pTabList
){
793 int iCol
= p
->iColumn
;
794 for(j
=0; j
<pTabList
->nSrc
&& pTabList
->a
[j
].iCursor
!=p
->iTable
; j
++){}
795 assert( j
<pTabList
->nSrc
);
796 pTab
= pTabList
->a
[j
].pTab
;
797 if( iCol
<0 ) iCol
= pTab
->iPKey
;
798 assert( iCol
==-1 || (iCol
>=0 && iCol
<pTab
->nCol
) );
802 zCol
= pTab
->aCol
[iCol
].zName
;
804 if( !shortNames
&& !fullNames
&& p
->span
.z
&& p
->span
.z
[0] ){
805 sqlite3VdbeSetColName(v
, i
, p
->span
.z
, p
->span
.n
);
806 }else if( fullNames
|| (!shortNames
&& pTabList
->nSrc
>1) ){
810 zTab
= pTabList
->a
[j
].zAlias
;
811 if( fullNames
|| zTab
==0 ) zTab
= pTab
->zName
;
812 sqlite3SetString(&zName
, zTab
, ".", zCol
, 0);
813 sqlite3VdbeSetColName(v
, i
, zName
, P3_DYNAMIC
);
815 sqlite3VdbeSetColName(v
, i
, zCol
, strlen(zCol
));
817 }else if( p
->span
.z
&& p
->span
.z
[0] ){
818 sqlite3VdbeSetColName(v
, i
, p
->span
.z
, p
->span
.n
);
819 /* sqlite3VdbeCompressSpace(v, addr); */
822 assert( p
->op
!=TK_COLUMN
|| pTabList
==0 );
823 sprintf(zName
, "column%d", i
+1);
824 sqlite3VdbeSetColName(v
, i
, zName
, 0);
827 generateColumnTypes(pParse
, pTabList
, pEList
);
830 #ifndef SQLITE_OMIT_COMPOUND_SELECT
832 ** Name of the connection operator, used for error messages.
834 static const char *selectOpName(int id
){
837 case TK_ALL
: z
= "UNION ALL"; break;
838 case TK_INTERSECT
: z
= "INTERSECT"; break;
839 case TK_EXCEPT
: z
= "EXCEPT"; break;
840 default: z
= "UNION"; break;
844 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
847 ** Forward declaration
849 static int prepSelectStmt(Parse
*, Select
*);
852 ** Given a SELECT statement, generate a Table structure that describes
853 ** the result set of that SELECT.
855 Table
*sqlite3ResultSetOfSelect(Parse
*pParse
, char *zTabName
, Select
*pSelect
){
861 if( prepSelectStmt(pParse
, pSelect
) ){
864 if( sqlite3SelectResolve(pParse
, pSelect
, 0) ){
867 pTab
= sqliteMalloc( sizeof(Table
) );
872 pTab
->zName
= zTabName
? sqliteStrDup(zTabName
) : 0;
873 pEList
= pSelect
->pEList
;
874 pTab
->nCol
= pEList
->nExpr
;
875 assert( pTab
->nCol
>0 );
876 pTab
->aCol
= aCol
= sqliteMalloc( sizeof(pTab
->aCol
[0])*pTab
->nCol
);
877 for(i
=0, pCol
=aCol
; i
<pTab
->nCol
; i
++, pCol
++){
885 /* Get an appropriate name for the column
887 p
= pEList
->a
[i
].pExpr
;
888 assert( p
->pRight
==0 || p
->pRight
->token
.z
==0 || p
->pRight
->token
.z
[0]!=0 );
889 if( (zName
= pEList
->a
[i
].zName
)!=0 ){
890 /* If the column contains an "AS <name>" phrase, use <name> as the name */
891 zName
= sqliteStrDup(zName
);
892 }else if( p
->op
==TK_DOT
893 && (pR
=p
->pRight
)!=0 && pR
->token
.z
&& pR
->token
.z
[0] ){
894 /* For columns of the from A.B use B as the name */
895 zName
= sqlite3MPrintf("%T", &pR
->token
);
896 }else if( p
->span
.z
&& p
->span
.z
[0] ){
897 /* Use the original text of the column expression as its name */
898 zName
= sqlite3MPrintf("%T", &p
->span
);
900 /* If all else fails, make up a name */
901 zName
= sqlite3MPrintf("column%d", i
+1);
903 sqlite3Dequote(zName
);
904 if( sqlite3_malloc_failed
){
906 sqlite3DeleteTable(0, pTab
);
910 /* Make sure the column name is unique. If the name is not unique,
911 ** append a integer to the name so that it becomes unique.
914 for(j
=cnt
=0; j
<i
; j
++){
915 if( sqlite3StrICmp(aCol
[j
].zName
, zName
)==0 ){
916 zName
= sqlite3MPrintf("%s:%d", zBasename
, ++cnt
);
918 if( zName
==0 ) break;
921 if( zBasename
!=zName
){
922 sqliteFree(zBasename
);
926 /* Get the typename, type affinity, and collating sequence for the
929 memset(&sNC
, 0, sizeof(sNC
));
930 sNC
.pSrcList
= pSelect
->pSrc
;
931 zType
= sqliteStrDup(columnType(&sNC
, p
));
933 pCol
->affinity
= sqlite3ExprAffinity(p
);
934 pCol
->pColl
= sqlite3ExprCollSeq(pParse
, p
);
936 pCol
->pColl
= pParse
->db
->pDfltColl
;
944 ** Prepare a SELECT statement for processing by doing the following
947 ** (1) Make sure VDBE cursor numbers have been assigned to every
948 ** element of the FROM clause.
950 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
951 ** defines FROM clause. When views appear in the FROM clause,
952 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
953 ** that implements the view. A copy is made of the view's SELECT
954 ** statement so that we can freely modify or delete that statement
955 ** without worrying about messing up the presistent representation
958 ** (3) Add terms to the WHERE clause to accomodate the NATURAL keyword
959 ** on joins and the ON and USING clause of joins.
961 ** (4) Scan the list of columns in the result set (pEList) looking
962 ** for instances of the "*" operator or the TABLE.* operator.
963 ** If found, expand each "*" to be every column in every table
964 ** and TABLE.* to be every column in TABLE.
966 ** Return 0 on success. If there are problems, leave an error message
967 ** in pParse and return non-zero.
969 static int prepSelectStmt(Parse
*pParse
, Select
*p
){
974 struct SrcList_item
*pFrom
;
976 if( p
==0 || p
->pSrc
==0 || sqlite3_malloc_failed
) return 1;
980 /* Make sure cursor numbers have been assigned to all entries in
981 ** the FROM clause of the SELECT statement.
983 sqlite3SrcListAssignCursors(pParse
, p
->pSrc
);
985 /* Look up every table named in the FROM clause of the select. If
986 ** an entry of the FROM clause is a subquery instead of a table or view,
987 ** then create a transient table structure to describe the subquery.
989 for(i
=0, pFrom
=pTabList
->a
; i
<pTabList
->nSrc
; i
++, pFrom
++){
990 if( pFrom
->pTab
!=0 ){
991 /* This statement has already been prepared. There is no need
996 if( pFrom
->zName
==0 ){
997 #ifndef SQLITE_OMIT_SUBQUERY
998 /* A sub-query in the FROM clause of a SELECT */
999 assert( pFrom
->pSelect
!=0 );
1000 if( pFrom
->zAlias
==0 ){
1002 sqlite3MPrintf("sqlite_subquery_%p_", (void*)pFrom
->pSelect
);
1004 assert( pFrom
->pTab
==0 );
1005 pFrom
->pTab
= pTab
=
1006 sqlite3ResultSetOfSelect(pParse
, pFrom
->zAlias
, pFrom
->pSelect
);
1010 /* The isTransient flag indicates that the Table structure has been
1011 ** dynamically allocated and may be freed at any time. In other words,
1012 ** pTab is not pointing to a persistent table structure that defines
1013 ** part of the schema. */
1014 pTab
->isTransient
= 1;
1017 /* An ordinary table or view name in the FROM clause */
1018 assert( pFrom
->pTab
==0 );
1019 pFrom
->pTab
= pTab
=
1020 sqlite3LocateTable(pParse
,pFrom
->zName
,pFrom
->zDatabase
);
1025 #ifndef SQLITE_OMIT_VIEW
1026 if( pTab
->pSelect
){
1027 /* We reach here if the named table is a really a view */
1028 if( sqlite3ViewGetColumnNames(pParse
, pTab
) ){
1031 /* If pFrom->pSelect!=0 it means we are dealing with a
1032 ** view within a view. The SELECT structure has already been
1033 ** copied by the outer view so we can skip the copy step here
1034 ** in the inner view.
1036 if( pFrom
->pSelect
==0 ){
1037 pFrom
->pSelect
= sqlite3SelectDup(pTab
->pSelect
);
1044 /* Process NATURAL keywords, and ON and USING clauses of joins.
1046 if( sqliteProcessJoin(pParse
, p
) ) return 1;
1048 /* For every "*" that occurs in the column list, insert the names of
1049 ** all columns in all tables. And for every TABLE.* insert the names
1050 ** of all columns in TABLE. The parser inserted a special expression
1051 ** with the TK_ALL operator for each "*" that it found in the column list.
1052 ** The following code just has to locate the TK_ALL expressions and expand
1053 ** each one to the list of all columns in all tables.
1055 ** The first loop just checks to see if there are any "*" operators
1056 ** that need expanding.
1058 for(k
=0; k
<pEList
->nExpr
; k
++){
1059 Expr
*pE
= pEList
->a
[k
].pExpr
;
1060 if( pE
->op
==TK_ALL
) break;
1061 if( pE
->op
==TK_DOT
&& pE
->pRight
&& pE
->pRight
->op
==TK_ALL
1062 && pE
->pLeft
&& pE
->pLeft
->op
==TK_ID
) break;
1065 if( k
<pEList
->nExpr
){
1067 ** If we get here it means the result set contains one or more "*"
1068 ** operators that need to be expanded. Loop through each expression
1069 ** in the result set and expand them one by one.
1071 struct ExprList_item
*a
= pEList
->a
;
1073 int flags
= pParse
->db
->flags
;
1074 int longNames
= (flags
& SQLITE_FullColNames
)!=0 &&
1075 (flags
& SQLITE_ShortColNames
)==0;
1077 for(k
=0; k
<pEList
->nExpr
; k
++){
1078 Expr
*pE
= a
[k
].pExpr
;
1079 if( pE
->op
!=TK_ALL
&&
1080 (pE
->op
!=TK_DOT
|| pE
->pRight
==0 || pE
->pRight
->op
!=TK_ALL
) ){
1081 /* This particular expression does not need to be expanded.
1083 pNew
= sqlite3ExprListAppend(pNew
, a
[k
].pExpr
, 0);
1084 pNew
->a
[pNew
->nExpr
-1].zName
= a
[k
].zName
;
1088 /* This expression is a "*" or a "TABLE.*" and needs to be
1090 int tableSeen
= 0; /* Set to 1 when TABLE matches */
1091 char *zTName
; /* text of name of TABLE */
1092 if( pE
->op
==TK_DOT
&& pE
->pLeft
){
1093 zTName
= sqlite3NameFromToken(&pE
->pLeft
->token
);
1097 for(i
=0, pFrom
=pTabList
->a
; i
<pTabList
->nSrc
; i
++, pFrom
++){
1098 Table
*pTab
= pFrom
->pTab
;
1099 char *zTabName
= pFrom
->zAlias
;
1100 if( zTabName
==0 || zTabName
[0]==0 ){
1101 zTabName
= pTab
->zName
;
1103 if( zTName
&& (zTabName
==0 || zTabName
[0]==0 ||
1104 sqlite3StrICmp(zTName
, zTabName
)!=0) ){
1108 for(j
=0; j
<pTab
->nCol
; j
++){
1109 Expr
*pExpr
, *pLeft
, *pRight
;
1110 char *zName
= pTab
->aCol
[j
].zName
;
1113 struct SrcList_item
*pLeft
= &pTabList
->a
[i
-1];
1114 if( (pLeft
->jointype
& JT_NATURAL
)!=0 &&
1115 columnIndex(pLeft
->pTab
, zName
)>=0 ){
1116 /* In a NATURAL join, omit the join columns from the
1117 ** table on the right */
1120 if( sqlite3IdListIndex(pLeft
->pUsing
, zName
)>=0 ){
1121 /* In a join with a USING clause, omit columns in the
1122 ** using clause from the table on the right. */
1126 pRight
= sqlite3Expr(TK_ID
, 0, 0, 0);
1127 if( pRight
==0 ) break;
1128 setToken(&pRight
->token
, zName
);
1129 if( zTabName
&& (longNames
|| pTabList
->nSrc
>1) ){
1130 pLeft
= sqlite3Expr(TK_ID
, 0, 0, 0);
1131 pExpr
= sqlite3Expr(TK_DOT
, pLeft
, pRight
, 0);
1132 if( pExpr
==0 ) break;
1133 setToken(&pLeft
->token
, zTabName
);
1134 setToken(&pExpr
->span
, sqlite3MPrintf("%s.%s", zTabName
, zName
));
1135 pExpr
->span
.dyn
= 1;
1138 pExpr
->token
.dyn
= 0;
1141 pExpr
->span
= pExpr
->token
;
1144 pNew
= sqlite3ExprListAppend(pNew
, pExpr
, &pExpr
->span
);
1146 pNew
= sqlite3ExprListAppend(pNew
, pExpr
, &pRight
->token
);
1152 sqlite3ErrorMsg(pParse
, "no such table: %s", zTName
);
1154 sqlite3ErrorMsg(pParse
, "no tables specified");
1161 sqlite3ExprListDelete(pEList
);
1167 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1169 ** This routine associates entries in an ORDER BY expression list with
1170 ** columns in a result. For each ORDER BY expression, the opcode of
1171 ** the top-level node is changed to TK_COLUMN and the iColumn value of
1172 ** the top-level node is filled in with column number and the iTable
1173 ** value of the top-level node is filled with iTable parameter.
1175 ** If there are prior SELECT clauses, they are processed first. A match
1176 ** in an earlier SELECT takes precedence over a later SELECT.
1178 ** Any entry that does not match is flagged as an error. The number
1179 ** of errors is returned.
1181 static int matchOrderbyToColumn(
1182 Parse
*pParse
, /* A place to leave error messages */
1183 Select
*pSelect
, /* Match to result columns of this SELECT */
1184 ExprList
*pOrderBy
, /* The ORDER BY values to match against columns */
1185 int iTable
, /* Insert this value in iTable */
1186 int mustComplete
/* If TRUE all ORDER BYs must match */
1192 if( pSelect
==0 || pOrderBy
==0 ) return 1;
1194 for(i
=0; i
<pOrderBy
->nExpr
; i
++){ pOrderBy
->a
[i
].done
= 0; }
1196 if( prepSelectStmt(pParse
, pSelect
) ){
1199 if( pSelect
->pPrior
){
1200 if( matchOrderbyToColumn(pParse
, pSelect
->pPrior
, pOrderBy
, iTable
, 0) ){
1204 pEList
= pSelect
->pEList
;
1205 for(i
=0; i
<pOrderBy
->nExpr
; i
++){
1206 Expr
*pE
= pOrderBy
->a
[i
].pExpr
;
1208 if( pOrderBy
->a
[i
].done
) continue;
1209 if( sqlite3ExprIsInteger(pE
, &iCol
) ){
1210 if( iCol
<=0 || iCol
>pEList
->nExpr
){
1211 sqlite3ErrorMsg(pParse
,
1212 "ORDER BY position %d should be between 1 and %d",
1213 iCol
, pEList
->nExpr
);
1217 if( !mustComplete
) continue;
1220 for(j
=0; iCol
<0 && j
<pEList
->nExpr
; j
++){
1221 if( pEList
->a
[j
].zName
&& (pE
->op
==TK_ID
|| pE
->op
==TK_STRING
) ){
1222 char *zName
, *zLabel
;
1223 zName
= pEList
->a
[j
].zName
;
1224 zLabel
= sqlite3NameFromToken(&pE
->token
);
1225 assert( zLabel
!=0 );
1226 if( sqlite3StrICmp(zName
, zLabel
)==0 ){
1231 if( iCol
<0 && sqlite3ExprCompare(pE
, pEList
->a
[j
].pExpr
) ){
1238 pE
->iTable
= iTable
;
1240 pOrderBy
->a
[i
].done
= 1;
1242 if( iCol
<0 && mustComplete
){
1243 sqlite3ErrorMsg(pParse
,
1244 "ORDER BY term number %d does not match any result column", i
+1);
1251 #endif /* #ifndef SQLITE_OMIT_COMPOUND_SELECT */
1254 ** Get a VDBE for the given parser context. Create a new one if necessary.
1255 ** If an error occurs, return NULL and leave a message in pParse.
1257 Vdbe
*sqlite3GetVdbe(Parse
*pParse
){
1258 Vdbe
*v
= pParse
->pVdbe
;
1260 v
= pParse
->pVdbe
= sqlite3VdbeCreate(pParse
->db
);
1266 ** Compute the iLimit and iOffset fields of the SELECT based on the
1267 ** pLimit and pOffset expressions. nLimit and nOffset hold the expressions
1268 ** that appear in the original SQL statement after the LIMIT and OFFSET
1269 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
1270 ** are the integer memory register numbers for counters used to compute
1271 ** the limit and offset. If there is no limit and/or offset, then
1272 ** iLimit and iOffset are negative.
1274 ** This routine changes the values if iLimit and iOffset only if
1275 ** a limit or offset is defined by nLimit and nOffset. iLimit and
1276 ** iOffset should have been preset to appropriate default values
1277 ** (usually but not always -1) prior to calling this routine.
1278 ** Only if nLimit>=0 or nOffset>0 do the limit registers get
1279 ** redefined. The UNION ALL operator uses this property to force
1280 ** the reuse of the same limit and offset registers across multiple
1281 ** SELECT statements.
1283 static void computeLimitRegisters(Parse
*pParse
, Select
*p
){
1285 ** "LIMIT -1" always shows all rows. There is some
1286 ** contraversy about what the correct behavior should be.
1287 ** The current implementation interprets "LIMIT 0" to mean
1291 int iMem
= pParse
->nMem
++;
1292 Vdbe
*v
= sqlite3GetVdbe(pParse
);
1294 sqlite3ExprCode(pParse
, p
->pLimit
);
1295 sqlite3VdbeAddOp(v
, OP_MustBeInt
, 0, 0);
1296 sqlite3VdbeAddOp(v
, OP_Negative
, 0, 0);
1297 sqlite3VdbeAddOp(v
, OP_MemStore
, iMem
, 1);
1298 VdbeComment((v
, "# LIMIT counter"));
1302 int iMem
= pParse
->nMem
++;
1303 Vdbe
*v
= sqlite3GetVdbe(pParse
);
1305 sqlite3ExprCode(pParse
, p
->pOffset
);
1306 sqlite3VdbeAddOp(v
, OP_MustBeInt
, 0, 0);
1307 sqlite3VdbeAddOp(v
, OP_Negative
, 0, 0);
1308 sqlite3VdbeAddOp(v
, OP_MemStore
, iMem
, 1);
1309 VdbeComment((v
, "# OFFSET counter"));
1315 ** Generate VDBE instructions that will open a transient table that
1316 ** will be used for an index or to store keyed results for a compound
1317 ** select. In other words, open a transient table that needs a
1318 ** KeyInfo structure. The number of columns in the KeyInfo is determined
1319 ** by the result set of the SELECT statement in the second argument.
1321 ** Specifically, this routine is called to open an index table for
1322 ** DISTINCT, UNION, INTERSECT and EXCEPT select statements (but not
1325 ** The value returned is the address of the OP_OpenTemp instruction.
1327 static int openTempIndex(Parse
*pParse
, Select
*p
, int iTab
){
1330 sqlite3
*db
= pParse
->db
;
1332 Vdbe
*v
= pParse
->pVdbe
;
1335 if( prepSelectStmt(pParse
, p
) ){
1338 nColumn
= p
->pEList
->nExpr
;
1339 pKeyInfo
= sqliteMalloc( sizeof(*pKeyInfo
)+nColumn
*sizeof(CollSeq
*) );
1340 if( pKeyInfo
==0 ) return 0;
1341 pKeyInfo
->enc
= db
->enc
;
1342 pKeyInfo
->nField
= nColumn
;
1343 for(i
=0; i
<nColumn
; i
++){
1344 pKeyInfo
->aColl
[i
] = sqlite3ExprCollSeq(pParse
, p
->pEList
->a
[i
].pExpr
);
1345 if( !pKeyInfo
->aColl
[i
] ){
1346 pKeyInfo
->aColl
[i
] = db
->pDfltColl
;
1349 addr
= sqlite3VdbeOp3(v
, OP_OpenTemp
, iTab
, 0,
1350 (char*)pKeyInfo
, P3_KEYINFO_HANDOFF
);
1354 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1356 ** Add the address "addr" to the set of all OpenTemp opcode addresses
1357 ** that are being accumulated in p->ppOpenTemp.
1359 static int multiSelectOpenTempAddr(Select
*p
, int addr
){
1360 IdList
*pList
= *p
->ppOpenTemp
= sqlite3IdListAppend(*p
->ppOpenTemp
, 0);
1362 return SQLITE_NOMEM
;
1364 pList
->a
[pList
->nId
-1].idx
= addr
;
1367 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
1369 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1371 ** Return the appropriate collating sequence for the iCol-th column of
1372 ** the result set for the compound-select statement "p". Return NULL if
1373 ** the column has no default collating sequence.
1375 ** The collating sequence for the compound select is taken from the
1376 ** left-most term of the select that has a collating sequence.
1378 static CollSeq
*multiSelectCollSeq(Parse
*pParse
, Select
*p
, int iCol
){
1381 pRet
= multiSelectCollSeq(pParse
, p
->pPrior
, iCol
);
1386 pRet
= sqlite3ExprCollSeq(pParse
, p
->pEList
->a
[iCol
].pExpr
);
1390 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
1392 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1394 ** This routine is called to process a query that is really the union
1395 ** or intersection of two or more separate queries.
1397 ** "p" points to the right-most of the two queries. the query on the
1398 ** left is p->pPrior. The left query could also be a compound query
1399 ** in which case this routine will be called recursively.
1401 ** The results of the total query are to be written into a destination
1402 ** of type eDest with parameter iParm.
1404 ** Example 1: Consider a three-way compound SQL statement.
1406 ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
1408 ** This statement is parsed up as follows:
1412 ** `-----> SELECT b FROM t2
1414 ** `------> SELECT a FROM t1
1416 ** The arrows in the diagram above represent the Select.pPrior pointer.
1417 ** So if this routine is called with p equal to the t3 query, then
1418 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
1420 ** Notice that because of the way SQLite parses compound SELECTs, the
1421 ** individual selects always group from left to right.
1423 static int multiSelect(
1424 Parse
*pParse
, /* Parsing context */
1425 Select
*p
, /* The right-most of SELECTs to be coded */
1426 int eDest
, /* \___ Store query results as specified */
1427 int iParm
, /* / by these two parameters. */
1428 char *aff
/* If eDest is SRT_Union, the affinity string */
1430 int rc
= SQLITE_OK
; /* Success code from a subroutine */
1431 Select
*pPrior
; /* Another SELECT immediately to our left */
1432 Vdbe
*v
; /* Generate code to this VDBE */
1433 IdList
*pOpenTemp
= 0;/* OP_OpenTemp opcodes that need a KeyInfo */
1434 int aAddr
[5]; /* Addresses of SetNumColumns operators */
1435 int nAddr
= 0; /* Number used */
1436 int nCol
; /* Number of columns in the result set */
1438 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
1439 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
1441 if( p
==0 || p
->pPrior
==0 ){
1443 goto multi_select_end
;
1446 if( pPrior
->pOrderBy
){
1447 sqlite3ErrorMsg(pParse
,"ORDER BY clause should come after %s not before",
1448 selectOpName(p
->op
));
1450 goto multi_select_end
;
1452 if( pPrior
->pLimit
){
1453 sqlite3ErrorMsg(pParse
,"LIMIT clause should come after %s not before",
1454 selectOpName(p
->op
));
1456 goto multi_select_end
;
1459 /* Make sure we have a valid query engine. If not, create a new one.
1461 v
= sqlite3GetVdbe(pParse
);
1464 goto multi_select_end
;
1467 /* If *p this is the right-most select statement, then initialize
1468 ** p->ppOpenTemp to point to pOpenTemp. If *p is not the right most
1469 ** statement then p->ppOpenTemp will have already been initialized
1470 ** by a prior call to this same procedure. Pass along the pOpenTemp
1471 ** pointer to pPrior, the next statement to our left.
1473 if( p
->ppOpenTemp
==0 ){
1474 p
->ppOpenTemp
= &pOpenTemp
;
1476 pPrior
->ppOpenTemp
= p
->ppOpenTemp
;
1478 /* Create the destination temporary table if necessary
1480 if( eDest
==SRT_TempTable
){
1481 assert( p
->pEList
);
1482 sqlite3VdbeAddOp(v
, OP_OpenTemp
, iParm
, 0);
1484 aAddr
[nAddr
++] = sqlite3VdbeAddOp(v
, OP_SetNumColumns
, iParm
, 0);
1488 /* Generate code for the left and right SELECT statements.
1492 if( p
->pOrderBy
==0 ){
1493 assert( !pPrior
->pLimit
);
1494 pPrior
->pLimit
= p
->pLimit
;
1495 pPrior
->pOffset
= p
->pOffset
;
1496 rc
= sqlite3Select(pParse
, pPrior
, eDest
, iParm
, 0, 0, 0, aff
);
1498 goto multi_select_end
;
1501 p
->iLimit
= pPrior
->iLimit
;
1502 p
->iOffset
= pPrior
->iOffset
;
1505 rc
= sqlite3Select(pParse
, p
, eDest
, iParm
, 0, 0, 0, aff
);
1508 goto multi_select_end
;
1512 /* For UNION ALL ... ORDER BY fall through to the next case */
1516 int unionTab
; /* Cursor number of the temporary table holding result */
1517 int op
= 0; /* One of the SRT_ operations to apply to self */
1518 int priorOp
; /* The SRT_ operation to apply to prior selects */
1519 Expr
*pLimit
, *pOffset
; /* Saved values of p->nLimit and p->nOffset */
1520 ExprList
*pOrderBy
; /* The ORDER BY clause for the right SELECT */
1523 priorOp
= p
->op
==TK_ALL
? SRT_Table
: SRT_Union
;
1524 if( eDest
==priorOp
&& p
->pOrderBy
==0 && !p
->pLimit
&& !p
->pOffset
){
1525 /* We can reuse a temporary table generated by a SELECT to our
1530 /* We will need to create our own temporary table to hold the
1531 ** intermediate results.
1533 unionTab
= pParse
->nTab
++;
1535 && matchOrderbyToColumn(pParse
, p
, p
->pOrderBy
, unionTab
, 1) ){
1537 goto multi_select_end
;
1539 addr
= sqlite3VdbeAddOp(v
, OP_OpenTemp
, unionTab
, 0);
1540 if( p
->op
!=TK_ALL
){
1541 rc
= multiSelectOpenTempAddr(p
, addr
);
1542 if( rc
!=SQLITE_OK
){
1543 goto multi_select_end
;
1546 assert( nAddr
<sizeof(aAddr
)/sizeof(aAddr
[0]) );
1547 aAddr
[nAddr
++] = sqlite3VdbeAddOp(v
, OP_SetNumColumns
, unionTab
, 0);
1548 assert( p
->pEList
);
1551 /* Code the SELECT statements to our left
1553 assert( !pPrior
->pOrderBy
);
1554 rc
= sqlite3Select(pParse
, pPrior
, priorOp
, unionTab
, 0, 0, 0, aff
);
1556 goto multi_select_end
;
1559 /* Code the current SELECT statement
1562 case TK_EXCEPT
: op
= SRT_Except
; break;
1563 case TK_UNION
: op
= SRT_Union
; break;
1564 case TK_ALL
: op
= SRT_Table
; break;
1567 pOrderBy
= p
->pOrderBy
;
1571 pOffset
= p
->pOffset
;
1573 rc
= sqlite3Select(pParse
, p
, op
, unionTab
, 0, 0, 0, aff
);
1575 p
->pOrderBy
= pOrderBy
;
1576 sqlite3ExprDelete(p
->pLimit
);
1578 p
->pOffset
= pOffset
;
1582 goto multi_select_end
;
1586 /* Convert the data in the temporary table into whatever form
1587 ** it is that we currently need.
1589 if( eDest
!=priorOp
|| unionTab
!=iParm
){
1590 int iCont
, iBreak
, iStart
;
1591 assert( p
->pEList
);
1592 if( eDest
==SRT_Callback
){
1593 generateColumnNames(pParse
, 0, p
->pEList
);
1595 iBreak
= sqlite3VdbeMakeLabel(v
);
1596 iCont
= sqlite3VdbeMakeLabel(v
);
1597 sqlite3VdbeAddOp(v
, OP_Rewind
, unionTab
, iBreak
);
1598 computeLimitRegisters(pParse
, p
);
1599 iStart
= sqlite3VdbeCurrentAddr(v
);
1600 rc
= selectInnerLoop(pParse
, p
, p
->pEList
, unionTab
, p
->pEList
->nExpr
,
1601 p
->pOrderBy
, -1, eDest
, iParm
,
1605 goto multi_select_end
;
1607 sqlite3VdbeResolveLabel(v
, iCont
);
1608 sqlite3VdbeAddOp(v
, OP_Next
, unionTab
, iStart
);
1609 sqlite3VdbeResolveLabel(v
, iBreak
);
1610 sqlite3VdbeAddOp(v
, OP_Close
, unionTab
, 0);
1614 case TK_INTERSECT
: {
1616 int iCont
, iBreak
, iStart
;
1617 Expr
*pLimit
, *pOffset
;
1620 /* INTERSECT is different from the others since it requires
1621 ** two temporary tables. Hence it has its own case. Begin
1622 ** by allocating the tables we will need.
1624 tab1
= pParse
->nTab
++;
1625 tab2
= pParse
->nTab
++;
1626 if( p
->pOrderBy
&& matchOrderbyToColumn(pParse
,p
,p
->pOrderBy
,tab1
,1) ){
1628 goto multi_select_end
;
1631 addr
= sqlite3VdbeAddOp(v
, OP_OpenTemp
, tab1
, 0);
1632 rc
= multiSelectOpenTempAddr(p
, addr
);
1633 if( rc
!=SQLITE_OK
){
1634 goto multi_select_end
;
1636 assert( nAddr
<sizeof(aAddr
)/sizeof(aAddr
[0]) );
1637 aAddr
[nAddr
++] = sqlite3VdbeAddOp(v
, OP_SetNumColumns
, tab1
, 0);
1638 assert( p
->pEList
);
1640 /* Code the SELECTs to our left into temporary table "tab1".
1642 rc
= sqlite3Select(pParse
, pPrior
, SRT_Union
, tab1
, 0, 0, 0, aff
);
1644 goto multi_select_end
;
1647 /* Code the current SELECT into temporary table "tab2"
1649 addr
= sqlite3VdbeAddOp(v
, OP_OpenTemp
, tab2
, 0);
1650 rc
= multiSelectOpenTempAddr(p
, addr
);
1651 if( rc
!=SQLITE_OK
){
1652 goto multi_select_end
;
1654 assert( nAddr
<sizeof(aAddr
)/sizeof(aAddr
[0]) );
1655 aAddr
[nAddr
++] = sqlite3VdbeAddOp(v
, OP_SetNumColumns
, tab2
, 0);
1659 pOffset
= p
->pOffset
;
1661 rc
= sqlite3Select(pParse
, p
, SRT_Union
, tab2
, 0, 0, 0, aff
);
1663 sqlite3ExprDelete(p
->pLimit
);
1665 p
->pOffset
= pOffset
;
1667 goto multi_select_end
;
1670 /* Generate code to take the intersection of the two temporary
1673 assert( p
->pEList
);
1674 if( eDest
==SRT_Callback
){
1675 generateColumnNames(pParse
, 0, p
->pEList
);
1677 iBreak
= sqlite3VdbeMakeLabel(v
);
1678 iCont
= sqlite3VdbeMakeLabel(v
);
1679 sqlite3VdbeAddOp(v
, OP_Rewind
, tab1
, iBreak
);
1680 computeLimitRegisters(pParse
, p
);
1681 iStart
= sqlite3VdbeAddOp(v
, OP_RowKey
, tab1
, 0);
1682 sqlite3VdbeAddOp(v
, OP_NotFound
, tab2
, iCont
);
1683 rc
= selectInnerLoop(pParse
, p
, p
->pEList
, tab1
, p
->pEList
->nExpr
,
1684 p
->pOrderBy
, -1, eDest
, iParm
,
1688 goto multi_select_end
;
1690 sqlite3VdbeResolveLabel(v
, iCont
);
1691 sqlite3VdbeAddOp(v
, OP_Next
, tab1
, iStart
);
1692 sqlite3VdbeResolveLabel(v
, iBreak
);
1693 sqlite3VdbeAddOp(v
, OP_Close
, tab2
, 0);
1694 sqlite3VdbeAddOp(v
, OP_Close
, tab1
, 0);
1699 /* Make sure all SELECTs in the statement have the same number of elements
1700 ** in their result sets.
1702 assert( p
->pEList
&& pPrior
->pEList
);
1703 if( p
->pEList
->nExpr
!=pPrior
->pEList
->nExpr
){
1704 sqlite3ErrorMsg(pParse
, "SELECTs to the left and right of %s"
1705 " do not have the same number of result columns", selectOpName(p
->op
));
1707 goto multi_select_end
;
1710 /* Set the number of columns in temporary tables
1712 nCol
= p
->pEList
->nExpr
;
1715 sqlite3VdbeChangeP2(v
, aAddr
[nAddr
], nCol
);
1718 /* Compute collating sequences used by either the ORDER BY clause or
1719 ** by any temporary tables needed to implement the compound select.
1720 ** Attach the KeyInfo structure to all temporary tables. Invoke the
1721 ** ORDER BY processing if there is an ORDER BY clause.
1723 ** This section is run by the right-most SELECT statement only.
1724 ** SELECT statements to the left always skip this part. The right-most
1725 ** SELECT might also skip this part if it has no ORDER BY clause and
1726 ** no temp tables are required.
1728 if( p
->pOrderBy
|| (pOpenTemp
&& pOpenTemp
->nId
>0) ){
1729 int i
; /* Loop counter */
1730 KeyInfo
*pKeyInfo
; /* Collating sequence for the result set */
1732 assert( p
->ppOpenTemp
== &pOpenTemp
);
1733 pKeyInfo
= sqliteMalloc(sizeof(*pKeyInfo
)+nCol
*sizeof(CollSeq
*));
1736 goto multi_select_end
;
1739 pKeyInfo
->enc
= pParse
->db
->enc
;
1740 pKeyInfo
->nField
= nCol
;
1742 for(i
=0; i
<nCol
; i
++){
1743 pKeyInfo
->aColl
[i
] = multiSelectCollSeq(pParse
, p
, i
);
1744 if( !pKeyInfo
->aColl
[i
] ){
1745 pKeyInfo
->aColl
[i
] = pParse
->db
->pDfltColl
;
1749 for(i
=0; pOpenTemp
&& i
<pOpenTemp
->nId
; i
++){
1750 int p3type
= (i
==0?P3_KEYINFO_HANDOFF
:P3_KEYINFO
);
1751 int addr
= pOpenTemp
->a
[i
].idx
;
1752 sqlite3VdbeChangeP3(v
, addr
, (char *)pKeyInfo
, p3type
);
1756 struct ExprList_item
*pOrderByTerm
= p
->pOrderBy
->a
;
1757 for(i
=0; i
<p
->pOrderBy
->nExpr
; i
++, pOrderByTerm
++){
1758 Expr
*pExpr
= pOrderByTerm
->pExpr
;
1759 char *zName
= pOrderByTerm
->zName
;
1760 assert( pExpr
->op
==TK_COLUMN
&& pExpr
->iColumn
<nCol
);
1761 /* assert( !pExpr->pColl ); */
1763 pExpr
->pColl
= sqlite3LocateCollSeq(pParse
, zName
, -1);
1765 pExpr
->pColl
= pKeyInfo
->aColl
[pExpr
->iColumn
];
1768 generateSortTail(pParse
, p
, v
, p
->pEList
->nExpr
, eDest
, iParm
);
1772 /* This happens for UNION ALL ... ORDER BY */
1773 sqliteFree(pKeyInfo
);
1779 sqlite3IdListDelete(pOpenTemp
);
1784 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
1786 #ifndef SQLITE_OMIT_VIEW
1788 ** Scan through the expression pExpr. Replace every reference to
1789 ** a column in table number iTable with a copy of the iColumn-th
1790 ** entry in pEList. (But leave references to the ROWID column
1793 ** This routine is part of the flattening procedure. A subquery
1794 ** whose result set is defined by pEList appears as entry in the
1795 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
1796 ** FORM clause entry is iTable. This routine make the necessary
1797 ** changes to pExpr so that it refers directly to the source table
1798 ** of the subquery rather the result set of the subquery.
1800 static void substExprList(ExprList
*,int,ExprList
*); /* Forward Decl */
1801 static void substSelect(Select
*, int, ExprList
*); /* Forward Decl */
1802 static void substExpr(Expr
*pExpr
, int iTable
, ExprList
*pEList
){
1803 if( pExpr
==0 ) return;
1804 if( pExpr
->op
==TK_COLUMN
&& pExpr
->iTable
==iTable
){
1805 if( pExpr
->iColumn
<0 ){
1806 pExpr
->op
= TK_NULL
;
1809 assert( pEList
!=0 && pExpr
->iColumn
<pEList
->nExpr
);
1810 assert( pExpr
->pLeft
==0 && pExpr
->pRight
==0 && pExpr
->pList
==0 );
1811 pNew
= pEList
->a
[pExpr
->iColumn
].pExpr
;
1813 pExpr
->op
= pNew
->op
;
1814 assert( pExpr
->pLeft
==0 );
1815 pExpr
->pLeft
= sqlite3ExprDup(pNew
->pLeft
);
1816 assert( pExpr
->pRight
==0 );
1817 pExpr
->pRight
= sqlite3ExprDup(pNew
->pRight
);
1818 assert( pExpr
->pList
==0 );
1819 pExpr
->pList
= sqlite3ExprListDup(pNew
->pList
);
1820 pExpr
->iTable
= pNew
->iTable
;
1821 pExpr
->iColumn
= pNew
->iColumn
;
1822 pExpr
->iAgg
= pNew
->iAgg
;
1823 sqlite3TokenCopy(&pExpr
->token
, &pNew
->token
);
1824 sqlite3TokenCopy(&pExpr
->span
, &pNew
->span
);
1825 pExpr
->pSelect
= sqlite3SelectDup(pNew
->pSelect
);
1826 pExpr
->flags
= pNew
->flags
;
1829 substExpr(pExpr
->pLeft
, iTable
, pEList
);
1830 substExpr(pExpr
->pRight
, iTable
, pEList
);
1831 substSelect(pExpr
->pSelect
, iTable
, pEList
);
1832 substExprList(pExpr
->pList
, iTable
, pEList
);
1835 static void substExprList(ExprList
*pList
, int iTable
, ExprList
*pEList
){
1837 if( pList
==0 ) return;
1838 for(i
=0; i
<pList
->nExpr
; i
++){
1839 substExpr(pList
->a
[i
].pExpr
, iTable
, pEList
);
1842 static void substSelect(Select
*p
, int iTable
, ExprList
*pEList
){
1844 substExprList(p
->pEList
, iTable
, pEList
);
1845 substExprList(p
->pGroupBy
, iTable
, pEList
);
1846 substExprList(p
->pOrderBy
, iTable
, pEList
);
1847 substExpr(p
->pHaving
, iTable
, pEList
);
1848 substExpr(p
->pWhere
, iTable
, pEList
);
1850 #endif /* !defined(SQLITE_OMIT_VIEW) */
1852 #ifndef SQLITE_OMIT_VIEW
1854 ** This routine attempts to flatten subqueries in order to speed
1855 ** execution. It returns 1 if it makes changes and 0 if no flattening
1858 ** To understand the concept of flattening, consider the following
1861 ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
1863 ** The default way of implementing this query is to execute the
1864 ** subquery first and store the results in a temporary table, then
1865 ** run the outer query on that temporary table. This requires two
1866 ** passes over the data. Furthermore, because the temporary table
1867 ** has no indices, the WHERE clause on the outer query cannot be
1870 ** This routine attempts to rewrite queries such as the above into
1871 ** a single flat select, like this:
1873 ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
1875 ** The code generated for this simpification gives the same result
1876 ** but only has to scan the data once. And because indices might
1877 ** exist on the table t1, a complete scan of the data might be
1880 ** Flattening is only attempted if all of the following are true:
1882 ** (1) The subquery and the outer query do not both use aggregates.
1884 ** (2) The subquery is not an aggregate or the outer query is not a join.
1886 ** (3) The subquery is not the right operand of a left outer join, or
1887 ** the subquery is not itself a join. (Ticket #306)
1889 ** (4) The subquery is not DISTINCT or the outer query is not a join.
1891 ** (5) The subquery is not DISTINCT or the outer query does not use
1894 ** (6) The subquery does not use aggregates or the outer query is not
1897 ** (7) The subquery has a FROM clause.
1899 ** (8) The subquery does not use LIMIT or the outer query is not a join.
1901 ** (9) The subquery does not use LIMIT or the outer query does not use
1904 ** (10) The subquery does not use aggregates or the outer query does not
1907 ** (11) The subquery and the outer query do not both have ORDER BY clauses.
1909 ** (12) The subquery is not the right term of a LEFT OUTER JOIN or the
1910 ** subquery has no WHERE clause. (added by ticket #350)
1912 ** In this routine, the "p" parameter is a pointer to the outer query.
1913 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
1914 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1916 ** If flattening is not attempted, this routine is a no-op and returns 0.
1917 ** If flattening is attempted this routine returns 1.
1919 ** All of the expression analysis must occur on both the outer query and
1920 ** the subquery before this routine runs.
1922 static int flattenSubquery(
1923 Parse
*pParse
, /* The parsing context */
1924 Select
*p
, /* The parent or outer SELECT statement */
1925 int iFrom
, /* Index in p->pSrc->a[] of the inner subquery */
1926 int isAgg
, /* True if outer SELECT uses aggregate functions */
1927 int subqueryIsAgg
/* True if the subquery uses aggregate functions */
1929 Select
*pSub
; /* The inner query or "subquery" */
1930 SrcList
*pSrc
; /* The FROM clause of the outer query */
1931 SrcList
*pSubSrc
; /* The FROM clause of the subquery */
1932 ExprList
*pList
; /* The result set of the outer query */
1933 int iParent
; /* VDBE cursor number of the pSub result set temp table */
1934 int i
; /* Loop counter */
1935 Expr
*pWhere
; /* The WHERE clause */
1936 struct SrcList_item
*pSubitem
; /* The subquery */
1938 /* Check to see if flattening is permitted. Return 0 if not.
1940 if( p
==0 ) return 0;
1942 assert( pSrc
&& iFrom
>=0 && iFrom
<pSrc
->nSrc
);
1943 pSubitem
= &pSrc
->a
[iFrom
];
1944 pSub
= pSubitem
->pSelect
;
1946 if( isAgg
&& subqueryIsAgg
) return 0;
1947 if( subqueryIsAgg
&& pSrc
->nSrc
>1 ) return 0;
1948 pSubSrc
= pSub
->pSrc
;
1950 if( (pSub
->pLimit
&& p
->pLimit
) || pSub
->pOffset
||
1951 (pSub
->pLimit
&& isAgg
) ) return 0;
1952 if( pSubSrc
->nSrc
==0 ) return 0;
1953 if( pSub
->isDistinct
&& (pSrc
->nSrc
>1 || isAgg
) ){
1956 if( p
->isDistinct
&& subqueryIsAgg
) return 0;
1957 if( p
->pOrderBy
&& pSub
->pOrderBy
) return 0;
1959 /* Restriction 3: If the subquery is a join, make sure the subquery is
1960 ** not used as the right operand of an outer join. Examples of why this
1963 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
1965 ** If we flatten the above, we would get
1967 ** (t1 LEFT OUTER JOIN t2) JOIN t3
1969 ** which is not at all the same thing.
1971 if( pSubSrc
->nSrc
>1 && iFrom
>0 && (pSrc
->a
[iFrom
-1].jointype
& JT_OUTER
)!=0 ){
1975 /* Restriction 12: If the subquery is the right operand of a left outer
1976 ** join, make sure the subquery has no WHERE clause.
1977 ** An examples of why this is not allowed:
1979 ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
1981 ** If we flatten the above, we would get
1983 ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
1985 ** But the t2.x>0 test will always fail on a NULL row of t2, which
1986 ** effectively converts the OUTER JOIN into an INNER JOIN.
1988 if( iFrom
>0 && (pSrc
->a
[iFrom
-1].jointype
& JT_OUTER
)!=0
1989 && pSub
->pWhere
!=0 ){
1993 /* If we reach this point, it means flattening is permitted for the
1994 ** iFrom-th entry of the FROM clause in the outer query.
1997 /* Move all of the FROM elements of the subquery into the
1998 ** the FROM clause of the outer query. Before doing this, remember
1999 ** the cursor number for the original outer query FROM element in
2000 ** iParent. The iParent cursor will never be used. Subsequent code
2001 ** will scan expressions looking for iParent references and replace
2002 ** those references with expressions that resolve to the subquery FROM
2003 ** elements we are now copying in.
2005 iParent
= pSubitem
->iCursor
;
2007 int nSubSrc
= pSubSrc
->nSrc
;
2008 int jointype
= pSubitem
->jointype
;
2010 sqlite3DeleteTable(0, pSubitem
->pTab
);
2011 sqliteFree(pSubitem
->zDatabase
);
2012 sqliteFree(pSubitem
->zName
);
2013 sqliteFree(pSubitem
->zAlias
);
2015 int extra
= nSubSrc
- 1;
2016 for(i
=1; i
<nSubSrc
; i
++){
2017 pSrc
= sqlite3SrcListAppend(pSrc
, 0, 0);
2020 for(i
=pSrc
->nSrc
-1; i
-extra
>=iFrom
; i
--){
2021 pSrc
->a
[i
] = pSrc
->a
[i
-extra
];
2024 for(i
=0; i
<nSubSrc
; i
++){
2025 pSrc
->a
[i
+iFrom
] = pSubSrc
->a
[i
];
2026 memset(&pSubSrc
->a
[i
], 0, sizeof(pSubSrc
->a
[i
]));
2028 pSrc
->a
[iFrom
+nSubSrc
-1].jointype
= jointype
;
2031 /* Now begin substituting subquery result set expressions for
2032 ** references to the iParent in the outer query.
2036 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
2037 ** \ \_____________ subquery __________/ /
2038 ** \_____________________ outer query ______________________________/
2040 ** We look at every expression in the outer query and every place we see
2041 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
2043 substExprList(p
->pEList
, iParent
, pSub
->pEList
);
2045 for(i
=0; i
<pList
->nExpr
; i
++){
2047 if( pList
->a
[i
].zName
==0 && (pExpr
= pList
->a
[i
].pExpr
)->span
.z
!=0 ){
2048 pList
->a
[i
].zName
= sqliteStrNDup(pExpr
->span
.z
, pExpr
->span
.n
);
2052 substExprList(p
->pGroupBy
, iParent
, pSub
->pEList
);
2053 substExpr(p
->pHaving
, iParent
, pSub
->pEList
);
2055 if( pSub
->pOrderBy
){
2056 assert( p
->pOrderBy
==0 );
2057 p
->pOrderBy
= pSub
->pOrderBy
;
2059 }else if( p
->pOrderBy
){
2060 substExprList(p
->pOrderBy
, iParent
, pSub
->pEList
);
2063 pWhere
= sqlite3ExprDup(pSub
->pWhere
);
2067 if( subqueryIsAgg
){
2068 assert( p
->pHaving
==0 );
2069 p
->pHaving
= p
->pWhere
;
2071 substExpr(p
->pHaving
, iParent
, pSub
->pEList
);
2072 p
->pHaving
= sqlite3ExprAnd(p
->pHaving
, sqlite3ExprDup(pSub
->pHaving
));
2073 assert( p
->pGroupBy
==0 );
2074 p
->pGroupBy
= sqlite3ExprListDup(pSub
->pGroupBy
);
2076 substExpr(p
->pWhere
, iParent
, pSub
->pEList
);
2077 p
->pWhere
= sqlite3ExprAnd(p
->pWhere
, pWhere
);
2080 /* The flattened query is distinct if either the inner or the
2081 ** outer query is distinct.
2083 p
->isDistinct
= p
->isDistinct
|| pSub
->isDistinct
;
2086 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
2089 p
->pLimit
= pSub
->pLimit
;
2093 /* Finially, delete what is left of the subquery and return
2096 sqlite3SelectDelete(pSub
);
2099 #endif /* SQLITE_OMIT_VIEW */
2102 ** Analyze the SELECT statement passed in as an argument to see if it
2103 ** is a simple min() or max() query. If it is and this query can be
2104 ** satisfied using a single seek to the beginning or end of an index,
2105 ** then generate the code for this SELECT and return 1. If this is not a
2106 ** simple min() or max() query, then return 0;
2108 ** A simply min() or max() query looks like this:
2110 ** SELECT min(a) FROM table;
2111 ** SELECT max(a) FROM table;
2113 ** The query may have only a single table in its FROM argument. There
2114 ** can be no GROUP BY or HAVING or WHERE clauses. The result set must
2115 ** be the min() or max() of a single column of the table. The column
2116 ** in the min() or max() function must be indexed.
2118 ** The parameters to this routine are the same as for sqlite3Select().
2119 ** See the header comment on that routine for additional information.
2121 static int simpleMinMaxQuery(Parse
*pParse
, Select
*p
, int eDest
, int iParm
){
2130 ExprList
*pEList
, *pList
, eList
;
2131 struct ExprList_item eListItem
;
2134 /* Check to see if this query is a simple min() or max() query. Return
2135 ** zero if it is not.
2137 if( p
->pGroupBy
|| p
->pHaving
|| p
->pWhere
) return 0;
2139 if( pSrc
->nSrc
!=1 ) return 0;
2141 if( pEList
->nExpr
!=1 ) return 0;
2142 pExpr
= pEList
->a
[0].pExpr
;
2143 if( pExpr
->op
!=TK_AGG_FUNCTION
) return 0;
2144 pList
= pExpr
->pList
;
2145 if( pList
==0 || pList
->nExpr
!=1 ) return 0;
2146 if( pExpr
->token
.n
!=3 ) return 0;
2147 if( sqlite3StrNICmp(pExpr
->token
.z
,"min",3)==0 ){
2149 }else if( sqlite3StrNICmp(pExpr
->token
.z
,"max",3)==0 ){
2154 pExpr
= pList
->a
[0].pExpr
;
2155 if( pExpr
->op
!=TK_COLUMN
) return 0;
2156 iCol
= pExpr
->iColumn
;
2157 pTab
= pSrc
->a
[0].pTab
;
2159 /* If we get to here, it means the query is of the correct form.
2160 ** Check to make sure we have an index and make pIdx point to the
2161 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
2162 ** key column, no index is necessary so set pIdx to NULL. If no
2163 ** usable index is found, return 0.
2168 CollSeq
*pColl
= sqlite3ExprCollSeq(pParse
, pExpr
);
2169 for(pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
){
2170 assert( pIdx
->nColumn
>=1 );
2171 if( pIdx
->aiColumn
[0]==iCol
&& pIdx
->keyInfo
.aColl
[0]==pColl
) break;
2173 if( pIdx
==0 ) return 0;
2176 /* Identify column types if we will be using the callback. This
2177 ** step is skipped if the output is going to a table or a memory cell.
2178 ** The column names have already been generated in the calling function.
2180 v
= sqlite3GetVdbe(pParse
);
2181 if( v
==0 ) return 0;
2183 /* If the output is destined for a temporary table, open that table.
2185 if( eDest
==SRT_TempTable
){
2186 sqlite3VdbeAddOp(v
, OP_OpenTemp
, iParm
, 0);
2187 sqlite3VdbeAddOp(v
, OP_SetNumColumns
, iParm
, 1);
2190 /* Generating code to find the min or the max. Basically all we have
2191 ** to do is find the first or the last entry in the chosen index. If
2192 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
2193 ** or last entry in the main table.
2195 sqlite3CodeVerifySchema(pParse
, pTab
->iDb
);
2196 base
= pSrc
->a
[0].iCursor
;
2197 computeLimitRegisters(pParse
, p
);
2198 if( pSrc
->a
[0].pSelect
==0 ){
2199 sqlite3OpenTableForReading(v
, base
, pTab
);
2201 cont
= sqlite3VdbeMakeLabel(v
);
2203 sqlite3VdbeAddOp(v
, seekOp
, base
, 0);
2205 /* Even though the cursor used to open the index here is closed
2206 ** as soon as a single value has been read from it, allocate it
2207 ** using (pParse->nTab++) to prevent the cursor id from being
2208 ** reused. This is important for statements of the form
2209 ** "INSERT INTO x SELECT max() FROM x".
2212 iIdx
= pParse
->nTab
++;
2213 sqlite3VdbeAddOp(v
, OP_Integer
, pIdx
->iDb
, 0);
2214 sqlite3VdbeOp3(v
, OP_OpenRead
, iIdx
, pIdx
->tnum
,
2215 (char*)&pIdx
->keyInfo
, P3_KEYINFO
);
2216 if( seekOp
==OP_Rewind
){
2217 sqlite3VdbeAddOp(v
, OP_Null
, 0, 0);
2218 sqlite3VdbeAddOp(v
, OP_MakeRecord
, 1, 0);
2221 sqlite3VdbeAddOp(v
, seekOp
, iIdx
, 0);
2222 sqlite3VdbeAddOp(v
, OP_IdxRowid
, iIdx
, 0);
2223 sqlite3VdbeAddOp(v
, OP_Close
, iIdx
, 0);
2224 sqlite3VdbeAddOp(v
, OP_MoveGe
, base
, 0);
2227 memset(&eListItem
, 0, sizeof(eListItem
));
2228 eList
.a
= &eListItem
;
2229 eList
.a
[0].pExpr
= pExpr
;
2230 selectInnerLoop(pParse
, p
, &eList
, 0, 0, 0, -1, eDest
, iParm
, cont
, cont
, 0);
2231 sqlite3VdbeResolveLabel(v
, cont
);
2232 sqlite3VdbeAddOp(v
, OP_Close
, base
, 0);
2238 ** Analyze and ORDER BY or GROUP BY clause in a SELECT statement. Return
2239 ** the number of errors seen.
2241 ** An ORDER BY or GROUP BY is a list of expressions. If any expression
2242 ** is an integer constant, then that expression is replaced by the
2243 ** corresponding entry in the result set.
2245 static int processOrderGroupBy(
2246 NameContext
*pNC
, /* Name context of the SELECT statement. */
2247 ExprList
*pOrderBy
, /* The ORDER BY or GROUP BY clause to be processed */
2248 const char *zType
/* Either "ORDER" or "GROUP", as appropriate */
2251 ExprList
*pEList
= pNC
->pEList
; /* The result set of the SELECT */
2252 Parse
*pParse
= pNC
->pParse
; /* The result set of the SELECT */
2255 if( pOrderBy
==0 ) return 0;
2256 for(i
=0; i
<pOrderBy
->nExpr
; i
++){
2258 Expr
*pE
= pOrderBy
->a
[i
].pExpr
;
2259 if( sqlite3ExprIsInteger(pE
, &iCol
) ){
2260 if( iCol
>0 && iCol
<=pEList
->nExpr
){
2261 sqlite3ExprDelete(pE
);
2262 pE
= pOrderBy
->a
[i
].pExpr
= sqlite3ExprDup(pEList
->a
[iCol
-1].pExpr
);
2264 sqlite3ErrorMsg(pParse
,
2265 "%s BY column number %d out of range - should be "
2266 "between 1 and %d", zType
, iCol
, pEList
->nExpr
);
2270 if( sqlite3ExprResolveNames(pNC
, pE
) ){
2273 if( sqlite3ExprIsConstant(pE
) ){
2274 sqlite3ErrorMsg(pParse
,
2275 "%s BY terms must not be non-integer constants", zType
);
2283 ** This routine resolves any names used in the result set of the
2284 ** supplied SELECT statement. If the SELECT statement being resolved
2285 ** is a sub-select, then pOuterNC is a pointer to the NameContext
2286 ** of the parent SELECT.
2288 int sqlite3SelectResolve(
2289 Parse
*pParse
, /* The parser context */
2290 Select
*p
, /* The SELECT statement being coded. */
2291 NameContext
*pOuterNC
/* The outer name context. May be NULL. */
2293 ExprList
*pEList
; /* Result set. */
2294 int i
; /* For-loop variable used in multiple places */
2295 NameContext sNC
; /* Local name-context */
2297 /* If this routine has run before, return immediately. */
2298 if( p
->isResolved
){
2299 assert( !pOuterNC
);
2304 /* If there have already been errors, do nothing. */
2305 if( pParse
->nErr
>0 ){
2306 return SQLITE_ERROR
;
2309 /* Prepare the select statement. This call will allocate all cursors
2310 ** required to handle the tables and subqueries in the FROM clause.
2312 if( prepSelectStmt(pParse
, p
) ){
2313 return SQLITE_ERROR
;
2316 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
2317 ** are not allowed to refer to any names, so pass an empty NameContext.
2319 sNC
.pParse
= pParse
;
2327 if( sqlite3ExprResolveNames(&sNC
, p
->pLimit
) ||
2328 sqlite3ExprResolveNames(&sNC
, p
->pOffset
) ){
2329 return SQLITE_ERROR
;
2332 /* Set up the local name-context to pass to ExprResolveNames() to
2333 ** resolve the expression-list.
2336 sNC
.pSrcList
= p
->pSrc
;
2337 sNC
.pNext
= pOuterNC
;
2339 /* NameContext.nDepth stores the depth of recursion for this query. For
2340 ** an outer query (e.g. SELECT * FROM sqlite_master) this is 1. For
2341 ** a subquery it is 2. For a subquery of a subquery, 3. And so on.
2342 ** Parse.nMaxDepth is the maximum depth for any subquery resolved so
2343 ** far. This is used to determine the number of aggregate contexts
2344 ** required at runtime.
2346 sNC
.nDepth
= (pOuterNC
?pOuterNC
->nDepth
+1:1);
2347 if( sNC
.nDepth
>pParse
->nMaxDepth
){
2348 pParse
->nMaxDepth
= sNC
.nDepth
;
2351 /* Resolve names in the result set. */
2353 if( !pEList
) return SQLITE_ERROR
;
2354 for(i
=0; i
<pEList
->nExpr
; i
++){
2355 Expr
*pX
= pEList
->a
[i
].pExpr
;
2356 if( sqlite3ExprResolveNames(&sNC
, pX
) ){
2357 return SQLITE_ERROR
;
2361 /* If there are no aggregate functions in the result-set, and no GROUP BY
2362 ** expression, do not allow aggregates in any of the other expressions.
2364 assert( !p
->isAgg
);
2365 if( p
->pGroupBy
|| sNC
.hasAgg
){
2371 /* If a HAVING clause is present, then there must be a GROUP BY clause.
2373 if( p
->pHaving
&& !p
->pGroupBy
){
2374 sqlite3ErrorMsg(pParse
, "a GROUP BY clause is required before HAVING");
2375 return SQLITE_ERROR
;
2378 /* Add the expression list to the name-context before parsing the
2379 ** other expressions in the SELECT statement. This is so that
2380 ** expressions in the WHERE clause (etc.) can refer to expressions by
2381 ** aliases in the result set.
2383 ** Minor point: If this is the case, then the expression will be
2384 ** re-evaluated for each reference to it.
2386 sNC
.pEList
= p
->pEList
;
2387 if( sqlite3ExprResolveNames(&sNC
, p
->pWhere
) ||
2388 sqlite3ExprResolveNames(&sNC
, p
->pHaving
) ||
2389 processOrderGroupBy(&sNC
, p
->pOrderBy
, "ORDER") ||
2390 processOrderGroupBy(&sNC
, p
->pGroupBy
, "GROUP")
2392 return SQLITE_ERROR
;
2399 ** An instance of the following struct is used by sqlite3Select()
2400 ** to save aggregate related information from the Parse object
2401 ** at the start of each call and to restore it at the end. See
2402 ** saveAggregateInfo() and restoreAggregateInfo().
2404 struct AggregateInfo
{
2408 typedef struct AggregateInfo AggregateInfo
;
2411 ** Copy aggregate related information from the Parse structure
2412 ** into the AggregateInfo structure. Zero the aggregate related
2413 ** values in the Parse struct.
2415 static void saveAggregateInfo(Parse
*pParse
, AggregateInfo
*pInfo
){
2416 pInfo
->aAgg
= pParse
->aAgg
;
2417 pInfo
->nAgg
= pParse
->nAgg
;
2423 ** Copy aggregate related information from the AggregateInfo struct
2424 ** back into the Parse structure. The aggregate related information
2425 ** currently stored in the Parse structure is deleted.
2427 static void restoreAggregateInfo(Parse
*pParse
, AggregateInfo
*pInfo
){
2428 sqliteFree(pParse
->aAgg
);
2429 pParse
->aAgg
= pInfo
->aAgg
;
2430 pParse
->nAgg
= pInfo
->nAgg
;
2434 ** Generate code for the given SELECT statement.
2436 ** The results are distributed in various ways depending on the
2437 ** value of eDest and iParm.
2439 ** eDest Value Result
2440 ** ------------ -------------------------------------------
2441 ** SRT_Callback Invoke the callback for each row of the result.
2443 ** SRT_Mem Store first result in memory cell iParm
2445 ** SRT_Set Store results as keys of table iParm.
2447 ** SRT_Union Store results as a key in a temporary table iParm
2449 ** SRT_Except Remove results from the temporary table iParm.
2451 ** SRT_Table Store results in temporary table iParm
2453 ** The table above is incomplete. Additional eDist value have be added
2454 ** since this comment was written. See the selectInnerLoop() function for
2455 ** a complete listing of the allowed values of eDest and their meanings.
2457 ** This routine returns the number of errors. If any errors are
2458 ** encountered, then an appropriate error message is left in
2461 ** This routine does NOT free the Select structure passed in. The
2462 ** calling function needs to do that.
2464 ** The pParent, parentTab, and *pParentAgg fields are filled in if this
2465 ** SELECT is a subquery. This routine may try to combine this SELECT
2466 ** with its parent to form a single flat query. In so doing, it might
2467 ** change the parent query from a non-aggregate to an aggregate query.
2468 ** For that reason, the pParentAgg flag is passed as a pointer, so it
2471 ** Example 1: The meaning of the pParent parameter.
2473 ** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3;
2474 ** \ \_______ subquery _______/ /
2476 ** \____________________ outer query ___________________/
2478 ** This routine is called for the outer query first. For that call,
2479 ** pParent will be NULL. During the processing of the outer query, this
2480 ** routine is called recursively to handle the subquery. For the recursive
2481 ** call, pParent will point to the outer query. Because the subquery is
2482 ** the second element in a three-way join, the parentTab parameter will
2483 ** be 1 (the 2nd value of a 0-indexed array.)
2486 Parse
*pParse
, /* The parser context */
2487 Select
*p
, /* The SELECT statement being coded. */
2488 int eDest
, /* How to dispose of the results */
2489 int iParm
, /* A parameter used by the eDest disposal method */
2490 Select
*pParent
, /* Another SELECT for which this is a sub-query */
2491 int parentTab
, /* Index in pParent->pSrc of this query */
2492 int *pParentAgg
, /* True if pParent uses aggregate functions */
2493 char *aff
/* If eDest is SRT_Union, the affinity string */
2498 int isAgg
; /* True for select lists like "count(*)" */
2499 ExprList
*pEList
; /* List of columns to extract. */
2500 SrcList
*pTabList
; /* List of tables to select from */
2501 Expr
*pWhere
; /* The WHERE clause. May be NULL */
2502 ExprList
*pOrderBy
; /* The ORDER BY clause. May be NULL */
2503 ExprList
*pGroupBy
; /* The GROUP BY clause. May be NULL */
2504 Expr
*pHaving
; /* The HAVING clause. May be NULL */
2505 int isDistinct
; /* True if the DISTINCT keyword is present */
2506 int distinct
; /* Table to use for the distinct set */
2507 int rc
= 1; /* Value to return from this function */
2508 AggregateInfo sAggInfo
;
2510 if( sqlite3_malloc_failed
|| pParse
->nErr
|| p
==0 ) return 1;
2511 if( sqlite3AuthCheck(pParse
, SQLITE_SELECT
, 0, 0, 0) ) return 1;
2513 #ifndef SQLITE_OMIT_COMPOUND_SELECT
2514 /* If there is are a sequence of queries, do the earlier ones first.
2517 return multiSelect(pParse
, p
, eDest
, iParm
, aff
);
2521 saveAggregateInfo(pParse
, &sAggInfo
);
2522 pOrderBy
= p
->pOrderBy
;
2523 if( eDest
==SRT_Union
|| eDest
==SRT_Except
|| eDest
==SRT_Discard
){
2526 if( sqlite3SelectResolve(pParse
, p
, 0) ){
2529 p
->pOrderBy
= pOrderBy
;
2531 /* Make local copies of the parameters for this query.
2535 pGroupBy
= p
->pGroupBy
;
2536 pHaving
= p
->pHaving
;
2538 isDistinct
= p
->isDistinct
;
2540 if( pEList
==0 ) goto select_end
;
2543 ** Do not even attempt to generate any code if we have already seen
2544 ** errors before this routine starts.
2546 if( pParse
->nErr
>0 ) goto select_end
;
2548 /* If writing to memory or generating a set
2549 ** only a single column may be output.
2551 assert( eDest
!=SRT_Exists
|| pEList
->nExpr
==1 );
2552 #ifndef SQLITE_OMIT_SUBQUERY
2553 if( (eDest
==SRT_Mem
|| eDest
==SRT_Set
) && pEList
->nExpr
>1 ){
2554 sqlite3ErrorMsg(pParse
, "only a single result allowed for "
2555 "a SELECT that is part of an expression");
2560 /* ORDER BY is ignored for some destinations.
2572 /* Begin generating code.
2574 v
= sqlite3GetVdbe(pParse
);
2575 if( v
==0 ) goto select_end
;
2577 /* Identify column names if we will be using them in a callback. This
2578 ** step is skipped if the output is going to some other destination.
2580 if( eDest
==SRT_Callback
){
2581 generateColumnNames(pParse
, pTabList
, pEList
);
2584 /* Generate code for all sub-queries in the FROM clause
2586 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
2587 for(i
=0; i
<pTabList
->nSrc
; i
++){
2588 const char *zSavedAuthContext
= 0;
2589 int needRestoreContext
;
2591 if( pTabList
->a
[i
].pSelect
==0 ) continue;
2592 if( pTabList
->a
[i
].zName
!=0 ){
2593 zSavedAuthContext
= pParse
->zAuthContext
;
2594 pParse
->zAuthContext
= pTabList
->a
[i
].zName
;
2595 needRestoreContext
= 1;
2597 needRestoreContext
= 0;
2599 sqlite3Select(pParse
, pTabList
->a
[i
].pSelect
, SRT_TempTable
,
2600 pTabList
->a
[i
].iCursor
, p
, i
, &isAgg
, 0);
2601 if( needRestoreContext
){
2602 pParse
->zAuthContext
= zSavedAuthContext
;
2606 if( eDest
!=SRT_Union
&& eDest
!=SRT_Except
&& eDest
!=SRT_Discard
){
2607 pOrderBy
= p
->pOrderBy
;
2609 pGroupBy
= p
->pGroupBy
;
2610 pHaving
= p
->pHaving
;
2611 isDistinct
= p
->isDistinct
;
2615 /* Check for the special case of a min() or max() function by itself
2616 ** in the result set.
2618 if( simpleMinMaxQuery(pParse
, p
, eDest
, iParm
) ){
2623 /* Check to see if this is a subquery that can be "flattened" into its parent.
2624 ** If flattening is a possiblity, do so and return immediately.
2626 #ifndef SQLITE_OMIT_VIEW
2627 if( pParent
&& pParentAgg
&&
2628 flattenSubquery(pParse
, pParent
, parentTab
, *pParentAgg
, isAgg
) ){
2629 if( isAgg
) *pParentAgg
= 1;
2634 /* If there is an ORDER BY clause, resolve any collation sequences
2635 ** names that have been explicitly specified.
2638 for(i
=0; i
<pOrderBy
->nExpr
; i
++){
2639 if( pOrderBy
->a
[i
].zName
){
2640 pOrderBy
->a
[i
].pExpr
->pColl
=
2641 sqlite3LocateCollSeq(pParse
, pOrderBy
->a
[i
].zName
, -1);
2651 computeLimitRegisters(pParse
, p
);
2653 /* If the output is destined for a temporary table, open that table.
2655 if( eDest
==SRT_TempTable
){
2656 sqlite3VdbeAddOp(v
, OP_OpenTemp
, iParm
, 0);
2657 sqlite3VdbeAddOp(v
, OP_SetNumColumns
, iParm
, pEList
->nExpr
);
2660 /* Do an analysis of aggregate expressions.
2662 if( isAgg
|| pGroupBy
){
2664 memset(&sNC
, 0, sizeof(sNC
));
2665 sNC
.pParse
= pParse
;
2666 sNC
.pSrcList
= pTabList
;
2668 assert( pParse
->nAgg
==0 );
2670 for(i
=0; i
<pEList
->nExpr
; i
++){
2671 if( sqlite3ExprAnalyzeAggregates(&sNC
, pEList
->a
[i
].pExpr
) ){
2676 for(i
=0; i
<pGroupBy
->nExpr
; i
++){
2677 if( sqlite3ExprAnalyzeAggregates(&sNC
, pGroupBy
->a
[i
].pExpr
) ){
2682 if( pHaving
&& sqlite3ExprAnalyzeAggregates(&sNC
, pHaving
) ){
2686 for(i
=0; i
<pOrderBy
->nExpr
; i
++){
2687 if( sqlite3ExprAnalyzeAggregates(&sNC
, pOrderBy
->a
[i
].pExpr
) ){
2694 /* Reset the aggregator
2697 int addr
= sqlite3VdbeAddOp(v
, OP_AggReset
, (pGroupBy
?0:1), pParse
->nAgg
);
2698 for(i
=0; i
<pParse
->nAgg
; i
++){
2700 if( (pFunc
= pParse
->aAgg
[i
].pFunc
)!=0 && pFunc
->xFinalize
!=0 ){
2703 Expr
*pAggExpr
= pParse
->aAgg
[i
].pExpr
;
2704 if( pAggExpr
&& pAggExpr
->pList
){
2705 nExpr
= pAggExpr
->pList
->nExpr
;
2708 sqlite3VdbeOp3(v
, OP_AggInit
, nExpr
, i
, (char*)pFunc
, P3_FUNCDEF
);
2712 int sz
= sizeof(KeyInfo
) + pGroupBy
->nExpr
*sizeof(CollSeq
*);
2713 KeyInfo
*pKey
= (KeyInfo
*)sqliteMalloc(sz
);
2717 pKey
->enc
= pParse
->db
->enc
;
2718 pKey
->nField
= pGroupBy
->nExpr
;
2719 for(i
=0; i
<pGroupBy
->nExpr
; i
++){
2720 pKey
->aColl
[i
] = sqlite3ExprCollSeq(pParse
, pGroupBy
->a
[i
].pExpr
);
2721 if( !pKey
->aColl
[i
] ){
2722 pKey
->aColl
[i
] = pParse
->db
->pDfltColl
;
2725 sqlite3VdbeChangeP3(v
, addr
, (char *)pKey
, P3_KEYINFO_HANDOFF
);
2729 /* Initialize the memory cell to NULL for SRT_Mem or 0 for SRT_Exists
2731 if( eDest
==SRT_Mem
|| eDest
==SRT_Exists
){
2732 sqlite3VdbeAddOp(v
, eDest
==SRT_Mem
? OP_Null
: OP_Integer
, 0, 0);
2733 sqlite3VdbeAddOp(v
, OP_MemStore
, iParm
, 1);
2736 /* Open a temporary table to use for the distinct set.
2739 distinct
= pParse
->nTab
++;
2740 openTempIndex(pParse
, p
, distinct
);
2745 /* Begin the database scan
2747 pWInfo
= sqlite3WhereBegin(pParse
, pTabList
, pWhere
,
2748 pGroupBy
? 0 : &pOrderBy
);
2749 if( pWInfo
==0 ) goto select_end
;
2751 /* Use the standard inner loop if we are not dealing with
2755 if( selectInnerLoop(pParse
, p
, pEList
, 0, 0, pOrderBy
, distinct
, eDest
,
2756 iParm
, pWInfo
->iContinue
, pWInfo
->iBreak
, aff
) ){
2761 /* If we are dealing with aggregates, then do the special aggregate
2767 pParse
->fillAgg
= 1;
2769 for(i
=0; i
<pGroupBy
->nExpr
; i
++){
2770 sqlite3ExprCode(pParse
, pGroupBy
->a
[i
].pExpr
);
2772 /* No affinity string is attached to the following OP_MakeRecord
2773 ** because we do not need to do any coercion of datatypes. */
2774 sqlite3VdbeAddOp(v
, OP_MakeRecord
, pGroupBy
->nExpr
, 0);
2775 lbl1
= sqlite3VdbeMakeLabel(v
);
2776 sqlite3VdbeAddOp(v
, OP_AggFocus
, 0, lbl1
);
2778 for(i
=0, pAgg
=pParse
->aAgg
; i
<pParse
->nAgg
; i
++, pAgg
++){
2779 if( pAgg
->isAgg
) continue;
2780 sqlite3ExprCode(pParse
, pAgg
->pExpr
);
2781 sqlite3VdbeAddOp(v
, OP_AggSet
, 0, i
);
2783 pParse
->fillAgg
= 0;
2785 sqlite3VdbeResolveLabel(v
, lbl1
);
2787 for(i
=0, pAgg
=pParse
->aAgg
; i
<pParse
->nAgg
; i
++, pAgg
++){
2791 if( !pAgg
->isAgg
) continue;
2792 assert( pAgg
->pFunc
!=0 );
2793 assert( pAgg
->pFunc
->xStep
!=0 );
2797 assert( pE
->op
==TK_AGG_FUNCTION
);
2798 nExpr
= sqlite3ExprCodeExprList(pParse
, pE
->pList
);
2799 sqlite3VdbeAddOp(v
, OP_Integer
, i
, 0);
2800 if( pDef
->needCollSeq
){
2803 for(j
=0; !pColl
&& j
<nExpr
; j
++){
2804 pColl
= sqlite3ExprCollSeq(pParse
, pE
->pList
->a
[j
].pExpr
);
2806 if( !pColl
) pColl
= pParse
->db
->pDfltColl
;
2807 sqlite3VdbeOp3(v
, OP_CollSeq
, 0, 0, (char *)pColl
, P3_COLLSEQ
);
2809 sqlite3VdbeOp3(v
, OP_AggFunc
, 0, nExpr
, (char*)pDef
, P3_FUNCDEF
);
2813 /* End the database scan loop.
2815 sqlite3WhereEnd(pWInfo
);
2817 /* If we are processing aggregates, we need to set up a second loop
2818 ** over all of the aggregate values and process them.
2821 int endagg
= sqlite3VdbeMakeLabel(v
);
2823 startagg
= sqlite3VdbeAddOp(v
, OP_AggNext
, 0, endagg
);
2825 sqlite3ExprIfFalse(pParse
, pHaving
, startagg
, 1);
2827 if( selectInnerLoop(pParse
, p
, pEList
, 0, 0, pOrderBy
, distinct
, eDest
,
2828 iParm
, startagg
, endagg
, aff
) ){
2831 sqlite3VdbeAddOp(v
, OP_Goto
, 0, startagg
);
2832 sqlite3VdbeResolveLabel(v
, endagg
);
2833 sqlite3VdbeAddOp(v
, OP_Noop
, 0, 0);
2836 /* If there is an ORDER BY clause, then we need to sort the results
2837 ** and send them to the callback one by one.
2840 generateSortTail(pParse
, p
, v
, pEList
->nExpr
, eDest
, iParm
);
2843 #ifndef SQLITE_OMIT_SUBQUERY
2844 /* If this was a subquery, we have now converted the subquery into a
2845 ** temporary table. So delete the subquery structure from the parent
2846 ** to prevent this subquery from being evaluated again and to force the
2847 ** the use of the temporary table.
2850 assert( pParent
->pSrc
->nSrc
>parentTab
);
2851 assert( pParent
->pSrc
->a
[parentTab
].pSelect
==p
);
2852 sqlite3SelectDelete(p
);
2853 pParent
->pSrc
->a
[parentTab
].pSelect
= 0;
2857 /* The SELECT was successfully coded. Set the return code to 0
2858 ** to indicate no errors.
2862 /* Control jumps to here if an error is encountered above, or upon
2863 ** successful coding of the SELECT.
2866 restoreAggregateInfo(pParse
, &sAggInfo
);