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.
15 #include "sqliteInt.h"
18 ** An instance of the following object is used to record information about
19 ** how to process the DISTINCT keyword, to simplify passing that information
20 ** into the selectInnerLoop() routine.
22 typedef struct DistinctCtx DistinctCtx
;
24 u8 isTnct
; /* 0: Not distinct. 1: DISTICT 2: DISTINCT and ORDER BY */
25 u8 eTnctType
; /* One of the WHERE_DISTINCT_* operators */
26 int tabTnct
; /* Ephemeral table used for DISTINCT processing */
27 int addrTnct
; /* Address of OP_OpenEphemeral opcode for tabTnct */
31 ** An instance of the following object is used to record information about
32 ** the ORDER BY (or GROUP BY) clause of query is being coded.
34 ** The aDefer[] array is used by the sorter-references optimization. For
35 ** example, assuming there is no index that can be used for the ORDER BY,
38 ** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
40 ** it may be more efficient to add just the "a" values to the sorter, and
41 ** retrieve the associated "bigblob" values directly from table t1 as the
42 ** 10 smallest "a" values are extracted from the sorter.
44 ** When the sorter-reference optimization is used, there is one entry in the
45 ** aDefer[] array for each database table that may be read as values are
46 ** extracted from the sorter.
48 typedef struct SortCtx SortCtx
;
50 ExprList
*pOrderBy
; /* The ORDER BY (or GROUP BY clause) */
51 int nOBSat
; /* Number of ORDER BY terms satisfied by indices */
52 int iECursor
; /* Cursor number for the sorter */
53 int regReturn
; /* Register holding block-output return address */
54 int labelBkOut
; /* Start label for the block-output subroutine */
55 int addrSortIndex
; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
56 int labelDone
; /* Jump here when done, ex: LIMIT reached */
57 int labelOBLopt
; /* Jump here when sorter is full */
58 u8 sortFlags
; /* Zero or more SORTFLAG_* bits */
59 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
60 u8 nDefer
; /* Number of valid entries in aDefer[] */
62 Table
*pTab
; /* Table definition */
63 int iCsr
; /* Cursor number for table */
64 int nKey
; /* Number of PK columns for table pTab (>=1) */
67 struct RowLoadInfo
*pDeferredRowLoad
; /* Deferred row loading info or NULL */
68 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
69 int addrPush
; /* First instruction to push data into sorter */
70 int addrPushEnd
; /* Last instruction that pushes data into sorter */
73 #define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
76 ** Delete all the content of a Select structure. Deallocate the structure
77 ** itself depending on the value of bFree
79 ** If bFree==1, call sqlite3DbFree() on the p object.
80 ** If bFree==0, Leave the first Select object unfreed
82 static void clearSelect(sqlite3
*db
, Select
*p
, int bFree
){
85 Select
*pPrior
= p
->pPrior
;
86 sqlite3ExprListDelete(db
, p
->pEList
);
87 sqlite3SrcListDelete(db
, p
->pSrc
);
88 sqlite3ExprDelete(db
, p
->pWhere
);
89 sqlite3ExprListDelete(db
, p
->pGroupBy
);
90 sqlite3ExprDelete(db
, p
->pHaving
);
91 sqlite3ExprListDelete(db
, p
->pOrderBy
);
92 sqlite3ExprDelete(db
, p
->pLimit
);
93 if( OK_IF_ALWAYS_TRUE(p
->pWith
) ) sqlite3WithDelete(db
, p
->pWith
);
94 #ifndef SQLITE_OMIT_WINDOWFUNC
95 if( OK_IF_ALWAYS_TRUE(p
->pWinDefn
) ){
96 sqlite3WindowListDelete(db
, p
->pWinDefn
);
99 assert( p
->pWin
->ppThis
==&p
->pWin
);
100 sqlite3WindowUnlinkFromSelect(p
->pWin
);
103 if( bFree
) sqlite3DbNNFreeNN(db
, p
);
110 ** Initialize a SelectDest structure.
112 void sqlite3SelectDestInit(SelectDest
*pDest
, int eDest
, int iParm
){
113 pDest
->eDest
= (u8
)eDest
;
114 pDest
->iSDParm
= iParm
;
123 ** Allocate a new Select structure and return a pointer to that
126 Select
*sqlite3SelectNew(
127 Parse
*pParse
, /* Parsing context */
128 ExprList
*pEList
, /* which columns to include in the result */
129 SrcList
*pSrc
, /* the FROM clause -- which tables to scan */
130 Expr
*pWhere
, /* the WHERE clause */
131 ExprList
*pGroupBy
, /* the GROUP BY clause */
132 Expr
*pHaving
, /* the HAVING clause */
133 ExprList
*pOrderBy
, /* the ORDER BY clause */
134 u32 selFlags
, /* Flag parameters, such as SF_Distinct */
135 Expr
*pLimit
/* LIMIT value. NULL means not used */
137 Select
*pNew
, *pAllocated
;
139 pAllocated
= pNew
= sqlite3DbMallocRawNN(pParse
->db
, sizeof(*pNew
) );
141 assert( pParse
->db
->mallocFailed
);
145 pEList
= sqlite3ExprListAppend(pParse
, 0,
146 sqlite3Expr(pParse
->db
,TK_ASTERISK
,0));
148 pNew
->pEList
= pEList
;
149 pNew
->op
= TK_SELECT
;
150 pNew
->selFlags
= selFlags
;
153 pNew
->selId
= ++pParse
->nSelect
;
154 pNew
->addrOpenEphm
[0] = -1;
155 pNew
->addrOpenEphm
[1] = -1;
156 pNew
->nSelectRow
= 0;
157 if( pSrc
==0 ) pSrc
= sqlite3DbMallocZero(pParse
->db
, sizeof(*pSrc
));
159 pNew
->pWhere
= pWhere
;
160 pNew
->pGroupBy
= pGroupBy
;
161 pNew
->pHaving
= pHaving
;
162 pNew
->pOrderBy
= pOrderBy
;
165 pNew
->pLimit
= pLimit
;
167 #ifndef SQLITE_OMIT_WINDOWFUNC
171 if( pParse
->db
->mallocFailed
) {
172 clearSelect(pParse
->db
, pNew
, pNew
!=&standin
);
175 assert( pNew
->pSrc
!=0 || pParse
->nErr
>0 );
182 ** Delete the given Select structure and all of its substructures.
184 void sqlite3SelectDelete(sqlite3
*db
, Select
*p
){
185 if( OK_IF_ALWAYS_TRUE(p
) ) clearSelect(db
, p
, 1);
187 void sqlite3SelectDeleteGeneric(sqlite3
*db
, void *p
){
188 if( ALWAYS(p
) ) clearSelect(db
, (Select
*)p
, 1);
192 ** Return a pointer to the right-most SELECT statement in a compound.
194 static Select
*findRightmost(Select
*p
){
195 while( p
->pNext
) p
= p
->pNext
;
200 ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
201 ** type of join. Return an integer constant that expresses that type
202 ** in terms of the following bit values:
211 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
213 ** If an illegal or unsupported join type is seen, then still return
214 ** a join type, but put an error in the pParse structure.
216 ** These are the valid join types:
219 ** pA pB pC Return Value
220 ** ------- ----- ----- ------------
221 ** CROSS - - JT_CROSS
222 ** INNER - - JT_INNER
223 ** LEFT - - JT_LEFT|JT_OUTER
224 ** LEFT OUTER - JT_LEFT|JT_OUTER
225 ** RIGHT - - JT_RIGHT|JT_OUTER
226 ** RIGHT OUTER - JT_RIGHT|JT_OUTER
227 ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
228 ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
229 ** NATURAL INNER - JT_NATURAL|JT_INNER
230 ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
231 ** NATURAL LEFT OUTER JT_NATURAL|JT_LEFT|JT_OUTER
232 ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
233 ** NATURAL RIGHT OUTER JT_NATURAL|JT_RIGHT|JT_OUTER
234 ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
235 ** NATURAL FULL OUTER JT_NATRUAL|JT_LEFT|JT_RIGHT
237 ** To preserve historical compatibly, SQLite also accepts a variety
238 ** of other non-standard and in many cases nonsensical join types.
239 ** This routine makes as much sense at it can from the nonsense join
240 ** type and returns a result. Examples of accepted nonsense join types
241 ** include but are not limited to:
243 ** INNER CROSS JOIN -> same as JOIN
244 ** NATURAL CROSS JOIN -> same as NATURAL JOIN
245 ** OUTER LEFT JOIN -> same as LEFT JOIN
246 ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
247 ** LEFT RIGHT JOIN -> same as FULL JOIN
248 ** RIGHT OUTER FULL JOIN -> same as FULL JOIN
249 ** CROSS CROSS CROSS JOIN -> same as JOIN
251 ** The only restrictions on the join type name are:
253 ** * "INNER" cannot appear together with "OUTER", "LEFT", "RIGHT",
256 ** * "CROSS" cannot appear together with "OUTER", "LEFT", "RIGHT,
259 ** * If "OUTER" is present then there must also be one of
260 ** "LEFT", "RIGHT", or "FULL"
262 int sqlite3JoinType(Parse
*pParse
, Token
*pA
, Token
*pB
, Token
*pC
){
266 /* 0123456789 123456789 123456789 123 */
267 static const char zKeyText
[] = "naturaleftouterightfullinnercross";
268 static const struct {
269 u8 i
; /* Beginning of keyword text in zKeyText[] */
270 u8 nChar
; /* Length of the keyword in characters */
271 u8 code
; /* Join type mask */
273 /* (0) natural */ { 0, 7, JT_NATURAL
},
274 /* (1) left */ { 6, 4, JT_LEFT
|JT_OUTER
},
275 /* (2) outer */ { 10, 5, JT_OUTER
},
276 /* (3) right */ { 14, 5, JT_RIGHT
|JT_OUTER
},
277 /* (4) full */ { 19, 4, JT_LEFT
|JT_RIGHT
|JT_OUTER
},
278 /* (5) inner */ { 23, 5, JT_INNER
},
279 /* (6) cross */ { 28, 5, JT_INNER
|JT_CROSS
},
285 for(i
=0; i
<3 && apAll
[i
]; i
++){
287 for(j
=0; j
<ArraySize(aKeyword
); j
++){
288 if( p
->n
==aKeyword
[j
].nChar
289 && sqlite3StrNICmp((char*)p
->z
, &zKeyText
[aKeyword
[j
].i
], p
->n
)==0 ){
290 jointype
|= aKeyword
[j
].code
;
294 testcase( j
==0 || j
==1 || j
==2 || j
==3 || j
==4 || j
==5 || j
==6 );
295 if( j
>=ArraySize(aKeyword
) ){
296 jointype
|= JT_ERROR
;
301 (jointype
& (JT_INNER
|JT_OUTER
))==(JT_INNER
|JT_OUTER
) ||
302 (jointype
& JT_ERROR
)!=0 ||
303 (jointype
& (JT_OUTER
|JT_LEFT
|JT_RIGHT
))==JT_OUTER
305 const char *zSp1
= " ";
306 const char *zSp2
= " ";
307 if( pB
==0 ){ zSp1
++; }
308 if( pC
==0 ){ zSp2
++; }
309 sqlite3ErrorMsg(pParse
, "unknown join type: "
310 "%T%s%T%s%T", pA
, zSp1
, pB
, zSp2
, pC
);
317 ** Return the index of a column in a table. Return -1 if the column
318 ** is not contained in the table.
320 int sqlite3ColumnIndex(Table
*pTab
, const char *zCol
){
322 u8 h
= sqlite3StrIHash(zCol
);
324 for(pCol
=pTab
->aCol
, i
=0; i
<pTab
->nCol
; pCol
++, i
++){
325 if( pCol
->hName
==h
&& sqlite3StrICmp(pCol
->zCnName
, zCol
)==0 ) return i
;
331 ** Mark a subquery result column as having been used.
333 void sqlite3SrcItemColumnUsed(SrcItem
*pItem
, int iCol
){
335 assert( (int)pItem
->fg
.isNestedFrom
== IsNestedFrom(pItem
) );
336 if( pItem
->fg
.isNestedFrom
){
338 assert( pItem
->fg
.isSubquery
);
339 assert( pItem
->u4
.pSubq
!=0 );
340 assert( pItem
->u4
.pSubq
->pSelect
!=0 );
341 pResults
= pItem
->u4
.pSubq
->pSelect
->pEList
;
342 assert( pResults
!=0 );
343 assert( iCol
>=0 && iCol
<pResults
->nExpr
);
344 pResults
->a
[iCol
].fg
.bUsed
= 1;
349 ** Search the tables iStart..iEnd (inclusive) in pSrc, looking for a
350 ** table that has a column named zCol. The search is left-to-right.
351 ** The first match found is returned.
353 ** When found, set *piTab and *piCol to the table index and column index
354 ** of the matching column and return TRUE.
356 ** If not found, return FALSE.
358 static int tableAndColumnIndex(
359 SrcList
*pSrc
, /* Array of tables to search */
360 int iStart
, /* First member of pSrc->a[] to check */
361 int iEnd
, /* Last member of pSrc->a[] to check */
362 const char *zCol
, /* Name of the column we are looking for */
363 int *piTab
, /* Write index of pSrc->a[] here */
364 int *piCol
, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
365 int bIgnoreHidden
/* Ignore hidden columns */
367 int i
; /* For looping over tables in pSrc */
368 int iCol
; /* Index of column matching zCol */
370 assert( iEnd
<pSrc
->nSrc
);
372 assert( (piTab
==0)==(piCol
==0) ); /* Both or neither are NULL */
374 for(i
=iStart
; i
<=iEnd
; i
++){
375 iCol
= sqlite3ColumnIndex(pSrc
->a
[i
].pSTab
, zCol
);
377 && (bIgnoreHidden
==0 || IsHiddenColumn(&pSrc
->a
[i
].pSTab
->aCol
[iCol
])==0)
380 sqlite3SrcItemColumnUsed(&pSrc
->a
[i
], iCol
);
391 ** Set the EP_OuterON property on all terms of the given expression.
392 ** And set the Expr.w.iJoin to iTable for every term in the
395 ** The EP_OuterON property is used on terms of an expression to tell
396 ** the OUTER JOIN processing logic that this term is part of the
397 ** join restriction specified in the ON or USING clause and not a part
398 ** of the more general WHERE clause. These terms are moved over to the
399 ** WHERE clause during join processing but we need to remember that they
400 ** originated in the ON or USING clause.
402 ** The Expr.w.iJoin tells the WHERE clause processing that the
403 ** expression depends on table w.iJoin even if that table is not
404 ** explicitly mentioned in the expression. That information is needed
405 ** for cases like this:
407 ** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
409 ** The where clause needs to defer the handling of the t1.x=5
410 ** term until after the t2 loop of the join. In that way, a
411 ** NULL t2 row will be inserted whenever t1.x!=5. If we do not
412 ** defer the handling of t1.x=5, it will be processed immediately
413 ** after the t1 loop and rows with t1.x!=5 will never appear in
414 ** the output, which is incorrect.
416 void sqlite3SetJoinExpr(Expr
*p
, int iTable
, u32 joinFlag
){
417 assert( joinFlag
==EP_OuterON
|| joinFlag
==EP_InnerON
);
419 ExprSetProperty(p
, joinFlag
);
420 assert( !ExprHasProperty(p
, EP_TokenOnly
|EP_Reduced
) );
421 ExprSetVVAProperty(p
, EP_NoReduce
);
423 if( p
->op
==TK_FUNCTION
){
424 assert( ExprUseXList(p
) );
427 for(i
=0; i
<p
->x
.pList
->nExpr
; i
++){
428 sqlite3SetJoinExpr(p
->x
.pList
->a
[i
].pExpr
, iTable
, joinFlag
);
432 sqlite3SetJoinExpr(p
->pLeft
, iTable
, joinFlag
);
437 /* Undo the work of sqlite3SetJoinExpr(). This is used when a LEFT JOIN
438 ** is simplified into an ordinary JOIN, and when an ON expression is
439 ** "pushed down" into the WHERE clause of a subquery.
441 ** Convert every term that is marked with EP_OuterON and w.iJoin==iTable into
442 ** an ordinary term that omits the EP_OuterON mark. Or if iTable<0, then
443 ** just clear every EP_OuterON and EP_InnerON mark from the expression tree.
445 ** If nullable is true, that means that Expr p might evaluate to NULL even
446 ** if it is a reference to a NOT NULL column. This can happen, for example,
447 ** if the table that p references is on the left side of a RIGHT JOIN.
448 ** If nullable is true, then take care to not remove the EP_CanBeNull bit.
449 ** See forum thread https://sqlite.org/forum/forumpost/b40696f50145d21c
451 static void unsetJoinExpr(Expr
*p
, int iTable
, int nullable
){
453 if( iTable
<0 || (ExprHasProperty(p
, EP_OuterON
) && p
->w
.iJoin
==iTable
) ){
454 ExprClearProperty(p
, EP_OuterON
|EP_InnerON
);
455 if( iTable
>=0 ) ExprSetProperty(p
, EP_InnerON
);
457 if( p
->op
==TK_COLUMN
&& p
->iTable
==iTable
&& !nullable
){
458 ExprClearProperty(p
, EP_CanBeNull
);
460 if( p
->op
==TK_FUNCTION
){
461 assert( ExprUseXList(p
) );
462 assert( p
->pLeft
==0 );
465 for(i
=0; i
<p
->x
.pList
->nExpr
; i
++){
466 unsetJoinExpr(p
->x
.pList
->a
[i
].pExpr
, iTable
, nullable
);
470 unsetJoinExpr(p
->pLeft
, iTable
, nullable
);
476 ** This routine processes the join information for a SELECT statement.
478 ** * A NATURAL join is converted into a USING join. After that, we
479 ** do not need to be concerned with NATURAL joins and we only have
480 ** think about USING joins.
482 ** * ON and USING clauses result in extra terms being added to the
483 ** WHERE clause to enforce the specified constraints. The extra
484 ** WHERE clause terms will be tagged with EP_OuterON or
485 ** EP_InnerON so that we know that they originated in ON/USING.
487 ** The terms of a FROM clause are contained in the Select.pSrc structure.
488 ** The left most table is the first entry in Select.pSrc. The right-most
489 ** table is the last entry. The join operator is held in the entry to
490 ** the right. Thus entry 1 contains the join operator for the join between
491 ** entries 0 and 1. Any ON or USING clauses associated with the join are
492 ** also attached to the right entry.
494 ** This routine returns the number of errors encountered.
496 static int sqlite3ProcessJoin(Parse
*pParse
, Select
*p
){
497 SrcList
*pSrc
; /* All tables in the FROM clause */
498 int i
, j
; /* Loop counters */
499 SrcItem
*pLeft
; /* Left table being joined */
500 SrcItem
*pRight
; /* Right table being joined */
505 for(i
=0; i
<pSrc
->nSrc
-1; i
++, pRight
++, pLeft
++){
506 Table
*pRightTab
= pRight
->pSTab
;
509 if( NEVER(pLeft
->pSTab
==0 || pRightTab
==0) ) continue;
510 joinType
= (pRight
->fg
.jointype
& JT_OUTER
)!=0 ? EP_OuterON
: EP_InnerON
;
512 /* If this is a NATURAL join, synthesize an appropriate USING clause
513 ** to specify which columns should be joined.
515 if( pRight
->fg
.jointype
& JT_NATURAL
){
517 if( pRight
->fg
.isUsing
|| pRight
->u3
.pOn
){
518 sqlite3ErrorMsg(pParse
, "a NATURAL join may not have "
519 "an ON or USING clause", 0);
522 for(j
=0; j
<pRightTab
->nCol
; j
++){
523 char *zName
; /* Name of column in the right table */
525 if( IsHiddenColumn(&pRightTab
->aCol
[j
]) ) continue;
526 zName
= pRightTab
->aCol
[j
].zCnName
;
527 if( tableAndColumnIndex(pSrc
, 0, i
, zName
, 0, 0, 1) ){
528 pUsing
= sqlite3IdListAppend(pParse
, pUsing
, 0);
530 assert( pUsing
->nId
>0 );
531 assert( pUsing
->a
[pUsing
->nId
-1].zName
==0 );
532 pUsing
->a
[pUsing
->nId
-1].zName
= sqlite3DbStrDup(pParse
->db
, zName
);
537 pRight
->fg
.isUsing
= 1;
538 pRight
->fg
.isSynthUsing
= 1;
539 pRight
->u3
.pUsing
= pUsing
;
541 if( pParse
->nErr
) return 1;
544 /* Create extra terms on the WHERE clause for each column named
545 ** in the USING clause. Example: If the two tables to be joined are
546 ** A and B and the USING clause names X, Y, and Z, then add this
547 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
548 ** Report an error if any column mentioned in the USING clause is
549 ** not contained in both tables to be joined.
551 if( pRight
->fg
.isUsing
){
552 IdList
*pList
= pRight
->u3
.pUsing
;
553 sqlite3
*db
= pParse
->db
;
555 for(j
=0; j
<pList
->nId
; j
++){
556 char *zName
; /* Name of the term in the USING clause */
557 int iLeft
; /* Table on the left with matching column name */
558 int iLeftCol
; /* Column number of matching column on the left */
559 int iRightCol
; /* Column number of matching column on the right */
560 Expr
*pE1
; /* Reference to the column on the LEFT of the join */
561 Expr
*pE2
; /* Reference to the column on the RIGHT of the join */
562 Expr
*pEq
; /* Equality constraint. pE1 == pE2 */
564 zName
= pList
->a
[j
].zName
;
565 iRightCol
= sqlite3ColumnIndex(pRightTab
, zName
);
567 || tableAndColumnIndex(pSrc
, 0, i
, zName
, &iLeft
, &iLeftCol
,
568 pRight
->fg
.isSynthUsing
)==0
570 sqlite3ErrorMsg(pParse
, "cannot join using column %s - column "
571 "not present in both tables", zName
);
574 pE1
= sqlite3CreateColumnExpr(db
, pSrc
, iLeft
, iLeftCol
);
575 sqlite3SrcItemColumnUsed(&pSrc
->a
[iLeft
], iLeftCol
);
576 if( (pSrc
->a
[0].fg
.jointype
& JT_LTORJ
)!=0 ){
577 /* This branch runs if the query contains one or more RIGHT or FULL
578 ** JOINs. If only a single table on the left side of this join
579 ** contains the zName column, then this branch is a no-op.
580 ** But if there are two or more tables on the left side
581 ** of the join, construct a coalesce() function that gathers all
582 ** such tables. Raise an error if more than one of those references
583 ** to zName is not also within a prior USING clause.
585 ** We really ought to raise an error if there are two or more
586 ** non-USING references to zName on the left of an INNER or LEFT
587 ** JOIN. But older versions of SQLite do not do that, so we avoid
588 ** adding a new error so as to not break legacy applications.
590 ExprList
*pFuncArgs
= 0; /* Arguments to the coalesce() */
591 static const Token tkCoalesce
= { "coalesce", 8 };
592 while( tableAndColumnIndex(pSrc
, iLeft
+1, i
, zName
, &iLeft
, &iLeftCol
,
593 pRight
->fg
.isSynthUsing
)!=0 ){
594 if( pSrc
->a
[iLeft
].fg
.isUsing
==0
595 || sqlite3IdListIndex(pSrc
->a
[iLeft
].u3
.pUsing
, zName
)<0
597 sqlite3ErrorMsg(pParse
, "ambiguous reference to %s in USING()",
601 pFuncArgs
= sqlite3ExprListAppend(pParse
, pFuncArgs
, pE1
);
602 pE1
= sqlite3CreateColumnExpr(db
, pSrc
, iLeft
, iLeftCol
);
603 sqlite3SrcItemColumnUsed(&pSrc
->a
[iLeft
], iLeftCol
);
606 pFuncArgs
= sqlite3ExprListAppend(pParse
, pFuncArgs
, pE1
);
607 pE1
= sqlite3ExprFunction(pParse
, pFuncArgs
, &tkCoalesce
, 0);
610 pE2
= sqlite3CreateColumnExpr(db
, pSrc
, i
+1, iRightCol
);
611 sqlite3SrcItemColumnUsed(pRight
, iRightCol
);
612 pEq
= sqlite3PExpr(pParse
, TK_EQ
, pE1
, pE2
);
613 assert( pE2
!=0 || pEq
==0 );
615 ExprSetProperty(pEq
, joinType
);
616 assert( !ExprHasProperty(pEq
, EP_TokenOnly
|EP_Reduced
) );
617 ExprSetVVAProperty(pEq
, EP_NoReduce
);
618 pEq
->w
.iJoin
= pE2
->iTable
;
620 p
->pWhere
= sqlite3ExprAnd(pParse
, p
->pWhere
, pEq
);
624 /* Add the ON clause to the end of the WHERE clause, connected by
627 else if( pRight
->u3
.pOn
){
628 sqlite3SetJoinExpr(pRight
->u3
.pOn
, pRight
->iCursor
, joinType
);
629 p
->pWhere
= sqlite3ExprAnd(pParse
, p
->pWhere
, pRight
->u3
.pOn
);
638 ** An instance of this object holds information (beyond pParse and pSelect)
639 ** needed to load the next result row that is to be added to the sorter.
641 typedef struct RowLoadInfo RowLoadInfo
;
643 int regResult
; /* Store results in array of registers here */
644 u8 ecelFlags
; /* Flag argument to ExprCodeExprList() */
645 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
646 ExprList
*pExtra
; /* Extra columns needed by sorter refs */
647 int regExtraResult
; /* Where to load the extra columns */
652 ** This routine does the work of loading query data into an array of
653 ** registers so that it can be added to the sorter.
655 static void innerLoopLoadRow(
656 Parse
*pParse
, /* Statement under construction */
657 Select
*pSelect
, /* The query being coded */
658 RowLoadInfo
*pInfo
/* Info needed to complete the row load */
660 sqlite3ExprCodeExprList(pParse
, pSelect
->pEList
, pInfo
->regResult
,
661 0, pInfo
->ecelFlags
);
662 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
664 sqlite3ExprCodeExprList(pParse
, pInfo
->pExtra
, pInfo
->regExtraResult
, 0, 0);
665 sqlite3ExprListDelete(pParse
->db
, pInfo
->pExtra
);
671 ** Code the OP_MakeRecord instruction that generates the entry to be
672 ** added into the sorter.
674 ** Return the register in which the result is stored.
676 static int makeSorterRecord(
683 int nOBSat
= pSort
->nOBSat
;
684 Vdbe
*v
= pParse
->pVdbe
;
685 int regOut
= ++pParse
->nMem
;
686 if( pSort
->pDeferredRowLoad
){
687 innerLoopLoadRow(pParse
, pSelect
, pSort
->pDeferredRowLoad
);
689 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regBase
+nOBSat
, nBase
-nOBSat
, regOut
);
694 ** Generate code that will push the record in registers regData
695 ** through regData+nData-1 onto the sorter.
697 static void pushOntoSorter(
698 Parse
*pParse
, /* Parser context */
699 SortCtx
*pSort
, /* Information about the ORDER BY clause */
700 Select
*pSelect
, /* The whole SELECT statement */
701 int regData
, /* First register holding data to be sorted */
702 int regOrigData
, /* First register holding data before packing */
703 int nData
, /* Number of elements in the regData data array */
704 int nPrefixReg
/* No. of reg prior to regData available for use */
706 Vdbe
*v
= pParse
->pVdbe
; /* Stmt under construction */
707 int bSeq
= ((pSort
->sortFlags
& SORTFLAG_UseSorter
)==0);
708 int nExpr
= pSort
->pOrderBy
->nExpr
; /* No. of ORDER BY terms */
709 int nBase
= nExpr
+ bSeq
+ nData
; /* Fields in sorter record */
710 int regBase
; /* Regs for sorter record */
711 int regRecord
= 0; /* Assembled sorter record */
712 int nOBSat
= pSort
->nOBSat
; /* ORDER BY terms to skip */
713 int op
; /* Opcode to add sorter record to sorter */
714 int iLimit
; /* LIMIT counter */
715 int iSkip
= 0; /* End of the sorter insert loop */
717 assert( bSeq
==0 || bSeq
==1 );
720 ** (1) The data to be sorted has already been packed into a Record
721 ** by a prior OP_MakeRecord. In this case nData==1 and regData
722 ** will be completely unrelated to regOrigData.
723 ** (2) All output columns are included in the sort record. In that
724 ** case regData==regOrigData.
725 ** (3) Some output columns are omitted from the sort record due to
726 ** the SQLITE_ENABLE_SORTER_REFERENCES optimization, or due to the
727 ** SQLITE_ECEL_OMITREF optimization, or due to the
728 ** SortCtx.pDeferredRowLoad optimization. In any of these cases
729 ** regOrigData is 0 to prevent this routine from trying to copy
730 ** values that might not yet exist.
732 assert( nData
==1 || regData
==regOrigData
|| regOrigData
==0 );
734 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
735 pSort
->addrPush
= sqlite3VdbeCurrentAddr(v
);
739 assert( nPrefixReg
==nExpr
+bSeq
);
740 regBase
= regData
- nPrefixReg
;
742 regBase
= pParse
->nMem
+ 1;
743 pParse
->nMem
+= nBase
;
745 assert( pSelect
->iOffset
==0 || pSelect
->iLimit
!=0 );
746 iLimit
= pSelect
->iOffset
? pSelect
->iOffset
+1 : pSelect
->iLimit
;
747 pSort
->labelDone
= sqlite3VdbeMakeLabel(pParse
);
748 sqlite3ExprCodeExprList(pParse
, pSort
->pOrderBy
, regBase
, regOrigData
,
749 SQLITE_ECEL_DUP
| (regOrigData
? SQLITE_ECEL_REF
: 0));
751 sqlite3VdbeAddOp2(v
, OP_Sequence
, pSort
->iECursor
, regBase
+nExpr
);
753 if( nPrefixReg
==0 && nData
>0 ){
754 sqlite3ExprCodeMove(pParse
, regData
, regBase
+nExpr
+bSeq
, nData
);
757 int regPrevKey
; /* The first nOBSat columns of the previous row */
758 int addrFirst
; /* Address of the OP_IfNot opcode */
759 int addrJmp
; /* Address of the OP_Jump opcode */
760 VdbeOp
*pOp
; /* Opcode that opens the sorter */
761 int nKey
; /* Number of sorting key columns, including OP_Sequence */
762 KeyInfo
*pKI
; /* Original KeyInfo on the sorter table */
764 regRecord
= makeSorterRecord(pParse
, pSort
, pSelect
, regBase
, nBase
);
765 regPrevKey
= pParse
->nMem
+1;
766 pParse
->nMem
+= pSort
->nOBSat
;
767 nKey
= nExpr
- pSort
->nOBSat
+ bSeq
;
769 addrFirst
= sqlite3VdbeAddOp1(v
, OP_IfNot
, regBase
+nExpr
);
771 addrFirst
= sqlite3VdbeAddOp1(v
, OP_SequenceTest
, pSort
->iECursor
);
774 sqlite3VdbeAddOp3(v
, OP_Compare
, regPrevKey
, regBase
, pSort
->nOBSat
);
775 pOp
= sqlite3VdbeGetOp(v
, pSort
->addrSortIndex
);
776 if( pParse
->db
->mallocFailed
) return;
777 pOp
->p2
= nKey
+ nData
;
778 pKI
= pOp
->p4
.pKeyInfo
;
779 memset(pKI
->aSortFlags
, 0, pKI
->nKeyField
); /* Makes OP_Jump testable */
780 sqlite3VdbeChangeP4(v
, -1, (char*)pKI
, P4_KEYINFO
);
781 testcase( pKI
->nAllField
> pKI
->nKeyField
+2 );
782 pOp
->p4
.pKeyInfo
= sqlite3KeyInfoFromExprList(pParse
,pSort
->pOrderBy
,nOBSat
,
783 pKI
->nAllField
-pKI
->nKeyField
-1);
784 pOp
= 0; /* Ensure pOp not used after sqlite3VdbeAddOp3() */
785 addrJmp
= sqlite3VdbeCurrentAddr(v
);
786 sqlite3VdbeAddOp3(v
, OP_Jump
, addrJmp
+1, 0, addrJmp
+1); VdbeCoverage(v
);
787 pSort
->labelBkOut
= sqlite3VdbeMakeLabel(pParse
);
788 pSort
->regReturn
= ++pParse
->nMem
;
789 sqlite3VdbeAddOp2(v
, OP_Gosub
, pSort
->regReturn
, pSort
->labelBkOut
);
790 sqlite3VdbeAddOp1(v
, OP_ResetSorter
, pSort
->iECursor
);
792 sqlite3VdbeAddOp2(v
, OP_IfNot
, iLimit
, pSort
->labelDone
);
795 sqlite3VdbeJumpHere(v
, addrFirst
);
796 sqlite3ExprCodeMove(pParse
, regBase
, regPrevKey
, pSort
->nOBSat
);
797 sqlite3VdbeJumpHere(v
, addrJmp
);
800 /* At this point the values for the new sorter entry are stored
801 ** in an array of registers. They need to be composed into a record
802 ** and inserted into the sorter if either (a) there are currently
803 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
804 ** the largest record currently in the sorter. If (b) is true and there
805 ** are already LIMIT+OFFSET items in the sorter, delete the largest
806 ** entry before inserting the new one. This way there are never more
807 ** than LIMIT+OFFSET items in the sorter.
809 ** If the new record does not need to be inserted into the sorter,
810 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
811 ** value is not zero, then it is a label of where to jump. Otherwise,
812 ** just bypass the row insert logic. See the header comment on the
813 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
815 int iCsr
= pSort
->iECursor
;
816 sqlite3VdbeAddOp2(v
, OP_IfNotZero
, iLimit
, sqlite3VdbeCurrentAddr(v
)+4);
818 sqlite3VdbeAddOp2(v
, OP_Last
, iCsr
, 0);
819 iSkip
= sqlite3VdbeAddOp4Int(v
, OP_IdxLE
,
820 iCsr
, 0, regBase
+nOBSat
, nExpr
-nOBSat
);
822 sqlite3VdbeAddOp1(v
, OP_Delete
, iCsr
);
825 regRecord
= makeSorterRecord(pParse
, pSort
, pSelect
, regBase
, nBase
);
827 if( pSort
->sortFlags
& SORTFLAG_UseSorter
){
828 op
= OP_SorterInsert
;
832 sqlite3VdbeAddOp4Int(v
, op
, pSort
->iECursor
, regRecord
,
833 regBase
+nOBSat
, nBase
-nOBSat
);
835 sqlite3VdbeChangeP2(v
, iSkip
,
836 pSort
->labelOBLopt
? pSort
->labelOBLopt
: sqlite3VdbeCurrentAddr(v
));
838 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
839 pSort
->addrPushEnd
= sqlite3VdbeCurrentAddr(v
)-1;
844 ** Add code to implement the OFFSET
846 static void codeOffset(
847 Vdbe
*v
, /* Generate code into this VM */
848 int iOffset
, /* Register holding the offset counter */
849 int iContinue
/* Jump here to skip the current record */
852 sqlite3VdbeAddOp3(v
, OP_IfPos
, iOffset
, iContinue
, 1); VdbeCoverage(v
);
853 VdbeComment((v
, "OFFSET"));
858 ** Add code that will check to make sure the array of registers starting at
859 ** iMem form a distinct entry. This is used by both "SELECT DISTINCT ..." and
860 ** distinct aggregates ("SELECT count(DISTINCT <expr>) ..."). Three strategies
861 ** are available. Which is used depends on the value of parameter eTnctType,
864 ** WHERE_DISTINCT_UNORDERED/WHERE_DISTINCT_NOOP:
865 ** Build an ephemeral table that contains all entries seen before and
866 ** skip entries which have been seen before.
868 ** Parameter iTab is the cursor number of an ephemeral table that must
869 ** be opened before the VM code generated by this routine is executed.
870 ** The ephemeral cursor table is queried for a record identical to the
871 ** record formed by the current array of registers. If one is found,
872 ** jump to VM address addrRepeat. Otherwise, insert a new record into
873 ** the ephemeral cursor and proceed.
875 ** The returned value in this case is a copy of parameter iTab.
877 ** WHERE_DISTINCT_ORDERED:
878 ** In this case rows are being delivered sorted order. The ephemeral
879 ** table is not required. Instead, the current set of values
880 ** is compared against previous row. If they match, the new row
881 ** is not distinct and control jumps to VM address addrRepeat. Otherwise,
882 ** the VM program proceeds with processing the new row.
884 ** The returned value in this case is the register number of the first
885 ** in an array of registers used to store the previous result row so that
886 ** it can be compared to the next. The caller must ensure that this
887 ** register is initialized to NULL. (The fixDistinctOpenEph() routine
888 ** will take care of this initialization.)
890 ** WHERE_DISTINCT_UNIQUE:
891 ** In this case it has already been determined that the rows are distinct.
892 ** No special action is required. The return value is zero.
894 ** Parameter pEList is the list of expressions used to generated the
895 ** contents of each row. It is used by this routine to determine (a)
896 ** how many elements there are in the array of registers and (b) the
897 ** collation sequences that should be used for the comparisons if
898 ** eTnctType is WHERE_DISTINCT_ORDERED.
900 static int codeDistinct(
901 Parse
*pParse
, /* Parsing and code generating context */
902 int eTnctType
, /* WHERE_DISTINCT_* value */
903 int iTab
, /* A sorting index used to test for distinctness */
904 int addrRepeat
, /* Jump to here if not distinct */
905 ExprList
*pEList
, /* Expression for each element */
906 int regElem
/* First element */
909 int nResultCol
= pEList
->nExpr
;
910 Vdbe
*v
= pParse
->pVdbe
;
913 case WHERE_DISTINCT_ORDERED
: {
915 int iJump
; /* Jump destination */
916 int regPrev
; /* Previous row content */
918 /* Allocate space for the previous row */
919 iRet
= regPrev
= pParse
->nMem
+1;
920 pParse
->nMem
+= nResultCol
;
922 iJump
= sqlite3VdbeCurrentAddr(v
) + nResultCol
;
923 for(i
=0; i
<nResultCol
; i
++){
924 CollSeq
*pColl
= sqlite3ExprCollSeq(pParse
, pEList
->a
[i
].pExpr
);
925 if( i
<nResultCol
-1 ){
926 sqlite3VdbeAddOp3(v
, OP_Ne
, regElem
+i
, iJump
, regPrev
+i
);
929 sqlite3VdbeAddOp3(v
, OP_Eq
, regElem
+i
, addrRepeat
, regPrev
+i
);
932 sqlite3VdbeChangeP4(v
, -1, (const char *)pColl
, P4_COLLSEQ
);
933 sqlite3VdbeChangeP5(v
, SQLITE_NULLEQ
);
935 assert( sqlite3VdbeCurrentAddr(v
)==iJump
|| pParse
->db
->mallocFailed
);
936 sqlite3VdbeAddOp3(v
, OP_Copy
, regElem
, regPrev
, nResultCol
-1);
940 case WHERE_DISTINCT_UNIQUE
: {
946 int r1
= sqlite3GetTempReg(pParse
);
947 sqlite3VdbeAddOp4Int(v
, OP_Found
, iTab
, addrRepeat
, regElem
, nResultCol
);
949 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regElem
, nResultCol
, r1
);
950 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iTab
, r1
, regElem
, nResultCol
);
951 sqlite3VdbeChangeP5(v
, OPFLAG_USESEEKRESULT
);
952 sqlite3ReleaseTempReg(pParse
, r1
);
962 ** This routine runs after codeDistinct(). It makes necessary
963 ** adjustments to the OP_OpenEphemeral opcode that the codeDistinct()
964 ** routine made use of. This processing must be done separately since
965 ** sometimes codeDistinct is called before the OP_OpenEphemeral is actually
968 ** WHERE_DISTINCT_NOOP:
969 ** WHERE_DISTINCT_UNORDERED:
971 ** No adjustments necessary. This function is a no-op.
973 ** WHERE_DISTINCT_UNIQUE:
975 ** The ephemeral table is not needed. So change the
976 ** OP_OpenEphemeral opcode into an OP_Noop.
978 ** WHERE_DISTINCT_ORDERED:
980 ** The ephemeral table is not needed. But we do need register
981 ** iVal to be initialized to NULL. So change the OP_OpenEphemeral
982 ** into an OP_Null on the iVal register.
984 static void fixDistinctOpenEph(
985 Parse
*pParse
, /* Parsing and code generating context */
986 int eTnctType
, /* WHERE_DISTINCT_* value */
987 int iVal
, /* Value returned by codeDistinct() */
988 int iOpenEphAddr
/* Address of OP_OpenEphemeral instruction for iTab */
991 && (eTnctType
==WHERE_DISTINCT_UNIQUE
|| eTnctType
==WHERE_DISTINCT_ORDERED
)
993 Vdbe
*v
= pParse
->pVdbe
;
994 sqlite3VdbeChangeToNoop(v
, iOpenEphAddr
);
995 if( sqlite3VdbeGetOp(v
, iOpenEphAddr
+1)->opcode
==OP_Explain
){
996 sqlite3VdbeChangeToNoop(v
, iOpenEphAddr
+1);
998 if( eTnctType
==WHERE_DISTINCT_ORDERED
){
999 /* Change the OP_OpenEphemeral to an OP_Null that sets the MEM_Cleared
1000 ** bit on the first register of the previous value. This will cause the
1001 ** OP_Ne added in codeDistinct() to always fail on the first iteration of
1002 ** the loop even if the first row is all NULLs. */
1003 VdbeOp
*pOp
= sqlite3VdbeGetOp(v
, iOpenEphAddr
);
1004 pOp
->opcode
= OP_Null
;
1011 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1013 ** This function is called as part of inner-loop generation for a SELECT
1014 ** statement with an ORDER BY that is not optimized by an index. It
1015 ** determines the expressions, if any, that the sorter-reference
1016 ** optimization should be used for. The sorter-reference optimization
1017 ** is used for SELECT queries like:
1019 ** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
1021 ** If the optimization is used for expression "bigblob", then instead of
1022 ** storing values read from that column in the sorter records, the PK of
1023 ** the row from table t1 is stored instead. Then, as records are extracted from
1024 ** the sorter to return to the user, the required value of bigblob is
1025 ** retrieved directly from table t1. If the values are very large, this
1026 ** can be more efficient than storing them directly in the sorter records.
1028 ** The ExprList_item.fg.bSorterRef flag is set for each expression in pEList
1029 ** for which the sorter-reference optimization should be enabled.
1030 ** Additionally, the pSort->aDefer[] array is populated with entries
1031 ** for all cursors required to evaluate all selected expressions. Finally.
1032 ** output variable (*ppExtra) is set to an expression list containing
1033 ** expressions for all extra PK values that should be stored in the
1036 static void selectExprDefer(
1037 Parse
*pParse
, /* Leave any error here */
1038 SortCtx
*pSort
, /* Sorter context */
1039 ExprList
*pEList
, /* Expressions destined for sorter */
1040 ExprList
**ppExtra
/* Expressions to append to sorter record */
1044 ExprList
*pExtra
= 0;
1045 for(i
=0; i
<pEList
->nExpr
; i
++){
1046 struct ExprList_item
*pItem
= &pEList
->a
[i
];
1047 if( pItem
->u
.x
.iOrderByCol
==0 ){
1048 Expr
*pExpr
= pItem
->pExpr
;
1050 if( pExpr
->op
==TK_COLUMN
1051 && pExpr
->iColumn
>=0
1052 && ALWAYS( ExprUseYTab(pExpr
) )
1053 && (pTab
= pExpr
->y
.pTab
)!=0
1054 && IsOrdinaryTable(pTab
)
1055 && (pTab
->aCol
[pExpr
->iColumn
].colFlags
& COLFLAG_SORTERREF
)!=0
1058 for(j
=0; j
<nDefer
; j
++){
1059 if( pSort
->aDefer
[j
].iCsr
==pExpr
->iTable
) break;
1062 if( nDefer
==ArraySize(pSort
->aDefer
) ){
1068 if( !HasRowid(pTab
) ){
1069 pPk
= sqlite3PrimaryKeyIndex(pTab
);
1070 nKey
= pPk
->nKeyCol
;
1072 for(k
=0; k
<nKey
; k
++){
1073 Expr
*pNew
= sqlite3PExpr(pParse
, TK_COLUMN
, 0, 0);
1075 pNew
->iTable
= pExpr
->iTable
;
1076 assert( ExprUseYTab(pNew
) );
1077 pNew
->y
.pTab
= pExpr
->y
.pTab
;
1078 pNew
->iColumn
= pPk
? pPk
->aiColumn
[k
] : -1;
1079 pExtra
= sqlite3ExprListAppend(pParse
, pExtra
, pNew
);
1082 pSort
->aDefer
[nDefer
].pTab
= pExpr
->y
.pTab
;
1083 pSort
->aDefer
[nDefer
].iCsr
= pExpr
->iTable
;
1084 pSort
->aDefer
[nDefer
].nKey
= nKey
;
1088 pItem
->fg
.bSorterRef
= 1;
1092 pSort
->nDefer
= (u8
)nDefer
;
1098 ** This routine generates the code for the inside of the inner loop
1101 ** If srcTab is negative, then the p->pEList expressions
1102 ** are evaluated in order to get the data for this row. If srcTab is
1103 ** zero or more, then data is pulled from srcTab and p->pEList is used only
1104 ** to get the number of columns and the collation sequence for each column.
1106 static void selectInnerLoop(
1107 Parse
*pParse
, /* The parser context */
1108 Select
*p
, /* The complete select statement being coded */
1109 int srcTab
, /* Pull data from this table if non-negative */
1110 SortCtx
*pSort
, /* If not NULL, info on how to process ORDER BY */
1111 DistinctCtx
*pDistinct
, /* If not NULL, info on how to process DISTINCT */
1112 SelectDest
*pDest
, /* How to dispose of the results */
1113 int iContinue
, /* Jump here to continue with next row */
1114 int iBreak
/* Jump here to break out of the inner loop */
1116 Vdbe
*v
= pParse
->pVdbe
;
1118 int hasDistinct
; /* True if the DISTINCT keyword is present */
1119 int eDest
= pDest
->eDest
; /* How to dispose of results */
1120 int iParm
= pDest
->iSDParm
; /* First argument to disposal method */
1121 int nResultCol
; /* Number of result columns */
1122 int nPrefixReg
= 0; /* Number of extra registers before regResult */
1123 RowLoadInfo sRowLoadInfo
; /* Info for deferred row loading */
1125 /* Usually, regResult is the first cell in an array of memory cells
1126 ** containing the current result row. In this case regOrig is set to the
1127 ** same value. However, if the results are being sent to the sorter, the
1128 ** values for any expressions that are also part of the sort-key are omitted
1129 ** from this array. In this case regOrig is set to zero. */
1130 int regResult
; /* Start of memory holding current results */
1131 int regOrig
; /* Start of memory holding full result (or 0) */
1134 assert( p
->pEList
!=0 );
1135 hasDistinct
= pDistinct
? pDistinct
->eTnctType
: WHERE_DISTINCT_NOOP
;
1136 if( pSort
&& pSort
->pOrderBy
==0 ) pSort
= 0;
1137 if( pSort
==0 && !hasDistinct
){
1138 assert( iContinue
!=0 );
1139 codeOffset(v
, p
->iOffset
, iContinue
);
1142 /* Pull the requested columns.
1144 nResultCol
= p
->pEList
->nExpr
;
1146 if( pDest
->iSdst
==0 ){
1148 nPrefixReg
= pSort
->pOrderBy
->nExpr
;
1149 if( !(pSort
->sortFlags
& SORTFLAG_UseSorter
) ) nPrefixReg
++;
1150 pParse
->nMem
+= nPrefixReg
;
1152 pDest
->iSdst
= pParse
->nMem
+1;
1153 pParse
->nMem
+= nResultCol
;
1154 }else if( pDest
->iSdst
+nResultCol
> pParse
->nMem
){
1155 /* This is an error condition that can result, for example, when a SELECT
1156 ** on the right-hand side of an INSERT contains more result columns than
1157 ** there are columns in the table on the left. The error will be caught
1158 ** and reported later. But we need to make sure enough memory is allocated
1159 ** to avoid other spurious errors in the meantime. */
1160 pParse
->nMem
+= nResultCol
;
1162 pDest
->nSdst
= nResultCol
;
1163 regOrig
= regResult
= pDest
->iSdst
;
1165 for(i
=0; i
<nResultCol
; i
++){
1166 sqlite3VdbeAddOp3(v
, OP_Column
, srcTab
, i
, regResult
+i
);
1167 VdbeComment((v
, "%s", p
->pEList
->a
[i
].zEName
));
1169 }else if( eDest
!=SRT_Exists
){
1170 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1171 ExprList
*pExtra
= 0;
1173 /* If the destination is an EXISTS(...) expression, the actual
1174 ** values returned by the SELECT are not required.
1176 u8 ecelFlags
; /* "ecel" is an abbreviation of "ExprCodeExprList" */
1178 if( eDest
==SRT_Mem
|| eDest
==SRT_Output
|| eDest
==SRT_Coroutine
){
1179 ecelFlags
= SQLITE_ECEL_DUP
;
1183 if( pSort
&& hasDistinct
==0 && eDest
!=SRT_EphemTab
&& eDest
!=SRT_Table
){
1184 /* For each expression in p->pEList that is a copy of an expression in
1185 ** the ORDER BY clause (pSort->pOrderBy), set the associated
1186 ** iOrderByCol value to one more than the index of the ORDER BY
1187 ** expression within the sort-key that pushOntoSorter() will generate.
1188 ** This allows the p->pEList field to be omitted from the sorted record,
1189 ** saving space and CPU cycles. */
1190 ecelFlags
|= (SQLITE_ECEL_OMITREF
|SQLITE_ECEL_REF
);
1192 for(i
=pSort
->nOBSat
; i
<pSort
->pOrderBy
->nExpr
; i
++){
1194 if( (j
= pSort
->pOrderBy
->a
[i
].u
.x
.iOrderByCol
)>0 ){
1195 p
->pEList
->a
[j
-1].u
.x
.iOrderByCol
= i
+1-pSort
->nOBSat
;
1198 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1199 selectExprDefer(pParse
, pSort
, p
->pEList
, &pExtra
);
1200 if( pExtra
&& pParse
->db
->mallocFailed
==0 ){
1201 /* If there are any extra PK columns to add to the sorter records,
1202 ** allocate extra memory cells and adjust the OpenEphemeral
1203 ** instruction to account for the larger records. This is only
1204 ** required if there are one or more WITHOUT ROWID tables with
1205 ** composite primary keys in the SortCtx.aDefer[] array. */
1206 VdbeOp
*pOp
= sqlite3VdbeGetOp(v
, pSort
->addrSortIndex
);
1207 pOp
->p2
+= (pExtra
->nExpr
- pSort
->nDefer
);
1208 pOp
->p4
.pKeyInfo
->nAllField
+= (pExtra
->nExpr
- pSort
->nDefer
);
1209 pParse
->nMem
+= pExtra
->nExpr
;
1213 /* Adjust nResultCol to account for columns that are omitted
1214 ** from the sorter by the optimizations in this branch */
1216 for(i
=0; i
<pEList
->nExpr
; i
++){
1217 if( pEList
->a
[i
].u
.x
.iOrderByCol
>0
1218 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1219 || pEList
->a
[i
].fg
.bSorterRef
1227 testcase( regOrig
);
1228 testcase( eDest
==SRT_Set
);
1229 testcase( eDest
==SRT_Mem
);
1230 testcase( eDest
==SRT_Coroutine
);
1231 testcase( eDest
==SRT_Output
);
1232 assert( eDest
==SRT_Set
|| eDest
==SRT_Mem
1233 || eDest
==SRT_Coroutine
|| eDest
==SRT_Output
1234 || eDest
==SRT_Upfrom
);
1236 sRowLoadInfo
.regResult
= regResult
;
1237 sRowLoadInfo
.ecelFlags
= ecelFlags
;
1238 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1239 sRowLoadInfo
.pExtra
= pExtra
;
1240 sRowLoadInfo
.regExtraResult
= regResult
+ nResultCol
;
1241 if( pExtra
) nResultCol
+= pExtra
->nExpr
;
1244 && (ecelFlags
& SQLITE_ECEL_OMITREF
)!=0
1248 assert( hasDistinct
==0 );
1249 pSort
->pDeferredRowLoad
= &sRowLoadInfo
;
1252 innerLoopLoadRow(pParse
, p
, &sRowLoadInfo
);
1256 /* If the DISTINCT keyword was present on the SELECT statement
1257 ** and this row has been seen before, then do not make this row
1258 ** part of the result.
1261 int eType
= pDistinct
->eTnctType
;
1262 int iTab
= pDistinct
->tabTnct
;
1263 assert( nResultCol
==p
->pEList
->nExpr
);
1264 iTab
= codeDistinct(pParse
, eType
, iTab
, iContinue
, p
->pEList
, regResult
);
1265 fixDistinctOpenEph(pParse
, eType
, iTab
, pDistinct
->addrTnct
);
1267 codeOffset(v
, p
->iOffset
, iContinue
);
1272 /* In this mode, write each query result to the key of the temporary
1275 #ifndef SQLITE_OMIT_COMPOUND_SELECT
1278 r1
= sqlite3GetTempReg(pParse
);
1279 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regResult
, nResultCol
, r1
);
1280 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iParm
, r1
, regResult
, nResultCol
);
1281 sqlite3ReleaseTempReg(pParse
, r1
);
1285 /* Construct a record from the query result, but instead of
1286 ** saving that record, use it as a key to delete elements from
1287 ** the temporary table iParm.
1290 sqlite3VdbeAddOp3(v
, OP_IdxDelete
, iParm
, regResult
, nResultCol
);
1293 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
1295 /* Store the result as data using a unique key.
1300 case SRT_EphemTab
: {
1301 int r1
= sqlite3GetTempRange(pParse
, nPrefixReg
+1);
1302 testcase( eDest
==SRT_Table
);
1303 testcase( eDest
==SRT_EphemTab
);
1304 testcase( eDest
==SRT_Fifo
);
1305 testcase( eDest
==SRT_DistFifo
);
1306 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regResult
, nResultCol
, r1
+nPrefixReg
);
1307 #if !defined(SQLITE_ENABLE_NULL_TRIM) && defined(SQLITE_DEBUG)
1308 /* A destination of SRT_Table and a non-zero iSDParm2 parameter means
1309 ** that this is an "UPDATE ... FROM" on a virtual table or view. In this
1310 ** case set the p5 parameter of the OP_MakeRecord to OPFLAG_NOCHNG_MAGIC.
1311 ** This does not affect operation in any way - it just allows MakeRecord
1312 ** to process OPFLAG_NOCHANGE values without an assert() failing. */
1313 if( eDest
==SRT_Table
&& pDest
->iSDParm2
){
1314 sqlite3VdbeChangeP5(v
, OPFLAG_NOCHNG_MAGIC
);
1317 #ifndef SQLITE_OMIT_CTE
1318 if( eDest
==SRT_DistFifo
){
1319 /* If the destination is DistFifo, then cursor (iParm+1) is open
1320 ** on an ephemeral index. If the current row is already present
1321 ** in the index, do not write it to the output. If not, add the
1322 ** current row to the index and proceed with writing it to the
1323 ** output table as well. */
1324 int addr
= sqlite3VdbeCurrentAddr(v
) + 4;
1325 sqlite3VdbeAddOp4Int(v
, OP_Found
, iParm
+1, addr
, r1
, 0);
1327 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iParm
+1, r1
,regResult
,nResultCol
);
1332 assert( regResult
==regOrig
);
1333 pushOntoSorter(pParse
, pSort
, p
, r1
+nPrefixReg
, regOrig
, 1, nPrefixReg
);
1335 int r2
= sqlite3GetTempReg(pParse
);
1336 sqlite3VdbeAddOp2(v
, OP_NewRowid
, iParm
, r2
);
1337 sqlite3VdbeAddOp3(v
, OP_Insert
, iParm
, r1
, r2
);
1338 sqlite3VdbeChangeP5(v
, OPFLAG_APPEND
);
1339 sqlite3ReleaseTempReg(pParse
, r2
);
1341 sqlite3ReleaseTempRange(pParse
, r1
, nPrefixReg
+1);
1348 pParse
, pSort
, p
, regResult
, regOrig
, nResultCol
, nPrefixReg
);
1350 int i2
= pDest
->iSDParm2
;
1351 int r1
= sqlite3GetTempReg(pParse
);
1353 /* If the UPDATE FROM join is an aggregate that matches no rows, it
1354 ** might still be trying to return one row, because that is what
1355 ** aggregates do. Don't record that empty row in the output table. */
1356 sqlite3VdbeAddOp2(v
, OP_IsNull
, regResult
, iBreak
); VdbeCoverage(v
);
1358 sqlite3VdbeAddOp3(v
, OP_MakeRecord
,
1359 regResult
+(i2
<0), nResultCol
-(i2
<0), r1
);
1361 sqlite3VdbeAddOp3(v
, OP_Insert
, iParm
, r1
, regResult
);
1363 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iParm
, r1
, regResult
, i2
);
1369 #ifndef SQLITE_OMIT_SUBQUERY
1370 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
1371 ** then there should be a single item on the stack. Write this
1372 ** item into the set table with bogus data.
1376 /* At first glance you would think we could optimize out the
1377 ** ORDER BY in this case since the order of entries in the set
1378 ** does not matter. But there might be a LIMIT clause, in which
1379 ** case the order does matter */
1381 pParse
, pSort
, p
, regResult
, regOrig
, nResultCol
, nPrefixReg
);
1382 pDest
->iSDParm2
= 0; /* Signal that any Bloom filter is unpopulated */
1384 int r1
= sqlite3GetTempReg(pParse
);
1385 assert( sqlite3Strlen30(pDest
->zAffSdst
)==nResultCol
);
1386 sqlite3VdbeAddOp4(v
, OP_MakeRecord
, regResult
, nResultCol
,
1387 r1
, pDest
->zAffSdst
, nResultCol
);
1388 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iParm
, r1
, regResult
, nResultCol
);
1389 if( pDest
->iSDParm2
){
1390 sqlite3VdbeAddOp4Int(v
, OP_FilterAdd
, pDest
->iSDParm2
, 0,
1391 regResult
, nResultCol
);
1392 ExplainQueryPlan((pParse
, 0, "CREATE BLOOM FILTER"));
1394 sqlite3ReleaseTempReg(pParse
, r1
);
1400 /* If any row exist in the result set, record that fact and abort.
1403 sqlite3VdbeAddOp2(v
, OP_Integer
, 1, iParm
);
1404 /* The LIMIT clause will terminate the loop for us */
1408 /* If this is a scalar select that is part of an expression, then
1409 ** store the results in the appropriate memory cell or array of
1410 ** memory cells and break out of the scan loop.
1414 assert( nResultCol
<=pDest
->nSdst
);
1416 pParse
, pSort
, p
, regResult
, regOrig
, nResultCol
, nPrefixReg
);
1418 assert( nResultCol
==pDest
->nSdst
);
1419 assert( regResult
==iParm
);
1420 /* The LIMIT clause will jump out of the loop for us */
1424 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
1426 case SRT_Coroutine
: /* Send data to a co-routine */
1427 case SRT_Output
: { /* Return the results */
1428 testcase( eDest
==SRT_Coroutine
);
1429 testcase( eDest
==SRT_Output
);
1431 pushOntoSorter(pParse
, pSort
, p
, regResult
, regOrig
, nResultCol
,
1433 }else if( eDest
==SRT_Coroutine
){
1434 sqlite3VdbeAddOp1(v
, OP_Yield
, pDest
->iSDParm
);
1436 sqlite3VdbeAddOp2(v
, OP_ResultRow
, regResult
, nResultCol
);
1441 #ifndef SQLITE_OMIT_CTE
1442 /* Write the results into a priority queue that is order according to
1443 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
1444 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
1445 ** pSO->nExpr columns, then make sure all keys are unique by adding a
1446 ** final OP_Sequence column. The last column is the record as a blob.
1454 pSO
= pDest
->pOrderBy
;
1457 r1
= sqlite3GetTempReg(pParse
);
1458 r2
= sqlite3GetTempRange(pParse
, nKey
+2);
1460 if( eDest
==SRT_DistQueue
){
1461 /* If the destination is DistQueue, then cursor (iParm+1) is open
1462 ** on a second ephemeral index that holds all values every previously
1463 ** added to the queue. */
1464 addrTest
= sqlite3VdbeAddOp4Int(v
, OP_Found
, iParm
+1, 0,
1465 regResult
, nResultCol
);
1468 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regResult
, nResultCol
, r3
);
1469 if( eDest
==SRT_DistQueue
){
1470 sqlite3VdbeAddOp2(v
, OP_IdxInsert
, iParm
+1, r3
);
1471 sqlite3VdbeChangeP5(v
, OPFLAG_USESEEKRESULT
);
1473 for(i
=0; i
<nKey
; i
++){
1474 sqlite3VdbeAddOp2(v
, OP_SCopy
,
1475 regResult
+ pSO
->a
[i
].u
.x
.iOrderByCol
- 1,
1478 sqlite3VdbeAddOp2(v
, OP_Sequence
, iParm
, r2
+nKey
);
1479 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, r2
, nKey
+2, r1
);
1480 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iParm
, r1
, r2
, nKey
+2);
1481 if( addrTest
) sqlite3VdbeJumpHere(v
, addrTest
);
1482 sqlite3ReleaseTempReg(pParse
, r1
);
1483 sqlite3ReleaseTempRange(pParse
, r2
, nKey
+2);
1486 #endif /* SQLITE_OMIT_CTE */
1490 #if !defined(SQLITE_OMIT_TRIGGER)
1491 /* Discard the results. This is used for SELECT statements inside
1492 ** the body of a TRIGGER. The purpose of such selects is to call
1493 ** user-defined functions that have side effects. We do not care
1494 ** about the actual results of the select.
1497 assert( eDest
==SRT_Discard
);
1503 /* Jump to the end of the loop if the LIMIT is reached. Except, if
1504 ** there is a sorter, in which case the sorter has already limited
1505 ** the output for us.
1507 if( pSort
==0 && p
->iLimit
){
1508 sqlite3VdbeAddOp2(v
, OP_DecrJumpZero
, p
->iLimit
, iBreak
); VdbeCoverage(v
);
1513 ** Allocate a KeyInfo object sufficient for an index of N key columns and
1516 KeyInfo
*sqlite3KeyInfoAlloc(sqlite3
*db
, int N
, int X
){
1517 int nExtra
= (N
+X
)*(sizeof(CollSeq
*)+1) - sizeof(CollSeq
*);
1518 KeyInfo
*p
= sqlite3DbMallocRawNN(db
, sizeof(KeyInfo
) + nExtra
);
1520 p
->aSortFlags
= (u8
*)&p
->aColl
[N
+X
];
1521 p
->nKeyField
= (u16
)N
;
1522 p
->nAllField
= (u16
)(N
+X
);
1526 memset(&p
[1], 0, nExtra
);
1528 return (KeyInfo
*)sqlite3OomFault(db
);
1534 ** Deallocate a KeyInfo object
1536 void sqlite3KeyInfoUnref(KeyInfo
*p
){
1539 assert( p
->nRef
>0 );
1541 if( p
->nRef
==0 ) sqlite3DbNNFreeNN(p
->db
, p
);
1546 ** Make a new pointer to a KeyInfo object
1548 KeyInfo
*sqlite3KeyInfoRef(KeyInfo
*p
){
1550 assert( p
->nRef
>0 );
1558 ** Return TRUE if a KeyInfo object can be change. The KeyInfo object
1559 ** can only be changed if this is just a single reference to the object.
1561 ** This routine is used only inside of assert() statements.
1563 int sqlite3KeyInfoIsWriteable(KeyInfo
*p
){ return p
->nRef
==1; }
1564 #endif /* SQLITE_DEBUG */
1567 ** Given an expression list, generate a KeyInfo structure that records
1568 ** the collating sequence for each expression in that expression list.
1570 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
1571 ** KeyInfo structure is appropriate for initializing a virtual index to
1572 ** implement that clause. If the ExprList is the result set of a SELECT
1573 ** then the KeyInfo structure is appropriate for initializing a virtual
1574 ** index to implement a DISTINCT test.
1576 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
1577 ** function is responsible for seeing that this structure is eventually
1580 KeyInfo
*sqlite3KeyInfoFromExprList(
1581 Parse
*pParse
, /* Parsing context */
1582 ExprList
*pList
, /* Form the KeyInfo object from this ExprList */
1583 int iStart
, /* Begin with this column of pList */
1584 int nExtra
/* Add this many extra columns to the end */
1588 struct ExprList_item
*pItem
;
1589 sqlite3
*db
= pParse
->db
;
1592 nExpr
= pList
->nExpr
;
1593 pInfo
= sqlite3KeyInfoAlloc(db
, nExpr
-iStart
, nExtra
+1);
1595 assert( sqlite3KeyInfoIsWriteable(pInfo
) );
1596 for(i
=iStart
, pItem
=pList
->a
+iStart
; i
<nExpr
; i
++, pItem
++){
1597 pInfo
->aColl
[i
-iStart
] = sqlite3ExprNNCollSeq(pParse
, pItem
->pExpr
);
1598 pInfo
->aSortFlags
[i
-iStart
] = pItem
->fg
.sortFlags
;
1605 ** Name of the connection operator, used for error messages.
1607 const char *sqlite3SelectOpName(int id
){
1610 case TK_ALL
: z
= "UNION ALL"; break;
1611 case TK_INTERSECT
: z
= "INTERSECT"; break;
1612 case TK_EXCEPT
: z
= "EXCEPT"; break;
1613 default: z
= "UNION"; break;
1618 #ifndef SQLITE_OMIT_EXPLAIN
1620 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
1621 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
1622 ** where the caption is of the form:
1624 ** "USE TEMP B-TREE FOR xxx"
1626 ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
1627 ** is determined by the zUsage argument.
1629 static void explainTempTable(Parse
*pParse
, const char *zUsage
){
1630 ExplainQueryPlan((pParse
, 0, "USE TEMP B-TREE FOR %s", zUsage
));
1634 ** Assign expression b to lvalue a. A second, no-op, version of this macro
1635 ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
1636 ** in sqlite3Select() to assign values to structure member variables that
1637 ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
1638 ** code with #ifndef directives.
1640 # define explainSetInteger(a, b) a = b
1643 /* No-op versions of the explainXXX() functions and macros. */
1644 # define explainTempTable(y,z)
1645 # define explainSetInteger(y,z)
1650 ** If the inner loop was generated using a non-null pOrderBy argument,
1651 ** then the results were placed in a sorter. After the loop is terminated
1652 ** we need to run the sorter and output the results. The following
1653 ** routine generates the code needed to do that.
1655 static void generateSortTail(
1656 Parse
*pParse
, /* Parsing context */
1657 Select
*p
, /* The SELECT statement */
1658 SortCtx
*pSort
, /* Information on the ORDER BY clause */
1659 int nColumn
, /* Number of columns of data */
1660 SelectDest
*pDest
/* Write the sorted results here */
1662 Vdbe
*v
= pParse
->pVdbe
; /* The prepared statement */
1663 int addrBreak
= pSort
->labelDone
; /* Jump here to exit loop */
1664 int addrContinue
= sqlite3VdbeMakeLabel(pParse
);/* Jump here for next cycle */
1665 int addr
; /* Top of output loop. Jump for Next. */
1668 ExprList
*pOrderBy
= pSort
->pOrderBy
;
1669 int eDest
= pDest
->eDest
;
1670 int iParm
= pDest
->iSDParm
;
1674 int nKey
; /* Number of key columns in sorter record */
1675 int iSortTab
; /* Sorter cursor to read from */
1677 int bSeq
; /* True if sorter record includes seq. no. */
1679 struct ExprList_item
*aOutEx
= p
->pEList
->a
;
1680 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
1681 int addrExplain
; /* Address of OP_Explain instruction */
1684 nKey
= pOrderBy
->nExpr
- pSort
->nOBSat
;
1685 if( pSort
->nOBSat
==0 || nKey
==1 ){
1686 ExplainQueryPlan2(addrExplain
, (pParse
, 0,
1687 "USE TEMP B-TREE FOR %sORDER BY", pSort
->nOBSat
?"LAST TERM OF ":""
1690 ExplainQueryPlan2(addrExplain
, (pParse
, 0,
1691 "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY", nKey
1694 sqlite3VdbeScanStatusRange(v
, addrExplain
,pSort
->addrPush
,pSort
->addrPushEnd
);
1695 sqlite3VdbeScanStatusCounters(v
, addrExplain
, addrExplain
, pSort
->addrPush
);
1698 assert( addrBreak
<0 );
1699 if( pSort
->labelBkOut
){
1700 sqlite3VdbeAddOp2(v
, OP_Gosub
, pSort
->regReturn
, pSort
->labelBkOut
);
1701 sqlite3VdbeGoto(v
, addrBreak
);
1702 sqlite3VdbeResolveLabel(v
, pSort
->labelBkOut
);
1705 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1706 /* Open any cursors needed for sorter-reference expressions */
1707 for(i
=0; i
<pSort
->nDefer
; i
++){
1708 Table
*pTab
= pSort
->aDefer
[i
].pTab
;
1709 int iDb
= sqlite3SchemaToIndex(pParse
->db
, pTab
->pSchema
);
1710 sqlite3OpenTable(pParse
, pSort
->aDefer
[i
].iCsr
, iDb
, pTab
, OP_OpenRead
);
1711 nRefKey
= MAX(nRefKey
, pSort
->aDefer
[i
].nKey
);
1715 iTab
= pSort
->iECursor
;
1716 if( eDest
==SRT_Output
|| eDest
==SRT_Coroutine
|| eDest
==SRT_Mem
){
1717 if( eDest
==SRT_Mem
&& p
->iOffset
){
1718 sqlite3VdbeAddOp2(v
, OP_Null
, 0, pDest
->iSdst
);
1721 regRow
= pDest
->iSdst
;
1723 regRowid
= sqlite3GetTempReg(pParse
);
1724 if( eDest
==SRT_EphemTab
|| eDest
==SRT_Table
){
1725 regRow
= sqlite3GetTempReg(pParse
);
1728 regRow
= sqlite3GetTempRange(pParse
, nColumn
);
1731 if( pSort
->sortFlags
& SORTFLAG_UseSorter
){
1732 int regSortOut
= ++pParse
->nMem
;
1733 iSortTab
= pParse
->nTab
++;
1734 if( pSort
->labelBkOut
){
1735 addrOnce
= sqlite3VdbeAddOp0(v
, OP_Once
); VdbeCoverage(v
);
1737 sqlite3VdbeAddOp3(v
, OP_OpenPseudo
, iSortTab
, regSortOut
,
1738 nKey
+1+nColumn
+nRefKey
);
1739 if( addrOnce
) sqlite3VdbeJumpHere(v
, addrOnce
);
1740 addr
= 1 + sqlite3VdbeAddOp2(v
, OP_SorterSort
, iTab
, addrBreak
);
1742 assert( p
->iLimit
==0 && p
->iOffset
==0 );
1743 sqlite3VdbeAddOp3(v
, OP_SorterData
, iTab
, regSortOut
, iSortTab
);
1746 addr
= 1 + sqlite3VdbeAddOp2(v
, OP_Sort
, iTab
, addrBreak
); VdbeCoverage(v
);
1747 codeOffset(v
, p
->iOffset
, addrContinue
);
1751 sqlite3VdbeAddOp2(v
, OP_AddImm
, p
->iLimit
, -1);
1754 for(i
=0, iCol
=nKey
+bSeq
-1; i
<nColumn
; i
++){
1755 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1756 if( aOutEx
[i
].fg
.bSorterRef
) continue;
1758 if( aOutEx
[i
].u
.x
.iOrderByCol
==0 ) iCol
++;
1760 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1761 if( pSort
->nDefer
){
1763 int regKey
= sqlite3GetTempRange(pParse
, nRefKey
);
1765 for(i
=0; i
<pSort
->nDefer
; i
++){
1766 int iCsr
= pSort
->aDefer
[i
].iCsr
;
1767 Table
*pTab
= pSort
->aDefer
[i
].pTab
;
1768 int nKey
= pSort
->aDefer
[i
].nKey
;
1770 sqlite3VdbeAddOp1(v
, OP_NullRow
, iCsr
);
1771 if( HasRowid(pTab
) ){
1772 sqlite3VdbeAddOp3(v
, OP_Column
, iSortTab
, iKey
++, regKey
);
1773 sqlite3VdbeAddOp3(v
, OP_SeekRowid
, iCsr
,
1774 sqlite3VdbeCurrentAddr(v
)+1, regKey
);
1778 assert( sqlite3PrimaryKeyIndex(pTab
)->nKeyCol
==nKey
);
1779 for(k
=0; k
<nKey
; k
++){
1780 sqlite3VdbeAddOp3(v
, OP_Column
, iSortTab
, iKey
++, regKey
+k
);
1782 iJmp
= sqlite3VdbeCurrentAddr(v
);
1783 sqlite3VdbeAddOp4Int(v
, OP_SeekGE
, iCsr
, iJmp
+2, regKey
, nKey
);
1784 sqlite3VdbeAddOp4Int(v
, OP_IdxLE
, iCsr
, iJmp
+3, regKey
, nKey
);
1785 sqlite3VdbeAddOp1(v
, OP_NullRow
, iCsr
);
1788 sqlite3ReleaseTempRange(pParse
, regKey
, nRefKey
);
1791 for(i
=nColumn
-1; i
>=0; i
--){
1792 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1793 if( aOutEx
[i
].fg
.bSorterRef
){
1794 sqlite3ExprCode(pParse
, aOutEx
[i
].pExpr
, regRow
+i
);
1799 if( aOutEx
[i
].u
.x
.iOrderByCol
){
1800 iRead
= aOutEx
[i
].u
.x
.iOrderByCol
-1;
1804 sqlite3VdbeAddOp3(v
, OP_Column
, iSortTab
, iRead
, regRow
+i
);
1805 VdbeComment((v
, "%s", aOutEx
[i
].zEName
));
1808 sqlite3VdbeScanStatusRange(v
, addrExplain
, addrExplain
, -1);
1811 case SRT_EphemTab
: {
1812 sqlite3VdbeAddOp3(v
, OP_Column
, iSortTab
, nKey
+bSeq
, regRow
);
1813 sqlite3VdbeAddOp2(v
, OP_NewRowid
, iParm
, regRowid
);
1814 sqlite3VdbeAddOp3(v
, OP_Insert
, iParm
, regRow
, regRowid
);
1815 sqlite3VdbeChangeP5(v
, OPFLAG_APPEND
);
1818 #ifndef SQLITE_OMIT_SUBQUERY
1820 assert( nColumn
==sqlite3Strlen30(pDest
->zAffSdst
) );
1821 sqlite3VdbeAddOp4(v
, OP_MakeRecord
, regRow
, nColumn
, regRowid
,
1822 pDest
->zAffSdst
, nColumn
);
1823 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iParm
, regRowid
, regRow
, nColumn
);
1827 /* The LIMIT clause will terminate the loop for us */
1832 int i2
= pDest
->iSDParm2
;
1833 int r1
= sqlite3GetTempReg(pParse
);
1834 sqlite3VdbeAddOp3(v
, OP_MakeRecord
,regRow
+(i2
<0),nColumn
-(i2
<0),r1
);
1836 sqlite3VdbeAddOp3(v
, OP_Insert
, iParm
, r1
, regRow
);
1838 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, iParm
, r1
, regRow
, i2
);
1843 assert( eDest
==SRT_Output
|| eDest
==SRT_Coroutine
);
1844 testcase( eDest
==SRT_Output
);
1845 testcase( eDest
==SRT_Coroutine
);
1846 if( eDest
==SRT_Output
){
1847 sqlite3VdbeAddOp2(v
, OP_ResultRow
, pDest
->iSdst
, nColumn
);
1849 sqlite3VdbeAddOp1(v
, OP_Yield
, pDest
->iSDParm
);
1855 if( eDest
==SRT_Set
){
1856 sqlite3ReleaseTempRange(pParse
, regRow
, nColumn
);
1858 sqlite3ReleaseTempReg(pParse
, regRow
);
1860 sqlite3ReleaseTempReg(pParse
, regRowid
);
1862 /* The bottom of the loop
1864 sqlite3VdbeResolveLabel(v
, addrContinue
);
1865 if( pSort
->sortFlags
& SORTFLAG_UseSorter
){
1866 sqlite3VdbeAddOp2(v
, OP_SorterNext
, iTab
, addr
); VdbeCoverage(v
);
1868 sqlite3VdbeAddOp2(v
, OP_Next
, iTab
, addr
); VdbeCoverage(v
);
1870 sqlite3VdbeScanStatusRange(v
, addrExplain
, sqlite3VdbeCurrentAddr(v
)-1, -1);
1871 if( pSort
->regReturn
) sqlite3VdbeAddOp1(v
, OP_Return
, pSort
->regReturn
);
1872 sqlite3VdbeResolveLabel(v
, addrBreak
);
1876 ** Return a pointer to a string containing the 'declaration type' of the
1877 ** expression pExpr. The string may be treated as static by the caller.
1879 ** The declaration type is the exact datatype definition extracted from the
1880 ** original CREATE TABLE statement if the expression is a column. The
1881 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
1882 ** is considered a column can be complex in the presence of subqueries. The
1883 ** result-set expression in all of the following SELECT statements is
1884 ** considered a column by this function.
1886 ** SELECT col FROM tbl;
1887 ** SELECT (SELECT col FROM tbl;
1888 ** SELECT (SELECT col FROM tbl);
1889 ** SELECT abc FROM (SELECT col AS abc FROM tbl);
1891 ** The declaration type for any expression other than a column is NULL.
1893 ** This routine has either 3 or 6 parameters depending on whether or not
1894 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
1896 #ifdef SQLITE_ENABLE_COLUMN_METADATA
1897 # define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
1898 #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
1899 # define columnType(A,B,C,D,E) columnTypeImpl(A,B)
1901 static const char *columnTypeImpl(
1903 #ifndef SQLITE_ENABLE_COLUMN_METADATA
1907 const char **pzOrigDb
,
1908 const char **pzOrigTab
,
1909 const char **pzOrigCol
1912 char const *zType
= 0;
1914 #ifdef SQLITE_ENABLE_COLUMN_METADATA
1915 char const *zOrigDb
= 0;
1916 char const *zOrigTab
= 0;
1917 char const *zOrigCol
= 0;
1921 assert( pNC
->pSrcList
!=0 );
1922 switch( pExpr
->op
){
1924 /* The expression is a column. Locate the table the column is being
1925 ** extracted from in NameContext.pSrcList. This table may be real
1926 ** database table or a subquery.
1928 Table
*pTab
= 0; /* Table structure column is extracted from */
1929 Select
*pS
= 0; /* Select the column is extracted from */
1930 int iCol
= pExpr
->iColumn
; /* Index of column in pTab */
1931 while( pNC
&& !pTab
){
1932 SrcList
*pTabList
= pNC
->pSrcList
;
1933 for(j
=0;j
<pTabList
->nSrc
&& pTabList
->a
[j
].iCursor
!=pExpr
->iTable
;j
++);
1934 if( j
<pTabList
->nSrc
){
1935 pTab
= pTabList
->a
[j
].pSTab
;
1936 if( pTabList
->a
[j
].fg
.isSubquery
){
1937 pS
= pTabList
->a
[j
].u4
.pSubq
->pSelect
;
1947 /* At one time, code such as "SELECT new.x" within a trigger would
1948 ** cause this condition to run. Since then, we have restructured how
1949 ** trigger code is generated and so this condition is no longer
1950 ** possible. However, it can still be true for statements like
1953 ** CREATE TABLE t1(col INTEGER);
1954 ** SELECT (SELECT t1.col) FROM FROM t1;
1956 ** when columnType() is called on the expression "t1.col" in the
1957 ** sub-select. In this case, set the column type to NULL, even
1958 ** though it should really be "INTEGER".
1960 ** This is not a problem, as the column type of "t1.col" is never
1961 ** used. When columnType() is called on the expression
1962 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
1967 assert( pTab
&& ExprUseYTab(pExpr
) && pExpr
->y
.pTab
==pTab
);
1969 /* The "table" is actually a sub-select or a view in the FROM clause
1970 ** of the SELECT statement. Return the declaration type and origin
1971 ** data for the result-set column of the sub-select.
1973 if( iCol
<pS
->pEList
->nExpr
1974 && (!ViewCanHaveRowid
|| iCol
>=0)
1976 /* If iCol is less than zero, then the expression requests the
1977 ** rowid of the sub-select or view. This expression is legal (see
1978 ** test case misc2.2.2) - it always evaluates to NULL.
1981 Expr
*p
= pS
->pEList
->a
[iCol
].pExpr
;
1982 sNC
.pSrcList
= pS
->pSrc
;
1984 sNC
.pParse
= pNC
->pParse
;
1985 zType
= columnType(&sNC
, p
,&zOrigDb
,&zOrigTab
,&zOrigCol
);
1988 /* A real table or a CTE table */
1990 #ifdef SQLITE_ENABLE_COLUMN_METADATA
1991 if( iCol
<0 ) iCol
= pTab
->iPKey
;
1992 assert( iCol
==XN_ROWID
|| (iCol
>=0 && iCol
<pTab
->nCol
) );
1997 zOrigCol
= pTab
->aCol
[iCol
].zCnName
;
1998 zType
= sqlite3ColumnType(&pTab
->aCol
[iCol
],0);
2000 zOrigTab
= pTab
->zName
;
2001 if( pNC
->pParse
&& pTab
->pSchema
){
2002 int iDb
= sqlite3SchemaToIndex(pNC
->pParse
->db
, pTab
->pSchema
);
2003 zOrigDb
= pNC
->pParse
->db
->aDb
[iDb
].zDbSName
;
2006 assert( iCol
==XN_ROWID
|| (iCol
>=0 && iCol
<pTab
->nCol
) );
2010 zType
= sqlite3ColumnType(&pTab
->aCol
[iCol
],0);
2016 #ifndef SQLITE_OMIT_SUBQUERY
2018 /* The expression is a sub-select. Return the declaration type and
2019 ** origin info for the single column in the result set of the SELECT
2025 assert( ExprUseXSelect(pExpr
) );
2026 pS
= pExpr
->x
.pSelect
;
2027 p
= pS
->pEList
->a
[0].pExpr
;
2028 sNC
.pSrcList
= pS
->pSrc
;
2030 sNC
.pParse
= pNC
->pParse
;
2031 zType
= columnType(&sNC
, p
, &zOrigDb
, &zOrigTab
, &zOrigCol
);
2037 #ifdef SQLITE_ENABLE_COLUMN_METADATA
2039 assert( pzOrigTab
&& pzOrigCol
);
2040 *pzOrigDb
= zOrigDb
;
2041 *pzOrigTab
= zOrigTab
;
2042 *pzOrigCol
= zOrigCol
;
2049 ** Generate code that will tell the VDBE the declaration types of columns
2050 ** in the result set.
2052 static void generateColumnTypes(
2053 Parse
*pParse
, /* Parser context */
2054 SrcList
*pTabList
, /* List of tables */
2055 ExprList
*pEList
/* Expressions defining the result set */
2057 #ifndef SQLITE_OMIT_DECLTYPE
2058 Vdbe
*v
= pParse
->pVdbe
;
2061 sNC
.pSrcList
= pTabList
;
2062 sNC
.pParse
= pParse
;
2064 for(i
=0; i
<pEList
->nExpr
; i
++){
2065 Expr
*p
= pEList
->a
[i
].pExpr
;
2067 #ifdef SQLITE_ENABLE_COLUMN_METADATA
2068 const char *zOrigDb
= 0;
2069 const char *zOrigTab
= 0;
2070 const char *zOrigCol
= 0;
2071 zType
= columnType(&sNC
, p
, &zOrigDb
, &zOrigTab
, &zOrigCol
);
2073 /* The vdbe must make its own copy of the column-type and other
2074 ** column specific strings, in case the schema is reset before this
2075 ** virtual machine is deleted.
2077 sqlite3VdbeSetColName(v
, i
, COLNAME_DATABASE
, zOrigDb
, SQLITE_TRANSIENT
);
2078 sqlite3VdbeSetColName(v
, i
, COLNAME_TABLE
, zOrigTab
, SQLITE_TRANSIENT
);
2079 sqlite3VdbeSetColName(v
, i
, COLNAME_COLUMN
, zOrigCol
, SQLITE_TRANSIENT
);
2081 zType
= columnType(&sNC
, p
, 0, 0, 0);
2083 sqlite3VdbeSetColName(v
, i
, COLNAME_DECLTYPE
, zType
, SQLITE_TRANSIENT
);
2085 #endif /* !defined(SQLITE_OMIT_DECLTYPE) */
2090 ** Compute the column names for a SELECT statement.
2092 ** The only guarantee that SQLite makes about column names is that if the
2093 ** column has an AS clause assigning it a name, that will be the name used.
2094 ** That is the only documented guarantee. However, countless applications
2095 ** developed over the years have made baseless assumptions about column names
2096 ** and will break if those assumptions changes. Hence, use extreme caution
2097 ** when modifying this routine to avoid breaking legacy.
2099 ** See Also: sqlite3ColumnsFromExprList()
2101 ** The PRAGMA short_column_names and PRAGMA full_column_names settings are
2102 ** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
2103 ** applications should operate this way. Nevertheless, we need to support the
2104 ** other modes for legacy:
2106 ** short=OFF, full=OFF: Column name is the text of the expression has it
2107 ** originally appears in the SELECT statement. In
2108 ** other words, the zSpan of the result expression.
2110 ** short=ON, full=OFF: (This is the default setting). If the result
2111 ** refers directly to a table column, then the
2112 ** result column name is just the table column
2113 ** name: COLUMN. Otherwise use zSpan.
2115 ** full=ON, short=ANY: If the result refers directly to a table column,
2116 ** then the result column name with the table name
2117 ** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
2119 void sqlite3GenerateColumnNames(
2120 Parse
*pParse
, /* Parser context */
2121 Select
*pSelect
/* Generate column names for this SELECT statement */
2123 Vdbe
*v
= pParse
->pVdbe
;
2128 sqlite3
*db
= pParse
->db
;
2129 int fullName
; /* TABLE.COLUMN if no AS clause and is a direct table ref */
2130 int srcName
; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
2132 if( pParse
->colNamesSet
) return;
2133 /* Column names are determined by the left-most term of a compound select */
2134 while( pSelect
->pPrior
) pSelect
= pSelect
->pPrior
;
2135 TREETRACE(0x80,pParse
,pSelect
,("generating column names\n"));
2136 pTabList
= pSelect
->pSrc
;
2137 pEList
= pSelect
->pEList
;
2139 assert( pTabList
!=0 );
2140 pParse
->colNamesSet
= 1;
2141 fullName
= (db
->flags
& SQLITE_FullColNames
)!=0;
2142 srcName
= (db
->flags
& SQLITE_ShortColNames
)!=0 || fullName
;
2143 sqlite3VdbeSetNumCols(v
, pEList
->nExpr
);
2144 for(i
=0; i
<pEList
->nExpr
; i
++){
2145 Expr
*p
= pEList
->a
[i
].pExpr
;
2148 assert( p
->op
!=TK_AGG_COLUMN
); /* Agg processing has not run yet */
2149 assert( p
->op
!=TK_COLUMN
2150 || (ExprUseYTab(p
) && p
->y
.pTab
!=0) ); /* Covering idx not yet coded */
2151 if( pEList
->a
[i
].zEName
&& pEList
->a
[i
].fg
.eEName
==ENAME_NAME
){
2152 /* An AS clause always takes first priority */
2153 char *zName
= pEList
->a
[i
].zEName
;
2154 sqlite3VdbeSetColName(v
, i
, COLNAME_NAME
, zName
, SQLITE_TRANSIENT
);
2155 }else if( srcName
&& p
->op
==TK_COLUMN
){
2157 int iCol
= p
->iColumn
;
2160 if( iCol
<0 ) iCol
= pTab
->iPKey
;
2161 assert( iCol
==-1 || (iCol
>=0 && iCol
<pTab
->nCol
) );
2165 zCol
= pTab
->aCol
[iCol
].zCnName
;
2169 zName
= sqlite3MPrintf(db
, "%s.%s", pTab
->zName
, zCol
);
2170 sqlite3VdbeSetColName(v
, i
, COLNAME_NAME
, zName
, SQLITE_DYNAMIC
);
2172 sqlite3VdbeSetColName(v
, i
, COLNAME_NAME
, zCol
, SQLITE_TRANSIENT
);
2175 const char *z
= pEList
->a
[i
].zEName
;
2176 z
= z
==0 ? sqlite3MPrintf(db
, "column%d", i
+1) : sqlite3DbStrDup(db
, z
);
2177 sqlite3VdbeSetColName(v
, i
, COLNAME_NAME
, z
, SQLITE_DYNAMIC
);
2180 generateColumnTypes(pParse
, pTabList
, pEList
);
2184 ** Given an expression list (which is really the list of expressions
2185 ** that form the result set of a SELECT statement) compute appropriate
2186 ** column names for a table that would hold the expression list.
2188 ** All column names will be unique.
2190 ** Only the column names are computed. Column.zType, Column.zColl,
2191 ** and other fields of Column are zeroed.
2193 ** Return SQLITE_OK on success. If a memory allocation error occurs,
2194 ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
2196 ** The only guarantee that SQLite makes about column names is that if the
2197 ** column has an AS clause assigning it a name, that will be the name used.
2198 ** That is the only documented guarantee. However, countless applications
2199 ** developed over the years have made baseless assumptions about column names
2200 ** and will break if those assumptions changes. Hence, use extreme caution
2201 ** when modifying this routine to avoid breaking legacy.
2203 ** See Also: sqlite3GenerateColumnNames()
2205 int sqlite3ColumnsFromExprList(
2206 Parse
*pParse
, /* Parsing context */
2207 ExprList
*pEList
, /* Expr list from which to derive column names */
2208 i16
*pnCol
, /* Write the number of columns here */
2209 Column
**paCol
/* Write the new column list here */
2211 sqlite3
*db
= pParse
->db
; /* Database connection */
2212 int i
, j
; /* Loop counters */
2213 u32 cnt
; /* Index added to make the name unique */
2214 Column
*aCol
, *pCol
; /* For looping over result columns */
2215 int nCol
; /* Number of columns in the result set */
2216 char *zName
; /* Column name */
2217 int nName
; /* Size of name in zName[] */
2218 Hash ht
; /* Hash table of column names */
2221 sqlite3HashInit(&ht
);
2223 nCol
= pEList
->nExpr
;
2224 aCol
= sqlite3DbMallocZero(db
, sizeof(aCol
[0])*nCol
);
2225 testcase( aCol
==0 );
2226 if( NEVER(nCol
>32767) ) nCol
= 32767;
2231 assert( nCol
==(i16
)nCol
);
2235 for(i
=0, pCol
=aCol
; i
<nCol
&& !pParse
->nErr
; i
++, pCol
++){
2236 struct ExprList_item
*pX
= &pEList
->a
[i
];
2237 struct ExprList_item
*pCollide
;
2238 /* Get an appropriate name for the column
2240 if( (zName
= pX
->zEName
)!=0 && pX
->fg
.eEName
==ENAME_NAME
){
2241 /* If the column contains an "AS <name>" phrase, use <name> as the name */
2243 Expr
*pColExpr
= sqlite3ExprSkipCollateAndLikely(pX
->pExpr
);
2244 while( ALWAYS(pColExpr
!=0) && pColExpr
->op
==TK_DOT
){
2245 pColExpr
= pColExpr
->pRight
;
2246 assert( pColExpr
!=0 );
2248 if( pColExpr
->op
==TK_COLUMN
2249 && ALWAYS( ExprUseYTab(pColExpr
) )
2250 && ALWAYS( pColExpr
->y
.pTab
!=0 )
2252 /* For columns use the column name name */
2253 int iCol
= pColExpr
->iColumn
;
2254 pTab
= pColExpr
->y
.pTab
;
2255 if( iCol
<0 ) iCol
= pTab
->iPKey
;
2256 zName
= iCol
>=0 ? pTab
->aCol
[iCol
].zCnName
: "rowid";
2257 }else if( pColExpr
->op
==TK_ID
){
2258 assert( !ExprHasProperty(pColExpr
, EP_IntValue
) );
2259 zName
= pColExpr
->u
.zToken
;
2261 /* Use the original text of the column expression as its name */
2262 assert( zName
==pX
->zEName
); /* pointer comparison intended */
2265 if( zName
&& !sqlite3IsTrueOrFalse(zName
) ){
2266 zName
= sqlite3DbStrDup(db
, zName
);
2268 zName
= sqlite3MPrintf(db
,"column%d",i
+1);
2271 /* Make sure the column name is unique. If the name is not unique,
2272 ** append an integer to the name so that it becomes unique.
2275 while( zName
&& (pCollide
= sqlite3HashFind(&ht
, zName
))!=0 ){
2276 if( pCollide
->fg
.bUsingTerm
){
2277 pCol
->colFlags
|= COLFLAG_NOEXPAND
;
2279 nName
= sqlite3Strlen30(zName
);
2281 for(j
=nName
-1; j
>0 && sqlite3Isdigit(zName
[j
]); j
--){}
2282 if( zName
[j
]==':' ) nName
= j
;
2284 zName
= sqlite3MPrintf(db
, "%.*z:%u", nName
, zName
, ++cnt
);
2285 sqlite3ProgressCheck(pParse
);
2287 sqlite3_randomness(sizeof(cnt
), &cnt
);
2290 pCol
->zCnName
= zName
;
2291 pCol
->hName
= sqlite3StrIHash(zName
);
2292 if( pX
->fg
.bNoExpand
){
2293 pCol
->colFlags
|= COLFLAG_NOEXPAND
;
2295 sqlite3ColumnPropertiesFromName(0, pCol
);
2296 if( zName
&& sqlite3HashInsert(&ht
, zName
, pX
)==pX
){
2297 sqlite3OomFault(db
);
2300 sqlite3HashClear(&ht
);
2303 sqlite3DbFree(db
, aCol
[j
].zCnName
);
2305 sqlite3DbFree(db
, aCol
);
2314 ** pTab is a transient Table object that represents a subquery of some
2315 ** kind (maybe a parenthesized subquery in the FROM clause of a larger
2316 ** query, or a VIEW, or a CTE). This routine computes type information
2317 ** for that Table object based on the Select object that implements the
2318 ** subquery. For the purposes of this routine, "type information" means:
2320 ** * The datatype name, as it might appear in a CREATE TABLE statement
2321 ** * Which collating sequence to use for the column
2322 ** * The affinity of the column
2324 void sqlite3SubqueryColumnTypes(
2325 Parse
*pParse
, /* Parsing contexts */
2326 Table
*pTab
, /* Add column type information to this table */
2327 Select
*pSelect
, /* SELECT used to determine types and collations */
2328 char aff
/* Default affinity. */
2330 sqlite3
*db
= pParse
->db
;
2335 struct ExprList_item
*a
;
2338 assert( pSelect
!=0 );
2339 assert( (pSelect
->selFlags
& SF_Resolved
)!=0 );
2340 assert( pTab
->nCol
==pSelect
->pEList
->nExpr
|| pParse
->nErr
>0 );
2341 assert( aff
==SQLITE_AFF_NONE
|| aff
==SQLITE_AFF_BLOB
);
2342 if( db
->mallocFailed
|| IN_RENAME_OBJECT
) return;
2343 while( pSelect
->pPrior
) pSelect
= pSelect
->pPrior
;
2344 a
= pSelect
->pEList
->a
;
2345 memset(&sNC
, 0, sizeof(sNC
));
2346 sNC
.pSrcList
= pSelect
->pSrc
;
2347 for(i
=0, pCol
=pTab
->aCol
; i
<pTab
->nCol
; i
++, pCol
++){
2351 Select
*pS2
= pSelect
;
2352 pTab
->tabFlags
|= (pCol
->colFlags
& COLFLAG_NOINSERT
);
2354 /* pCol->szEst = ... // Column size est for SELECT tables never used */
2355 pCol
->affinity
= sqlite3ExprAffinity(p
);
2356 while( pCol
->affinity
<=SQLITE_AFF_NONE
&& pS2
->pNext
!=0 ){
2357 m
|= sqlite3ExprDataType(pS2
->pEList
->a
[i
].pExpr
);
2359 pCol
->affinity
= sqlite3ExprAffinity(pS2
->pEList
->a
[i
].pExpr
);
2361 if( pCol
->affinity
<=SQLITE_AFF_NONE
){
2362 pCol
->affinity
= aff
;
2364 if( pCol
->affinity
>=SQLITE_AFF_TEXT
&& (pS2
->pNext
|| pS2
!=pSelect
) ){
2365 for(pS2
=pS2
->pNext
; pS2
; pS2
=pS2
->pNext
){
2366 m
|= sqlite3ExprDataType(pS2
->pEList
->a
[i
].pExpr
);
2368 if( pCol
->affinity
==SQLITE_AFF_TEXT
&& (m
&0x01)!=0 ){
2369 pCol
->affinity
= SQLITE_AFF_BLOB
;
2371 if( pCol
->affinity
>=SQLITE_AFF_NUMERIC
&& (m
&0x02)!=0 ){
2372 pCol
->affinity
= SQLITE_AFF_BLOB
;
2374 if( pCol
->affinity
>=SQLITE_AFF_NUMERIC
&& p
->op
==TK_CAST
){
2375 pCol
->affinity
= SQLITE_AFF_FLEXNUM
;
2378 zType
= columnType(&sNC
, p
, 0, 0, 0);
2379 if( zType
==0 || pCol
->affinity
!=sqlite3AffinityType(zType
, 0) ){
2380 if( pCol
->affinity
==SQLITE_AFF_NUMERIC
2381 || pCol
->affinity
==SQLITE_AFF_FLEXNUM
2386 for(j
=1; j
<SQLITE_N_STDTYPE
; j
++){
2387 if( sqlite3StdTypeAffinity
[j
]==pCol
->affinity
){
2388 zType
= sqlite3StdType
[j
];
2395 const i64 k
= sqlite3Strlen30(zType
);
2396 n
= sqlite3Strlen30(pCol
->zCnName
);
2397 pCol
->zCnName
= sqlite3DbReallocOrFree(db
, pCol
->zCnName
, n
+k
+2);
2398 pCol
->colFlags
&= ~(COLFLAG_HASTYPE
|COLFLAG_HASCOLL
);
2399 if( pCol
->zCnName
){
2400 memcpy(&pCol
->zCnName
[n
+1], zType
, k
+1);
2401 pCol
->colFlags
|= COLFLAG_HASTYPE
;
2404 pColl
= sqlite3ExprCollSeq(pParse
, p
);
2406 assert( pTab
->pIndex
==0 );
2407 sqlite3ColumnSetColl(db
, pCol
, pColl
->zName
);
2410 pTab
->szTabRow
= 1; /* Any non-zero value works */
2414 ** Given a SELECT statement, generate a Table structure that describes
2415 ** the result set of that SELECT.
2417 Table
*sqlite3ResultSetOfSelect(Parse
*pParse
, Select
*pSelect
, char aff
){
2419 sqlite3
*db
= pParse
->db
;
2422 savedFlags
= db
->flags
;
2423 db
->flags
&= ~(u64
)SQLITE_FullColNames
;
2424 db
->flags
|= SQLITE_ShortColNames
;
2425 sqlite3SelectPrep(pParse
, pSelect
, 0);
2426 db
->flags
= savedFlags
;
2427 if( pParse
->nErr
) return 0;
2428 while( pSelect
->pPrior
) pSelect
= pSelect
->pPrior
;
2429 pTab
= sqlite3DbMallocZero(db
, sizeof(Table
) );
2435 pTab
->nRowLogEst
= 200; assert( 200==sqlite3LogEst(1048576) );
2436 sqlite3ColumnsFromExprList(pParse
, pSelect
->pEList
, &pTab
->nCol
, &pTab
->aCol
);
2437 sqlite3SubqueryColumnTypes(pParse
, pTab
, pSelect
, aff
);
2439 if( db
->mallocFailed
){
2440 sqlite3DeleteTable(db
, pTab
);
2447 ** Get a VDBE for the given parser context. Create a new one if necessary.
2448 ** If an error occurs, return NULL and leave a message in pParse.
2450 Vdbe
*sqlite3GetVdbe(Parse
*pParse
){
2451 if( pParse
->pVdbe
){
2452 return pParse
->pVdbe
;
2454 if( pParse
->pToplevel
==0
2455 && OptimizationEnabled(pParse
->db
,SQLITE_FactorOutConst
)
2457 pParse
->okConstFactor
= 1;
2459 return sqlite3VdbeCreate(pParse
);
2464 ** Compute the iLimit and iOffset fields of the SELECT based on the
2465 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
2466 ** that appear in the original SQL statement after the LIMIT and OFFSET
2467 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
2468 ** are the integer memory register numbers for counters used to compute
2469 ** the limit and offset. If there is no limit and/or offset, then
2470 ** iLimit and iOffset are negative.
2472 ** This routine changes the values of iLimit and iOffset only if
2473 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
2474 ** and iOffset should have been preset to appropriate default values (zero)
2475 ** prior to calling this routine.
2477 ** The iOffset register (if it exists) is initialized to the value
2478 ** of the OFFSET. The iLimit register is initialized to LIMIT. Register
2479 ** iOffset+1 is initialized to LIMIT+OFFSET.
2481 ** Only if pLimit->pLeft!=0 do the limit registers get
2482 ** redefined. The UNION ALL operator uses this property to force
2483 ** the reuse of the same limit and offset registers across multiple
2484 ** SELECT statements.
2486 static void computeLimitRegisters(Parse
*pParse
, Select
*p
, int iBreak
){
2491 Expr
*pLimit
= p
->pLimit
;
2493 if( p
->iLimit
) return;
2496 ** "LIMIT -1" always shows all rows. There is some
2497 ** controversy about what the correct behavior should be.
2498 ** The current implementation interprets "LIMIT 0" to mean
2502 assert( pLimit
->op
==TK_LIMIT
);
2503 assert( pLimit
->pLeft
!=0 );
2504 p
->iLimit
= iLimit
= ++pParse
->nMem
;
2505 v
= sqlite3GetVdbe(pParse
);
2507 if( sqlite3ExprIsInteger(pLimit
->pLeft
, &n
, pParse
) ){
2508 sqlite3VdbeAddOp2(v
, OP_Integer
, n
, iLimit
);
2509 VdbeComment((v
, "LIMIT counter"));
2511 sqlite3VdbeGoto(v
, iBreak
);
2512 }else if( n
>=0 && p
->nSelectRow
>sqlite3LogEst((u64
)n
) ){
2513 p
->nSelectRow
= sqlite3LogEst((u64
)n
);
2514 p
->selFlags
|= SF_FixedLimit
;
2517 sqlite3ExprCode(pParse
, pLimit
->pLeft
, iLimit
);
2518 sqlite3VdbeAddOp1(v
, OP_MustBeInt
, iLimit
); VdbeCoverage(v
);
2519 VdbeComment((v
, "LIMIT counter"));
2520 sqlite3VdbeAddOp2(v
, OP_IfNot
, iLimit
, iBreak
); VdbeCoverage(v
);
2522 if( pLimit
->pRight
){
2523 p
->iOffset
= iOffset
= ++pParse
->nMem
;
2524 pParse
->nMem
++; /* Allocate an extra register for limit+offset */
2525 sqlite3ExprCode(pParse
, pLimit
->pRight
, iOffset
);
2526 sqlite3VdbeAddOp1(v
, OP_MustBeInt
, iOffset
); VdbeCoverage(v
);
2527 VdbeComment((v
, "OFFSET counter"));
2528 sqlite3VdbeAddOp3(v
, OP_OffsetLimit
, iLimit
, iOffset
+1, iOffset
);
2529 VdbeComment((v
, "LIMIT+OFFSET"));
2534 #ifndef SQLITE_OMIT_COMPOUND_SELECT
2536 ** Return the appropriate collating sequence for the iCol-th column of
2537 ** the result set for the compound-select statement "p". Return NULL if
2538 ** the column has no default collating sequence.
2540 ** The collating sequence for the compound select is taken from the
2541 ** left-most term of the select that has a collating sequence.
2543 static CollSeq
*multiSelectCollSeq(Parse
*pParse
, Select
*p
, int iCol
){
2546 pRet
= multiSelectCollSeq(pParse
, p
->pPrior
, iCol
);
2551 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
2552 ** have been thrown during name resolution and we would not have gotten
2554 if( pRet
==0 && ALWAYS(iCol
<p
->pEList
->nExpr
) ){
2555 pRet
= sqlite3ExprCollSeq(pParse
, p
->pEList
->a
[iCol
].pExpr
);
2561 ** The select statement passed as the second parameter is a compound SELECT
2562 ** with an ORDER BY clause. This function allocates and returns a KeyInfo
2563 ** structure suitable for implementing the ORDER BY.
2565 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
2566 ** function is responsible for ensuring that this structure is eventually
2569 static KeyInfo
*multiSelectOrderByKeyInfo(Parse
*pParse
, Select
*p
, int nExtra
){
2570 ExprList
*pOrderBy
= p
->pOrderBy
;
2571 int nOrderBy
= ALWAYS(pOrderBy
!=0) ? pOrderBy
->nExpr
: 0;
2572 sqlite3
*db
= pParse
->db
;
2573 KeyInfo
*pRet
= sqlite3KeyInfoAlloc(db
, nOrderBy
+nExtra
, 1);
2576 for(i
=0; i
<nOrderBy
; i
++){
2577 struct ExprList_item
*pItem
= &pOrderBy
->a
[i
];
2578 Expr
*pTerm
= pItem
->pExpr
;
2581 if( pTerm
->flags
& EP_Collate
){
2582 pColl
= sqlite3ExprCollSeq(pParse
, pTerm
);
2584 pColl
= multiSelectCollSeq(pParse
, p
, pItem
->u
.x
.iOrderByCol
-1);
2585 if( pColl
==0 ) pColl
= db
->pDfltColl
;
2586 pOrderBy
->a
[i
].pExpr
=
2587 sqlite3ExprAddCollateString(pParse
, pTerm
, pColl
->zName
);
2589 assert( sqlite3KeyInfoIsWriteable(pRet
) );
2590 pRet
->aColl
[i
] = pColl
;
2591 pRet
->aSortFlags
[i
] = pOrderBy
->a
[i
].fg
.sortFlags
;
2598 #ifndef SQLITE_OMIT_CTE
2600 ** This routine generates VDBE code to compute the content of a WITH RECURSIVE
2601 ** query of the form:
2603 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
2604 ** \___________/ \_______________/
2608 ** There is exactly one reference to the recursive-table in the FROM clause
2609 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
2611 ** The setup-query runs once to generate an initial set of rows that go
2612 ** into a Queue table. Rows are extracted from the Queue table one by
2613 ** one. Each row extracted from Queue is output to pDest. Then the single
2614 ** extracted row (now in the iCurrent table) becomes the content of the
2615 ** recursive-table for a recursive-query run. The output of the recursive-query
2616 ** is added back into the Queue table. Then another row is extracted from Queue
2617 ** and the iteration continues until the Queue table is empty.
2619 ** If the compound query operator is UNION then no duplicate rows are ever
2620 ** inserted into the Queue table. The iDistinct table keeps a copy of all rows
2621 ** that have ever been inserted into Queue and causes duplicates to be
2622 ** discarded. If the operator is UNION ALL, then duplicates are allowed.
2624 ** If the query has an ORDER BY, then entries in the Queue table are kept in
2625 ** ORDER BY order and the first entry is extracted for each cycle. Without
2626 ** an ORDER BY, the Queue table is just a FIFO.
2628 ** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
2629 ** have been output to pDest. A LIMIT of zero means to output no rows and a
2630 ** negative LIMIT means to output all rows. If there is also an OFFSET clause
2631 ** with a positive value, then the first OFFSET outputs are discarded rather
2632 ** than being sent to pDest. The LIMIT count does not begin until after OFFSET
2633 ** rows have been skipped.
2635 static void generateWithRecursiveQuery(
2636 Parse
*pParse
, /* Parsing context */
2637 Select
*p
, /* The recursive SELECT to be coded */
2638 SelectDest
*pDest
/* What to do with query results */
2640 SrcList
*pSrc
= p
->pSrc
; /* The FROM clause of the recursive query */
2641 int nCol
= p
->pEList
->nExpr
; /* Number of columns in the recursive table */
2642 Vdbe
*v
= pParse
->pVdbe
; /* The prepared statement under construction */
2643 Select
*pSetup
; /* The setup query */
2644 Select
*pFirstRec
; /* Left-most recursive term */
2645 int addrTop
; /* Top of the loop */
2646 int addrCont
, addrBreak
; /* CONTINUE and BREAK addresses */
2647 int iCurrent
= 0; /* The Current table */
2648 int regCurrent
; /* Register holding Current table */
2649 int iQueue
; /* The Queue table */
2650 int iDistinct
= 0; /* To ensure unique results if UNION */
2651 int eDest
= SRT_Fifo
; /* How to write to Queue */
2652 SelectDest destQueue
; /* SelectDest targeting the Queue table */
2653 int i
; /* Loop counter */
2654 int rc
; /* Result code */
2655 ExprList
*pOrderBy
; /* The ORDER BY clause */
2656 Expr
*pLimit
; /* Saved LIMIT and OFFSET */
2657 int regLimit
, regOffset
; /* Registers used by LIMIT and OFFSET */
2659 #ifndef SQLITE_OMIT_WINDOWFUNC
2661 sqlite3ErrorMsg(pParse
, "cannot use window functions in recursive queries");
2666 /* Obtain authorization to do a recursive query */
2667 if( sqlite3AuthCheck(pParse
, SQLITE_RECURSIVE
, 0, 0, 0) ) return;
2669 /* Process the LIMIT and OFFSET clauses, if they exist */
2670 addrBreak
= sqlite3VdbeMakeLabel(pParse
);
2671 p
->nSelectRow
= 320; /* 4 billion rows */
2672 computeLimitRegisters(pParse
, p
, addrBreak
);
2674 regLimit
= p
->iLimit
;
2675 regOffset
= p
->iOffset
;
2677 p
->iLimit
= p
->iOffset
= 0;
2678 pOrderBy
= p
->pOrderBy
;
2680 /* Locate the cursor number of the Current table */
2681 for(i
=0; ALWAYS(i
<pSrc
->nSrc
); i
++){
2682 if( pSrc
->a
[i
].fg
.isRecursive
){
2683 iCurrent
= pSrc
->a
[i
].iCursor
;
2688 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
2689 ** the Distinct table must be exactly one greater than Queue in order
2690 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
2691 iQueue
= pParse
->nTab
++;
2692 if( p
->op
==TK_UNION
){
2693 eDest
= pOrderBy
? SRT_DistQueue
: SRT_DistFifo
;
2694 iDistinct
= pParse
->nTab
++;
2696 eDest
= pOrderBy
? SRT_Queue
: SRT_Fifo
;
2698 sqlite3SelectDestInit(&destQueue
, eDest
, iQueue
);
2700 /* Allocate cursors for Current, Queue, and Distinct. */
2701 regCurrent
= ++pParse
->nMem
;
2702 sqlite3VdbeAddOp3(v
, OP_OpenPseudo
, iCurrent
, regCurrent
, nCol
);
2704 KeyInfo
*pKeyInfo
= multiSelectOrderByKeyInfo(pParse
, p
, 1);
2705 sqlite3VdbeAddOp4(v
, OP_OpenEphemeral
, iQueue
, pOrderBy
->nExpr
+2, 0,
2706 (char*)pKeyInfo
, P4_KEYINFO
);
2707 destQueue
.pOrderBy
= pOrderBy
;
2709 sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, iQueue
, nCol
);
2711 VdbeComment((v
, "Queue table"));
2713 p
->addrOpenEphm
[0] = sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, iDistinct
, 0);
2714 p
->selFlags
|= SF_UsesEphemeral
;
2717 /* Detach the ORDER BY clause from the compound SELECT */
2720 /* Figure out how many elements of the compound SELECT are part of the
2721 ** recursive query. Make sure no recursive elements use aggregate
2722 ** functions. Mark the recursive elements as UNION ALL even if they
2723 ** are really UNION because the distinctness will be enforced by the
2724 ** iDistinct table. pFirstRec is left pointing to the left-most
2725 ** recursive term of the CTE.
2727 for(pFirstRec
=p
; ALWAYS(pFirstRec
!=0); pFirstRec
=pFirstRec
->pPrior
){
2728 if( pFirstRec
->selFlags
& SF_Aggregate
){
2729 sqlite3ErrorMsg(pParse
, "recursive aggregate queries not supported");
2730 goto end_of_recursive_query
;
2732 pFirstRec
->op
= TK_ALL
;
2733 if( (pFirstRec
->pPrior
->selFlags
& SF_Recursive
)==0 ) break;
2736 /* Store the results of the setup-query in Queue. */
2737 pSetup
= pFirstRec
->pPrior
;
2739 ExplainQueryPlan((pParse
, 1, "SETUP"));
2740 rc
= sqlite3Select(pParse
, pSetup
, &destQueue
);
2742 if( rc
) goto end_of_recursive_query
;
2744 /* Find the next row in the Queue and output that row */
2745 addrTop
= sqlite3VdbeAddOp2(v
, OP_Rewind
, iQueue
, addrBreak
); VdbeCoverage(v
);
2747 /* Transfer the next row in Queue over to Current */
2748 sqlite3VdbeAddOp1(v
, OP_NullRow
, iCurrent
); /* To reset column cache */
2750 sqlite3VdbeAddOp3(v
, OP_Column
, iQueue
, pOrderBy
->nExpr
+1, regCurrent
);
2752 sqlite3VdbeAddOp2(v
, OP_RowData
, iQueue
, regCurrent
);
2754 sqlite3VdbeAddOp1(v
, OP_Delete
, iQueue
);
2756 /* Output the single row in Current */
2757 addrCont
= sqlite3VdbeMakeLabel(pParse
);
2758 codeOffset(v
, regOffset
, addrCont
);
2759 selectInnerLoop(pParse
, p
, iCurrent
,
2760 0, 0, pDest
, addrCont
, addrBreak
);
2762 sqlite3VdbeAddOp2(v
, OP_DecrJumpZero
, regLimit
, addrBreak
);
2765 sqlite3VdbeResolveLabel(v
, addrCont
);
2767 /* Execute the recursive SELECT taking the single row in Current as
2768 ** the value for the recursive-table. Store the results in the Queue.
2770 pFirstRec
->pPrior
= 0;
2771 ExplainQueryPlan((pParse
, 1, "RECURSIVE STEP"));
2772 sqlite3Select(pParse
, p
, &destQueue
);
2773 assert( pFirstRec
->pPrior
==0 );
2774 pFirstRec
->pPrior
= pSetup
;
2776 /* Keep running the loop until the Queue is empty */
2777 sqlite3VdbeGoto(v
, addrTop
);
2778 sqlite3VdbeResolveLabel(v
, addrBreak
);
2780 end_of_recursive_query
:
2781 sqlite3ExprListDelete(pParse
->db
, p
->pOrderBy
);
2782 p
->pOrderBy
= pOrderBy
;
2786 #endif /* SQLITE_OMIT_CTE */
2788 /* Forward references */
2789 static int multiSelectOrderBy(
2790 Parse
*pParse
, /* Parsing context */
2791 Select
*p
, /* The right-most of SELECTs to be coded */
2792 SelectDest
*pDest
/* What to do with query results */
2796 ** Handle the special case of a compound-select that originates from a
2797 ** VALUES clause. By handling this as a special case, we avoid deep
2798 ** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
2799 ** on a VALUES clause.
2801 ** Because the Select object originates from a VALUES clause:
2802 ** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
2803 ** (2) All terms are UNION ALL
2804 ** (3) There is no ORDER BY clause
2806 ** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
2807 ** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
2808 ** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
2809 ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
2811 static int multiSelectValues(
2812 Parse
*pParse
, /* Parsing context */
2813 Select
*p
, /* The right-most of SELECTs to be coded */
2814 SelectDest
*pDest
/* What to do with query results */
2818 int bShowAll
= p
->pLimit
==0;
2819 assert( p
->selFlags
& SF_MultiValue
);
2821 assert( p
->selFlags
& SF_Values
);
2822 assert( p
->op
==TK_ALL
|| (p
->op
==TK_SELECT
&& p
->pPrior
==0) );
2823 assert( p
->pNext
==0 || p
->pEList
->nExpr
==p
->pNext
->pEList
->nExpr
);
2824 #ifndef SQLITE_OMIT_WINDOWFUNC
2825 if( p
->pWin
) return -1;
2827 if( p
->pPrior
==0 ) break;
2828 assert( p
->pPrior
->pNext
==p
);
2832 ExplainQueryPlan((pParse
, 0, "SCAN %d CONSTANT ROW%s", nRow
,
2833 nRow
==1 ? "" : "S"));
2835 selectInnerLoop(pParse
, p
, -1, 0, 0, pDest
, 1, 1);
2836 if( !bShowAll
) break;
2837 p
->nSelectRow
= nRow
;
2844 ** Return true if the SELECT statement which is known to be the recursive
2845 ** part of a recursive CTE still has its anchor terms attached. If the
2846 ** anchor terms have already been removed, then return false.
2848 static int hasAnchor(Select
*p
){
2849 while( p
&& (p
->selFlags
& SF_Recursive
)!=0 ){ p
= p
->pPrior
; }
2854 ** This routine is called to process a compound query form from
2855 ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
2858 ** "p" points to the right-most of the two queries. the query on the
2859 ** left is p->pPrior. The left query could also be a compound query
2860 ** in which case this routine will be called recursively.
2862 ** The results of the total query are to be written into a destination
2863 ** of type eDest with parameter iParm.
2865 ** Example 1: Consider a three-way compound SQL statement.
2867 ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
2869 ** This statement is parsed up as follows:
2873 ** `-----> SELECT b FROM t2
2875 ** `------> SELECT a FROM t1
2877 ** The arrows in the diagram above represent the Select.pPrior pointer.
2878 ** So if this routine is called with p equal to the t3 query, then
2879 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
2881 ** Notice that because of the way SQLite parses compound SELECTs, the
2882 ** individual selects always group from left to right.
2884 static int multiSelect(
2885 Parse
*pParse
, /* Parsing context */
2886 Select
*p
, /* The right-most of SELECTs to be coded */
2887 SelectDest
*pDest
/* What to do with query results */
2889 int rc
= SQLITE_OK
; /* Success code from a subroutine */
2890 Select
*pPrior
; /* Another SELECT immediately to our left */
2891 Vdbe
*v
; /* Generate code to this VDBE */
2892 SelectDest dest
; /* Alternative data destination */
2893 Select
*pDelete
= 0; /* Chain of simple selects to delete */
2894 sqlite3
*db
; /* Database connection */
2896 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
2897 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
2899 assert( p
&& p
->pPrior
); /* Calling function guarantees this much */
2900 assert( (p
->selFlags
& SF_Recursive
)==0 || p
->op
==TK_ALL
|| p
->op
==TK_UNION
);
2901 assert( p
->selFlags
& SF_Compound
);
2905 assert( pPrior
->pOrderBy
==0 );
2906 assert( pPrior
->pLimit
==0 );
2908 v
= sqlite3GetVdbe(pParse
);
2909 assert( v
!=0 ); /* The VDBE already created by calling function */
2911 /* Create the destination temporary table if necessary
2913 if( dest
.eDest
==SRT_EphemTab
){
2914 assert( p
->pEList
);
2915 sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, dest
.iSDParm
, p
->pEList
->nExpr
);
2916 dest
.eDest
= SRT_Table
;
2919 /* Special handling for a compound-select that originates as a VALUES clause.
2921 if( p
->selFlags
& SF_MultiValue
){
2922 rc
= multiSelectValues(pParse
, p
, &dest
);
2923 if( rc
>=0 ) goto multi_select_end
;
2927 /* Make sure all SELECTs in the statement have the same number of elements
2928 ** in their result sets.
2930 assert( p
->pEList
&& pPrior
->pEList
);
2931 assert( p
->pEList
->nExpr
==pPrior
->pEList
->nExpr
);
2933 #ifndef SQLITE_OMIT_CTE
2934 if( (p
->selFlags
& SF_Recursive
)!=0 && hasAnchor(p
) ){
2935 generateWithRecursiveQuery(pParse
, p
, &dest
);
2939 /* Compound SELECTs that have an ORDER BY clause are handled separately.
2942 return multiSelectOrderBy(pParse
, p
, pDest
);
2945 #ifndef SQLITE_OMIT_EXPLAIN
2946 if( pPrior
->pPrior
==0 ){
2947 ExplainQueryPlan((pParse
, 1, "COMPOUND QUERY"));
2948 ExplainQueryPlan((pParse
, 1, "LEFT-MOST SUBQUERY"));
2952 /* Generate code for the left and right SELECT statements.
2957 int nLimit
= 0; /* Initialize to suppress harmless compiler warning */
2958 assert( !pPrior
->pLimit
);
2959 pPrior
->iLimit
= p
->iLimit
;
2960 pPrior
->iOffset
= p
->iOffset
;
2961 pPrior
->pLimit
= p
->pLimit
;
2962 TREETRACE(0x200, pParse
, p
, ("multiSelect UNION ALL left...\n"));
2963 rc
= sqlite3Select(pParse
, pPrior
, &dest
);
2966 goto multi_select_end
;
2969 p
->iLimit
= pPrior
->iLimit
;
2970 p
->iOffset
= pPrior
->iOffset
;
2972 addr
= sqlite3VdbeAddOp1(v
, OP_IfNot
, p
->iLimit
); VdbeCoverage(v
);
2973 VdbeComment((v
, "Jump ahead if LIMIT reached"));
2975 sqlite3VdbeAddOp3(v
, OP_OffsetLimit
,
2976 p
->iLimit
, p
->iOffset
+1, p
->iOffset
);
2979 ExplainQueryPlan((pParse
, 1, "UNION ALL"));
2980 TREETRACE(0x200, pParse
, p
, ("multiSelect UNION ALL right...\n"));
2981 rc
= sqlite3Select(pParse
, p
, &dest
);
2982 testcase( rc
!=SQLITE_OK
);
2983 pDelete
= p
->pPrior
;
2985 p
->nSelectRow
= sqlite3LogEstAdd(p
->nSelectRow
, pPrior
->nSelectRow
);
2987 && sqlite3ExprIsInteger(p
->pLimit
->pLeft
, &nLimit
, pParse
)
2988 && nLimit
>0 && p
->nSelectRow
> sqlite3LogEst((u64
)nLimit
)
2990 p
->nSelectRow
= sqlite3LogEst((u64
)nLimit
);
2993 sqlite3VdbeJumpHere(v
, addr
);
2999 int unionTab
; /* Cursor number of the temp table holding result */
3000 u8 op
= 0; /* One of the SRT_ operations to apply to self */
3001 int priorOp
; /* The SRT_ operation to apply to prior selects */
3002 Expr
*pLimit
; /* Saved values of p->nLimit */
3004 SelectDest uniondest
;
3006 testcase( p
->op
==TK_EXCEPT
);
3007 testcase( p
->op
==TK_UNION
);
3008 priorOp
= SRT_Union
;
3009 if( dest
.eDest
==priorOp
){
3010 /* We can reuse a temporary table generated by a SELECT to our
3013 assert( p
->pLimit
==0 ); /* Not allowed on leftward elements */
3014 unionTab
= dest
.iSDParm
;
3016 /* We will need to create our own temporary table to hold the
3017 ** intermediate results.
3019 unionTab
= pParse
->nTab
++;
3020 assert( p
->pOrderBy
==0 );
3021 addr
= sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, unionTab
, 0);
3022 assert( p
->addrOpenEphm
[0] == -1 );
3023 p
->addrOpenEphm
[0] = addr
;
3024 findRightmost(p
)->selFlags
|= SF_UsesEphemeral
;
3025 assert( p
->pEList
);
3029 /* Code the SELECT statements to our left
3031 assert( !pPrior
->pOrderBy
);
3032 sqlite3SelectDestInit(&uniondest
, priorOp
, unionTab
);
3033 TREETRACE(0x200, pParse
, p
, ("multiSelect EXCEPT/UNION left...\n"));
3034 rc
= sqlite3Select(pParse
, pPrior
, &uniondest
);
3036 goto multi_select_end
;
3039 /* Code the current SELECT statement
3041 if( p
->op
==TK_EXCEPT
){
3044 assert( p
->op
==TK_UNION
);
3050 uniondest
.eDest
= op
;
3051 ExplainQueryPlan((pParse
, 1, "%s USING TEMP B-TREE",
3052 sqlite3SelectOpName(p
->op
)));
3053 TREETRACE(0x200, pParse
, p
, ("multiSelect EXCEPT/UNION right...\n"));
3054 rc
= sqlite3Select(pParse
, p
, &uniondest
);
3055 testcase( rc
!=SQLITE_OK
);
3056 assert( p
->pOrderBy
==0 );
3057 pDelete
= p
->pPrior
;
3060 if( p
->op
==TK_UNION
){
3061 p
->nSelectRow
= sqlite3LogEstAdd(p
->nSelectRow
, pPrior
->nSelectRow
);
3063 sqlite3ExprDelete(db
, p
->pLimit
);
3068 /* Convert the data in the temporary table into whatever form
3069 ** it is that we currently need.
3071 assert( unionTab
==dest
.iSDParm
|| dest
.eDest
!=priorOp
);
3072 assert( p
->pEList
|| db
->mallocFailed
);
3073 if( dest
.eDest
!=priorOp
&& db
->mallocFailed
==0 ){
3074 int iCont
, iBreak
, iStart
;
3075 iBreak
= sqlite3VdbeMakeLabel(pParse
);
3076 iCont
= sqlite3VdbeMakeLabel(pParse
);
3077 computeLimitRegisters(pParse
, p
, iBreak
);
3078 sqlite3VdbeAddOp2(v
, OP_Rewind
, unionTab
, iBreak
); VdbeCoverage(v
);
3079 iStart
= sqlite3VdbeCurrentAddr(v
);
3080 selectInnerLoop(pParse
, p
, unionTab
,
3081 0, 0, &dest
, iCont
, iBreak
);
3082 sqlite3VdbeResolveLabel(v
, iCont
);
3083 sqlite3VdbeAddOp2(v
, OP_Next
, unionTab
, iStart
); VdbeCoverage(v
);
3084 sqlite3VdbeResolveLabel(v
, iBreak
);
3085 sqlite3VdbeAddOp2(v
, OP_Close
, unionTab
, 0);
3089 default: assert( p
->op
==TK_INTERSECT
); {
3091 int iCont
, iBreak
, iStart
;
3094 SelectDest intersectdest
;
3097 /* INTERSECT is different from the others since it requires
3098 ** two temporary tables. Hence it has its own case. Begin
3099 ** by allocating the tables we will need.
3101 tab1
= pParse
->nTab
++;
3102 tab2
= pParse
->nTab
++;
3103 assert( p
->pOrderBy
==0 );
3105 addr
= sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, tab1
, 0);
3106 assert( p
->addrOpenEphm
[0] == -1 );
3107 p
->addrOpenEphm
[0] = addr
;
3108 findRightmost(p
)->selFlags
|= SF_UsesEphemeral
;
3109 assert( p
->pEList
);
3111 /* Code the SELECTs to our left into temporary table "tab1".
3113 sqlite3SelectDestInit(&intersectdest
, SRT_Union
, tab1
);
3114 TREETRACE(0x400, pParse
, p
, ("multiSelect INTERSECT left...\n"));
3115 rc
= sqlite3Select(pParse
, pPrior
, &intersectdest
);
3117 goto multi_select_end
;
3120 /* Code the current SELECT into temporary table "tab2"
3122 addr
= sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, tab2
, 0);
3123 assert( p
->addrOpenEphm
[1] == -1 );
3124 p
->addrOpenEphm
[1] = addr
;
3128 intersectdest
.iSDParm
= tab2
;
3129 ExplainQueryPlan((pParse
, 1, "%s USING TEMP B-TREE",
3130 sqlite3SelectOpName(p
->op
)));
3131 TREETRACE(0x400, pParse
, p
, ("multiSelect INTERSECT right...\n"));
3132 rc
= sqlite3Select(pParse
, p
, &intersectdest
);
3133 testcase( rc
!=SQLITE_OK
);
3134 pDelete
= p
->pPrior
;
3136 if( p
->nSelectRow
>pPrior
->nSelectRow
){
3137 p
->nSelectRow
= pPrior
->nSelectRow
;
3139 sqlite3ExprDelete(db
, p
->pLimit
);
3142 /* Generate code to take the intersection of the two temporary
3146 assert( p
->pEList
);
3147 iBreak
= sqlite3VdbeMakeLabel(pParse
);
3148 iCont
= sqlite3VdbeMakeLabel(pParse
);
3149 computeLimitRegisters(pParse
, p
, iBreak
);
3150 sqlite3VdbeAddOp2(v
, OP_Rewind
, tab1
, iBreak
); VdbeCoverage(v
);
3151 r1
= sqlite3GetTempReg(pParse
);
3152 iStart
= sqlite3VdbeAddOp2(v
, OP_RowData
, tab1
, r1
);
3153 sqlite3VdbeAddOp4Int(v
, OP_NotFound
, tab2
, iCont
, r1
, 0);
3155 sqlite3ReleaseTempReg(pParse
, r1
);
3156 selectInnerLoop(pParse
, p
, tab1
,
3157 0, 0, &dest
, iCont
, iBreak
);
3158 sqlite3VdbeResolveLabel(v
, iCont
);
3159 sqlite3VdbeAddOp2(v
, OP_Next
, tab1
, iStart
); VdbeCoverage(v
);
3160 sqlite3VdbeResolveLabel(v
, iBreak
);
3161 sqlite3VdbeAddOp2(v
, OP_Close
, tab2
, 0);
3162 sqlite3VdbeAddOp2(v
, OP_Close
, tab1
, 0);
3167 #ifndef SQLITE_OMIT_EXPLAIN
3169 ExplainQueryPlanPop(pParse
);
3173 if( pParse
->nErr
) goto multi_select_end
;
3175 /* Compute collating sequences used by
3176 ** temporary tables needed to implement the compound select.
3177 ** Attach the KeyInfo structure to all temporary tables.
3179 ** This section is run by the right-most SELECT statement only.
3180 ** SELECT statements to the left always skip this part. The right-most
3181 ** SELECT might also skip this part if it has no ORDER BY clause and
3182 ** no temp tables are required.
3184 if( p
->selFlags
& SF_UsesEphemeral
){
3185 int i
; /* Loop counter */
3186 KeyInfo
*pKeyInfo
; /* Collating sequence for the result set */
3187 Select
*pLoop
; /* For looping through SELECT statements */
3188 CollSeq
**apColl
; /* For looping through pKeyInfo->aColl[] */
3189 int nCol
; /* Number of columns in result set */
3191 assert( p
->pNext
==0 );
3192 assert( p
->pEList
!=0 );
3193 nCol
= p
->pEList
->nExpr
;
3194 pKeyInfo
= sqlite3KeyInfoAlloc(db
, nCol
, 1);
3196 rc
= SQLITE_NOMEM_BKPT
;
3197 goto multi_select_end
;
3199 for(i
=0, apColl
=pKeyInfo
->aColl
; i
<nCol
; i
++, apColl
++){
3200 *apColl
= multiSelectCollSeq(pParse
, p
, i
);
3202 *apColl
= db
->pDfltColl
;
3206 for(pLoop
=p
; pLoop
; pLoop
=pLoop
->pPrior
){
3208 int addr
= pLoop
->addrOpenEphm
[i
];
3210 /* If [0] is unused then [1] is also unused. So we can
3211 ** always safely abort as soon as the first unused slot is found */
3212 assert( pLoop
->addrOpenEphm
[1]<0 );
3215 sqlite3VdbeChangeP2(v
, addr
, nCol
);
3216 sqlite3VdbeChangeP4(v
, addr
, (char*)sqlite3KeyInfoRef(pKeyInfo
),
3218 pLoop
->addrOpenEphm
[i
] = -1;
3221 sqlite3KeyInfoUnref(pKeyInfo
);
3225 pDest
->iSdst
= dest
.iSdst
;
3226 pDest
->nSdst
= dest
.nSdst
;
3228 sqlite3ParserAddCleanup(pParse
, sqlite3SelectDeleteGeneric
, pDelete
);
3232 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
3235 ** Error message for when two or more terms of a compound select have different
3236 ** size result sets.
3238 void sqlite3SelectWrongNumTermsError(Parse
*pParse
, Select
*p
){
3239 if( p
->selFlags
& SF_Values
){
3240 sqlite3ErrorMsg(pParse
, "all VALUES must have the same number of terms");
3242 sqlite3ErrorMsg(pParse
, "SELECTs to the left and right of %s"
3243 " do not have the same number of result columns",
3244 sqlite3SelectOpName(p
->op
));
3249 ** Code an output subroutine for a coroutine implementation of a
3250 ** SELECT statement.
3252 ** The data to be output is contained in pIn->iSdst. There are
3253 ** pIn->nSdst columns to be output. pDest is where the output should
3256 ** regReturn is the number of the register holding the subroutine
3259 ** If regPrev>0 then it is the first register in a vector that
3260 ** records the previous output. mem[regPrev] is a flag that is false
3261 ** if there has been no previous output. If regPrev>0 then code is
3262 ** generated to suppress duplicates. pKeyInfo is used for comparing
3265 ** If the LIMIT found in p->iLimit is reached, jump immediately to
3268 static int generateOutputSubroutine(
3269 Parse
*pParse
, /* Parsing context */
3270 Select
*p
, /* The SELECT statement */
3271 SelectDest
*pIn
, /* Coroutine supplying data */
3272 SelectDest
*pDest
, /* Where to send the data */
3273 int regReturn
, /* The return address register */
3274 int regPrev
, /* Previous result register. No uniqueness if 0 */
3275 KeyInfo
*pKeyInfo
, /* For comparing with previous entry */
3276 int iBreak
/* Jump here if we hit the LIMIT */
3278 Vdbe
*v
= pParse
->pVdbe
;
3282 addr
= sqlite3VdbeCurrentAddr(v
);
3283 iContinue
= sqlite3VdbeMakeLabel(pParse
);
3285 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
3289 addr1
= sqlite3VdbeAddOp1(v
, OP_IfNot
, regPrev
); VdbeCoverage(v
);
3290 addr2
= sqlite3VdbeAddOp4(v
, OP_Compare
, pIn
->iSdst
, regPrev
+1, pIn
->nSdst
,
3291 (char*)sqlite3KeyInfoRef(pKeyInfo
), P4_KEYINFO
);
3292 sqlite3VdbeAddOp3(v
, OP_Jump
, addr2
+2, iContinue
, addr2
+2); VdbeCoverage(v
);
3293 sqlite3VdbeJumpHere(v
, addr1
);
3294 sqlite3VdbeAddOp3(v
, OP_Copy
, pIn
->iSdst
, regPrev
+1, pIn
->nSdst
-1);
3295 sqlite3VdbeAddOp2(v
, OP_Integer
, 1, regPrev
);
3297 if( pParse
->db
->mallocFailed
) return 0;
3299 /* Suppress the first OFFSET entries if there is an OFFSET clause
3301 codeOffset(v
, p
->iOffset
, iContinue
);
3303 assert( pDest
->eDest
!=SRT_Exists
);
3304 assert( pDest
->eDest
!=SRT_Table
);
3305 switch( pDest
->eDest
){
3306 /* Store the result as data using a unique key.
3308 case SRT_EphemTab
: {
3309 int r1
= sqlite3GetTempReg(pParse
);
3310 int r2
= sqlite3GetTempReg(pParse
);
3311 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, pIn
->iSdst
, pIn
->nSdst
, r1
);
3312 sqlite3VdbeAddOp2(v
, OP_NewRowid
, pDest
->iSDParm
, r2
);
3313 sqlite3VdbeAddOp3(v
, OP_Insert
, pDest
->iSDParm
, r1
, r2
);
3314 sqlite3VdbeChangeP5(v
, OPFLAG_APPEND
);
3315 sqlite3ReleaseTempReg(pParse
, r2
);
3316 sqlite3ReleaseTempReg(pParse
, r1
);
3320 #ifndef SQLITE_OMIT_SUBQUERY
3321 /* If we are creating a set for an "expr IN (SELECT ...)".
3325 testcase( pIn
->nSdst
>1 );
3326 r1
= sqlite3GetTempReg(pParse
);
3327 sqlite3VdbeAddOp4(v
, OP_MakeRecord
, pIn
->iSdst
, pIn
->nSdst
,
3328 r1
, pDest
->zAffSdst
, pIn
->nSdst
);
3329 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, pDest
->iSDParm
, r1
,
3330 pIn
->iSdst
, pIn
->nSdst
);
3331 if( pDest
->iSDParm2
>0 ){
3332 sqlite3VdbeAddOp4Int(v
, OP_FilterAdd
, pDest
->iSDParm2
, 0,
3333 pIn
->iSdst
, pIn
->nSdst
);
3334 ExplainQueryPlan((pParse
, 0, "CREATE BLOOM FILTER"));
3336 sqlite3ReleaseTempReg(pParse
, r1
);
3340 /* If this is a scalar select that is part of an expression, then
3341 ** store the results in the appropriate memory cell and break out
3342 ** of the scan loop. Note that the select might return multiple columns
3343 ** if it is the RHS of a row-value IN operator.
3346 testcase( pIn
->nSdst
>1 );
3347 sqlite3ExprCodeMove(pParse
, pIn
->iSdst
, pDest
->iSDParm
, pIn
->nSdst
);
3348 /* The LIMIT clause will jump out of the loop for us */
3351 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
3353 /* The results are stored in a sequence of registers
3354 ** starting at pDest->iSdst. Then the co-routine yields.
3356 case SRT_Coroutine
: {
3357 if( pDest
->iSdst
==0 ){
3358 pDest
->iSdst
= sqlite3GetTempRange(pParse
, pIn
->nSdst
);
3359 pDest
->nSdst
= pIn
->nSdst
;
3361 sqlite3ExprCodeMove(pParse
, pIn
->iSdst
, pDest
->iSdst
, pIn
->nSdst
);
3362 sqlite3VdbeAddOp1(v
, OP_Yield
, pDest
->iSDParm
);
3366 /* If none of the above, then the result destination must be
3367 ** SRT_Output. This routine is never called with any other
3368 ** destination other than the ones handled above or SRT_Output.
3370 ** For SRT_Output, results are stored in a sequence of registers.
3371 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
3372 ** return the next row of result.
3375 assert( pDest
->eDest
==SRT_Output
);
3376 sqlite3VdbeAddOp2(v
, OP_ResultRow
, pIn
->iSdst
, pIn
->nSdst
);
3381 /* Jump to the end of the loop if the LIMIT is reached.
3384 sqlite3VdbeAddOp2(v
, OP_DecrJumpZero
, p
->iLimit
, iBreak
); VdbeCoverage(v
);
3387 /* Generate the subroutine return
3389 sqlite3VdbeResolveLabel(v
, iContinue
);
3390 sqlite3VdbeAddOp1(v
, OP_Return
, regReturn
);
3396 ** Alternative compound select code generator for cases when there
3397 ** is an ORDER BY clause.
3399 ** We assume a query of the following form:
3401 ** <selectA> <operator> <selectB> ORDER BY <orderbylist>
3403 ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
3404 ** is to code both <selectA> and <selectB> with the ORDER BY clause as
3405 ** co-routines. Then run the co-routines in parallel and merge the results
3406 ** into the output. In addition to the two coroutines (called selectA and
3407 ** selectB) there are 7 subroutines:
3409 ** outA: Move the output of the selectA coroutine into the output
3410 ** of the compound query.
3412 ** outB: Move the output of the selectB coroutine into the output
3413 ** of the compound query. (Only generated for UNION and
3414 ** UNION ALL. EXCEPT and INSERTSECT never output a row that
3415 ** appears only in B.)
3417 ** AltB: Called when there is data from both coroutines and A<B.
3419 ** AeqB: Called when there is data from both coroutines and A==B.
3421 ** AgtB: Called when there is data from both coroutines and A>B.
3423 ** EofA: Called when data is exhausted from selectA.
3425 ** EofB: Called when data is exhausted from selectB.
3427 ** The implementation of the latter five subroutines depend on which
3428 ** <operator> is used:
3431 ** UNION ALL UNION EXCEPT INTERSECT
3432 ** ------------- ----------------- -------------- -----------------
3433 ** AltB: outA, nextA outA, nextA outA, nextA nextA
3435 ** AeqB: outA, nextA nextA nextA outA, nextA
3437 ** AgtB: outB, nextB outB, nextB nextB nextB
3439 ** EofA: outB, nextB outB, nextB halt halt
3441 ** EofB: outA, nextA outA, nextA outA, nextA halt
3443 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
3444 ** causes an immediate jump to EofA and an EOF on B following nextB causes
3445 ** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
3446 ** following nextX causes a jump to the end of the select processing.
3448 ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
3449 ** within the output subroutine. The regPrev register set holds the previously
3450 ** output value. A comparison is made against this value and the output
3451 ** is skipped if the next results would be the same as the previous.
3453 ** The implementation plan is to implement the two coroutines and seven
3454 ** subroutines first, then put the control logic at the bottom. Like this:
3457 ** coA: coroutine for left query (A)
3458 ** coB: coroutine for right query (B)
3459 ** outA: output one row of A
3460 ** outB: output one row of B (UNION and UNION ALL only)
3466 ** Init: initialize coroutine registers
3468 ** if eof(A) goto EofA
3470 ** if eof(B) goto EofB
3471 ** Cmpr: Compare A, B
3472 ** Jump AltB, AeqB, AgtB
3475 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
3476 ** actually called using Gosub and they do not Return. EofA and EofB loop
3477 ** until all data is exhausted then jump to the "end" label. AltB, AeqB,
3478 ** and AgtB jump to either L2 or to one of EofA or EofB.
3480 #ifndef SQLITE_OMIT_COMPOUND_SELECT
3481 static int multiSelectOrderBy(
3482 Parse
*pParse
, /* Parsing context */
3483 Select
*p
, /* The right-most of SELECTs to be coded */
3484 SelectDest
*pDest
/* What to do with query results */
3486 int i
, j
; /* Loop counters */
3487 Select
*pPrior
; /* Another SELECT immediately to our left */
3488 Select
*pSplit
; /* Left-most SELECT in the right-hand group */
3489 int nSelect
; /* Number of SELECT statements in the compound */
3490 Vdbe
*v
; /* Generate code to this VDBE */
3491 SelectDest destA
; /* Destination for coroutine A */
3492 SelectDest destB
; /* Destination for coroutine B */
3493 int regAddrA
; /* Address register for select-A coroutine */
3494 int regAddrB
; /* Address register for select-B coroutine */
3495 int addrSelectA
; /* Address of the select-A coroutine */
3496 int addrSelectB
; /* Address of the select-B coroutine */
3497 int regOutA
; /* Address register for the output-A subroutine */
3498 int regOutB
; /* Address register for the output-B subroutine */
3499 int addrOutA
; /* Address of the output-A subroutine */
3500 int addrOutB
= 0; /* Address of the output-B subroutine */
3501 int addrEofA
; /* Address of the select-A-exhausted subroutine */
3502 int addrEofA_noB
; /* Alternate addrEofA if B is uninitialized */
3503 int addrEofB
; /* Address of the select-B-exhausted subroutine */
3504 int addrAltB
; /* Address of the A<B subroutine */
3505 int addrAeqB
; /* Address of the A==B subroutine */
3506 int addrAgtB
; /* Address of the A>B subroutine */
3507 int regLimitA
; /* Limit register for select-A */
3508 int regLimitB
; /* Limit register for select-A */
3509 int regPrev
; /* A range of registers to hold previous output */
3510 int savedLimit
; /* Saved value of p->iLimit */
3511 int savedOffset
; /* Saved value of p->iOffset */
3512 int labelCmpr
; /* Label for the start of the merge algorithm */
3513 int labelEnd
; /* Label for the end of the overall SELECT stmt */
3514 int addr1
; /* Jump instructions that get retargeted */
3515 int op
; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
3516 KeyInfo
*pKeyDup
= 0; /* Comparison information for duplicate removal */
3517 KeyInfo
*pKeyMerge
; /* Comparison information for merging rows */
3518 sqlite3
*db
; /* Database connection */
3519 ExprList
*pOrderBy
; /* The ORDER BY clause */
3520 int nOrderBy
; /* Number of terms in the ORDER BY clause */
3521 u32
*aPermute
; /* Mapping from ORDER BY terms to result set columns */
3523 assert( p
->pOrderBy
!=0 );
3524 assert( pKeyDup
==0 ); /* "Managed" code needs this. Ticket #3382. */
3527 assert( v
!=0 ); /* Already thrown the error if VDBE alloc failed */
3528 labelEnd
= sqlite3VdbeMakeLabel(pParse
);
3529 labelCmpr
= sqlite3VdbeMakeLabel(pParse
);
3532 /* Patch up the ORDER BY clause
3535 assert( p
->pPrior
->pOrderBy
==0 );
3536 pOrderBy
= p
->pOrderBy
;
3538 nOrderBy
= pOrderBy
->nExpr
;
3540 /* For operators other than UNION ALL we have to make sure that
3541 ** the ORDER BY clause covers every term of the result set. Add
3542 ** terms to the ORDER BY clause as necessary.
3545 for(i
=1; db
->mallocFailed
==0 && i
<=p
->pEList
->nExpr
; i
++){
3546 struct ExprList_item
*pItem
;
3547 for(j
=0, pItem
=pOrderBy
->a
; j
<nOrderBy
; j
++, pItem
++){
3549 assert( pItem
->u
.x
.iOrderByCol
>0 );
3550 if( pItem
->u
.x
.iOrderByCol
==i
) break;
3553 Expr
*pNew
= sqlite3Expr(db
, TK_INTEGER
, 0);
3554 if( pNew
==0 ) return SQLITE_NOMEM_BKPT
;
3555 pNew
->flags
|= EP_IntValue
;
3557 p
->pOrderBy
= pOrderBy
= sqlite3ExprListAppend(pParse
, pOrderBy
, pNew
);
3558 if( pOrderBy
) pOrderBy
->a
[nOrderBy
++].u
.x
.iOrderByCol
= (u16
)i
;
3563 /* Compute the comparison permutation and keyinfo that is used with
3564 ** the permutation used to determine if the next
3565 ** row of results comes from selectA or selectB. Also add explicit
3566 ** collations to the ORDER BY clause terms so that when the subqueries
3567 ** to the right and the left are evaluated, they use the correct
3570 aPermute
= sqlite3DbMallocRawNN(db
, sizeof(u32
)*(nOrderBy
+ 1));
3572 struct ExprList_item
*pItem
;
3573 aPermute
[0] = nOrderBy
;
3574 for(i
=1, pItem
=pOrderBy
->a
; i
<=nOrderBy
; i
++, pItem
++){
3576 assert( pItem
->u
.x
.iOrderByCol
>0 );
3577 assert( pItem
->u
.x
.iOrderByCol
<=p
->pEList
->nExpr
);
3578 aPermute
[i
] = pItem
->u
.x
.iOrderByCol
- 1;
3580 pKeyMerge
= multiSelectOrderByKeyInfo(pParse
, p
, 1);
3585 /* Allocate a range of temporary registers and the KeyInfo needed
3586 ** for the logic that removes duplicate result rows when the
3587 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
3592 int nExpr
= p
->pEList
->nExpr
;
3593 assert( nOrderBy
>=nExpr
|| db
->mallocFailed
);
3594 regPrev
= pParse
->nMem
+1;
3595 pParse
->nMem
+= nExpr
+1;
3596 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, regPrev
);
3597 pKeyDup
= sqlite3KeyInfoAlloc(db
, nExpr
, 1);
3599 assert( sqlite3KeyInfoIsWriteable(pKeyDup
) );
3600 for(i
=0; i
<nExpr
; i
++){
3601 pKeyDup
->aColl
[i
] = multiSelectCollSeq(pParse
, p
, i
);
3602 pKeyDup
->aSortFlags
[i
] = 0;
3607 /* Separate the left and the right query from one another
3610 if( (op
==TK_ALL
|| op
==TK_UNION
)
3611 && OptimizationEnabled(db
, SQLITE_BalancedMerge
)
3613 for(pSplit
=p
; pSplit
->pPrior
!=0 && pSplit
->op
==op
; pSplit
=pSplit
->pPrior
){
3615 assert( pSplit
->pPrior
->pNext
==pSplit
);
3622 for(i
=2; i
<nSelect
; i
+=2){ pSplit
= pSplit
->pPrior
; }
3624 pPrior
= pSplit
->pPrior
;
3625 assert( pPrior
!=0 );
3628 assert( p
->pOrderBy
== pOrderBy
);
3629 assert( pOrderBy
!=0 || db
->mallocFailed
);
3630 pPrior
->pOrderBy
= sqlite3ExprListDup(pParse
->db
, pOrderBy
, 0);
3631 sqlite3ResolveOrderGroupBy(pParse
, p
, p
->pOrderBy
, "ORDER");
3632 sqlite3ResolveOrderGroupBy(pParse
, pPrior
, pPrior
->pOrderBy
, "ORDER");
3634 /* Compute the limit registers */
3635 computeLimitRegisters(pParse
, p
, labelEnd
);
3636 if( p
->iLimit
&& op
==TK_ALL
){
3637 regLimitA
= ++pParse
->nMem
;
3638 regLimitB
= ++pParse
->nMem
;
3639 sqlite3VdbeAddOp2(v
, OP_Copy
, p
->iOffset
? p
->iOffset
+1 : p
->iLimit
,
3641 sqlite3VdbeAddOp2(v
, OP_Copy
, regLimitA
, regLimitB
);
3643 regLimitA
= regLimitB
= 0;
3645 sqlite3ExprDelete(db
, p
->pLimit
);
3648 regAddrA
= ++pParse
->nMem
;
3649 regAddrB
= ++pParse
->nMem
;
3650 regOutA
= ++pParse
->nMem
;
3651 regOutB
= ++pParse
->nMem
;
3652 sqlite3SelectDestInit(&destA
, SRT_Coroutine
, regAddrA
);
3653 sqlite3SelectDestInit(&destB
, SRT_Coroutine
, regAddrB
);
3655 ExplainQueryPlan((pParse
, 1, "MERGE (%s)", sqlite3SelectOpName(p
->op
)));
3657 /* Generate a coroutine to evaluate the SELECT statement to the
3658 ** left of the compound operator - the "A" select.
3660 addrSelectA
= sqlite3VdbeCurrentAddr(v
) + 1;
3661 addr1
= sqlite3VdbeAddOp3(v
, OP_InitCoroutine
, regAddrA
, 0, addrSelectA
);
3662 VdbeComment((v
, "left SELECT"));
3663 pPrior
->iLimit
= regLimitA
;
3664 ExplainQueryPlan((pParse
, 1, "LEFT"));
3665 sqlite3Select(pParse
, pPrior
, &destA
);
3666 sqlite3VdbeEndCoroutine(v
, regAddrA
);
3667 sqlite3VdbeJumpHere(v
, addr1
);
3669 /* Generate a coroutine to evaluate the SELECT statement on
3670 ** the right - the "B" select
3672 addrSelectB
= sqlite3VdbeCurrentAddr(v
) + 1;
3673 addr1
= sqlite3VdbeAddOp3(v
, OP_InitCoroutine
, regAddrB
, 0, addrSelectB
);
3674 VdbeComment((v
, "right SELECT"));
3675 savedLimit
= p
->iLimit
;
3676 savedOffset
= p
->iOffset
;
3677 p
->iLimit
= regLimitB
;
3679 ExplainQueryPlan((pParse
, 1, "RIGHT"));
3680 sqlite3Select(pParse
, p
, &destB
);
3681 p
->iLimit
= savedLimit
;
3682 p
->iOffset
= savedOffset
;
3683 sqlite3VdbeEndCoroutine(v
, regAddrB
);
3685 /* Generate a subroutine that outputs the current row of the A
3686 ** select as the next output row of the compound select.
3688 VdbeNoopComment((v
, "Output routine for A"));
3689 addrOutA
= generateOutputSubroutine(pParse
,
3690 p
, &destA
, pDest
, regOutA
,
3691 regPrev
, pKeyDup
, labelEnd
);
3693 /* Generate a subroutine that outputs the current row of the B
3694 ** select as the next output row of the compound select.
3696 if( op
==TK_ALL
|| op
==TK_UNION
){
3697 VdbeNoopComment((v
, "Output routine for B"));
3698 addrOutB
= generateOutputSubroutine(pParse
,
3699 p
, &destB
, pDest
, regOutB
,
3700 regPrev
, pKeyDup
, labelEnd
);
3702 sqlite3KeyInfoUnref(pKeyDup
);
3704 /* Generate a subroutine to run when the results from select A
3705 ** are exhausted and only data in select B remains.
3707 if( op
==TK_EXCEPT
|| op
==TK_INTERSECT
){
3708 addrEofA_noB
= addrEofA
= labelEnd
;
3710 VdbeNoopComment((v
, "eof-A subroutine"));
3711 addrEofA
= sqlite3VdbeAddOp2(v
, OP_Gosub
, regOutB
, addrOutB
);
3712 addrEofA_noB
= sqlite3VdbeAddOp2(v
, OP_Yield
, regAddrB
, labelEnd
);
3714 sqlite3VdbeGoto(v
, addrEofA
);
3715 p
->nSelectRow
= sqlite3LogEstAdd(p
->nSelectRow
, pPrior
->nSelectRow
);
3718 /* Generate a subroutine to run when the results from select B
3719 ** are exhausted and only data in select A remains.
3721 if( op
==TK_INTERSECT
){
3722 addrEofB
= addrEofA
;
3723 if( p
->nSelectRow
> pPrior
->nSelectRow
) p
->nSelectRow
= pPrior
->nSelectRow
;
3725 VdbeNoopComment((v
, "eof-B subroutine"));
3726 addrEofB
= sqlite3VdbeAddOp2(v
, OP_Gosub
, regOutA
, addrOutA
);
3727 sqlite3VdbeAddOp2(v
, OP_Yield
, regAddrA
, labelEnd
); VdbeCoverage(v
);
3728 sqlite3VdbeGoto(v
, addrEofB
);
3731 /* Generate code to handle the case of A<B
3733 VdbeNoopComment((v
, "A-lt-B subroutine"));
3734 addrAltB
= sqlite3VdbeAddOp2(v
, OP_Gosub
, regOutA
, addrOutA
);
3735 sqlite3VdbeAddOp2(v
, OP_Yield
, regAddrA
, addrEofA
); VdbeCoverage(v
);
3736 sqlite3VdbeGoto(v
, labelCmpr
);
3738 /* Generate code to handle the case of A==B
3741 addrAeqB
= addrAltB
;
3742 }else if( op
==TK_INTERSECT
){
3743 addrAeqB
= addrAltB
;
3746 VdbeNoopComment((v
, "A-eq-B subroutine"));
3748 sqlite3VdbeAddOp2(v
, OP_Yield
, regAddrA
, addrEofA
); VdbeCoverage(v
);
3749 sqlite3VdbeGoto(v
, labelCmpr
);
3752 /* Generate code to handle the case of A>B
3754 VdbeNoopComment((v
, "A-gt-B subroutine"));
3755 addrAgtB
= sqlite3VdbeCurrentAddr(v
);
3756 if( op
==TK_ALL
|| op
==TK_UNION
){
3757 sqlite3VdbeAddOp2(v
, OP_Gosub
, regOutB
, addrOutB
);
3759 sqlite3VdbeAddOp2(v
, OP_Yield
, regAddrB
, addrEofB
); VdbeCoverage(v
);
3760 sqlite3VdbeGoto(v
, labelCmpr
);
3762 /* This code runs once to initialize everything.
3764 sqlite3VdbeJumpHere(v
, addr1
);
3765 sqlite3VdbeAddOp2(v
, OP_Yield
, regAddrA
, addrEofA_noB
); VdbeCoverage(v
);
3766 sqlite3VdbeAddOp2(v
, OP_Yield
, regAddrB
, addrEofB
); VdbeCoverage(v
);
3768 /* Implement the main merge loop
3770 sqlite3VdbeResolveLabel(v
, labelCmpr
);
3771 sqlite3VdbeAddOp4(v
, OP_Permutation
, 0, 0, 0, (char*)aPermute
, P4_INTARRAY
);
3772 sqlite3VdbeAddOp4(v
, OP_Compare
, destA
.iSdst
, destB
.iSdst
, nOrderBy
,
3773 (char*)pKeyMerge
, P4_KEYINFO
);
3774 sqlite3VdbeChangeP5(v
, OPFLAG_PERMUTE
);
3775 sqlite3VdbeAddOp3(v
, OP_Jump
, addrAltB
, addrAeqB
, addrAgtB
); VdbeCoverage(v
);
3777 /* Jump to the this point in order to terminate the query.
3779 sqlite3VdbeResolveLabel(v
, labelEnd
);
3781 /* Make arrangements to free the 2nd and subsequent arms of the compound
3782 ** after the parse has finished */
3783 if( pSplit
->pPrior
){
3784 sqlite3ParserAddCleanup(pParse
, sqlite3SelectDeleteGeneric
, pSplit
->pPrior
);
3786 pSplit
->pPrior
= pPrior
;
3787 pPrior
->pNext
= pSplit
;
3788 sqlite3ExprListDelete(db
, pPrior
->pOrderBy
);
3789 pPrior
->pOrderBy
= 0;
3791 /*** TBD: Insert subroutine calls to close cursors on incomplete
3792 **** subqueries ****/
3793 ExplainQueryPlanPop(pParse
);
3794 return pParse
->nErr
!=0;
3798 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
3800 /* An instance of the SubstContext object describes an substitution edit
3801 ** to be performed on a parse tree.
3803 ** All references to columns in table iTable are to be replaced by corresponding
3804 ** expressions in pEList.
3806 ** ## About "isOuterJoin":
3808 ** The isOuterJoin column indicates that the replacement will occur into a
3809 ** position in the parent that NULL-able due to an OUTER JOIN. Either the
3810 ** target slot in the parent is the right operand of a LEFT JOIN, or one of
3811 ** the left operands of a RIGHT JOIN. In either case, we need to potentially
3812 ** bypass the substituted expression with OP_IfNullRow.
3814 ** Suppose the original expression is an integer constant. Even though the table
3815 ** has the nullRow flag set, because the expression is an integer constant,
3816 ** it will not be NULLed out. So instead, we insert an OP_IfNullRow opcode
3817 ** that checks to see if the nullRow flag is set on the table. If the nullRow
3818 ** flag is set, then the value in the register is set to NULL and the original
3819 ** expression is bypassed. If the nullRow flag is not set, then the original
3820 ** expression runs to populate the register.
3822 ** Example where this is needed:
3824 ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT);
3825 ** CREATE TABLE t2(x INT UNIQUE);
3827 ** SELECT a,b,m,x FROM t1 LEFT JOIN (SELECT 59 AS m,x FROM t2) ON b=x;
3829 ** When the subquery on the right side of the LEFT JOIN is flattened, we
3830 ** have to add OP_IfNullRow in front of the OP_Integer that implements the
3831 ** "m" value of the subquery so that a NULL will be loaded instead of 59
3832 ** when processing a non-matched row of the left.
3834 typedef struct SubstContext
{
3835 Parse
*pParse
; /* The parsing context */
3836 int iTable
; /* Replace references to this table */
3837 int iNewTable
; /* New table number */
3838 int isOuterJoin
; /* Add TK_IF_NULL_ROW opcodes on each replacement */
3839 ExprList
*pEList
; /* Replacement expressions */
3840 ExprList
*pCList
; /* Collation sequences for replacement expr */
3843 /* Forward Declarations */
3844 static void substExprList(SubstContext
*, ExprList
*);
3845 static void substSelect(SubstContext
*, Select
*, int);
3848 ** Scan through the expression pExpr. Replace every reference to
3849 ** a column in table number iTable with a copy of the iColumn-th
3850 ** entry in pEList. (But leave references to the ROWID column
3853 ** This routine is part of the flattening procedure. A subquery
3854 ** whose result set is defined by pEList appears as entry in the
3855 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
3856 ** FORM clause entry is iTable. This routine makes the necessary
3857 ** changes to pExpr so that it refers directly to the source table
3858 ** of the subquery rather the result set of the subquery.
3860 static Expr
*substExpr(
3861 SubstContext
*pSubst
, /* Description of the substitution */
3862 Expr
*pExpr
/* Expr in which substitution occurs */
3864 if( pExpr
==0 ) return 0;
3865 if( ExprHasProperty(pExpr
, EP_OuterON
|EP_InnerON
)
3866 && pExpr
->w
.iJoin
==pSubst
->iTable
3868 testcase( ExprHasProperty(pExpr
, EP_InnerON
) );
3869 pExpr
->w
.iJoin
= pSubst
->iNewTable
;
3871 if( pExpr
->op
==TK_COLUMN
3872 && pExpr
->iTable
==pSubst
->iTable
3873 && !ExprHasProperty(pExpr
, EP_FixedCol
)
3875 #ifdef SQLITE_ALLOW_ROWID_IN_VIEW
3876 if( pExpr
->iColumn
<0 ){
3877 pExpr
->op
= TK_NULL
;
3885 iColumn
= pExpr
->iColumn
;
3886 assert( iColumn
>=0 );
3887 assert( pSubst
->pEList
!=0 && iColumn
<pSubst
->pEList
->nExpr
);
3888 assert( pExpr
->pRight
==0 );
3889 pCopy
= pSubst
->pEList
->a
[iColumn
].pExpr
;
3890 if( sqlite3ExprIsVector(pCopy
) ){
3891 sqlite3VectorErrorMsg(pSubst
->pParse
, pCopy
);
3893 sqlite3
*db
= pSubst
->pParse
->db
;
3894 if( pSubst
->isOuterJoin
3895 && (pCopy
->op
!=TK_COLUMN
|| pCopy
->iTable
!=pSubst
->iNewTable
)
3897 memset(&ifNullRow
, 0, sizeof(ifNullRow
));
3898 ifNullRow
.op
= TK_IF_NULL_ROW
;
3899 ifNullRow
.pLeft
= pCopy
;
3900 ifNullRow
.iTable
= pSubst
->iNewTable
;
3901 ifNullRow
.iColumn
= -99;
3902 ifNullRow
.flags
= EP_IfNullRow
;
3905 testcase( ExprHasProperty(pCopy
, EP_Subquery
) );
3906 pNew
= sqlite3ExprDup(db
, pCopy
, 0);
3907 if( db
->mallocFailed
){
3908 sqlite3ExprDelete(db
, pNew
);
3911 if( pSubst
->isOuterJoin
){
3912 ExprSetProperty(pNew
, EP_CanBeNull
);
3914 if( ExprHasProperty(pExpr
,EP_OuterON
|EP_InnerON
) ){
3915 sqlite3SetJoinExpr(pNew
, pExpr
->w
.iJoin
,
3916 pExpr
->flags
& (EP_OuterON
|EP_InnerON
));
3918 sqlite3ExprDelete(db
, pExpr
);
3920 if( pExpr
->op
==TK_TRUEFALSE
){
3921 pExpr
->u
.iValue
= sqlite3ExprTruthValue(pExpr
);
3922 pExpr
->op
= TK_INTEGER
;
3923 ExprSetProperty(pExpr
, EP_IntValue
);
3926 /* Ensure that the expression now has an implicit collation sequence,
3927 ** just as it did when it was a column of a view or sub-query. */
3929 CollSeq
*pNat
= sqlite3ExprCollSeq(pSubst
->pParse
, pExpr
);
3930 CollSeq
*pColl
= sqlite3ExprCollSeq(pSubst
->pParse
,
3931 pSubst
->pCList
->a
[iColumn
].pExpr
3933 if( pNat
!=pColl
|| (pExpr
->op
!=TK_COLUMN
&& pExpr
->op
!=TK_COLLATE
) ){
3934 pExpr
= sqlite3ExprAddCollateString(pSubst
->pParse
, pExpr
,
3935 (pColl
? pColl
->zName
: "BINARY")
3939 ExprClearProperty(pExpr
, EP_Collate
);
3943 if( pExpr
->op
==TK_IF_NULL_ROW
&& pExpr
->iTable
==pSubst
->iTable
){
3944 pExpr
->iTable
= pSubst
->iNewTable
;
3946 pExpr
->pLeft
= substExpr(pSubst
, pExpr
->pLeft
);
3947 pExpr
->pRight
= substExpr(pSubst
, pExpr
->pRight
);
3948 if( ExprUseXSelect(pExpr
) ){
3949 substSelect(pSubst
, pExpr
->x
.pSelect
, 1);
3951 substExprList(pSubst
, pExpr
->x
.pList
);
3953 #ifndef SQLITE_OMIT_WINDOWFUNC
3954 if( ExprHasProperty(pExpr
, EP_WinFunc
) ){
3955 Window
*pWin
= pExpr
->y
.pWin
;
3956 pWin
->pFilter
= substExpr(pSubst
, pWin
->pFilter
);
3957 substExprList(pSubst
, pWin
->pPartition
);
3958 substExprList(pSubst
, pWin
->pOrderBy
);
3964 static void substExprList(
3965 SubstContext
*pSubst
, /* Description of the substitution */
3966 ExprList
*pList
/* List to scan and in which to make substitutes */
3969 if( pList
==0 ) return;
3970 for(i
=0; i
<pList
->nExpr
; i
++){
3971 pList
->a
[i
].pExpr
= substExpr(pSubst
, pList
->a
[i
].pExpr
);
3974 static void substSelect(
3975 SubstContext
*pSubst
, /* Description of the substitution */
3976 Select
*p
, /* SELECT statement in which to make substitutions */
3977 int doPrior
/* Do substitutes on p->pPrior too */
3984 substExprList(pSubst
, p
->pEList
);
3985 substExprList(pSubst
, p
->pGroupBy
);
3986 substExprList(pSubst
, p
->pOrderBy
);
3987 p
->pHaving
= substExpr(pSubst
, p
->pHaving
);
3988 p
->pWhere
= substExpr(pSubst
, p
->pWhere
);
3991 for(i
=pSrc
->nSrc
, pItem
=pSrc
->a
; i
>0; i
--, pItem
++){
3992 if( pItem
->fg
.isSubquery
){
3993 substSelect(pSubst
, pItem
->u4
.pSubq
->pSelect
, 1);
3995 if( pItem
->fg
.isTabFunc
){
3996 substExprList(pSubst
, pItem
->u1
.pFuncArg
);
3999 }while( doPrior
&& (p
= p
->pPrior
)!=0 );
4001 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
4003 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
4005 ** pSelect is a SELECT statement and pSrcItem is one item in the FROM
4006 ** clause of that SELECT.
4008 ** This routine scans the entire SELECT statement and recomputes the
4009 ** pSrcItem->colUsed mask.
4011 static int recomputeColumnsUsedExpr(Walker
*pWalker
, Expr
*pExpr
){
4013 if( pExpr
->op
!=TK_COLUMN
) return WRC_Continue
;
4014 pItem
= pWalker
->u
.pSrcItem
;
4015 if( pItem
->iCursor
!=pExpr
->iTable
) return WRC_Continue
;
4016 if( pExpr
->iColumn
<0 ) return WRC_Continue
;
4017 pItem
->colUsed
|= sqlite3ExprColUsed(pExpr
);
4018 return WRC_Continue
;
4020 static void recomputeColumnsUsed(
4021 Select
*pSelect
, /* The complete SELECT statement */
4022 SrcItem
*pSrcItem
/* Which FROM clause item to recompute */
4025 if( NEVER(pSrcItem
->pSTab
==0) ) return;
4026 memset(&w
, 0, sizeof(w
));
4027 w
.xExprCallback
= recomputeColumnsUsedExpr
;
4028 w
.xSelectCallback
= sqlite3SelectWalkNoop
;
4029 w
.u
.pSrcItem
= pSrcItem
;
4030 pSrcItem
->colUsed
= 0;
4031 sqlite3WalkSelect(&w
, pSelect
);
4033 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
4035 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
4037 ** Assign new cursor numbers to each of the items in pSrc. For each
4038 ** new cursor number assigned, set an entry in the aCsrMap[] array
4039 ** to map the old cursor number to the new:
4041 ** aCsrMap[iOld+1] = iNew;
4043 ** The array is guaranteed by the caller to be large enough for all
4044 ** existing cursor numbers in pSrc. aCsrMap[0] is the array size.
4046 ** If pSrc contains any sub-selects, call this routine recursively
4047 ** on the FROM clause of each such sub-select, with iExcept set to -1.
4049 static void srclistRenumberCursors(
4050 Parse
*pParse
, /* Parse context */
4051 int *aCsrMap
, /* Array to store cursor mappings in */
4052 SrcList
*pSrc
, /* FROM clause to renumber */
4053 int iExcept
/* FROM clause item to skip */
4057 for(i
=0, pItem
=pSrc
->a
; i
<pSrc
->nSrc
; i
++, pItem
++){
4060 assert( pItem
->iCursor
< aCsrMap
[0] );
4061 if( !pItem
->fg
.isRecursive
|| aCsrMap
[pItem
->iCursor
+1]==0 ){
4062 aCsrMap
[pItem
->iCursor
+1] = pParse
->nTab
++;
4064 pItem
->iCursor
= aCsrMap
[pItem
->iCursor
+1];
4065 if( pItem
->fg
.isSubquery
){
4066 for(p
=pItem
->u4
.pSubq
->pSelect
; p
; p
=p
->pPrior
){
4067 srclistRenumberCursors(pParse
, aCsrMap
, p
->pSrc
, -1);
4075 ** *piCursor is a cursor number. Change it if it needs to be mapped.
4077 static void renumberCursorDoMapping(Walker
*pWalker
, int *piCursor
){
4078 int *aCsrMap
= pWalker
->u
.aiCol
;
4079 int iCsr
= *piCursor
;
4080 if( iCsr
< aCsrMap
[0] && aCsrMap
[iCsr
+1]>0 ){
4081 *piCursor
= aCsrMap
[iCsr
+1];
4086 ** Expression walker callback used by renumberCursors() to update
4087 ** Expr objects to match newly assigned cursor numbers.
4089 static int renumberCursorsCb(Walker
*pWalker
, Expr
*pExpr
){
4091 if( op
==TK_COLUMN
|| op
==TK_IF_NULL_ROW
){
4092 renumberCursorDoMapping(pWalker
, &pExpr
->iTable
);
4094 if( ExprHasProperty(pExpr
, EP_OuterON
) ){
4095 renumberCursorDoMapping(pWalker
, &pExpr
->w
.iJoin
);
4097 return WRC_Continue
;
4101 ** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc)
4102 ** of the SELECT statement passed as the second argument, and to each
4103 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
4104 ** Except, do not assign a new cursor number to the iExcept'th element in
4105 ** the FROM clause of (*p). Update all expressions and other references
4106 ** to refer to the new cursor numbers.
4108 ** Argument aCsrMap is an array that may be used for temporary working
4109 ** space. Two guarantees are made by the caller:
4111 ** * the array is larger than the largest cursor number used within the
4112 ** select statement passed as an argument, and
4114 ** * the array entries for all cursor numbers that do *not* appear in
4115 ** FROM clauses of the select statement as described above are
4116 ** initialized to zero.
4118 static void renumberCursors(
4119 Parse
*pParse
, /* Parse context */
4120 Select
*p
, /* Select to renumber cursors within */
4121 int iExcept
, /* FROM clause item to skip */
4122 int *aCsrMap
/* Working space */
4125 srclistRenumberCursors(pParse
, aCsrMap
, p
->pSrc
, iExcept
);
4126 memset(&w
, 0, sizeof(w
));
4127 w
.u
.aiCol
= aCsrMap
;
4128 w
.xExprCallback
= renumberCursorsCb
;
4129 w
.xSelectCallback
= sqlite3SelectWalkNoop
;
4130 sqlite3WalkSelect(&w
, p
);
4132 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
4135 ** If pSel is not part of a compound SELECT, return a pointer to its
4136 ** expression list. Otherwise, return a pointer to the expression list
4137 ** of the leftmost SELECT in the compound.
4139 static ExprList
*findLeftmostExprlist(Select
*pSel
){
4140 while( pSel
->pPrior
){
4141 pSel
= pSel
->pPrior
;
4143 return pSel
->pEList
;
4147 ** Return true if any of the result-set columns in the compound query
4148 ** have incompatible affinities on one or more arms of the compound.
4150 static int compoundHasDifferentAffinities(Select
*p
){
4154 assert( p
->pEList
!=0 );
4155 assert( p
->pPrior
!=0 );
4157 for(ii
=0; ii
<pList
->nExpr
; ii
++){
4160 assert( pList
->a
[ii
].pExpr
!=0 );
4161 aff
= sqlite3ExprAffinity(pList
->a
[ii
].pExpr
);
4162 for(pSub1
=p
->pPrior
; pSub1
; pSub1
=pSub1
->pPrior
){
4163 assert( pSub1
->pEList
!=0 );
4164 assert( pSub1
->pEList
->nExpr
>ii
);
4165 assert( pSub1
->pEList
->a
[ii
].pExpr
!=0 );
4166 if( sqlite3ExprAffinity(pSub1
->pEList
->a
[ii
].pExpr
)!=aff
){
4174 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
4176 ** This routine attempts to flatten subqueries as a performance optimization.
4177 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
4179 ** To understand the concept of flattening, consider the following
4182 ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
4184 ** The default way of implementing this query is to execute the
4185 ** subquery first and store the results in a temporary table, then
4186 ** run the outer query on that temporary table. This requires two
4187 ** passes over the data. Furthermore, because the temporary table
4188 ** has no indices, the WHERE clause on the outer query cannot be
4191 ** This routine attempts to rewrite queries such as the above into
4192 ** a single flat select, like this:
4194 ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
4196 ** The code generated for this simplification gives the same result
4197 ** but only has to scan the data once. And because indices might
4198 ** exist on the table t1, a complete scan of the data might be
4201 ** Flattening is subject to the following constraints:
4203 ** (**) We no longer attempt to flatten aggregate subqueries. Was:
4204 ** The subquery and the outer query cannot both be aggregates.
4206 ** (**) We no longer attempt to flatten aggregate subqueries. Was:
4207 ** (2) If the subquery is an aggregate then
4208 ** (2a) the outer query must not be a join and
4209 ** (2b) the outer query must not use subqueries
4210 ** other than the one FROM-clause subquery that is a candidate
4211 ** for flattening. (This is due to ticket [2f7170d73bf9abf80]
4212 ** from 2015-02-09.)
4214 ** (3) If the subquery is the right operand of a LEFT JOIN then
4215 ** (3a) the subquery may not be a join and
4216 ** (3b) the FROM clause of the subquery may not contain a virtual
4218 ** (**) Was: "The outer query may not have a GROUP BY." This case
4219 ** is now managed correctly
4220 ** (3d) the outer query may not be DISTINCT.
4221 ** See also (26) for restrictions on RIGHT JOIN.
4223 ** (4) The subquery can not be DISTINCT.
4225 ** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
4226 ** sub-queries that were excluded from this optimization. Restriction
4227 ** (4) has since been expanded to exclude all DISTINCT subqueries.
4229 ** (**) We no longer attempt to flatten aggregate subqueries. Was:
4230 ** If the subquery is aggregate, the outer query may not be DISTINCT.
4232 ** (7) The subquery must have a FROM clause. TODO: For subqueries without
4233 ** A FROM clause, consider adding a FROM clause with the special
4234 ** table sqlite_once that consists of a single row containing a
4237 ** (8) If the subquery uses LIMIT then the outer query may not be a join.
4239 ** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
4241 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
4242 ** accidentally carried the comment forward until 2014-09-15. Original
4243 ** constraint: "If the subquery is aggregate then the outer query
4244 ** may not use LIMIT."
4246 ** (11) The subquery and the outer query may not both have ORDER BY clauses.
4248 ** (**) Not implemented. Subsumed into restriction (3). Was previously
4249 ** a separate restriction deriving from ticket #350.
4251 ** (13) The subquery and outer query may not both use LIMIT.
4253 ** (14) The subquery may not use OFFSET.
4255 ** (15) If the outer query is part of a compound select, then the
4256 ** subquery may not use LIMIT.
4257 ** (See ticket #2339 and ticket [02a8e81d44]).
4259 ** (16) If the outer query is aggregate, then the subquery may not
4260 ** use ORDER BY. (Ticket #2942) This used to not matter
4261 ** until we introduced the group_concat() function.
4263 ** (17) If the subquery is a compound select, then
4264 ** (17a) all compound operators must be a UNION ALL, and
4265 ** (17b) no terms within the subquery compound may be aggregate
4267 ** (17c) every term within the subquery compound must have a FROM clause
4268 ** (17d) the outer query may not be
4269 ** (17d1) aggregate, or
4271 ** (17e) the subquery may not contain window functions, and
4272 ** (17f) the subquery must not be the RHS of a LEFT JOIN.
4273 ** (17g) either the subquery is the first element of the outer
4274 ** query or there are no RIGHT or FULL JOINs in any arm
4275 ** of the subquery. (This is a duplicate of condition (27b).)
4276 ** (17h) The corresponding result set expressions in all arms of the
4277 ** compound must have the same affinity.
4279 ** The parent and sub-query may contain WHERE clauses. Subject to
4280 ** rules (11), (13) and (14), they may also contain ORDER BY,
4281 ** LIMIT and OFFSET clauses. The subquery cannot use any compound
4282 ** operator other than UNION ALL because all the other compound
4283 ** operators have an implied DISTINCT which is disallowed by
4286 ** Also, each component of the sub-query must return the same number
4287 ** of result columns. This is actually a requirement for any compound
4288 ** SELECT statement, but all the code here does is make sure that no
4289 ** such (illegal) sub-query is flattened. The caller will detect the
4290 ** syntax error and return a detailed message.
4292 ** (18) If the sub-query is a compound select, then all terms of the
4293 ** ORDER BY clause of the parent must be copies of a term returned
4294 ** by the parent query.
4296 ** (19) If the subquery uses LIMIT then the outer query may not
4297 ** have a WHERE clause.
4299 ** (20) If the sub-query is a compound select, then it must not use
4300 ** an ORDER BY clause. Ticket #3773. We could relax this constraint
4301 ** somewhat by saying that the terms of the ORDER BY clause must
4302 ** appear as unmodified result columns in the outer query. But we
4303 ** have other optimizations in mind to deal with that case.
4305 ** (21) If the subquery uses LIMIT then the outer query may not be
4306 ** DISTINCT. (See ticket [752e1646fc]).
4308 ** (22) The subquery may not be a recursive CTE.
4310 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
4311 ** a compound query. This restriction is because transforming the
4312 ** parent to a compound query confuses the code that handles
4313 ** recursive queries in multiSelect().
4315 ** (**) We no longer attempt to flatten aggregate subqueries. Was:
4316 ** The subquery may not be an aggregate that uses the built-in min() or
4317 ** or max() functions. (Without this restriction, a query like:
4318 ** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
4319 ** return the value X for which Y was maximal.)
4321 ** (25) If either the subquery or the parent query contains a window
4322 ** function in the select list or ORDER BY clause, flattening
4323 ** is not attempted.
4325 ** (26) The subquery may not be the right operand of a RIGHT JOIN.
4326 ** See also (3) for restrictions on LEFT JOIN.
4328 ** (27) The subquery may not contain a FULL or RIGHT JOIN unless it
4329 ** is the first element of the parent query. Two subcases:
4330 ** (27a) the subquery is not a compound query.
4331 ** (27b) the subquery is a compound query and the RIGHT JOIN occurs
4332 ** in any arm of the compound query. (See also (17g).)
4334 ** (28) The subquery is not a MATERIALIZED CTE. (This is handled
4335 ** in the caller before ever reaching this routine.)
4338 ** In this routine, the "p" parameter is a pointer to the outer query.
4339 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
4342 ** If flattening is not attempted, this routine is a no-op and returns 0.
4343 ** If flattening is attempted this routine returns 1.
4345 ** All of the expression analysis must occur on both the outer query and
4346 ** the subquery before this routine runs.
4348 static int flattenSubquery(
4349 Parse
*pParse
, /* Parsing context */
4350 Select
*p
, /* The parent or outer SELECT statement */
4351 int iFrom
, /* Index in p->pSrc->a[] of the inner subquery */
4352 int isAgg
/* True if outer SELECT uses aggregate functions */
4354 const char *zSavedAuthContext
= pParse
->zAuthContext
;
4355 Select
*pParent
; /* Current UNION ALL term of the other query */
4356 Select
*pSub
; /* The inner query or "subquery" */
4357 Select
*pSub1
; /* Pointer to the rightmost select in sub-query */
4358 SrcList
*pSrc
; /* The FROM clause of the outer query */
4359 SrcList
*pSubSrc
; /* The FROM clause of the subquery */
4360 int iParent
; /* VDBE cursor number of the pSub result set temp table */
4361 int iNewParent
= -1;/* Replacement table for iParent */
4362 int isOuterJoin
= 0; /* True if pSub is the right side of a LEFT JOIN */
4363 int i
; /* Loop counter */
4364 Expr
*pWhere
; /* The WHERE clause */
4365 SrcItem
*pSubitem
; /* The subquery */
4366 sqlite3
*db
= pParse
->db
;
4367 Walker w
; /* Walker to persist agginfo data */
4370 /* Check to see if flattening is permitted. Return 0 if not.
4373 assert( p
->pPrior
==0 );
4374 if( OptimizationDisabled(db
, SQLITE_QueryFlattener
) ) return 0;
4376 assert( pSrc
&& iFrom
>=0 && iFrom
<pSrc
->nSrc
);
4377 pSubitem
= &pSrc
->a
[iFrom
];
4378 iParent
= pSubitem
->iCursor
;
4379 assert( pSubitem
->fg
.isSubquery
);
4380 pSub
= pSubitem
->u4
.pSubq
->pSelect
;
4383 #ifndef SQLITE_OMIT_WINDOWFUNC
4384 if( p
->pWin
|| pSub
->pWin
) return 0; /* Restriction (25) */
4387 pSubSrc
= pSub
->pSrc
;
4389 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
4390 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
4391 ** because they could be computed at compile-time. But when LIMIT and OFFSET
4392 ** became arbitrary expressions, we were forced to add restrictions (13)
4394 if( pSub
->pLimit
&& p
->pLimit
) return 0; /* Restriction (13) */
4395 if( pSub
->pLimit
&& pSub
->pLimit
->pRight
) return 0; /* Restriction (14) */
4396 if( (p
->selFlags
& SF_Compound
)!=0 && pSub
->pLimit
){
4397 return 0; /* Restriction (15) */
4399 if( pSubSrc
->nSrc
==0 ) return 0; /* Restriction (7) */
4400 if( pSub
->selFlags
& SF_Distinct
) return 0; /* Restriction (4) */
4401 if( pSub
->pLimit
&& (pSrc
->nSrc
>1 || isAgg
) ){
4402 return 0; /* Restrictions (8)(9) */
4404 if( p
->pOrderBy
&& pSub
->pOrderBy
){
4405 return 0; /* Restriction (11) */
4407 if( isAgg
&& pSub
->pOrderBy
) return 0; /* Restriction (16) */
4408 if( pSub
->pLimit
&& p
->pWhere
) return 0; /* Restriction (19) */
4409 if( pSub
->pLimit
&& (p
->selFlags
& SF_Distinct
)!=0 ){
4410 return 0; /* Restriction (21) */
4412 if( pSub
->selFlags
& (SF_Recursive
) ){
4413 return 0; /* Restrictions (22) */
4417 ** If the subquery is the right operand of a LEFT JOIN, then the
4418 ** subquery may not be a join itself (3a). Example of why this is not
4421 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
4423 ** If we flatten the above, we would get
4425 ** (t1 LEFT OUTER JOIN t2) JOIN t3
4427 ** which is not at all the same thing.
4429 ** See also tickets #306, #350, and #3300.
4431 if( (pSubitem
->fg
.jointype
& (JT_OUTER
|JT_LTORJ
))!=0 ){
4432 if( pSubSrc
->nSrc
>1 /* (3a) */
4433 || IsVirtual(pSubSrc
->a
[0].pSTab
) /* (3b) */
4434 || (p
->selFlags
& SF_Distinct
)!=0 /* (3d) */
4435 || (pSubitem
->fg
.jointype
& JT_RIGHT
)!=0 /* (26) */
4442 assert( pSubSrc
->nSrc
>0 ); /* True by restriction (7) */
4443 if( iFrom
>0 && (pSubSrc
->a
[0].fg
.jointype
& JT_LTORJ
)!=0 ){
4444 return 0; /* Restriction (27a) */
4447 /* Condition (28) is blocked by the caller */
4448 assert( !pSubitem
->fg
.isCte
|| pSubitem
->u2
.pCteUse
->eM10d
!=M10d_Yes
);
4450 /* Restriction (17): If the sub-query is a compound SELECT, then it must
4451 ** use only the UNION ALL operator. And none of the simple select queries
4452 ** that make up the compound SELECT are allowed to be aggregate or distinct
4457 if( pSub
->pOrderBy
){
4458 return 0; /* Restriction (20) */
4460 if( isAgg
|| (p
->selFlags
& SF_Distinct
)!=0 || isOuterJoin
>0 ){
4461 return 0; /* (17d1), (17d2), or (17f) */
4463 for(pSub1
=pSub
; pSub1
; pSub1
=pSub1
->pPrior
){
4464 testcase( (pSub1
->selFlags
& (SF_Distinct
|SF_Aggregate
))==SF_Distinct
);
4465 testcase( (pSub1
->selFlags
& (SF_Distinct
|SF_Aggregate
))==SF_Aggregate
);
4466 assert( pSub
->pSrc
!=0 );
4467 assert( (pSub
->selFlags
& SF_Recursive
)==0 );
4468 assert( pSub
->pEList
->nExpr
==pSub1
->pEList
->nExpr
);
4469 if( (pSub1
->selFlags
& (SF_Distinct
|SF_Aggregate
))!=0 /* (17b) */
4470 || (pSub1
->pPrior
&& pSub1
->op
!=TK_ALL
) /* (17a) */
4471 || pSub1
->pSrc
->nSrc
<1 /* (17c) */
4472 #ifndef SQLITE_OMIT_WINDOWFUNC
4473 || pSub1
->pWin
/* (17e) */
4478 if( iFrom
>0 && (pSub1
->pSrc
->a
[0].fg
.jointype
& JT_LTORJ
)!=0 ){
4479 /* Without this restriction, the JT_LTORJ flag would end up being
4480 ** omitted on left-hand tables of the right join that is being
4482 return 0; /* Restrictions (17g), (27b) */
4484 testcase( pSub1
->pSrc
->nSrc
>1 );
4487 /* Restriction (18). */
4489 for(ii
=0; ii
<p
->pOrderBy
->nExpr
; ii
++){
4490 if( p
->pOrderBy
->a
[ii
].u
.x
.iOrderByCol
==0 ) return 0;
4494 /* Restriction (23) */
4495 if( (p
->selFlags
& SF_Recursive
) ) return 0;
4497 /* Restriction (17h) */
4498 if( compoundHasDifferentAffinities(pSub
) ) return 0;
4501 if( pParse
->nSelect
>500 ) return 0;
4502 if( OptimizationDisabled(db
, SQLITE_FlttnUnionAll
) ) return 0;
4503 aCsrMap
= sqlite3DbMallocZero(db
, ((i64
)pParse
->nTab
+1)*sizeof(int));
4504 if( aCsrMap
) aCsrMap
[0] = pParse
->nTab
;
4508 /***** If we reach this point, flattening is permitted. *****/
4509 TREETRACE(0x4,pParse
,p
,("flatten %u.%p from term %d\n",
4510 pSub
->selId
, pSub
, iFrom
));
4512 /* Authorize the subquery */
4513 pParse
->zAuthContext
= pSubitem
->zName
;
4514 TESTONLY(i
=) sqlite3AuthCheck(pParse
, SQLITE_SELECT
, 0, 0, 0);
4515 testcase( i
==SQLITE_DENY
);
4516 pParse
->zAuthContext
= zSavedAuthContext
;
4518 /* Delete the transient structures associated with the subquery */
4520 if( ALWAYS(pSubitem
->fg
.isSubquery
) ){
4521 pSub1
= sqlite3SubqueryDetach(db
, pSubitem
);
4525 assert( pSubitem
->fg
.isSubquery
==0 );
4526 assert( pSubitem
->fg
.fixedSchema
==0 );
4527 sqlite3DbFree(db
, pSubitem
->zName
);
4528 sqlite3DbFree(db
, pSubitem
->zAlias
);
4529 pSubitem
->zName
= 0;
4530 pSubitem
->zAlias
= 0;
4531 assert( pSubitem
->fg
.isUsing
!=0 || pSubitem
->u3
.pOn
==0 );
4533 /* If the sub-query is a compound SELECT statement, then (by restrictions
4534 ** 17 and 18 above) it must be a UNION ALL and the parent query must
4537 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
4539 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
4540 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
4541 ** OFFSET clauses and joins them to the left-hand-side of the original
4542 ** using UNION ALL operators. In this case N is the number of simple
4543 ** select statements in the compound sub-query.
4547 ** SELECT a+1 FROM (
4548 ** SELECT x FROM tab
4550 ** SELECT y FROM tab
4552 ** SELECT abs(z*2) FROM tab2
4553 ** ) WHERE a!=5 ORDER BY 1
4555 ** Transformed into:
4557 ** SELECT x+1 FROM tab WHERE x+1!=5
4559 ** SELECT y+1 FROM tab WHERE y+1!=5
4561 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
4564 ** We call this the "compound-subquery flattening".
4566 for(pSub
=pSub
->pPrior
; pSub
; pSub
=pSub
->pPrior
){
4568 ExprList
*pOrderBy
= p
->pOrderBy
;
4569 Expr
*pLimit
= p
->pLimit
;
4570 Select
*pPrior
= p
->pPrior
;
4571 Table
*pItemTab
= pSubitem
->pSTab
;
4572 pSubitem
->pSTab
= 0;
4576 pNew
= sqlite3SelectDup(db
, p
, 0);
4578 p
->pOrderBy
= pOrderBy
;
4580 pSubitem
->pSTab
= pItemTab
;
4584 pNew
->selId
= ++pParse
->nSelect
;
4585 if( aCsrMap
&& ALWAYS(db
->mallocFailed
==0) ){
4586 renumberCursors(pParse
, pNew
, iFrom
, aCsrMap
);
4588 pNew
->pPrior
= pPrior
;
4589 if( pPrior
) pPrior
->pNext
= pNew
;
4592 TREETRACE(0x4,pParse
,p
,("compound-subquery flattener"
4593 " creates %u as peer\n",pNew
->selId
));
4595 assert( pSubitem
->fg
.isSubquery
==0 );
4597 sqlite3DbFree(db
, aCsrMap
);
4598 if( db
->mallocFailed
){
4599 assert( pSubitem
->fg
.fixedSchema
==0 );
4600 assert( pSubitem
->fg
.isSubquery
==0 );
4601 assert( pSubitem
->u4
.zDatabase
==0 );
4602 sqlite3SrcItemAttachSubquery(pParse
, pSubitem
, pSub1
, 0);
4606 /* Defer deleting the Table object associated with the
4607 ** subquery until code generation is
4608 ** complete, since there may still exist Expr.pTab entries that
4609 ** refer to the subquery even after flattening. Ticket #3346.
4611 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
4613 if( ALWAYS(pSubitem
->pSTab
!=0) ){
4614 Table
*pTabToDel
= pSubitem
->pSTab
;
4615 if( pTabToDel
->nTabRef
==1 ){
4616 Parse
*pToplevel
= sqlite3ParseToplevel(pParse
);
4617 sqlite3ParserAddCleanup(pToplevel
, sqlite3DeleteTableGeneric
, pTabToDel
);
4618 testcase( pToplevel
->earlyCleanup
);
4620 pTabToDel
->nTabRef
--;
4622 pSubitem
->pSTab
= 0;
4625 /* The following loop runs once for each term in a compound-subquery
4626 ** flattening (as described above). If we are doing a different kind
4627 ** of flattening - a flattening other than a compound-subquery flattening -
4628 ** then this loop only runs once.
4630 ** This loop moves all of the FROM elements of the subquery into the
4631 ** the FROM clause of the outer query. Before doing this, remember
4632 ** the cursor number for the original outer query FROM element in
4633 ** iParent. The iParent cursor will never be used. Subsequent code
4634 ** will scan expressions looking for iParent references and replace
4635 ** those references with expressions that resolve to the subquery FROM
4636 ** elements we are now copying in.
4639 for(pParent
=p
; pParent
; pParent
=pParent
->pPrior
, pSub
=pSub
->pPrior
){
4642 u8 ltorj
= pSrc
->a
[iFrom
].fg
.jointype
& JT_LTORJ
;
4644 pSubSrc
= pSub
->pSrc
; /* FROM clause of subquery */
4645 nSubSrc
= pSubSrc
->nSrc
; /* Number of terms in subquery FROM clause */
4646 pSrc
= pParent
->pSrc
; /* FROM clause of the outer query */
4649 jointype
= pSubitem
->fg
.jointype
; /* First time through the loop */
4652 /* The subquery uses a single slot of the FROM clause of the outer
4653 ** query. If the subquery has more than one element in its FROM clause,
4654 ** then expand the outer query to make space for it to hold all elements
4659 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
4661 ** The outer query has 3 slots in its FROM clause. One slot of the
4662 ** outer query (the middle slot) is used by the subquery. The next
4663 ** block of code will expand the outer query FROM clause to 4 slots.
4664 ** The middle slot is expanded to two slots in order to make space
4665 ** for the two elements in the FROM clause of the subquery.
4668 pSrc
= sqlite3SrcListEnlarge(pParse
, pSrc
, nSubSrc
-1,iFrom
+1);
4669 if( pSrc
==0 ) break;
4670 pParent
->pSrc
= pSrc
;
4673 /* Transfer the FROM clause terms from the subquery into the
4676 for(i
=0; i
<nSubSrc
; i
++){
4677 SrcItem
*pItem
= &pSrc
->a
[i
+iFrom
];
4678 assert( pItem
->fg
.isTabFunc
==0 );
4679 assert( pItem
->fg
.isSubquery
4680 || pItem
->fg
.fixedSchema
4681 || pItem
->u4
.zDatabase
==0 );
4682 if( pItem
->fg
.isUsing
) sqlite3IdListDelete(db
, pItem
->u3
.pUsing
);
4683 *pItem
= pSubSrc
->a
[i
];
4684 pItem
->fg
.jointype
|= ltorj
;
4685 iNewParent
= pSubSrc
->a
[i
].iCursor
;
4686 memset(&pSubSrc
->a
[i
], 0, sizeof(pSubSrc
->a
[i
]));
4688 pSrc
->a
[iFrom
].fg
.jointype
&= JT_LTORJ
;
4689 pSrc
->a
[iFrom
].fg
.jointype
|= jointype
| ltorj
;
4691 /* Now begin substituting subquery result set expressions for
4692 ** references to the iParent in the outer query.
4696 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
4697 ** \ \_____________ subquery __________/ /
4698 ** \_____________________ outer query ______________________________/
4700 ** We look at every expression in the outer query and every place we see
4701 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
4703 if( pSub
->pOrderBy
&& (pParent
->selFlags
& SF_NoopOrderBy
)==0 ){
4704 /* At this point, any non-zero iOrderByCol values indicate that the
4705 ** ORDER BY column expression is identical to the iOrderByCol'th
4706 ** expression returned by SELECT statement pSub. Since these values
4707 ** do not necessarily correspond to columns in SELECT statement pParent,
4708 ** zero them before transferring the ORDER BY clause.
4710 ** Not doing this may cause an error if a subsequent call to this
4711 ** function attempts to flatten a compound sub-query into pParent
4712 ** (the only way this can happen is if the compound sub-query is
4713 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
4714 ExprList
*pOrderBy
= pSub
->pOrderBy
;
4715 for(i
=0; i
<pOrderBy
->nExpr
; i
++){
4716 pOrderBy
->a
[i
].u
.x
.iOrderByCol
= 0;
4718 assert( pParent
->pOrderBy
==0 );
4719 pParent
->pOrderBy
= pOrderBy
;
4722 pWhere
= pSub
->pWhere
;
4724 if( isOuterJoin
>0 ){
4725 sqlite3SetJoinExpr(pWhere
, iNewParent
, EP_OuterON
);
4728 if( pParent
->pWhere
){
4729 pParent
->pWhere
= sqlite3PExpr(pParse
, TK_AND
, pWhere
, pParent
->pWhere
);
4731 pParent
->pWhere
= pWhere
;
4734 if( db
->mallocFailed
==0 ){
4738 x
.iNewTable
= iNewParent
;
4739 x
.isOuterJoin
= isOuterJoin
;
4740 x
.pEList
= pSub
->pEList
;
4741 x
.pCList
= findLeftmostExprlist(pSub
);
4742 substSelect(&x
, pParent
, 0);
4745 /* The flattened query is a compound if either the inner or the
4746 ** outer query is a compound. */
4747 pParent
->selFlags
|= pSub
->selFlags
& SF_Compound
;
4748 assert( (pSub
->selFlags
& SF_Distinct
)==0 ); /* restriction (17b) */
4751 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
4753 ** One is tempted to try to add a and b to combine the limits. But this
4754 ** does not work if either limit is negative.
4757 pParent
->pLimit
= pSub
->pLimit
;
4761 /* Recompute the SrcItem.colUsed masks for the flattened
4763 for(i
=0; i
<nSubSrc
; i
++){
4764 recomputeColumnsUsed(pParent
, &pSrc
->a
[i
+iFrom
]);
4768 /* Finally, delete what is left of the subquery and return success.
4770 sqlite3AggInfoPersistWalkerInit(&w
, pParse
);
4771 sqlite3WalkSelect(&w
,pSub1
);
4772 sqlite3SelectDelete(db
, pSub1
);
4774 #if TREETRACE_ENABLED
4775 if( sqlite3TreeTrace
& 0x4 ){
4776 TREETRACE(0x4,pParse
,p
,("After flattening:\n"));
4777 sqlite3TreeViewSelect(0, p
, 0);
4783 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
4786 ** A structure to keep track of all of the column values that are fixed to
4787 ** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
4789 typedef struct WhereConst WhereConst
;
4791 Parse
*pParse
; /* Parsing context */
4792 u8
*pOomFault
; /* Pointer to pParse->db->mallocFailed */
4793 int nConst
; /* Number for COLUMN=CONSTANT terms */
4794 int nChng
; /* Number of times a constant is propagated */
4795 int bHasAffBlob
; /* At least one column in apExpr[] as affinity BLOB */
4796 u32 mExcludeOn
; /* Which ON expressions to exclude from considertion.
4797 ** Either EP_OuterON or EP_InnerON|EP_OuterON */
4798 Expr
**apExpr
; /* [i*2] is COLUMN and [i*2+1] is VALUE */
4802 ** Add a new entry to the pConst object. Except, do not add duplicate
4803 ** pColumn entries. Also, do not add if doing so would not be appropriate.
4805 ** The caller guarantees the pColumn is a column and pValue is a constant.
4806 ** This routine has to do some additional checks before completing the
4809 static void constInsert(
4810 WhereConst
*pConst
, /* The WhereConst into which we are inserting */
4811 Expr
*pColumn
, /* The COLUMN part of the constraint */
4812 Expr
*pValue
, /* The VALUE part of the constraint */
4813 Expr
*pExpr
/* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
4816 assert( pColumn
->op
==TK_COLUMN
);
4817 assert( sqlite3ExprIsConstant(pConst
->pParse
, pValue
) );
4819 if( ExprHasProperty(pColumn
, EP_FixedCol
) ) return;
4820 if( sqlite3ExprAffinity(pValue
)!=0 ) return;
4821 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst
->pParse
,pExpr
)) ){
4825 /* 2018-10-25 ticket [cf5ed20f]
4826 ** Make sure the same pColumn is not inserted more than once */
4827 for(i
=0; i
<pConst
->nConst
; i
++){
4828 const Expr
*pE2
= pConst
->apExpr
[i
*2];
4829 assert( pE2
->op
==TK_COLUMN
);
4830 if( pE2
->iTable
==pColumn
->iTable
4831 && pE2
->iColumn
==pColumn
->iColumn
4833 return; /* Already present. Return without doing anything. */
4836 if( sqlite3ExprAffinity(pColumn
)==SQLITE_AFF_BLOB
){
4837 pConst
->bHasAffBlob
= 1;
4841 pConst
->apExpr
= sqlite3DbReallocOrFree(pConst
->pParse
->db
, pConst
->apExpr
,
4842 pConst
->nConst
*2*sizeof(Expr
*));
4843 if( pConst
->apExpr
==0 ){
4846 pConst
->apExpr
[pConst
->nConst
*2-2] = pColumn
;
4847 pConst
->apExpr
[pConst
->nConst
*2-1] = pValue
;
4852 ** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
4853 ** is a constant expression and where the term must be true because it
4854 ** is part of the AND-connected terms of the expression. For each term
4855 ** found, add it to the pConst structure.
4857 static void findConstInWhere(WhereConst
*pConst
, Expr
*pExpr
){
4858 Expr
*pRight
, *pLeft
;
4859 if( NEVER(pExpr
==0) ) return;
4860 if( ExprHasProperty(pExpr
, pConst
->mExcludeOn
) ){
4861 testcase( ExprHasProperty(pExpr
, EP_OuterON
) );
4862 testcase( ExprHasProperty(pExpr
, EP_InnerON
) );
4865 if( pExpr
->op
==TK_AND
){
4866 findConstInWhere(pConst
, pExpr
->pRight
);
4867 findConstInWhere(pConst
, pExpr
->pLeft
);
4870 if( pExpr
->op
!=TK_EQ
) return;
4871 pRight
= pExpr
->pRight
;
4872 pLeft
= pExpr
->pLeft
;
4873 assert( pRight
!=0 );
4875 if( pRight
->op
==TK_COLUMN
&& sqlite3ExprIsConstant(pConst
->pParse
, pLeft
) ){
4876 constInsert(pConst
,pRight
,pLeft
,pExpr
);
4878 if( pLeft
->op
==TK_COLUMN
&& sqlite3ExprIsConstant(pConst
->pParse
, pRight
) ){
4879 constInsert(pConst
,pLeft
,pRight
,pExpr
);
4884 ** This is a helper function for Walker callback propagateConstantExprRewrite().
4886 ** Argument pExpr is a candidate expression to be replaced by a value. If
4887 ** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
4888 ** then overwrite it with the corresponding value. Except, do not do so
4889 ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
4890 ** is SQLITE_AFF_BLOB.
4892 static int propagateConstantExprRewriteOne(
4898 if( pConst
->pOomFault
[0] ) return WRC_Prune
;
4899 if( pExpr
->op
!=TK_COLUMN
) return WRC_Continue
;
4900 if( ExprHasProperty(pExpr
, EP_FixedCol
|pConst
->mExcludeOn
) ){
4901 testcase( ExprHasProperty(pExpr
, EP_FixedCol
) );
4902 testcase( ExprHasProperty(pExpr
, EP_OuterON
) );
4903 testcase( ExprHasProperty(pExpr
, EP_InnerON
) );
4904 return WRC_Continue
;
4906 for(i
=0; i
<pConst
->nConst
; i
++){
4907 Expr
*pColumn
= pConst
->apExpr
[i
*2];
4908 if( pColumn
==pExpr
) continue;
4909 if( pColumn
->iTable
!=pExpr
->iTable
) continue;
4910 if( pColumn
->iColumn
!=pExpr
->iColumn
) continue;
4911 if( bIgnoreAffBlob
&& sqlite3ExprAffinity(pColumn
)==SQLITE_AFF_BLOB
){
4914 /* A match is found. Add the EP_FixedCol property */
4916 ExprClearProperty(pExpr
, EP_Leaf
);
4917 ExprSetProperty(pExpr
, EP_FixedCol
);
4918 assert( pExpr
->pLeft
==0 );
4919 pExpr
->pLeft
= sqlite3ExprDup(pConst
->pParse
->db
, pConst
->apExpr
[i
*2+1], 0);
4920 if( pConst
->pParse
->db
->mallocFailed
) return WRC_Prune
;
4927 ** This is a Walker expression callback. pExpr is a node from the WHERE
4928 ** clause of a SELECT statement. This function examines pExpr to see if
4929 ** any substitutions based on the contents of pWalker->u.pConst should
4930 ** be made to pExpr or its immediate children.
4932 ** A substitution is made if:
4934 ** + pExpr is a column with an affinity other than BLOB that matches
4935 ** one of the columns in pWalker->u.pConst, or
4937 ** + pExpr is a binary comparison operator (=, <=, >=, <, >) that
4938 ** uses an affinity other than TEXT and one of its immediate
4939 ** children is a column that matches one of the columns in
4940 ** pWalker->u.pConst.
4942 static int propagateConstantExprRewrite(Walker
*pWalker
, Expr
*pExpr
){
4943 WhereConst
*pConst
= pWalker
->u
.pConst
;
4944 assert( TK_GT
==TK_EQ
+1 );
4945 assert( TK_LE
==TK_EQ
+2 );
4946 assert( TK_LT
==TK_EQ
+3 );
4947 assert( TK_GE
==TK_EQ
+4 );
4948 if( pConst
->bHasAffBlob
){
4949 if( (pExpr
->op
>=TK_EQ
&& pExpr
->op
<=TK_GE
)
4952 propagateConstantExprRewriteOne(pConst
, pExpr
->pLeft
, 0);
4953 if( pConst
->pOomFault
[0] ) return WRC_Prune
;
4954 if( sqlite3ExprAffinity(pExpr
->pLeft
)!=SQLITE_AFF_TEXT
){
4955 propagateConstantExprRewriteOne(pConst
, pExpr
->pRight
, 0);
4959 return propagateConstantExprRewriteOne(pConst
, pExpr
, pConst
->bHasAffBlob
);
4963 ** The WHERE-clause constant propagation optimization.
4965 ** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
4966 ** CONSTANT=COLUMN that are top-level AND-connected terms that are not
4967 ** part of a ON clause from a LEFT JOIN, then throughout the query
4968 ** replace all other occurrences of COLUMN with CONSTANT.
4970 ** For example, the query:
4972 ** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
4974 ** Is transformed into
4976 ** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
4978 ** Return true if any transformations where made and false if not.
4980 ** Implementation note: Constant propagation is tricky due to affinity
4981 ** and collating sequence interactions. Consider this example:
4983 ** CREATE TABLE t1(a INT,b TEXT);
4984 ** INSERT INTO t1 VALUES(123,'0123');
4985 ** SELECT * FROM t1 WHERE a=123 AND b=a;
4986 ** SELECT * FROM t1 WHERE a=123 AND b=123;
4988 ** The two SELECT statements above should return different answers. b=a
4989 ** is always true because the comparison uses numeric affinity, but b=123
4990 ** is false because it uses text affinity and '0123' is not the same as '123'.
4991 ** To work around this, the expression tree is not actually changed from
4992 ** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
4993 ** and the "123" value is hung off of the pLeft pointer. Code generator
4994 ** routines know to generate the constant "123" instead of looking up the
4995 ** column value. Also, to avoid collation problems, this optimization is
4996 ** only attempted if the "a=123" term uses the default BINARY collation.
4998 ** 2021-05-25 forum post 6a06202608: Another troublesome case is...
5000 ** CREATE TABLE t1(x);
5001 ** INSERT INTO t1 VALUES(10.0);
5002 ** SELECT 1 FROM t1 WHERE x=10 AND x LIKE 10;
5004 ** The query should return no rows, because the t1.x value is '10.0' not '10'
5005 ** and '10.0' is not LIKE '10'. But if we are not careful, the first WHERE
5006 ** term "x=10" will cause the second WHERE term to become "10 LIKE 10",
5007 ** resulting in a false positive. To avoid this, constant propagation for
5008 ** columns with BLOB affinity is only allowed if the constant is used with
5009 ** operators ==, <=, <, >=, >, or IS in a way that will cause the correct
5010 ** type conversions to occur. See logic associated with the bHasAffBlob flag
5013 static int propagateConstants(
5014 Parse
*pParse
, /* The parsing context */
5015 Select
*p
/* The query in which to propagate constants */
5021 x
.pOomFault
= &pParse
->db
->mallocFailed
;
5027 if( ALWAYS(p
->pSrc
!=0)
5029 && (p
->pSrc
->a
[0].fg
.jointype
& JT_LTORJ
)!=0
5031 /* Do not propagate constants on any ON clause if there is a
5032 ** RIGHT JOIN anywhere in the query */
5033 x
.mExcludeOn
= EP_InnerON
| EP_OuterON
;
5035 /* Do not propagate constants through the ON clause of a LEFT JOIN */
5036 x
.mExcludeOn
= EP_OuterON
;
5038 findConstInWhere(&x
, p
->pWhere
);
5040 memset(&w
, 0, sizeof(w
));
5042 w
.xExprCallback
= propagateConstantExprRewrite
;
5043 w
.xSelectCallback
= sqlite3SelectWalkNoop
;
5044 w
.xSelectCallback2
= 0;
5047 sqlite3WalkExpr(&w
, p
->pWhere
);
5048 sqlite3DbFree(x
.pParse
->db
, x
.apExpr
);
5055 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
5056 # if !defined(SQLITE_OMIT_WINDOWFUNC)
5058 ** This function is called to determine whether or not it is safe to
5059 ** push WHERE clause expression pExpr down to FROM clause sub-query
5060 ** pSubq, which contains at least one window function. Return 1
5061 ** if it is safe and the expression should be pushed down, or 0
5064 ** It is only safe to push the expression down if it consists only
5065 ** of constants and copies of expressions that appear in the PARTITION
5066 ** BY clause of all window function used by the sub-query. It is safe
5067 ** to filter out entire partitions, but not rows within partitions, as
5068 ** this may change the results of the window functions.
5070 ** At the time this function is called it is guaranteed that
5072 ** * the sub-query uses only one distinct window frame, and
5073 ** * that the window frame has a PARTITION BY clause.
5075 static int pushDownWindowCheck(Parse
*pParse
, Select
*pSubq
, Expr
*pExpr
){
5076 assert( pSubq
->pWin
->pPartition
);
5077 assert( (pSubq
->selFlags
& SF_MultiPart
)==0 );
5078 assert( pSubq
->pPrior
==0 );
5079 return sqlite3ExprIsConstantOrGroupBy(pParse
, pExpr
, pSubq
->pWin
->pPartition
);
5081 # endif /* SQLITE_OMIT_WINDOWFUNC */
5082 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
5084 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
5086 ** Make copies of relevant WHERE clause terms of the outer query into
5087 ** the WHERE clause of subquery. Example:
5089 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
5091 ** Transformed into:
5093 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
5094 ** WHERE x=5 AND y=10;
5096 ** The hope is that the terms added to the inner query will make it more
5101 ** This optimization is called the "WHERE-clause push-down optimization"
5102 ** or sometimes the "predicate push-down optimization".
5104 ** Do not confuse this optimization with another unrelated optimization
5105 ** with a similar name: The "MySQL push-down optimization" causes WHERE
5106 ** clause terms that can be evaluated using only the index and without
5107 ** reference to the table are run first, so that if they are false,
5108 ** unnecessary table seeks are avoided.
5112 ** Do not attempt this optimization if:
5114 ** (1) (** This restriction was removed on 2017-09-29. We used to
5115 ** disallow this optimization for aggregate subqueries, but now
5116 ** it is allowed by putting the extra terms on the HAVING clause.
5117 ** The added HAVING clause is pointless if the subquery lacks
5118 ** a GROUP BY clause. But such a HAVING clause is also harmless
5119 ** so there does not appear to be any reason to add extra logic
5120 ** to suppress it. **)
5122 ** (2) The inner query is the recursive part of a common table expression.
5124 ** (3) The inner query has a LIMIT clause (since the changes to the WHERE
5125 ** clause would change the meaning of the LIMIT).
5127 ** (4) The inner query is the right operand of a LEFT JOIN and the
5128 ** expression to be pushed down does not come from the ON clause
5129 ** on that LEFT JOIN.
5131 ** (5) The WHERE clause expression originates in the ON or USING clause
5132 ** of a LEFT JOIN where iCursor is not the right-hand table of that
5133 ** left join. An example:
5136 ** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
5137 ** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
5138 ** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
5140 ** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
5141 ** But if the (b2=2) term were to be pushed down into the bb subquery,
5142 ** then the (1,1,NULL) row would be suppressed.
5144 ** (6) Window functions make things tricky as changes to the WHERE clause
5145 ** of the inner query could change the window over which window
5146 ** functions are calculated. Therefore, do not attempt the optimization
5149 ** (6a) The inner query uses multiple incompatible window partitions.
5151 ** (6b) The inner query is a compound and uses window-functions.
5153 ** (6c) The WHERE clause does not consist entirely of constants and
5154 ** copies of expressions found in the PARTITION BY clause of
5155 ** all window-functions used by the sub-query. It is safe to
5156 ** filter out entire partitions, as this does not change the
5157 ** window over which any window-function is calculated.
5159 ** (7) The inner query is a Common Table Expression (CTE) that should
5160 ** be materialized. (This restriction is implemented in the calling
5163 ** (8) If the subquery is a compound that uses UNION, INTERSECT,
5164 ** or EXCEPT, then all of the result set columns for all arms of
5165 ** the compound must use the BINARY collating sequence.
5167 ** (9) All three of the following are true:
5169 ** (9a) The WHERE clause expression originates in the ON or USING clause
5170 ** of a join (either an INNER or an OUTER join), and
5172 ** (9b) The subquery is to the right of the ON/USING clause
5174 ** (9c) There is a RIGHT JOIN (or FULL JOIN) in between the ON/USING
5175 ** clause and the subquery.
5177 ** Without this restriction, the WHERE-clause push-down optimization
5178 ** might move the ON/USING filter expression from the left side of a
5179 ** RIGHT JOIN over to the right side, which leads to incorrect answers.
5180 ** See also restriction (6) in sqlite3ExprIsSingleTableConstraint().
5182 ** (10) The inner query is not the right-hand table of a RIGHT JOIN.
5184 ** (11) The subquery is not a VALUES clause
5186 ** (12) The WHERE clause is not "rowid ISNULL" or the equivalent. This
5187 ** case only comes up if SQLite is compiled using
5188 ** SQLITE_ALLOW_ROWID_IN_VIEW.
5190 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
5191 ** terms are duplicated into the subquery.
5193 static int pushDownWhereTerms(
5194 Parse
*pParse
, /* Parse context (for malloc() and error reporting) */
5195 Select
*pSubq
, /* The subquery whose WHERE clause is to be augmented */
5196 Expr
*pWhere
, /* The WHERE clause of the outer query */
5197 SrcList
*pSrcList
, /* The complete from clause of the outer query */
5198 int iSrc
/* Which FROM clause term to try to push into */
5201 SrcItem
*pSrc
; /* The subquery FROM term into which WHERE is pushed */
5203 pSrc
= &pSrcList
->a
[iSrc
];
5204 if( pWhere
==0 ) return 0;
5205 if( pSubq
->selFlags
& (SF_Recursive
|SF_MultiPart
) ){
5206 return 0; /* restrictions (2) and (11) */
5208 if( pSrc
->fg
.jointype
& (JT_LTORJ
|JT_RIGHT
) ){
5209 return 0; /* restrictions (10) */
5212 if( pSubq
->pPrior
){
5214 int notUnionAll
= 0;
5215 for(pSel
=pSubq
; pSel
; pSel
=pSel
->pPrior
){
5217 assert( op
==TK_ALL
|| op
==TK_SELECT
5218 || op
==TK_UNION
|| op
==TK_INTERSECT
|| op
==TK_EXCEPT
);
5219 if( op
!=TK_ALL
&& op
!=TK_SELECT
){
5222 #ifndef SQLITE_OMIT_WINDOWFUNC
5223 if( pSel
->pWin
) return 0; /* restriction (6b) */
5227 /* If any of the compound arms are connected using UNION, INTERSECT,
5228 ** or EXCEPT, then we must ensure that none of the columns use a
5229 ** non-BINARY collating sequence. */
5230 for(pSel
=pSubq
; pSel
; pSel
=pSel
->pPrior
){
5232 const ExprList
*pList
= pSel
->pEList
;
5234 for(ii
=0; ii
<pList
->nExpr
; ii
++){
5235 CollSeq
*pColl
= sqlite3ExprCollSeq(pParse
, pList
->a
[ii
].pExpr
);
5236 if( !sqlite3IsBinary(pColl
) ){
5237 return 0; /* Restriction (8) */
5243 #ifndef SQLITE_OMIT_WINDOWFUNC
5244 if( pSubq
->pWin
&& pSubq
->pWin
->pPartition
==0 ) return 0;
5249 /* Only the first term of a compound can have a WITH clause. But make
5250 ** sure no other terms are marked SF_Recursive in case something changes
5255 for(pX
=pSubq
; pX
; pX
=pX
->pPrior
){
5256 assert( (pX
->selFlags
& (SF_Recursive
))==0 );
5261 if( pSubq
->pLimit
!=0 ){
5262 return 0; /* restriction (3) */
5264 while( pWhere
->op
==TK_AND
){
5265 nChng
+= pushDownWhereTerms(pParse
, pSubq
, pWhere
->pRight
, pSrcList
, iSrc
);
5266 pWhere
= pWhere
->pLeft
;
5269 #if 0 /* These checks now done by sqlite3ExprIsSingleTableConstraint() */
5270 if( ExprHasProperty(pWhere
, EP_OuterON
|EP_InnerON
) /* (9a) */
5271 && (pSrcList
->a
[0].fg
.jointype
& JT_LTORJ
)!=0 /* Fast pre-test of (9c) */
5274 for(jj
=0; jj
<iSrc
; jj
++){
5275 if( pWhere
->w
.iJoin
==pSrcList
->a
[jj
].iCursor
){
5276 /* If we reach this point, both (9a) and (9b) are satisfied.
5277 ** The following loop checks (9c):
5279 for(jj
++; jj
<iSrc
; jj
++){
5280 if( (pSrcList
->a
[jj
].fg
.jointype
& JT_RIGHT
)!=0 ){
5281 return 0; /* restriction (9) */
5288 && (ExprHasProperty(pWhere
,EP_OuterON
)==0
5289 || pWhere
->w
.iJoin
!=iCursor
)
5291 return 0; /* restriction (4) */
5293 if( ExprHasProperty(pWhere
,EP_OuterON
)
5294 && pWhere
->w
.iJoin
!=iCursor
5296 return 0; /* restriction (5) */
5300 #ifdef SQLITE_ALLOW_ROWID_IN_VIEW
5301 if( ViewCanHaveRowid
&& (pWhere
->op
==TK_ISNULL
|| pWhere
->op
==TK_NOTNULL
) ){
5302 Expr
*pLeft
= pWhere
->pLeft
;
5304 && pLeft
->op
==TK_COLUMN
5305 && pLeft
->iColumn
< 0
5307 return 0; /* Restriction (12) */
5312 if( sqlite3ExprIsSingleTableConstraint(pWhere
, pSrcList
, iSrc
, 1) ){
5314 pSubq
->selFlags
|= SF_PushDown
;
5317 pNew
= sqlite3ExprDup(pParse
->db
, pWhere
, 0);
5318 unsetJoinExpr(pNew
, -1, 1);
5320 x
.iTable
= pSrc
->iCursor
;
5321 x
.iNewTable
= pSrc
->iCursor
;
5323 x
.pEList
= pSubq
->pEList
;
5324 x
.pCList
= findLeftmostExprlist(pSubq
);
5325 pNew
= substExpr(&x
, pNew
);
5326 #ifndef SQLITE_OMIT_WINDOWFUNC
5327 if( pSubq
->pWin
&& 0==pushDownWindowCheck(pParse
, pSubq
, pNew
) ){
5328 /* Restriction 6c has prevented push-down in this case */
5329 sqlite3ExprDelete(pParse
->db
, pNew
);
5334 if( pSubq
->selFlags
& SF_Aggregate
){
5335 pSubq
->pHaving
= sqlite3ExprAnd(pParse
, pSubq
->pHaving
, pNew
);
5337 pSubq
->pWhere
= sqlite3ExprAnd(pParse
, pSubq
->pWhere
, pNew
);
5339 pSubq
= pSubq
->pPrior
;
5344 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
5347 ** Check to see if a subquery contains result-set columns that are
5348 ** never used. If it does, change the value of those result-set columns
5349 ** to NULL so that they do not cause unnecessary work to compute.
5351 ** Return the number of column that were changed to NULL.
5353 static int disableUnusedSubqueryResultColumns(SrcItem
*pItem
){
5355 Select
*pSub
; /* The subquery to be simplified */
5356 Select
*pX
; /* For looping over compound elements of pSub */
5357 Table
*pTab
; /* The table that describes the subquery */
5358 int j
; /* Column number */
5359 int nChng
= 0; /* Number of columns converted to NULL */
5360 Bitmask colUsed
; /* Columns that may not be NULLed out */
5363 if( pItem
->fg
.isCorrelated
|| pItem
->fg
.isCte
){
5366 assert( pItem
->pSTab
!=0 );
5367 pTab
= pItem
->pSTab
;
5368 assert( pItem
->fg
.isSubquery
);
5369 pSub
= pItem
->u4
.pSubq
->pSelect
;
5370 assert( pSub
->pEList
->nExpr
==pTab
->nCol
);
5371 for(pX
=pSub
; pX
; pX
=pX
->pPrior
){
5372 if( (pX
->selFlags
& (SF_Distinct
|SF_Aggregate
))!=0 ){
5373 testcase( pX
->selFlags
& SF_Distinct
);
5374 testcase( pX
->selFlags
& SF_Aggregate
);
5377 if( pX
->pPrior
&& pX
->op
!=TK_ALL
){
5378 /* This optimization does not work for compound subqueries that
5379 ** use UNION, INTERSECT, or EXCEPT. Only UNION ALL is allowed. */
5382 #ifndef SQLITE_OMIT_WINDOWFUNC
5384 /* This optimization does not work for subqueries that use window
5390 colUsed
= pItem
->colUsed
;
5391 if( pSub
->pOrderBy
){
5392 ExprList
*pList
= pSub
->pOrderBy
;
5393 for(j
=0; j
<pList
->nExpr
; j
++){
5394 u16 iCol
= pList
->a
[j
].u
.x
.iOrderByCol
;
5397 colUsed
|= ((Bitmask
)1)<<(iCol
>=BMS
? BMS
-1 : iCol
);
5402 for(j
=0; j
<nCol
; j
++){
5403 Bitmask m
= j
<BMS
-1 ? MASKBIT(j
) : TOPBIT
;
5404 if( (m
& colUsed
)!=0 ) continue;
5405 for(pX
=pSub
; pX
; pX
=pX
->pPrior
) {
5406 Expr
*pY
= pX
->pEList
->a
[j
].pExpr
;
5407 if( pY
->op
==TK_NULL
) continue;
5409 ExprClearProperty(pY
, EP_Skip
|EP_Unlikely
);
5410 pX
->selFlags
|= SF_PushDown
;
5419 ** The pFunc is the only aggregate function in the query. Check to see
5420 ** if the query is a candidate for the min/max optimization.
5422 ** If the query is a candidate for the min/max optimization, then set
5423 ** *ppMinMax to be an ORDER BY clause to be used for the optimization
5424 ** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
5425 ** whether pFunc is a min() or max() function.
5427 ** If the query is not a candidate for the min/max optimization, return
5428 ** WHERE_ORDERBY_NORMAL (which must be zero).
5430 ** This routine must be called after aggregate functions have been
5431 ** located but before their arguments have been subjected to aggregate
5434 static u8
minMaxQuery(sqlite3
*db
, Expr
*pFunc
, ExprList
**ppMinMax
){
5435 int eRet
= WHERE_ORDERBY_NORMAL
; /* Return value */
5436 ExprList
*pEList
; /* Arguments to agg function */
5437 const char *zFunc
; /* Name of aggregate function pFunc */
5441 assert( *ppMinMax
==0 );
5442 assert( pFunc
->op
==TK_AGG_FUNCTION
);
5443 assert( !IsWindowFunc(pFunc
) );
5444 assert( ExprUseXList(pFunc
) );
5445 pEList
= pFunc
->x
.pList
;
5448 || ExprHasProperty(pFunc
, EP_WinFunc
)
5449 || OptimizationDisabled(db
, SQLITE_MinMaxOpt
)
5453 assert( !ExprHasProperty(pFunc
, EP_IntValue
) );
5454 zFunc
= pFunc
->u
.zToken
;
5455 if( sqlite3StrICmp(zFunc
, "min")==0 ){
5456 eRet
= WHERE_ORDERBY_MIN
;
5457 if( sqlite3ExprCanBeNull(pEList
->a
[0].pExpr
) ){
5458 sortFlags
= KEYINFO_ORDER_BIGNULL
;
5460 }else if( sqlite3StrICmp(zFunc
, "max")==0 ){
5461 eRet
= WHERE_ORDERBY_MAX
;
5462 sortFlags
= KEYINFO_ORDER_DESC
;
5466 *ppMinMax
= pOrderBy
= sqlite3ExprListDup(db
, pEList
, 0);
5467 assert( pOrderBy
!=0 || db
->mallocFailed
);
5468 if( pOrderBy
) pOrderBy
->a
[0].fg
.sortFlags
= sortFlags
;
5473 ** The select statement passed as the first argument is an aggregate query.
5474 ** The second argument is the associated aggregate-info object. This
5475 ** function tests if the SELECT is of the form:
5477 ** SELECT count(*) FROM <tbl>
5479 ** where table is a database table, not a sub-select or view. If the query
5480 ** does match this pattern, then a pointer to the Table object representing
5481 ** <tbl> is returned. Otherwise, NULL is returned.
5483 ** This routine checks to see if it is safe to use the count optimization.
5484 ** A correct answer is still obtained (though perhaps more slowly) if
5485 ** this routine returns NULL when it could have returned a table pointer.
5486 ** But returning the pointer when NULL should have been returned can
5487 ** result in incorrect answers and/or crashes. So, when in doubt, return NULL.
5489 static Table
*isSimpleCount(Select
*p
, AggInfo
*pAggInfo
){
5493 assert( !p
->pGroupBy
);
5496 || p
->pEList
->nExpr
!=1
5498 || p
->pSrc
->a
[0].fg
.isSubquery
5499 || pAggInfo
->nFunc
!=1
5504 pTab
= p
->pSrc
->a
[0].pSTab
;
5506 assert( !IsView(pTab
) );
5507 if( !IsOrdinaryTable(pTab
) ) return 0;
5508 pExpr
= p
->pEList
->a
[0].pExpr
;
5510 if( pExpr
->op
!=TK_AGG_FUNCTION
) return 0;
5511 if( pExpr
->pAggInfo
!=pAggInfo
) return 0;
5512 if( (pAggInfo
->aFunc
[0].pFunc
->funcFlags
&SQLITE_FUNC_COUNT
)==0 ) return 0;
5513 assert( pAggInfo
->aFunc
[0].pFExpr
==pExpr
);
5514 testcase( ExprHasProperty(pExpr
, EP_Distinct
) );
5515 testcase( ExprHasProperty(pExpr
, EP_WinFunc
) );
5516 if( ExprHasProperty(pExpr
, EP_Distinct
|EP_WinFunc
) ) return 0;
5522 ** If the source-list item passed as an argument was augmented with an
5523 ** INDEXED BY clause, then try to locate the specified index. If there
5524 ** was such a clause and the named index cannot be found, return
5525 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
5526 ** pFrom->pIndex and return SQLITE_OK.
5528 int sqlite3IndexedByLookup(Parse
*pParse
, SrcItem
*pFrom
){
5529 Table
*pTab
= pFrom
->pSTab
;
5530 char *zIndexedBy
= pFrom
->u1
.zIndexedBy
;
5533 assert( pFrom
->fg
.isIndexedBy
!=0 );
5535 for(pIdx
=pTab
->pIndex
;
5536 pIdx
&& sqlite3StrICmp(pIdx
->zName
, zIndexedBy
);
5540 sqlite3ErrorMsg(pParse
, "no such index: %s", zIndexedBy
, 0);
5541 pParse
->checkSchema
= 1;
5542 return SQLITE_ERROR
;
5544 assert( pFrom
->fg
.isCte
==0 );
5545 pFrom
->u2
.pIBIndex
= pIdx
;
5550 ** Detect compound SELECT statements that use an ORDER BY clause with
5551 ** an alternative collating sequence.
5553 ** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
5555 ** These are rewritten as a subquery:
5557 ** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
5558 ** ORDER BY ... COLLATE ...
5560 ** This transformation is necessary because the multiSelectOrderBy() routine
5561 ** above that generates the code for a compound SELECT with an ORDER BY clause
5562 ** uses a merge algorithm that requires the same collating sequence on the
5563 ** result columns as on the ORDER BY clause. See ticket
5564 ** http://www.sqlite.org/src/info/6709574d2a
5566 ** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
5567 ** The UNION ALL operator works fine with multiSelectOrderBy() even when
5568 ** there are COLLATE terms in the ORDER BY.
5570 static int convertCompoundSelectToSubquery(Walker
*pWalker
, Select
*p
){
5575 struct ExprList_item
*a
;
5580 if( p
->pPrior
==0 ) return WRC_Continue
;
5581 if( p
->pOrderBy
==0 ) return WRC_Continue
;
5582 for(pX
=p
; pX
&& (pX
->op
==TK_ALL
|| pX
->op
==TK_SELECT
); pX
=pX
->pPrior
){}
5583 if( pX
==0 ) return WRC_Continue
;
5585 #ifndef SQLITE_OMIT_WINDOWFUNC
5586 /* If iOrderByCol is already non-zero, then it has already been matched
5587 ** to a result column of the SELECT statement. This occurs when the
5588 ** SELECT is rewritten for window-functions processing and then passed
5589 ** to sqlite3SelectPrep() and similar a second time. The rewriting done
5590 ** by this function is not required in this case. */
5591 if( a
[0].u
.x
.iOrderByCol
) return WRC_Continue
;
5593 for(i
=p
->pOrderBy
->nExpr
-1; i
>=0; i
--){
5594 if( a
[i
].pExpr
->flags
& EP_Collate
) break;
5596 if( i
<0 ) return WRC_Continue
;
5598 /* If we reach this point, that means the transformation is required. */
5600 pParse
= pWalker
->pParse
;
5602 pNew
= sqlite3DbMallocZero(db
, sizeof(*pNew
) );
5603 if( pNew
==0 ) return WRC_Abort
;
5604 memset(&dummy
, 0, sizeof(dummy
));
5605 pNewSrc
= sqlite3SrcListAppendFromTerm(pParse
,0,0,0,&dummy
,pNew
,0);
5606 assert( pNewSrc
!=0 || pParse
->nErr
);
5608 sqlite3SrcListDelete(db
, pNewSrc
);
5613 p
->pEList
= sqlite3ExprListAppend(pParse
, 0, sqlite3Expr(db
, TK_ASTERISK
, 0));
5622 #ifndef SQLITE_OMIT_WINDOWFUNC
5625 p
->selFlags
&= ~SF_Compound
;
5626 assert( (p
->selFlags
& SF_Converted
)==0 );
5627 p
->selFlags
|= SF_Converted
;
5628 assert( pNew
->pPrior
!=0 );
5629 pNew
->pPrior
->pNext
= pNew
;
5631 return WRC_Continue
;
5635 ** Check to see if the FROM clause term pFrom has table-valued function
5636 ** arguments. If it does, leave an error message in pParse and return
5637 ** non-zero, since pFrom is not allowed to be a table-valued function.
5639 static int cannotBeFunction(Parse
*pParse
, SrcItem
*pFrom
){
5640 if( pFrom
->fg
.isTabFunc
){
5641 sqlite3ErrorMsg(pParse
, "'%s' is not a function", pFrom
->zName
);
5647 #ifndef SQLITE_OMIT_CTE
5649 ** Argument pWith (which may be NULL) points to a linked list of nested
5650 ** WITH contexts, from inner to outermost. If the table identified by
5651 ** FROM clause element pItem is really a common-table-expression (CTE)
5652 ** then return a pointer to the CTE definition for that table. Otherwise
5655 ** If a non-NULL value is returned, set *ppContext to point to the With
5656 ** object that the returned CTE belongs to.
5658 static struct Cte
*searchWith(
5659 With
*pWith
, /* Current innermost WITH clause */
5660 SrcItem
*pItem
, /* FROM clause element to resolve */
5661 With
**ppContext
/* OUT: WITH clause return value belongs to */
5663 const char *zName
= pItem
->zName
;
5665 assert( pItem
->fg
.fixedSchema
|| pItem
->u4
.zDatabase
==0 );
5667 for(p
=pWith
; p
; p
=p
->pOuter
){
5669 for(i
=0; i
<p
->nCte
; i
++){
5670 if( sqlite3StrICmp(zName
, p
->a
[i
].zName
)==0 ){
5675 if( p
->bView
) break;
5680 /* The code generator maintains a stack of active WITH clauses
5681 ** with the inner-most WITH clause being at the top of the stack.
5683 ** This routine pushes the WITH clause passed as the second argument
5684 ** onto the top of the stack. If argument bFree is true, then this
5685 ** WITH clause will never be popped from the stack but should instead
5686 ** be freed along with the Parse object. In other cases, when
5687 ** bFree==0, the With object will be freed along with the SELECT
5688 ** statement with which it is associated.
5690 ** This routine returns a copy of pWith. Or, if bFree is true and
5691 ** the pWith object is destroyed immediately due to an OOM condition,
5692 ** then this routine return NULL.
5694 ** If bFree is true, do not continue to use the pWith pointer after
5695 ** calling this routine, Instead, use only the return value.
5697 With
*sqlite3WithPush(Parse
*pParse
, With
*pWith
, u8 bFree
){
5700 pWith
= (With
*)sqlite3ParserAddCleanup(pParse
, sqlite3WithDeleteGeneric
,
5702 if( pWith
==0 ) return 0;
5704 if( pParse
->nErr
==0 ){
5705 assert( pParse
->pWith
!=pWith
);
5706 pWith
->pOuter
= pParse
->pWith
;
5707 pParse
->pWith
= pWith
;
5714 ** This function checks if argument pFrom refers to a CTE declared by
5715 ** a WITH clause on the stack currently maintained by the parser (on the
5716 ** pParse->pWith linked list). And if currently processing a CTE
5717 ** CTE expression, through routine checks to see if the reference is
5718 ** a recursive reference to the CTE.
5720 ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
5721 ** and other fields are populated accordingly.
5723 ** Return 0 if no match is found.
5724 ** Return 1 if a match is found.
5725 ** Return 2 if an error condition is detected.
5727 static int resolveFromTermToCte(
5728 Parse
*pParse
, /* The parsing context */
5729 Walker
*pWalker
, /* Current tree walker */
5730 SrcItem
*pFrom
/* The FROM clause term to check */
5732 Cte
*pCte
; /* Matched CTE (or NULL if no match) */
5733 With
*pWith
; /* The matching WITH */
5735 assert( pFrom
->pSTab
==0 );
5736 if( pParse
->pWith
==0 ){
5737 /* There are no WITH clauses in the stack. No match is possible */
5741 /* Prior errors might have left pParse->pWith in a goofy state, so
5742 ** go no further. */
5745 assert( pFrom
->fg
.hadSchema
==0 || pFrom
->fg
.notCte
!=0 );
5746 if( pFrom
->fg
.fixedSchema
==0 && pFrom
->u4
.zDatabase
!=0 ){
5747 /* The FROM term contains a schema qualifier (ex: main.t1) and so
5748 ** it cannot possibly be a CTE reference. */
5751 if( pFrom
->fg
.notCte
){
5752 /* The FROM term is specifically excluded from matching a CTE.
5753 ** (1) It is part of a trigger that used to have zDatabase but had
5754 ** zDatabase removed by sqlite3FixTriggerStep().
5755 ** (2) This is the first term in the FROM clause of an UPDATE.
5759 pCte
= searchWith(pParse
->pWith
, pFrom
, &pWith
);
5761 sqlite3
*db
= pParse
->db
;
5765 Select
*pLeft
; /* Left-most SELECT statement */
5766 Select
*pRecTerm
; /* Left-most recursive term */
5767 int bMayRecursive
; /* True if compound joined by UNION [ALL] */
5768 With
*pSavedWith
; /* Initial value of pParse->pWith */
5769 int iRecTab
= -1; /* Cursor for recursive table */
5772 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
5773 ** recursive reference to CTE pCte. Leave an error in pParse and return
5774 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
5775 ** In this case, proceed. */
5776 if( pCte
->zCteErr
){
5777 sqlite3ErrorMsg(pParse
, pCte
->zCteErr
, pCte
->zName
);
5780 if( cannotBeFunction(pParse
, pFrom
) ) return 2;
5782 assert( pFrom
->pSTab
==0 );
5783 pTab
= sqlite3DbMallocZero(db
, sizeof(Table
));
5784 if( pTab
==0 ) return 2;
5785 pCteUse
= pCte
->pUse
;
5787 pCte
->pUse
= pCteUse
= sqlite3DbMallocZero(db
, sizeof(pCteUse
[0]));
5789 || sqlite3ParserAddCleanup(pParse
,sqlite3DbFree
,pCteUse
)==0
5791 sqlite3DbFree(db
, pTab
);
5794 pCteUse
->eM10d
= pCte
->eM10d
;
5796 pFrom
->pSTab
= pTab
;
5798 pTab
->zName
= sqlite3DbStrDup(db
, pCte
->zName
);
5800 pTab
->nRowLogEst
= 200; assert( 200==sqlite3LogEst(1048576) );
5801 pTab
->tabFlags
|= TF_Ephemeral
| TF_NoVisibleRowid
;
5802 sqlite3SrcItemAttachSubquery(pParse
, pFrom
, pCte
->pSelect
, 1);
5803 if( db
->mallocFailed
) return 2;
5804 assert( pFrom
->fg
.isSubquery
&& pFrom
->u4
.pSubq
);
5805 pSel
= pFrom
->u4
.pSubq
->pSelect
;
5807 pSel
->selFlags
|= SF_CopyCte
;
5808 if( pFrom
->fg
.isIndexedBy
){
5809 sqlite3ErrorMsg(pParse
, "no such index: \"%s\"", pFrom
->u1
.zIndexedBy
);
5812 assert( !pFrom
->fg
.isIndexedBy
);
5813 pFrom
->fg
.isCte
= 1;
5814 pFrom
->u2
.pCteUse
= pCteUse
;
5817 /* Check if this is a recursive CTE. */
5819 bMayRecursive
= ( pSel
->op
==TK_ALL
|| pSel
->op
==TK_UNION
);
5820 while( bMayRecursive
&& pRecTerm
->op
==pSel
->op
){
5822 SrcList
*pSrc
= pRecTerm
->pSrc
;
5823 assert( pRecTerm
->pPrior
!=0 );
5824 for(i
=0; i
<pSrc
->nSrc
; i
++){
5825 SrcItem
*pItem
= &pSrc
->a
[i
];
5827 && !pItem
->fg
.hadSchema
5828 && ALWAYS( !pItem
->fg
.isSubquery
)
5829 && (pItem
->fg
.fixedSchema
|| pItem
->u4
.zDatabase
==0)
5830 && 0==sqlite3StrICmp(pItem
->zName
, pCte
->zName
)
5832 pItem
->pSTab
= pTab
;
5834 pItem
->fg
.isRecursive
= 1;
5835 if( pRecTerm
->selFlags
& SF_Recursive
){
5836 sqlite3ErrorMsg(pParse
,
5837 "multiple references to recursive table: %s", pCte
->zName
5841 pRecTerm
->selFlags
|= SF_Recursive
;
5842 if( iRecTab
<0 ) iRecTab
= pParse
->nTab
++;
5843 pItem
->iCursor
= iRecTab
;
5846 if( (pRecTerm
->selFlags
& SF_Recursive
)==0 ) break;
5847 pRecTerm
= pRecTerm
->pPrior
;
5850 pCte
->zCteErr
= "circular reference: %s";
5851 pSavedWith
= pParse
->pWith
;
5852 pParse
->pWith
= pWith
;
5853 if( pSel
->selFlags
& SF_Recursive
){
5855 assert( pRecTerm
!=0 );
5856 assert( (pRecTerm
->selFlags
& SF_Recursive
)==0 );
5857 assert( pRecTerm
->pNext
!=0 );
5858 assert( (pRecTerm
->pNext
->selFlags
& SF_Recursive
)!=0 );
5859 assert( pRecTerm
->pWith
==0 );
5860 pRecTerm
->pWith
= pSel
->pWith
;
5861 rc
= sqlite3WalkSelect(pWalker
, pRecTerm
);
5862 pRecTerm
->pWith
= 0;
5864 pParse
->pWith
= pSavedWith
;
5868 if( sqlite3WalkSelect(pWalker
, pSel
) ){
5869 pParse
->pWith
= pSavedWith
;
5873 pParse
->pWith
= pWith
;
5875 for(pLeft
=pSel
; pLeft
->pPrior
; pLeft
=pLeft
->pPrior
);
5876 pEList
= pLeft
->pEList
;
5878 if( pEList
&& pEList
->nExpr
!=pCte
->pCols
->nExpr
){
5879 sqlite3ErrorMsg(pParse
, "table %s has %d values for %d columns",
5880 pCte
->zName
, pEList
->nExpr
, pCte
->pCols
->nExpr
5882 pParse
->pWith
= pSavedWith
;
5885 pEList
= pCte
->pCols
;
5888 sqlite3ColumnsFromExprList(pParse
, pEList
, &pTab
->nCol
, &pTab
->aCol
);
5889 if( bMayRecursive
){
5890 if( pSel
->selFlags
& SF_Recursive
){
5891 pCte
->zCteErr
= "multiple recursive references: %s";
5893 pCte
->zCteErr
= "recursive reference in a subquery: %s";
5895 sqlite3WalkSelect(pWalker
, pSel
);
5898 pParse
->pWith
= pSavedWith
;
5899 return 1; /* Success */
5901 return 0; /* No match */
5905 #ifndef SQLITE_OMIT_CTE
5907 ** If the SELECT passed as the second argument has an associated WITH
5908 ** clause, pop it from the stack stored as part of the Parse object.
5910 ** This function is used as the xSelectCallback2() callback by
5911 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
5912 ** names and other FROM clause elements.
5914 void sqlite3SelectPopWith(Walker
*pWalker
, Select
*p
){
5915 Parse
*pParse
= pWalker
->pParse
;
5916 if( OK_IF_ALWAYS_TRUE(pParse
->pWith
) && p
->pPrior
==0 ){
5917 With
*pWith
= findRightmost(p
)->pWith
;
5919 assert( pParse
->pWith
==pWith
|| pParse
->nErr
);
5920 pParse
->pWith
= pWith
->pOuter
;
5927 ** The SrcItem structure passed as the second argument represents a
5928 ** sub-query in the FROM clause of a SELECT statement. This function
5929 ** allocates and populates the SrcItem.pTab object. If successful,
5930 ** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
5933 int sqlite3ExpandSubquery(Parse
*pParse
, SrcItem
*pFrom
){
5937 assert( pFrom
->fg
.isSubquery
);
5938 assert( pFrom
->u4
.pSubq
!=0 );
5939 pSel
= pFrom
->u4
.pSubq
->pSelect
;
5941 pFrom
->pSTab
= pTab
= sqlite3DbMallocZero(pParse
->db
, sizeof(Table
));
5942 if( pTab
==0 ) return SQLITE_NOMEM
;
5944 if( pFrom
->zAlias
){
5945 pTab
->zName
= sqlite3DbStrDup(pParse
->db
, pFrom
->zAlias
);
5947 pTab
->zName
= sqlite3MPrintf(pParse
->db
, "%!S", pFrom
);
5949 while( pSel
->pPrior
){ pSel
= pSel
->pPrior
; }
5950 sqlite3ColumnsFromExprList(pParse
, pSel
->pEList
,&pTab
->nCol
,&pTab
->aCol
);
5952 pTab
->eTabType
= TABTYP_VIEW
;
5953 pTab
->nRowLogEst
= 200; assert( 200==sqlite3LogEst(1048576) );
5954 #ifndef SQLITE_ALLOW_ROWID_IN_VIEW
5955 /* The usual case - do not allow ROWID on a subquery */
5956 pTab
->tabFlags
|= TF_Ephemeral
| TF_NoVisibleRowid
;
5958 /* Legacy compatibility mode */
5959 pTab
->tabFlags
|= TF_Ephemeral
| sqlite3Config
.mNoVisibleRowid
;
5961 return pParse
->nErr
? SQLITE_ERROR
: SQLITE_OK
;
5966 ** Check the N SrcItem objects to the right of pBase. (N might be zero!)
5967 ** If any of those SrcItem objects have a USING clause containing zName
5968 ** then return true.
5970 ** If N is zero, or none of the N SrcItem objects to the right of pBase
5971 ** contains a USING clause, or if none of the USING clauses contain zName,
5972 ** then return false.
5974 static int inAnyUsingClause(
5975 const char *zName
, /* Name we are looking for */
5976 SrcItem
*pBase
, /* The base SrcItem. Looking at pBase[1] and following */
5977 int N
/* How many SrcItems to check */
5982 if( pBase
->fg
.isUsing
==0 ) continue;
5983 if( NEVER(pBase
->u3
.pUsing
==0) ) continue;
5984 if( sqlite3IdListIndex(pBase
->u3
.pUsing
, zName
)>=0 ) return 1;
5991 ** This routine is a Walker callback for "expanding" a SELECT statement.
5992 ** "Expanding" means to do the following:
5994 ** (1) Make sure VDBE cursor numbers have been assigned to every
5995 ** element of the FROM clause.
5997 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
5998 ** defines FROM clause. When views appear in the FROM clause,
5999 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
6000 ** that implements the view. A copy is made of the view's SELECT
6001 ** statement so that we can freely modify or delete that statement
6002 ** without worrying about messing up the persistent representation
6005 ** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
6006 ** on joins and the ON and USING clause of joins.
6008 ** (4) Scan the list of columns in the result set (pEList) looking
6009 ** for instances of the "*" operator or the TABLE.* operator.
6010 ** If found, expand each "*" to be every column in every table
6011 ** and TABLE.* to be every column in TABLE.
6014 static int selectExpander(Walker
*pWalker
, Select
*p
){
6015 Parse
*pParse
= pWalker
->pParse
;
6020 sqlite3
*db
= pParse
->db
;
6021 Expr
*pE
, *pRight
, *pExpr
;
6022 u16 selFlags
= p
->selFlags
;
6025 p
->selFlags
|= SF_Expanded
;
6026 if( db
->mallocFailed
){
6029 assert( p
->pSrc
!=0 );
6030 if( (selFlags
& SF_Expanded
)!=0 ){
6033 if( pWalker
->eCode
){
6034 /* Renumber selId because it has been copied from a view */
6035 p
->selId
= ++pParse
->nSelect
;
6039 if( pParse
->pWith
&& (p
->selFlags
& SF_View
) ){
6041 p
->pWith
= (With
*)sqlite3DbMallocZero(db
, sizeof(With
));
6046 p
->pWith
->bView
= 1;
6048 sqlite3WithPush(pParse
, p
->pWith
, 0);
6050 /* Make sure cursor numbers have been assigned to all entries in
6051 ** the FROM clause of the SELECT statement.
6053 sqlite3SrcListAssignCursors(pParse
, pTabList
);
6055 /* Look up every table named in the FROM clause of the select. If
6056 ** an entry of the FROM clause is a subquery instead of a table or view,
6057 ** then create a transient table structure to describe the subquery.
6059 for(i
=0, pFrom
=pTabList
->a
; i
<pTabList
->nSrc
; i
++, pFrom
++){
6061 assert( pFrom
->fg
.isRecursive
==0 || pFrom
->pSTab
!=0 );
6062 if( pFrom
->pSTab
) continue;
6063 assert( pFrom
->fg
.isRecursive
==0 );
6064 if( pFrom
->zName
==0 ){
6065 #ifndef SQLITE_OMIT_SUBQUERY
6067 assert( pFrom
->fg
.isSubquery
&& pFrom
->u4
.pSubq
!=0 );
6068 pSel
= pFrom
->u4
.pSubq
->pSelect
;
6069 /* A sub-query in the FROM clause of a SELECT */
6071 assert( pFrom
->pSTab
==0 );
6072 if( sqlite3WalkSelect(pWalker
, pSel
) ) return WRC_Abort
;
6073 if( sqlite3ExpandSubquery(pParse
, pFrom
) ) return WRC_Abort
;
6075 #ifndef SQLITE_OMIT_CTE
6076 }else if( (rc
= resolveFromTermToCte(pParse
, pWalker
, pFrom
))!=0 ){
6077 if( rc
>1 ) return WRC_Abort
;
6078 pTab
= pFrom
->pSTab
;
6082 /* An ordinary table or view name in the FROM clause */
6083 assert( pFrom
->pSTab
==0 );
6084 pFrom
->pSTab
= pTab
= sqlite3LocateTableItem(pParse
, 0, pFrom
);
6085 if( pTab
==0 ) return WRC_Abort
;
6086 if( pTab
->nTabRef
>=0xffff ){
6087 sqlite3ErrorMsg(pParse
, "too many references to \"%s\": max 65535",
6093 if( !IsVirtual(pTab
) && cannotBeFunction(pParse
, pFrom
) ){
6096 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
6097 if( !IsOrdinaryTable(pTab
) ){
6099 u8 eCodeOrig
= pWalker
->eCode
;
6100 if( sqlite3ViewGetColumnNames(pParse
, pTab
) ) return WRC_Abort
;
6101 assert( pFrom
->fg
.isSubquery
==0 );
6103 if( (db
->flags
& SQLITE_EnableView
)==0
6104 && pTab
->pSchema
!=db
->aDb
[1].pSchema
6106 sqlite3ErrorMsg(pParse
, "access to view \"%s\" prohibited",
6109 sqlite3SrcItemAttachSubquery(pParse
, pFrom
, pTab
->u
.view
.pSelect
, 1);
6111 #ifndef SQLITE_OMIT_VIRTUALTABLE
6112 else if( ALWAYS(IsVirtual(pTab
))
6113 && pFrom
->fg
.fromDDL
6114 && ALWAYS(pTab
->u
.vtab
.p
!=0)
6115 && pTab
->u
.vtab
.p
->eVtabRisk
> ((db
->flags
& SQLITE_TrustedSchema
)!=0)
6117 sqlite3ErrorMsg(pParse
, "unsafe use of virtual table \"%s\"",
6120 assert( SQLITE_VTABRISK_Normal
==1 && SQLITE_VTABRISK_High
==2 );
6124 pWalker
->eCode
= 1; /* Turn on Select.selId renumbering */
6125 if( pFrom
->fg
.isSubquery
){
6126 sqlite3WalkSelect(pWalker
, pFrom
->u4
.pSubq
->pSelect
);
6128 pWalker
->eCode
= eCodeOrig
;
6134 /* Locate the index named by the INDEXED BY clause, if any. */
6135 if( pFrom
->fg
.isIndexedBy
&& sqlite3IndexedByLookup(pParse
, pFrom
) ){
6140 /* Process NATURAL keywords, and ON and USING clauses of joins.
6142 assert( db
->mallocFailed
==0 || pParse
->nErr
!=0 );
6143 if( pParse
->nErr
|| sqlite3ProcessJoin(pParse
, p
) ){
6147 /* For every "*" that occurs in the column list, insert the names of
6148 ** all columns in all tables. And for every TABLE.* insert the names
6149 ** of all columns in TABLE. The parser inserted a special expression
6150 ** with the TK_ASTERISK operator for each "*" that it found in the column
6151 ** list. The following code just has to locate the TK_ASTERISK
6152 ** expressions and expand each one to the list of all columns in
6155 ** The first loop just checks to see if there are any "*" operators
6156 ** that need expanding.
6158 for(k
=0; k
<pEList
->nExpr
; k
++){
6159 pE
= pEList
->a
[k
].pExpr
;
6160 if( pE
->op
==TK_ASTERISK
) break;
6161 assert( pE
->op
!=TK_DOT
|| pE
->pRight
!=0 );
6162 assert( pE
->op
!=TK_DOT
|| (pE
->pLeft
!=0 && pE
->pLeft
->op
==TK_ID
) );
6163 if( pE
->op
==TK_DOT
&& pE
->pRight
->op
==TK_ASTERISK
) break;
6164 elistFlags
|= pE
->flags
;
6166 if( k
<pEList
->nExpr
){
6168 ** If we get here it means the result set contains one or more "*"
6169 ** operators that need to be expanded. Loop through each expression
6170 ** in the result set and expand them one by one.
6172 struct ExprList_item
*a
= pEList
->a
;
6174 int flags
= pParse
->db
->flags
;
6175 int longNames
= (flags
& SQLITE_FullColNames
)!=0
6176 && (flags
& SQLITE_ShortColNames
)==0;
6178 for(k
=0; k
<pEList
->nExpr
; k
++){
6180 elistFlags
|= pE
->flags
;
6181 pRight
= pE
->pRight
;
6182 assert( pE
->op
!=TK_DOT
|| pRight
!=0 );
6183 if( pE
->op
!=TK_ASTERISK
6184 && (pE
->op
!=TK_DOT
|| pRight
->op
!=TK_ASTERISK
)
6186 /* This particular expression does not need to be expanded.
6188 pNew
= sqlite3ExprListAppend(pParse
, pNew
, a
[k
].pExpr
);
6190 pNew
->a
[pNew
->nExpr
-1].zEName
= a
[k
].zEName
;
6191 pNew
->a
[pNew
->nExpr
-1].fg
.eEName
= a
[k
].fg
.eEName
;
6196 /* This expression is a "*" or a "TABLE.*" and needs to be
6198 int tableSeen
= 0; /* Set to 1 when TABLE matches */
6199 char *zTName
= 0; /* text of name of TABLE */
6201 if( pE
->op
==TK_DOT
){
6202 assert( (selFlags
& SF_NestedFrom
)==0 );
6203 assert( pE
->pLeft
!=0 );
6204 assert( !ExprHasProperty(pE
->pLeft
, EP_IntValue
) );
6205 zTName
= pE
->pLeft
->u
.zToken
;
6206 assert( ExprUseWOfst(pE
->pLeft
) );
6207 iErrOfst
= pE
->pRight
->w
.iOfst
;
6209 assert( ExprUseWOfst(pE
) );
6210 iErrOfst
= pE
->w
.iOfst
;
6212 for(i
=0, pFrom
=pTabList
->a
; i
<pTabList
->nSrc
; i
++, pFrom
++){
6213 int nAdd
; /* Number of cols including rowid */
6214 Table
*pTab
= pFrom
->pSTab
; /* Table for this data source */
6215 ExprList
*pNestedFrom
; /* Result-set of a nested FROM clause */
6216 char *zTabName
; /* AS name for this data source */
6217 const char *zSchemaName
= 0; /* Schema name for this data source */
6218 int iDb
; /* Schema index for this data src */
6219 IdList
*pUsing
; /* USING clause for pFrom[1] */
6221 if( (zTabName
= pFrom
->zAlias
)==0 ){
6222 zTabName
= pTab
->zName
;
6224 if( db
->mallocFailed
) break;
6225 assert( (int)pFrom
->fg
.isNestedFrom
== IsNestedFrom(pFrom
) );
6226 if( pFrom
->fg
.isNestedFrom
){
6227 assert( pFrom
->fg
.isSubquery
&& pFrom
->u4
.pSubq
);
6228 assert( pFrom
->u4
.pSubq
->pSelect
!=0 );
6229 pNestedFrom
= pFrom
->u4
.pSubq
->pSelect
->pEList
;
6230 assert( pNestedFrom
!=0 );
6231 assert( pNestedFrom
->nExpr
==pTab
->nCol
);
6232 assert( VisibleRowid(pTab
)==0 || ViewCanHaveRowid
);
6234 if( zTName
&& sqlite3StrICmp(zTName
, zTabName
)!=0 ){
6238 iDb
= sqlite3SchemaToIndex(db
, pTab
->pSchema
);
6239 zSchemaName
= iDb
>=0 ? db
->aDb
[iDb
].zDbSName
: "*";
6241 if( i
+1<pTabList
->nSrc
6242 && pFrom
[1].fg
.isUsing
6243 && (selFlags
& SF_NestedFrom
)!=0
6246 pUsing
= pFrom
[1].u3
.pUsing
;
6247 for(ii
=0; ii
<pUsing
->nId
; ii
++){
6248 const char *zUName
= pUsing
->a
[ii
].zName
;
6249 pRight
= sqlite3Expr(db
, TK_ID
, zUName
);
6250 sqlite3ExprSetErrorOffset(pRight
, iErrOfst
);
6251 pNew
= sqlite3ExprListAppend(pParse
, pNew
, pRight
);
6253 struct ExprList_item
*pX
= &pNew
->a
[pNew
->nExpr
-1];
6254 assert( pX
->zEName
==0 );
6255 pX
->zEName
= sqlite3MPrintf(db
,"..%s", zUName
);
6256 pX
->fg
.eEName
= ENAME_TAB
;
6257 pX
->fg
.bUsingTerm
= 1;
6265 if( VisibleRowid(pTab
) && (selFlags
& SF_NestedFrom
)!=0 ) nAdd
++;
6266 for(j
=0; j
<nAdd
; j
++){
6268 struct ExprList_item
*pX
; /* Newly added ExprList term */
6270 if( j
==pTab
->nCol
){
6271 zName
= sqlite3RowidAlias(pTab
);
6272 if( zName
==0 ) continue;
6274 zName
= pTab
->aCol
[j
].zCnName
;
6276 /* If pTab is actually an SF_NestedFrom sub-select, do not
6277 ** expand any ENAME_ROWID columns. */
6278 if( pNestedFrom
&& pNestedFrom
->a
[j
].fg
.eEName
==ENAME_ROWID
){
6284 && sqlite3MatchEName(&pNestedFrom
->a
[j
], 0, zTName
, 0, 0)==0
6289 /* If a column is marked as 'hidden', omit it from the expanded
6290 ** result-set list unless the SELECT has the SF_IncludeHidden
6293 if( (p
->selFlags
& SF_IncludeHidden
)==0
6294 && IsHiddenColumn(&pTab
->aCol
[j
])
6298 if( (pTab
->aCol
[j
].colFlags
& COLFLAG_NOEXPAND
)!=0
6300 && (selFlags
& (SF_NestedFrom
))==0
6308 if( i
>0 && zTName
==0 && (selFlags
& SF_NestedFrom
)==0 ){
6309 if( pFrom
->fg
.isUsing
6310 && sqlite3IdListIndex(pFrom
->u3
.pUsing
, zName
)>=0
6312 /* In a join with a USING clause, omit columns in the
6313 ** using clause from the table on the right. */
6317 pRight
= sqlite3Expr(db
, TK_ID
, zName
);
6318 if( (pTabList
->nSrc
>1
6319 && ( (pFrom
->fg
.jointype
& JT_LTORJ
)==0
6320 || (selFlags
& SF_NestedFrom
)!=0
6321 || !inAnyUsingClause(zName
,pFrom
,pTabList
->nSrc
-i
-1)
6327 pLeft
= sqlite3Expr(db
, TK_ID
, zTabName
);
6328 pExpr
= sqlite3PExpr(pParse
, TK_DOT
, pLeft
, pRight
);
6329 if( IN_RENAME_OBJECT
&& pE
->pLeft
){
6330 sqlite3RenameTokenRemap(pParse
, pLeft
, pE
->pLeft
);
6333 pLeft
= sqlite3Expr(db
, TK_ID
, zSchemaName
);
6334 pExpr
= sqlite3PExpr(pParse
, TK_DOT
, pLeft
, pExpr
);
6339 sqlite3ExprSetErrorOffset(pExpr
, iErrOfst
);
6340 pNew
= sqlite3ExprListAppend(pParse
, pNew
, pExpr
);
6344 pX
= &pNew
->a
[pNew
->nExpr
-1];
6345 assert( pX
->zEName
==0 );
6346 if( (selFlags
& SF_NestedFrom
)!=0 && !IN_RENAME_OBJECT
){
6347 if( pNestedFrom
&& (!ViewCanHaveRowid
|| j
<pNestedFrom
->nExpr
) ){
6348 assert( j
<pNestedFrom
->nExpr
);
6349 pX
->zEName
= sqlite3DbStrDup(db
, pNestedFrom
->a
[j
].zEName
);
6350 testcase( pX
->zEName
==0 );
6352 pX
->zEName
= sqlite3MPrintf(db
, "%s.%s.%s",
6353 zSchemaName
, zTabName
, zName
);
6354 testcase( pX
->zEName
==0 );
6356 pX
->fg
.eEName
= (j
==pTab
->nCol
? ENAME_ROWID
: ENAME_TAB
);
6357 if( (pFrom
->fg
.isUsing
6358 && sqlite3IdListIndex(pFrom
->u3
.pUsing
, zName
)>=0)
6359 || (pUsing
&& sqlite3IdListIndex(pUsing
, zName
)>=0)
6360 || (j
<pTab
->nCol
&& (pTab
->aCol
[j
].colFlags
& COLFLAG_NOEXPAND
))
6362 pX
->fg
.bNoExpand
= 1;
6364 }else if( longNames
){
6365 pX
->zEName
= sqlite3MPrintf(db
, "%s.%s", zTabName
, zName
);
6366 pX
->fg
.eEName
= ENAME_NAME
;
6368 pX
->zEName
= sqlite3DbStrDup(db
, zName
);
6369 pX
->fg
.eEName
= ENAME_NAME
;
6375 sqlite3ErrorMsg(pParse
, "no such table: %s", zTName
);
6377 sqlite3ErrorMsg(pParse
, "no tables specified");
6382 sqlite3ExprListDelete(db
, pEList
);
6386 if( p
->pEList
->nExpr
>db
->aLimit
[SQLITE_LIMIT_COLUMN
] ){
6387 sqlite3ErrorMsg(pParse
, "too many columns in result set");
6390 if( (elistFlags
& (EP_HasFunc
|EP_Subquery
))!=0 ){
6391 p
->selFlags
|= SF_ComplexResult
;
6394 #if TREETRACE_ENABLED
6395 if( sqlite3TreeTrace
& 0x8 ){
6396 TREETRACE(0x8,pParse
,p
,("After result-set wildcard expansion:\n"));
6397 sqlite3TreeViewSelect(0, p
, 0);
6400 return WRC_Continue
;
6405 ** Always assert. This xSelectCallback2 implementation proves that the
6406 ** xSelectCallback2 is never invoked.
6408 void sqlite3SelectWalkAssert2(Walker
*NotUsed
, Select
*NotUsed2
){
6409 UNUSED_PARAMETER2(NotUsed
, NotUsed2
);
6414 ** This routine "expands" a SELECT statement and all of its subqueries.
6415 ** For additional information on what it means to "expand" a SELECT
6416 ** statement, see the comment on the selectExpand worker callback above.
6418 ** Expanding a SELECT statement is the first step in processing a
6419 ** SELECT statement. The SELECT statement must be expanded before
6420 ** name resolution is performed.
6422 ** If anything goes wrong, an error message is written into pParse.
6423 ** The calling function can detect the problem by looking at pParse->nErr
6424 ** and/or pParse->db->mallocFailed.
6426 static void sqlite3SelectExpand(Parse
*pParse
, Select
*pSelect
){
6428 w
.xExprCallback
= sqlite3ExprWalkNoop
;
6430 if( OK_IF_ALWAYS_TRUE(pParse
->hasCompound
) ){
6431 w
.xSelectCallback
= convertCompoundSelectToSubquery
;
6432 w
.xSelectCallback2
= 0;
6433 sqlite3WalkSelect(&w
, pSelect
);
6435 w
.xSelectCallback
= selectExpander
;
6436 w
.xSelectCallback2
= sqlite3SelectPopWith
;
6438 sqlite3WalkSelect(&w
, pSelect
);
6442 #ifndef SQLITE_OMIT_SUBQUERY
6444 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
6447 ** For each FROM-clause subquery, add Column.zType, Column.zColl, and
6448 ** Column.affinity information to the Table structure that represents
6449 ** the result set of that subquery.
6451 ** The Table structure that represents the result set was constructed
6452 ** by selectExpander() but the type and collation and affinity information
6453 ** was omitted at that point because identifiers had not yet been resolved.
6454 ** This routine is called after identifier resolution.
6456 static void selectAddSubqueryTypeInfo(Walker
*pWalker
, Select
*p
){
6462 if( p
->selFlags
& SF_HasTypeInfo
) return;
6463 p
->selFlags
|= SF_HasTypeInfo
;
6464 pParse
= pWalker
->pParse
;
6465 assert( (p
->selFlags
& SF_Resolved
) );
6467 for(i
=0, pFrom
=pTabList
->a
; i
<pTabList
->nSrc
; i
++, pFrom
++){
6468 Table
*pTab
= pFrom
->pSTab
;
6470 if( (pTab
->tabFlags
& TF_Ephemeral
)!=0 && pFrom
->fg
.isSubquery
){
6471 /* A sub-query in the FROM clause of a SELECT */
6472 Select
*pSel
= pFrom
->u4
.pSubq
->pSelect
;
6473 sqlite3SubqueryColumnTypes(pParse
, pTab
, pSel
, SQLITE_AFF_NONE
);
6481 ** This routine adds datatype and collating sequence information to
6482 ** the Table structures of all FROM-clause subqueries in a
6483 ** SELECT statement.
6485 ** Use this routine after name resolution.
6487 static void sqlite3SelectAddTypeInfo(Parse
*pParse
, Select
*pSelect
){
6488 #ifndef SQLITE_OMIT_SUBQUERY
6490 w
.xSelectCallback
= sqlite3SelectWalkNoop
;
6491 w
.xSelectCallback2
= selectAddSubqueryTypeInfo
;
6492 w
.xExprCallback
= sqlite3ExprWalkNoop
;
6494 sqlite3WalkSelect(&w
, pSelect
);
6500 ** This routine sets up a SELECT statement for processing. The
6501 ** following is accomplished:
6503 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
6504 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
6505 ** * ON and USING clauses are shifted into WHERE statements
6506 ** * Wildcards "*" and "TABLE.*" in result sets are expanded.
6507 ** * Identifiers in expression are matched to tables.
6509 ** This routine acts recursively on all subqueries within the SELECT.
6511 void sqlite3SelectPrep(
6512 Parse
*pParse
, /* The parser context */
6513 Select
*p
, /* The SELECT statement being coded. */
6514 NameContext
*pOuterNC
/* Name context for container */
6516 assert( p
!=0 || pParse
->db
->mallocFailed
);
6517 assert( pParse
->db
->pParse
==pParse
);
6518 if( pParse
->db
->mallocFailed
) return;
6519 if( p
->selFlags
& SF_HasTypeInfo
) return;
6520 sqlite3SelectExpand(pParse
, p
);
6521 if( pParse
->nErr
) return;
6522 sqlite3ResolveSelectNames(pParse
, p
, pOuterNC
);
6523 if( pParse
->nErr
) return;
6524 sqlite3SelectAddTypeInfo(pParse
, p
);
6527 #if TREETRACE_ENABLED
6529 ** Display all information about an AggInfo object
6531 static void printAggInfo(AggInfo
*pAggInfo
){
6533 sqlite3DebugPrintf("AggInfo %d/%p:\n",
6534 pAggInfo
->selId
, pAggInfo
);
6535 for(ii
=0; ii
<pAggInfo
->nColumn
; ii
++){
6536 struct AggInfo_col
*pCol
= &pAggInfo
->aCol
[ii
];
6538 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
6539 " iSorterColumn=%d %s\n",
6540 ii
, pCol
->pTab
? pCol
->pTab
->zName
: "NULL",
6541 pCol
->iTable
, pCol
->iColumn
, pAggInfo
->iFirstReg
+ii
,
6542 pCol
->iSorterColumn
,
6543 ii
>=pAggInfo
->nAccumulator
? "" : " Accumulator");
6544 sqlite3TreeViewExpr(0, pAggInfo
->aCol
[ii
].pCExpr
, 0);
6546 for(ii
=0; ii
<pAggInfo
->nFunc
; ii
++){
6547 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
6548 ii
, pAggInfo
->iFirstReg
+pAggInfo
->nColumn
+ii
);
6549 sqlite3TreeViewExpr(0, pAggInfo
->aFunc
[ii
].pFExpr
, 0);
6552 #endif /* TREETRACE_ENABLED */
6555 ** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[]
6556 ** entries for columns that are arguments to aggregate functions but which
6557 ** are not otherwise used.
6559 ** The aCol[] entries in AggInfo prior to nAccumulator are columns that
6560 ** are referenced outside of aggregate functions. These might be columns
6561 ** that are part of the GROUP by clause, for example. Other database engines
6562 ** would throw an error if there is a column reference that is not in the
6563 ** GROUP BY clause and that is not part of an aggregate function argument.
6564 ** But SQLite allows this.
6566 ** The aCol[] entries beginning with the aCol[nAccumulator] and following
6567 ** are column references that are used exclusively as arguments to
6568 ** aggregate functions. This routine is responsible for computing
6569 ** (or recomputing) those aCol[] entries.
6571 static void analyzeAggFuncArgs(
6576 assert( pAggInfo
!=0 );
6577 assert( pAggInfo
->iFirstReg
==0 );
6578 pNC
->ncFlags
|= NC_InAggFunc
;
6579 for(i
=0; i
<pAggInfo
->nFunc
; i
++){
6580 Expr
*pExpr
= pAggInfo
->aFunc
[i
].pFExpr
;
6581 assert( pExpr
->op
==TK_FUNCTION
|| pExpr
->op
==TK_AGG_FUNCTION
);
6582 assert( ExprUseXList(pExpr
) );
6583 sqlite3ExprAnalyzeAggList(pNC
, pExpr
->x
.pList
);
6585 assert( pExpr
->pLeft
->op
==TK_ORDER
);
6586 assert( ExprUseXList(pExpr
->pLeft
) );
6587 sqlite3ExprAnalyzeAggList(pNC
, pExpr
->pLeft
->x
.pList
);
6589 #ifndef SQLITE_OMIT_WINDOWFUNC
6590 assert( !IsWindowFunc(pExpr
) );
6591 if( ExprHasProperty(pExpr
, EP_WinFunc
) ){
6592 sqlite3ExprAnalyzeAggregates(pNC
, pExpr
->y
.pWin
->pFilter
);
6596 pNC
->ncFlags
&= ~NC_InAggFunc
;
6600 ** An index on expressions is being used in the inner loop of an
6601 ** aggregate query with a GROUP BY clause. This routine attempts
6602 ** to adjust the AggInfo object to take advantage of index and to
6603 ** perhaps use the index as a covering index.
6606 static void optimizeAggregateUseOfIndexedExpr(
6607 Parse
*pParse
, /* Parsing context */
6608 Select
*pSelect
, /* The SELECT statement being processed */
6609 AggInfo
*pAggInfo
, /* The aggregate info */
6610 NameContext
*pNC
/* Name context used to resolve agg-func args */
6612 assert( pAggInfo
->iFirstReg
==0 );
6613 assert( pSelect
!=0 );
6614 assert( pSelect
->pGroupBy
!=0 );
6615 pAggInfo
->nColumn
= pAggInfo
->nAccumulator
;
6616 if( ALWAYS(pAggInfo
->nSortingColumn
>0) ){
6617 int mx
= pSelect
->pGroupBy
->nExpr
- 1;
6619 for(j
=0; j
<pAggInfo
->nColumn
; j
++){
6620 k
= pAggInfo
->aCol
[j
].iSorterColumn
;
6623 pAggInfo
->nSortingColumn
= mx
+1;
6625 analyzeAggFuncArgs(pAggInfo
, pNC
);
6626 #if TREETRACE_ENABLED
6627 if( sqlite3TreeTrace
& 0x20 ){
6629 TREETRACE(0x20, pParse
, pSelect
,
6630 ("AggInfo (possibly) adjusted for Indexed Exprs\n"));
6631 sqlite3TreeViewSelect(0, pSelect
, 0);
6632 for(pIEpr
=pParse
->pIdxEpr
; pIEpr
; pIEpr
=pIEpr
->pIENext
){
6633 printf("data-cursor=%d index={%d,%d}\n",
6634 pIEpr
->iDataCur
, pIEpr
->iIdxCur
, pIEpr
->iIdxCol
);
6635 sqlite3TreeViewExpr(0, pIEpr
->pExpr
, 0);
6637 printAggInfo(pAggInfo
);
6640 UNUSED_PARAMETER(pSelect
);
6641 UNUSED_PARAMETER(pParse
);
6646 ** Walker callback for aggregateConvertIndexedExprRefToColumn().
6648 static int aggregateIdxEprRefToColCallback(Walker
*pWalker
, Expr
*pExpr
){
6650 struct AggInfo_col
*pCol
;
6651 UNUSED_PARAMETER(pWalker
);
6652 if( pExpr
->pAggInfo
==0 ) return WRC_Continue
;
6653 if( pExpr
->op
==TK_AGG_COLUMN
) return WRC_Continue
;
6654 if( pExpr
->op
==TK_AGG_FUNCTION
) return WRC_Continue
;
6655 if( pExpr
->op
==TK_IF_NULL_ROW
) return WRC_Continue
;
6656 pAggInfo
= pExpr
->pAggInfo
;
6657 if( NEVER(pExpr
->iAgg
>=pAggInfo
->nColumn
) ) return WRC_Continue
;
6658 assert( pExpr
->iAgg
>=0 );
6659 pCol
= &pAggInfo
->aCol
[pExpr
->iAgg
];
6660 pExpr
->op
= TK_AGG_COLUMN
;
6661 pExpr
->iTable
= pCol
->iTable
;
6662 pExpr
->iColumn
= pCol
->iColumn
;
6663 ExprClearProperty(pExpr
, EP_Skip
|EP_Collate
|EP_Unlikely
);
6668 ** Convert every pAggInfo->aFunc[].pExpr such that any node within
6669 ** those expressions that has pAppInfo set is changed into a TK_AGG_COLUMN
6672 static void aggregateConvertIndexedExprRefToColumn(AggInfo
*pAggInfo
){
6675 memset(&w
, 0, sizeof(w
));
6676 w
.xExprCallback
= aggregateIdxEprRefToColCallback
;
6677 for(i
=0; i
<pAggInfo
->nFunc
; i
++){
6678 sqlite3WalkExpr(&w
, pAggInfo
->aFunc
[i
].pFExpr
);
6684 ** Allocate a block of registers so that there is one register for each
6685 ** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first
6686 ** register in this block is stored in pAggInfo->iFirstReg.
6688 ** This routine may only be called once for each AggInfo object. Prior
6689 ** to calling this routine:
6691 ** * The aCol[] and aFunc[] arrays may be modified
6692 ** * The AggInfoColumnReg() and AggInfoFuncReg() macros may not be used
6694 ** After calling this routine:
6696 ** * The aCol[] and aFunc[] arrays are fixed
6697 ** * The AggInfoColumnReg() and AggInfoFuncReg() macros may be used
6700 static void assignAggregateRegisters(Parse
*pParse
, AggInfo
*pAggInfo
){
6701 assert( pAggInfo
!=0 );
6702 assert( pAggInfo
->iFirstReg
==0 );
6703 pAggInfo
->iFirstReg
= pParse
->nMem
+ 1;
6704 pParse
->nMem
+= pAggInfo
->nColumn
+ pAggInfo
->nFunc
;
6708 ** Reset the aggregate accumulator.
6710 ** The aggregate accumulator is a set of memory cells that hold
6711 ** intermediate results while calculating an aggregate. This
6712 ** routine generates code that stores NULLs in all of those memory
6715 static void resetAccumulator(Parse
*pParse
, AggInfo
*pAggInfo
){
6716 Vdbe
*v
= pParse
->pVdbe
;
6718 struct AggInfo_func
*pFunc
;
6719 int nReg
= pAggInfo
->nFunc
+ pAggInfo
->nColumn
;
6720 assert( pAggInfo
->iFirstReg
>0 );
6721 assert( pParse
->db
->pParse
==pParse
);
6722 assert( pParse
->db
->mallocFailed
==0 || pParse
->nErr
!=0 );
6723 if( nReg
==0 ) return;
6724 if( pParse
->nErr
) return;
6725 sqlite3VdbeAddOp3(v
, OP_Null
, 0, pAggInfo
->iFirstReg
,
6726 pAggInfo
->iFirstReg
+nReg
-1);
6727 for(pFunc
=pAggInfo
->aFunc
, i
=0; i
<pAggInfo
->nFunc
; i
++, pFunc
++){
6728 if( pFunc
->iDistinct
>=0 ){
6729 Expr
*pE
= pFunc
->pFExpr
;
6730 assert( ExprUseXList(pE
) );
6731 if( pE
->x
.pList
==0 || pE
->x
.pList
->nExpr
!=1 ){
6732 sqlite3ErrorMsg(pParse
, "DISTINCT aggregates must have exactly one "
6734 pFunc
->iDistinct
= -1;
6736 KeyInfo
*pKeyInfo
= sqlite3KeyInfoFromExprList(pParse
, pE
->x
.pList
,0,0);
6737 pFunc
->iDistAddr
= sqlite3VdbeAddOp4(v
, OP_OpenEphemeral
,
6738 pFunc
->iDistinct
, 0, 0, (char*)pKeyInfo
, P4_KEYINFO
);
6739 ExplainQueryPlan((pParse
, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
6740 pFunc
->pFunc
->zName
));
6743 if( pFunc
->iOBTab
>=0 ){
6747 assert( pFunc
->pFExpr
->pLeft
!=0 );
6748 assert( pFunc
->pFExpr
->pLeft
->op
==TK_ORDER
);
6749 assert( ExprUseXList(pFunc
->pFExpr
->pLeft
) );
6750 assert( pFunc
->pFunc
!=0 );
6751 pOBList
= pFunc
->pFExpr
->pLeft
->x
.pList
;
6752 if( !pFunc
->bOBUnique
){
6753 nExtra
++; /* One extra column for the OP_Sequence */
6755 if( pFunc
->bOBPayload
){
6756 /* extra columns for the function arguments */
6757 assert( ExprUseXList(pFunc
->pFExpr
) );
6758 nExtra
+= pFunc
->pFExpr
->x
.pList
->nExpr
;
6760 if( pFunc
->bUseSubtype
){
6761 nExtra
+= pFunc
->pFExpr
->x
.pList
->nExpr
;
6763 pKeyInfo
= sqlite3KeyInfoFromExprList(pParse
, pOBList
, 0, nExtra
);
6764 if( !pFunc
->bOBUnique
&& pParse
->nErr
==0 ){
6765 pKeyInfo
->nKeyField
++;
6767 sqlite3VdbeAddOp4(v
, OP_OpenEphemeral
,
6768 pFunc
->iOBTab
, pOBList
->nExpr
+nExtra
, 0,
6769 (char*)pKeyInfo
, P4_KEYINFO
);
6770 ExplainQueryPlan((pParse
, 0, "USE TEMP B-TREE FOR %s(ORDER BY)",
6771 pFunc
->pFunc
->zName
));
6777 ** Invoke the OP_AggFinalize opcode for every aggregate function
6778 ** in the AggInfo structure.
6780 static void finalizeAggFunctions(Parse
*pParse
, AggInfo
*pAggInfo
){
6781 Vdbe
*v
= pParse
->pVdbe
;
6783 struct AggInfo_func
*pF
;
6784 for(i
=0, pF
=pAggInfo
->aFunc
; i
<pAggInfo
->nFunc
; i
++, pF
++){
6786 assert( ExprUseXList(pF
->pFExpr
) );
6787 if( pParse
->nErr
) return;
6788 pList
= pF
->pFExpr
->x
.pList
;
6789 if( pF
->iOBTab
>=0 ){
6790 /* For an ORDER BY aggregate, calls to OP_AggStep were deferred. Inputs
6791 ** were stored in emphermal table pF->iOBTab. Here, we extract those
6792 ** inputs (in ORDER BY order) and make all calls to OP_AggStep
6793 ** before doing the OP_AggFinal call. */
6794 int iTop
; /* Start of loop for extracting columns */
6795 int nArg
; /* Number of columns to extract */
6796 int nKey
; /* Key columns to be skipped */
6797 int regAgg
; /* Extract into this array */
6798 int j
; /* Loop counter */
6800 assert( pF
->pFunc
!=0 );
6801 nArg
= pList
->nExpr
;
6802 regAgg
= sqlite3GetTempRange(pParse
, nArg
);
6804 if( pF
->bOBPayload
==0 ){
6807 assert( pF
->pFExpr
->pLeft
!=0 );
6808 assert( ExprUseXList(pF
->pFExpr
->pLeft
) );
6809 assert( pF
->pFExpr
->pLeft
->x
.pList
!=0 );
6810 nKey
= pF
->pFExpr
->pLeft
->x
.pList
->nExpr
;
6811 if( ALWAYS(!pF
->bOBUnique
) ) nKey
++;
6813 iTop
= sqlite3VdbeAddOp1(v
, OP_Rewind
, pF
->iOBTab
); VdbeCoverage(v
);
6814 for(j
=nArg
-1; j
>=0; j
--){
6815 sqlite3VdbeAddOp3(v
, OP_Column
, pF
->iOBTab
, nKey
+j
, regAgg
+j
);
6817 if( pF
->bUseSubtype
){
6818 int regSubtype
= sqlite3GetTempReg(pParse
);
6819 int iBaseCol
= nKey
+ nArg
+ (pF
->bOBPayload
==0 && pF
->bOBUnique
==0);
6820 for(j
=nArg
-1; j
>=0; j
--){
6821 sqlite3VdbeAddOp3(v
, OP_Column
, pF
->iOBTab
, iBaseCol
+j
, regSubtype
);
6822 sqlite3VdbeAddOp2(v
, OP_SetSubtype
, regSubtype
, regAgg
+j
);
6824 sqlite3ReleaseTempReg(pParse
, regSubtype
);
6826 sqlite3VdbeAddOp3(v
, OP_AggStep
, 0, regAgg
, AggInfoFuncReg(pAggInfo
,i
));
6827 sqlite3VdbeAppendP4(v
, pF
->pFunc
, P4_FUNCDEF
);
6828 sqlite3VdbeChangeP5(v
, (u8
)nArg
);
6829 sqlite3VdbeAddOp2(v
, OP_Next
, pF
->iOBTab
, iTop
+1); VdbeCoverage(v
);
6830 sqlite3VdbeJumpHere(v
, iTop
);
6831 sqlite3ReleaseTempRange(pParse
, regAgg
, nArg
);
6833 sqlite3VdbeAddOp2(v
, OP_AggFinal
, AggInfoFuncReg(pAggInfo
,i
),
6834 pList
? pList
->nExpr
: 0);
6835 sqlite3VdbeAppendP4(v
, pF
->pFunc
, P4_FUNCDEF
);
6840 ** Generate code that will update the accumulator memory cells for an
6841 ** aggregate based on the current cursor position.
6843 ** If regAcc is non-zero and there are no min() or max() aggregates
6844 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
6845 ** registers if register regAcc contains 0. The caller will take care
6846 ** of setting and clearing regAcc.
6848 ** For an ORDER BY aggregate, the actual accumulator memory cell update
6849 ** is deferred until after all input rows have been received, so that they
6850 ** can be run in the requested order. In that case, instead of invoking
6851 ** OP_AggStep to update the accumulator, just add the arguments that would
6852 ** have been passed into OP_AggStep into the sorting ephemeral table
6853 ** (along with the appropriate sort key).
6855 static void updateAccumulator(
6861 Vdbe
*v
= pParse
->pVdbe
;
6864 int addrHitTest
= 0;
6865 struct AggInfo_func
*pF
;
6866 struct AggInfo_col
*pC
;
6868 assert( pAggInfo
->iFirstReg
>0 );
6869 if( pParse
->nErr
) return;
6870 pAggInfo
->directMode
= 1;
6871 for(i
=0, pF
=pAggInfo
->aFunc
; i
<pAggInfo
->nFunc
; i
++, pF
++){
6876 int regDistinct
= 0;
6878 assert( ExprUseXList(pF
->pFExpr
) );
6879 assert( !IsWindowFunc(pF
->pFExpr
) );
6880 assert( pF
->pFunc
!=0 );
6881 pList
= pF
->pFExpr
->x
.pList
;
6882 if( ExprHasProperty(pF
->pFExpr
, EP_WinFunc
) ){
6883 Expr
*pFilter
= pF
->pFExpr
->y
.pWin
->pFilter
;
6884 if( pAggInfo
->nAccumulator
6885 && (pF
->pFunc
->funcFlags
& SQLITE_FUNC_NEEDCOLL
)
6888 /* If regAcc==0, there there exists some min() or max() function
6889 ** without a FILTER clause that will ensure the magnet registers
6890 ** are populated. */
6891 if( regHit
==0 ) regHit
= ++pParse
->nMem
;
6892 /* If this is the first row of the group (regAcc contains 0), clear the
6893 ** "magnet" register regHit so that the accumulator registers
6894 ** are populated if the FILTER clause jumps over the the
6895 ** invocation of min() or max() altogether. Or, if this is not
6896 ** the first row (regAcc contains 1), set the magnet register so that
6897 ** the accumulators are not populated unless the min()/max() is invoked
6898 ** and indicates that they should be. */
6899 sqlite3VdbeAddOp2(v
, OP_Copy
, regAcc
, regHit
);
6901 addrNext
= sqlite3VdbeMakeLabel(pParse
);
6902 sqlite3ExprIfFalse(pParse
, pFilter
, addrNext
, SQLITE_JUMPIFNULL
);
6904 if( pF
->iOBTab
>=0 ){
6905 /* Instead of invoking AggStep, we must push the arguments that would
6906 ** have been passed to AggStep onto the sorting table. */
6907 int jj
; /* Registered used so far in building the record */
6908 ExprList
*pOBList
; /* The ORDER BY clause */
6910 nArg
= pList
->nExpr
;
6912 assert( pF
->pFExpr
->pLeft
!=0 );
6913 assert( pF
->pFExpr
->pLeft
->op
==TK_ORDER
);
6914 assert( ExprUseXList(pF
->pFExpr
->pLeft
) );
6915 pOBList
= pF
->pFExpr
->pLeft
->x
.pList
;
6916 assert( pOBList
!=0 );
6917 assert( pOBList
->nExpr
>0 );
6918 regAggSz
= pOBList
->nExpr
;
6919 if( !pF
->bOBUnique
){
6920 regAggSz
++; /* One register for OP_Sequence */
6922 if( pF
->bOBPayload
){
6925 if( pF
->bUseSubtype
){
6928 regAggSz
++; /* One extra register to hold result of MakeRecord */
6929 regAgg
= sqlite3GetTempRange(pParse
, regAggSz
);
6930 regDistinct
= regAgg
;
6931 sqlite3ExprCodeExprList(pParse
, pOBList
, regAgg
, 0, SQLITE_ECEL_DUP
);
6932 jj
= pOBList
->nExpr
;
6933 if( !pF
->bOBUnique
){
6934 sqlite3VdbeAddOp2(v
, OP_Sequence
, pF
->iOBTab
, regAgg
+jj
);
6937 if( pF
->bOBPayload
){
6938 regDistinct
= regAgg
+jj
;
6939 sqlite3ExprCodeExprList(pParse
, pList
, regDistinct
, 0, SQLITE_ECEL_DUP
);
6942 if( pF
->bUseSubtype
){
6944 int regBase
= pF
->bOBPayload
? regDistinct
: regAgg
;
6945 for(kk
=0; kk
<nArg
; kk
++, jj
++){
6946 sqlite3VdbeAddOp2(v
, OP_GetSubtype
, regBase
+kk
, regAgg
+jj
);
6950 nArg
= pList
->nExpr
;
6951 regAgg
= sqlite3GetTempRange(pParse
, nArg
);
6952 regDistinct
= regAgg
;
6953 sqlite3ExprCodeExprList(pParse
, pList
, regAgg
, 0, SQLITE_ECEL_DUP
);
6958 if( pF
->iDistinct
>=0 && pList
){
6960 addrNext
= sqlite3VdbeMakeLabel(pParse
);
6962 pF
->iDistinct
= codeDistinct(pParse
, eDistinctType
,
6963 pF
->iDistinct
, addrNext
, pList
, regDistinct
);
6965 if( pF
->iOBTab
>=0 ){
6966 /* Insert a new record into the ORDER BY table */
6967 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regAgg
, regAggSz
-1,
6969 sqlite3VdbeAddOp4Int(v
, OP_IdxInsert
, pF
->iOBTab
, regAgg
+regAggSz
-1,
6970 regAgg
, regAggSz
-1);
6971 sqlite3ReleaseTempRange(pParse
, regAgg
, regAggSz
);
6973 /* Invoke the AggStep function */
6974 if( pF
->pFunc
->funcFlags
& SQLITE_FUNC_NEEDCOLL
){
6976 struct ExprList_item
*pItem
;
6978 assert( pList
!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
6979 for(j
=0, pItem
=pList
->a
; !pColl
&& j
<nArg
; j
++, pItem
++){
6980 pColl
= sqlite3ExprCollSeq(pParse
, pItem
->pExpr
);
6983 pColl
= pParse
->db
->pDfltColl
;
6985 if( regHit
==0 && pAggInfo
->nAccumulator
) regHit
= ++pParse
->nMem
;
6986 sqlite3VdbeAddOp4(v
, OP_CollSeq
, regHit
, 0, 0,
6987 (char *)pColl
, P4_COLLSEQ
);
6989 sqlite3VdbeAddOp3(v
, OP_AggStep
, 0, regAgg
, AggInfoFuncReg(pAggInfo
,i
));
6990 sqlite3VdbeAppendP4(v
, pF
->pFunc
, P4_FUNCDEF
);
6991 sqlite3VdbeChangeP5(v
, (u8
)nArg
);
6992 sqlite3ReleaseTempRange(pParse
, regAgg
, nArg
);
6995 sqlite3VdbeResolveLabel(v
, addrNext
);
6997 if( pParse
->nErr
) return;
6999 if( regHit
==0 && pAggInfo
->nAccumulator
){
7003 addrHitTest
= sqlite3VdbeAddOp1(v
, OP_If
, regHit
); VdbeCoverage(v
);
7005 for(i
=0, pC
=pAggInfo
->aCol
; i
<pAggInfo
->nAccumulator
; i
++, pC
++){
7006 sqlite3ExprCode(pParse
, pC
->pCExpr
, AggInfoColumnReg(pAggInfo
,i
));
7007 if( pParse
->nErr
) return;
7010 pAggInfo
->directMode
= 0;
7012 sqlite3VdbeJumpHereOrPopInst(v
, addrHitTest
);
7017 ** Add a single OP_Explain instruction to the VDBE to explain a simple
7018 ** count(*) query ("SELECT count(*) FROM pTab").
7020 #ifndef SQLITE_OMIT_EXPLAIN
7021 static void explainSimpleCount(
7022 Parse
*pParse
, /* Parse context */
7023 Table
*pTab
, /* Table being queried */
7024 Index
*pIdx
/* Index used to optimize scan, or NULL */
7026 if( pParse
->explain
==2 ){
7027 int bCover
= (pIdx
!=0 && (HasRowid(pTab
) || !IsPrimaryKeyIndex(pIdx
)));
7028 sqlite3VdbeExplain(pParse
, 0, "SCAN %s%s%s",
7030 bCover
? " USING COVERING INDEX " : "",
7031 bCover
? pIdx
->zName
: ""
7036 # define explainSimpleCount(a,b,c)
7040 ** sqlite3WalkExpr() callback used by havingToWhere().
7042 ** If the node passed to the callback is a TK_AND node, return
7043 ** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
7045 ** Otherwise, return WRC_Prune. In this case, also check if the
7046 ** sub-expression matches the criteria for being moved to the WHERE
7047 ** clause. If so, add it to the WHERE clause and replace the sub-expression
7048 ** within the HAVING expression with a constant "1".
7050 static int havingToWhereExprCb(Walker
*pWalker
, Expr
*pExpr
){
7051 if( pExpr
->op
!=TK_AND
){
7052 Select
*pS
= pWalker
->u
.pSelect
;
7053 /* This routine is called before the HAVING clause of the current
7054 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
7055 ** here, it indicates that the expression is a correlated reference to a
7056 ** column from an outer aggregate query, or an aggregate function that
7057 ** belongs to an outer query. Do not move the expression to the WHERE
7058 ** clause in this obscure case, as doing so may corrupt the outer Select
7059 ** statements AggInfo structure. */
7060 if( sqlite3ExprIsConstantOrGroupBy(pWalker
->pParse
, pExpr
, pS
->pGroupBy
)
7061 && ExprAlwaysFalse(pExpr
)==0
7062 && pExpr
->pAggInfo
==0
7064 sqlite3
*db
= pWalker
->pParse
->db
;
7065 Expr
*pNew
= sqlite3Expr(db
, TK_INTEGER
, "1");
7067 Expr
*pWhere
= pS
->pWhere
;
7068 SWAP(Expr
, *pNew
, *pExpr
);
7069 pNew
= sqlite3ExprAnd(pWalker
->pParse
, pWhere
, pNew
);
7076 return WRC_Continue
;
7080 ** Transfer eligible terms from the HAVING clause of a query, which is
7081 ** processed after grouping, to the WHERE clause, which is processed before
7082 ** grouping. For example, the query:
7084 ** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
7086 ** can be rewritten as:
7088 ** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
7090 ** A term of the HAVING expression is eligible for transfer if it consists
7091 ** entirely of constants and expressions that are also GROUP BY terms that
7092 ** use the "BINARY" collation sequence.
7094 static void havingToWhere(Parse
*pParse
, Select
*p
){
7096 memset(&sWalker
, 0, sizeof(sWalker
));
7097 sWalker
.pParse
= pParse
;
7098 sWalker
.xExprCallback
= havingToWhereExprCb
;
7099 sWalker
.u
.pSelect
= p
;
7100 sqlite3WalkExpr(&sWalker
, p
->pHaving
);
7101 #if TREETRACE_ENABLED
7102 if( sWalker
.eCode
&& (sqlite3TreeTrace
& 0x100)!=0 ){
7103 TREETRACE(0x100,pParse
,p
,("Move HAVING terms into WHERE:\n"));
7104 sqlite3TreeViewSelect(0, p
, 0);
7110 ** Check to see if the pThis entry of pTabList is a self-join of another view.
7111 ** Search FROM-clause entries in the range of iFirst..iEnd, including iFirst
7112 ** but stopping before iEnd.
7114 ** If pThis is a self-join, then return the SrcItem for the first other
7115 ** instance of that view found. If pThis is not a self-join then return 0.
7117 static SrcItem
*isSelfJoinView(
7118 SrcList
*pTabList
, /* Search for self-joins in this FROM clause */
7119 SrcItem
*pThis
, /* Search for prior reference to this subquery */
7120 int iFirst
, int iEnd
/* Range of FROM-clause entries to search. */
7124 assert( pThis
->fg
.isSubquery
);
7125 pSel
= pThis
->u4
.pSubq
->pSelect
;
7127 if( pSel
->selFlags
& SF_PushDown
) return 0;
7128 while( iFirst
<iEnd
){
7130 pItem
= &pTabList
->a
[iFirst
++];
7131 if( !pItem
->fg
.isSubquery
) continue;
7132 if( pItem
->fg
.viaCoroutine
) continue;
7133 if( pItem
->zName
==0 ) continue;
7134 assert( pItem
->pSTab
!=0 );
7135 assert( pThis
->pSTab
!=0 );
7136 if( pItem
->pSTab
->pSchema
!=pThis
->pSTab
->pSchema
) continue;
7137 if( sqlite3_stricmp(pItem
->zName
, pThis
->zName
)!=0 ) continue;
7138 pS1
= pItem
->u4
.pSubq
->pSelect
;
7139 if( pItem
->pSTab
->pSchema
==0 && pSel
->selId
!=pS1
->selId
){
7140 /* The query flattener left two different CTE tables with identical
7141 ** names in the same FROM clause. */
7144 if( pS1
->selFlags
& SF_PushDown
){
7145 /* The view was modified by some other optimization such as
7146 ** pushDownWhereTerms() */
7155 ** Deallocate a single AggInfo object
7157 static void agginfoFree(sqlite3
*db
, void *pArg
){
7158 AggInfo
*p
= (AggInfo
*)pArg
;
7159 sqlite3DbFree(db
, p
->aCol
);
7160 sqlite3DbFree(db
, p
->aFunc
);
7161 sqlite3DbFreeNN(db
, p
);
7165 ** Attempt to transform a query of the form
7167 ** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
7171 ** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
7173 ** The transformation only works if all of the following are true:
7175 ** * The subquery is a UNION ALL of two or more terms
7176 ** * The subquery does not have a LIMIT clause
7177 ** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
7178 ** * The outer query is a simple count(*) with no WHERE clause or other
7179 ** extraneous syntax.
7181 ** Return TRUE if the optimization is undertaken.
7183 static int countOfViewOptimization(Parse
*pParse
, Select
*p
){
7184 Select
*pSub
, *pPrior
;
7189 if( (p
->selFlags
& SF_Aggregate
)==0 ) return 0; /* This is an aggregate */
7190 if( p
->pEList
->nExpr
!=1 ) return 0; /* Single result column */
7191 if( p
->pWhere
) return 0;
7192 if( p
->pHaving
) return 0;
7193 if( p
->pGroupBy
) return 0;
7194 if( p
->pOrderBy
) return 0;
7195 pExpr
= p
->pEList
->a
[0].pExpr
;
7196 if( pExpr
->op
!=TK_AGG_FUNCTION
) return 0; /* Result is an aggregate */
7197 assert( ExprUseUToken(pExpr
) );
7198 if( sqlite3_stricmp(pExpr
->u
.zToken
,"count") ) return 0; /* Is count() */
7199 assert( ExprUseXList(pExpr
) );
7200 if( pExpr
->x
.pList
!=0 ) return 0; /* Must be count(*) */
7201 if( p
->pSrc
->nSrc
!=1 ) return 0; /* One table in FROM */
7202 if( ExprHasProperty(pExpr
, EP_WinFunc
) ) return 0;/* Not a window function */
7204 if( pFrom
->fg
.isSubquery
==0 ) return 0; /* FROM is a subquery */
7205 pSub
= pFrom
->u4
.pSubq
->pSelect
;
7206 if( pSub
->pPrior
==0 ) return 0; /* Must be a compound */
7207 if( pSub
->selFlags
& SF_CopyCte
) return 0; /* Not a CTE */
7209 if( pSub
->op
!=TK_ALL
&& pSub
->pPrior
) return 0; /* Must be UNION ALL */
7210 if( pSub
->pWhere
) return 0; /* No WHERE clause */
7211 if( pSub
->pLimit
) return 0; /* No LIMIT clause */
7212 if( pSub
->selFlags
& SF_Aggregate
) return 0; /* Not an aggregate */
7213 assert( pSub
->pHaving
==0 ); /* Due to the previous */
7214 pSub
= pSub
->pPrior
; /* Repeat over compound */
7217 /* If we reach this point then it is OK to perform the transformation */
7222 pSub
= sqlite3SubqueryDetach(db
, pFrom
);
7223 sqlite3SrcListDelete(db
, p
->pSrc
);
7224 p
->pSrc
= sqlite3DbMallocZero(pParse
->db
, sizeof(*p
->pSrc
));
7227 pPrior
= pSub
->pPrior
;
7230 pSub
->selFlags
|= SF_Aggregate
;
7231 pSub
->selFlags
&= ~SF_Compound
;
7232 pSub
->nSelectRow
= 0;
7233 sqlite3ParserAddCleanup(pParse
, sqlite3ExprListDeleteGeneric
, pSub
->pEList
);
7234 pTerm
= pPrior
? sqlite3ExprDup(db
, pCount
, 0) : pCount
;
7235 pSub
->pEList
= sqlite3ExprListAppend(pParse
, 0, pTerm
);
7236 pTerm
= sqlite3PExpr(pParse
, TK_SELECT
, 0, 0);
7237 sqlite3PExprAddSelect(pParse
, pTerm
, pSub
);
7241 pExpr
= sqlite3PExpr(pParse
, TK_PLUS
, pTerm
, pExpr
);
7245 p
->pEList
->a
[0].pExpr
= pExpr
;
7246 p
->selFlags
&= ~SF_Aggregate
;
7248 #if TREETRACE_ENABLED
7249 if( sqlite3TreeTrace
& 0x200 ){
7250 TREETRACE(0x200,pParse
,p
,("After count-of-view optimization:\n"));
7251 sqlite3TreeViewSelect(0, p
, 0);
7258 ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
7259 ** as pSrcItem but has the same alias as p0, then return true.
7260 ** Otherwise return false.
7262 static int sameSrcAlias(SrcItem
*p0
, SrcList
*pSrc
){
7264 for(i
=0; i
<pSrc
->nSrc
; i
++){
7265 SrcItem
*p1
= &pSrc
->a
[i
];
7266 if( p1
==p0
) continue;
7267 if( p0
->pSTab
==p1
->pSTab
&& 0==sqlite3_stricmp(p0
->zAlias
, p1
->zAlias
) ){
7270 if( p1
->fg
.isSubquery
7271 && (p1
->u4
.pSubq
->pSelect
->selFlags
& SF_NestedFrom
)!=0
7272 && sameSrcAlias(p0
, p1
->u4
.pSubq
->pSelect
->pSrc
)
7281 ** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can
7282 ** be implemented as a co-routine. The i-th entry is guaranteed to be
7285 ** The subquery is implemented as a co-routine if all of the following are
7288 ** (1) The subquery will likely be implemented in the outer loop of
7289 ** the query. This will be the case if any one of the following
7291 ** (a) The subquery is the only term in the FROM clause
7292 ** (b) The subquery is the left-most term and a CROSS JOIN or similar
7293 ** requires it to be the outer loop
7294 ** (c) All of the following are true:
7295 ** (i) The subquery is the left-most subquery in the FROM clause
7296 ** (ii) There is nothing that would prevent the subquery from
7297 ** being used as the outer loop if the sqlite3WhereBegin()
7298 ** routine nominates it to that position.
7299 ** (iii) The query is not a UPDATE ... FROM
7300 ** (2) The subquery is not a CTE that should be materialized because
7301 ** (a) the AS MATERIALIZED keyword is used, or
7302 ** (b) the CTE is used multiple times and does not have the
7303 ** NOT MATERIALIZED keyword
7304 ** (3) The subquery is not part of a left operand for a RIGHT JOIN
7305 ** (4) The SQLITE_Coroutine optimization disable flag is not set
7306 ** (5) The subquery is not self-joined
7308 static int fromClauseTermCanBeCoroutine(
7309 Parse
*pParse
, /* Parsing context */
7310 SrcList
*pTabList
, /* FROM clause */
7311 int i
, /* Which term of the FROM clause holds the subquery */
7312 int selFlags
/* Flags on the SELECT statement */
7314 SrcItem
*pItem
= &pTabList
->a
[i
];
7315 if( pItem
->fg
.isCte
){
7316 const CteUse
*pCteUse
= pItem
->u2
.pCteUse
;
7317 if( pCteUse
->eM10d
==M10d_Yes
) return 0; /* (2a) */
7318 if( pCteUse
->nUse
>=2 && pCteUse
->eM10d
!=M10d_No
) return 0; /* (2b) */
7320 if( pTabList
->a
[0].fg
.jointype
& JT_LTORJ
) return 0; /* (3) */
7321 if( OptimizationDisabled(pParse
->db
, SQLITE_Coroutines
) ) return 0; /* (4) */
7322 if( isSelfJoinView(pTabList
, pItem
, i
+1, pTabList
->nSrc
)!=0 ){
7326 if( pTabList
->nSrc
==1 ) return 1; /* (1a) */
7327 if( pTabList
->a
[1].fg
.jointype
& JT_CROSS
) return 1; /* (1b) */
7328 if( selFlags
& SF_UpdateFrom
) return 0; /* (1c-iii) */
7331 if( selFlags
& SF_UpdateFrom
) return 0; /* (1c-iii) */
7332 while( 1 /*exit-by-break*/ ){
7333 if( pItem
->fg
.jointype
& (JT_OUTER
|JT_CROSS
) ) return 0; /* (1c-ii) */
7337 if( pItem
->fg
.isSubquery
) return 0; /* (1c-i) */
7343 ** Generate byte-code for the SELECT statement given in the p argument.
7345 ** The results are returned according to the SelectDest structure.
7346 ** See comments in sqliteInt.h for further information.
7348 ** This routine returns the number of errors. If any errors are
7349 ** encountered, then an appropriate error message is left in
7352 ** This routine does NOT free the Select structure passed in. The
7353 ** calling function needs to do that.
7355 ** This is a long function. The following is an outline of the processing
7356 ** steps, with tags referencing various milestones:
7358 ** * Resolve names and similar preparation tag-select-0100
7359 ** * Scan of the FROM clause tag-select-0200
7360 ** + OUTER JOIN strength reduction tag-select-0220
7361 ** + Sub-query ORDER BY removal tag-select-0230
7362 ** + Query flattening tag-select-0240
7363 ** * Separate subroutine for compound-SELECT tag-select-0300
7364 ** * WHERE-clause constant propagation tag-select-0330
7365 ** * Count()-of-VIEW optimization tag-select-0350
7366 ** * Scan of the FROM clause again tag-select-0400
7367 ** + Authorize unreferenced tables tag-select-0410
7368 ** + Predicate push-down optimization tag-select-0420
7369 ** + Omit unused subquery columns optimization tag-select-0440
7370 ** + Generate code to implement subqueries tag-select-0480
7371 ** - Co-routines tag-select-0482
7372 ** - Reuse previously computed CTE tag-select-0484
7373 ** - REuse previously computed VIEW tag-select-0486
7374 ** - Materialize a VIEW or CTE tag-select-0488
7375 ** * DISTINCT ORDER BY -> GROUP BY optimization tag-select-0500
7376 ** * Set up for ORDER BY tag-select-0600
7377 ** * Create output table tag-select-0630
7378 ** * Prepare registers for LIMIT tag-select-0650
7379 ** * Setup for DISTINCT tag-select-0680
7380 ** * Generate code for non-aggregate and non-GROUP BY tag-select-0700
7381 ** * Generate code for aggregate and/or GROUP BY tag-select-0800
7382 ** + GROUP BY queries tag-select-0810
7383 ** + non-GROUP BY queries tag-select-0820
7384 ** - Special case of count() w/o GROUP BY tag-select-0821
7385 ** - General case of non-GROUP BY aggregates tag-select-0822
7386 ** * Sort results, as needed tag-select-0900
7387 ** * Internal self-checks tag-select-1000
7390 Parse
*pParse
, /* The parser context */
7391 Select
*p
, /* The SELECT statement being coded. */
7392 SelectDest
*pDest
/* What to do with the query results */
7394 int i
, j
; /* Loop counters */
7395 WhereInfo
*pWInfo
; /* Return from sqlite3WhereBegin() */
7396 Vdbe
*v
; /* The virtual machine under construction */
7397 int isAgg
; /* True for select lists like "count(*)" */
7398 ExprList
*pEList
= 0; /* List of columns to extract. */
7399 SrcList
*pTabList
; /* List of tables to select from */
7400 Expr
*pWhere
; /* The WHERE clause. May be NULL */
7401 ExprList
*pGroupBy
; /* The GROUP BY clause. May be NULL */
7402 Expr
*pHaving
; /* The HAVING clause. May be NULL */
7403 AggInfo
*pAggInfo
= 0; /* Aggregate information */
7404 int rc
= 1; /* Value to return from this function */
7405 DistinctCtx sDistinct
; /* Info on how to code the DISTINCT keyword */
7406 SortCtx sSort
; /* Info on how to code the ORDER BY clause */
7407 int iEnd
; /* Address of the end of the query */
7408 sqlite3
*db
; /* The database connection */
7409 ExprList
*pMinMaxOrderBy
= 0; /* Added ORDER BY for min/max queries */
7410 u8 minMaxFlag
; /* Flag for min/max queries */
7413 assert( pParse
==db
->pParse
);
7414 v
= sqlite3GetVdbe(pParse
);
7415 if( p
==0 || pParse
->nErr
){
7418 assert( db
->mallocFailed
==0 );
7419 if( sqlite3AuthCheck(pParse
, SQLITE_SELECT
, 0, 0, 0) ) return 1;
7420 #if TREETRACE_ENABLED
7421 TREETRACE(0x1,pParse
,p
, ("begin processing:\n", pParse
->addrExplain
));
7422 if( sqlite3TreeTrace
& 0x10000 ){
7423 if( (sqlite3TreeTrace
& 0x10001)==0x10000 ){
7424 sqlite3TreeViewLine(0, "In sqlite3Select() at %s:%d",
7425 __FILE__
, __LINE__
);
7427 sqlite3ShowSelect(p
);
7431 /* tag-select-0100 */
7432 assert( p
->pOrderBy
==0 || pDest
->eDest
!=SRT_DistFifo
);
7433 assert( p
->pOrderBy
==0 || pDest
->eDest
!=SRT_Fifo
);
7434 assert( p
->pOrderBy
==0 || pDest
->eDest
!=SRT_DistQueue
);
7435 assert( p
->pOrderBy
==0 || pDest
->eDest
!=SRT_Queue
);
7436 if( IgnorableDistinct(pDest
) ){
7437 assert(pDest
->eDest
==SRT_Exists
|| pDest
->eDest
==SRT_Union
||
7438 pDest
->eDest
==SRT_Except
|| pDest
->eDest
==SRT_Discard
||
7439 pDest
->eDest
==SRT_DistQueue
|| pDest
->eDest
==SRT_DistFifo
);
7440 /* All of these destinations are also able to ignore the ORDER BY clause */
7442 #if TREETRACE_ENABLED
7443 TREETRACE(0x800,pParse
,p
, ("dropping superfluous ORDER BY:\n"));
7444 if( sqlite3TreeTrace
& 0x800 ){
7445 sqlite3TreeViewExprList(0, p
->pOrderBy
, 0, "ORDERBY");
7448 sqlite3ParserAddCleanup(pParse
, sqlite3ExprListDeleteGeneric
,
7450 testcase( pParse
->earlyCleanup
);
7453 p
->selFlags
&= ~SF_Distinct
;
7454 p
->selFlags
|= SF_NoopOrderBy
;
7456 sqlite3SelectPrep(pParse
, p
, 0);
7460 assert( db
->mallocFailed
==0 );
7461 assert( p
->pEList
!=0 );
7462 #if TREETRACE_ENABLED
7463 if( sqlite3TreeTrace
& 0x10 ){
7464 TREETRACE(0x10,pParse
,p
, ("after name resolution:\n"));
7465 sqlite3TreeViewSelect(0, p
, 0);
7469 /* If the SF_UFSrcCheck flag is set, then this function is being called
7470 ** as part of populating the temp table for an UPDATE...FROM statement.
7471 ** In this case, it is an error if the target object (pSrc->a[0]) name
7472 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
7474 ** Postgres disallows this case too. The reason is that some other
7475 ** systems handle this case differently, and not all the same way,
7476 ** which is just confusing. To avoid this, we follow PG's lead and
7477 ** disallow it altogether. */
7478 if( p
->selFlags
& SF_UFSrcCheck
){
7479 SrcItem
*p0
= &p
->pSrc
->a
[0];
7480 if( sameSrcAlias(p0
, p
->pSrc
) ){
7481 sqlite3ErrorMsg(pParse
,
7482 "target object/alias may not appear in FROM clause: %s",
7483 p0
->zAlias
? p0
->zAlias
: p0
->pSTab
->zName
7488 /* Clear the SF_UFSrcCheck flag. The check has already been performed,
7489 ** and leaving this flag set can cause errors if a compound sub-query
7490 ** in p->pSrc is flattened into this query and this function called
7491 ** again as part of compound SELECT processing. */
7492 p
->selFlags
&= ~SF_UFSrcCheck
;
7495 if( pDest
->eDest
==SRT_Output
){
7496 sqlite3GenerateColumnNames(pParse
, p
);
7499 #ifndef SQLITE_OMIT_WINDOWFUNC
7500 if( sqlite3WindowRewrite(pParse
, p
) ){
7501 assert( pParse
->nErr
);
7504 #if TREETRACE_ENABLED
7505 if( p
->pWin
&& (sqlite3TreeTrace
& 0x40)!=0 ){
7506 TREETRACE(0x40,pParse
,p
, ("after window rewrite:\n"));
7507 sqlite3TreeViewSelect(0, p
, 0);
7510 #endif /* SQLITE_OMIT_WINDOWFUNC */
7512 isAgg
= (p
->selFlags
& SF_Aggregate
)!=0;
7513 memset(&sSort
, 0, sizeof(sSort
));
7514 sSort
.pOrderBy
= p
->pOrderBy
;
7516 /* Try to do various optimizations (flattening subqueries, and strength
7517 ** reduction of join operators) in the FROM clause up into the main query
7520 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
7521 for(i
=0; !p
->pPrior
&& i
<pTabList
->nSrc
; i
++){
7522 SrcItem
*pItem
= &pTabList
->a
[i
];
7523 Select
*pSub
= pItem
->fg
.isSubquery
? pItem
->u4
.pSubq
->pSelect
: 0;
7524 Table
*pTab
= pItem
->pSTab
;
7526 /* The expander should have already created transient Table objects
7527 ** even for FROM clause elements such as subqueries that do not correspond
7528 ** to a real table */
7531 /* Try to simplify joins:
7533 ** LEFT JOIN -> JOIN
7534 ** RIGHT JOIN -> JOIN
7535 ** FULL JOIN -> RIGHT JOIN
7537 ** If terms of the i-th table are used in the WHERE clause in such a
7538 ** way that the i-th table cannot be the NULL row of a join, then
7539 ** perform the appropriate simplification. This is called
7540 ** "OUTER JOIN strength reduction" in the SQLite documentation.
7543 if( (pItem
->fg
.jointype
& (JT_LEFT
|JT_LTORJ
))!=0
7544 && sqlite3ExprImpliesNonNullRow(p
->pWhere
, pItem
->iCursor
,
7545 pItem
->fg
.jointype
& JT_LTORJ
)
7546 && OptimizationEnabled(db
, SQLITE_SimplifyJoin
)
7548 if( pItem
->fg
.jointype
& JT_LEFT
){
7549 if( pItem
->fg
.jointype
& JT_RIGHT
){
7550 TREETRACE(0x1000,pParse
,p
,
7551 ("FULL-JOIN simplifies to RIGHT-JOIN on term %d\n",i
));
7552 pItem
->fg
.jointype
&= ~JT_LEFT
;
7554 TREETRACE(0x1000,pParse
,p
,
7555 ("LEFT-JOIN simplifies to JOIN on term %d\n",i
));
7556 pItem
->fg
.jointype
&= ~(JT_LEFT
|JT_OUTER
);
7557 unsetJoinExpr(p
->pWhere
, pItem
->iCursor
, 0);
7560 if( pItem
->fg
.jointype
& JT_LTORJ
){
7561 for(j
=i
+1; j
<pTabList
->nSrc
; j
++){
7562 SrcItem
*pI2
= &pTabList
->a
[j
];
7563 if( pI2
->fg
.jointype
& JT_RIGHT
){
7564 if( pI2
->fg
.jointype
& JT_LEFT
){
7565 TREETRACE(0x1000,pParse
,p
,
7566 ("FULL-JOIN simplifies to LEFT-JOIN on term %d\n",j
));
7567 pI2
->fg
.jointype
&= ~JT_RIGHT
;
7569 TREETRACE(0x1000,pParse
,p
,
7570 ("RIGHT-JOIN simplifies to JOIN on term %d\n",j
));
7571 pI2
->fg
.jointype
&= ~(JT_RIGHT
|JT_OUTER
);
7572 unsetJoinExpr(p
->pWhere
, pI2
->iCursor
, 1);
7576 for(j
=pTabList
->nSrc
-1; j
>=0; j
--){
7577 pTabList
->a
[j
].fg
.jointype
&= ~JT_LTORJ
;
7578 if( pTabList
->a
[j
].fg
.jointype
& JT_RIGHT
) break;
7583 /* No further action if this term of the FROM clause is not a subquery */
7584 if( pSub
==0 ) continue;
7586 /* Catch mismatch in the declared columns of a view and the number of
7587 ** columns in the SELECT on the RHS */
7588 if( pTab
->nCol
!=pSub
->pEList
->nExpr
){
7589 sqlite3ErrorMsg(pParse
, "expected %d columns for '%s' but got %d",
7590 pTab
->nCol
, pTab
->zName
, pSub
->pEList
->nExpr
);
7594 /* Do not attempt the usual optimizations (flattening and ORDER BY
7595 ** elimination) on a MATERIALIZED common table expression because
7596 ** a MATERIALIZED common table expression is an optimization fence.
7598 if( pItem
->fg
.isCte
&& pItem
->u2
.pCteUse
->eM10d
==M10d_Yes
){
7602 /* Do not try to flatten an aggregate subquery.
7604 ** Flattening an aggregate subquery is only possible if the outer query
7605 ** is not a join. But if the outer query is not a join, then the subquery
7606 ** will be implemented as a co-routine and there is no advantage to
7607 ** flattening in that case.
7609 if( (pSub
->selFlags
& SF_Aggregate
)!=0 ) continue;
7610 assert( pSub
->pGroupBy
==0 );
7613 ** If a FROM-clause subquery has an ORDER BY clause that is not
7614 ** really doing anything, then delete it now so that it does not
7615 ** interfere with query flattening. See the discussion at
7616 ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a
7618 ** Beware of these cases where the ORDER BY clause may not be safely
7621 ** (1) There is also a LIMIT clause
7622 ** (2) The subquery was added to help with window-function
7624 ** (3) The subquery is in the FROM clause of an UPDATE
7625 ** (4) The outer query uses an aggregate function other than
7626 ** the built-in count(), min(), or max().
7627 ** (5) The ORDER BY isn't going to accomplish anything because
7629 ** (a) The outer query has a different ORDER BY clause
7630 ** (b) The subquery is part of a join
7631 ** See forum post 062d576715d277c8
7632 ** (6) The subquery is not a recursive CTE. ORDER BY has a different
7633 ** meaning for recursive CTEs and this optimization does not
7636 ** Also retain the ORDER BY if the OmitOrderBy optimization is disabled.
7638 if( pSub
->pOrderBy
!=0
7639 && (p
->pOrderBy
!=0 || pTabList
->nSrc
>1) /* Condition (5) */
7640 && pSub
->pLimit
==0 /* Condition (1) */
7641 && (pSub
->selFlags
& (SF_OrderByReqd
|SF_Recursive
))==0 /* (2) and (6) */
7642 && (p
->selFlags
& SF_OrderByReqd
)==0 /* Condition (3) and (4) */
7643 && OptimizationEnabled(db
, SQLITE_OmitOrderBy
)
7645 TREETRACE(0x800,pParse
,p
,
7646 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i
+1));
7647 sqlite3ParserAddCleanup(pParse
, sqlite3ExprListDeleteGeneric
,
7652 /* If the outer query contains a "complex" result set (that is,
7653 ** if the result set of the outer query uses functions or subqueries)
7654 ** and if the subquery contains an ORDER BY clause and if
7655 ** it will be implemented as a co-routine, then do not flatten. This
7656 ** restriction allows SQL constructs like this:
7658 ** SELECT expensive_function(x)
7659 ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
7661 ** The expensive_function() is only computed on the 10 rows that
7662 ** are output, rather than every row of the table.
7664 ** The requirement that the outer query have a complex result set
7665 ** means that flattening does occur on simpler SQL constraints without
7666 ** the expensive_function() like:
7668 ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
7670 if( pSub
->pOrderBy
!=0
7672 && (p
->selFlags
& SF_ComplexResult
)!=0
7673 && (pTabList
->nSrc
==1
7674 || (pTabList
->a
[1].fg
.jointype
&(JT_OUTER
|JT_CROSS
))!=0)
7679 /* tag-select-0240 */
7680 if( flattenSubquery(pParse
, p
, i
, isAgg
) ){
7681 if( pParse
->nErr
) goto select_end
;
7682 /* This subquery can be absorbed into its parent. */
7686 if( db
->mallocFailed
) goto select_end
;
7687 if( !IgnorableOrderby(pDest
) ){
7688 sSort
.pOrderBy
= p
->pOrderBy
;
7693 #ifndef SQLITE_OMIT_COMPOUND_SELECT
7694 /* Handle compound SELECT statements using the separate multiSelect()
7695 ** procedure. tag-select-0300
7698 rc
= multiSelect(pParse
, p
, pDest
);
7699 #if TREETRACE_ENABLED
7700 TREETRACE(0x400,pParse
,p
,("end compound-select processing\n"));
7701 if( (sqlite3TreeTrace
& 0x400)!=0 && ExplainQueryPlanParent(pParse
)==0 ){
7702 sqlite3TreeViewSelect(0, p
, 0);
7705 if( p
->pNext
==0 ) ExplainQueryPlanPop(pParse
);
7710 /* Do the WHERE-clause constant propagation optimization if this is
7711 ** a join. No need to spend time on this operation for non-join queries
7712 ** as the equivalent optimization will be handled by query planner in
7713 ** sqlite3WhereBegin(). tag-select-0330
7716 && p
->pWhere
->op
==TK_AND
7717 && OptimizationEnabled(db
, SQLITE_PropagateConst
)
7718 && propagateConstants(pParse
, p
)
7720 #if TREETRACE_ENABLED
7721 if( sqlite3TreeTrace
& 0x2000 ){
7722 TREETRACE(0x2000,pParse
,p
,("After constant propagation:\n"));
7723 sqlite3TreeViewSelect(0, p
, 0);
7727 TREETRACE(0x2000,pParse
,p
,("Constant propagation not helpful\n"));
7730 /* tag-select-0350 */
7731 if( OptimizationEnabled(db
, SQLITE_QueryFlattener
|SQLITE_CountOfView
)
7732 && countOfViewOptimization(pParse
, p
)
7734 if( db
->mallocFailed
) goto select_end
;
7738 /* Loop over all terms in the FROM clause and do two things for each term:
7740 ** (1) Authorize unreferenced tables
7741 ** (2) Generate code for all sub-queries
7745 for(i
=0; i
<pTabList
->nSrc
; i
++){
7746 SrcItem
*pItem
= &pTabList
->a
[i
];
7751 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
7752 const char *zSavedAuthContext
;
7755 /* Authorized unreferenced tables. tag-select-0410
7757 ** Issue SQLITE_READ authorizations with a fake column name for any
7758 ** tables that are referenced but from which no values are extracted.
7759 ** Examples of where these kinds of null SQLITE_READ authorizations
7762 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
7763 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
7765 ** The fake column name is an empty string. It is possible for a table to
7766 ** have a column named by the empty string, in which case there is no way to
7767 ** distinguish between an unreferenced table and an actual reference to the
7768 ** "" column. The original design was for the fake column name to be a NULL,
7769 ** which would be unambiguous. But legacy authorization callbacks might
7770 ** assume the column name is non-NULL and segfault. The use of an empty
7771 ** string for the fake column name seems safer.
7773 if( pItem
->colUsed
==0 && pItem
->zName
!=0 ){
7775 if( pItem
->fg
.fixedSchema
){
7776 int iDb
= sqlite3SchemaToIndex(pParse
->db
, pItem
->u4
.pSchema
);
7777 zDb
= db
->aDb
[iDb
].zDbSName
;
7778 }else if( pItem
->fg
.isSubquery
){
7781 zDb
= pItem
->u4
.zDatabase
;
7783 sqlite3AuthCheck(pParse
, SQLITE_READ
, pItem
->zName
, "", zDb
);
7786 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
7787 /* Generate code for all sub-queries in the FROM clause
7789 if( pItem
->fg
.isSubquery
==0 ) continue;
7790 pSubq
= pItem
->u4
.pSubq
;
7792 pSub
= pSubq
->pSelect
;
7794 /* The code for a subquery should only be generated once. */
7795 if( pSubq
->addrFillSub
!=0 ) continue;
7797 /* Increment Parse.nHeight by the height of the largest expression
7798 ** tree referred to by this, the parent select. The child select
7799 ** may contain expression trees of at most
7800 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
7801 ** more conservative than necessary, but much easier than enforcing
7804 pParse
->nHeight
+= sqlite3SelectExprHeight(p
);
7806 /* Make copies of constant WHERE-clause terms in the outer query down
7807 ** inside the subquery. This can help the subquery to run more efficiently.
7808 ** This is the "predicate push-down optimization". tag-select-0420
7810 if( OptimizationEnabled(db
, SQLITE_PushDown
)
7811 && (pItem
->fg
.isCte
==0
7812 || (pItem
->u2
.pCteUse
->eM10d
!=M10d_Yes
&& pItem
->u2
.pCteUse
->nUse
<2))
7813 && pushDownWhereTerms(pParse
, pSub
, p
->pWhere
, pTabList
, i
)
7815 #if TREETRACE_ENABLED
7816 if( sqlite3TreeTrace
& 0x4000 ){
7817 TREETRACE(0x4000,pParse
,p
,
7818 ("After WHERE-clause push-down into subquery %d:\n", pSub
->selId
));
7819 sqlite3TreeViewSelect(0, p
, 0);
7822 assert( pSubq
->pSelect
&& (pSub
->selFlags
& SF_PushDown
)!=0 );
7824 TREETRACE(0x4000,pParse
,p
,("WHERE-lcause push-down not possible\n"));
7827 /* Convert unused result columns of the subquery into simple NULL
7828 ** expressions, to avoid unneeded searching and computation.
7831 if( OptimizationEnabled(db
, SQLITE_NullUnusedCols
)
7832 && disableUnusedSubqueryResultColumns(pItem
)
7834 #if TREETRACE_ENABLED
7835 if( sqlite3TreeTrace
& 0x4000 ){
7836 TREETRACE(0x4000,pParse
,p
,
7837 ("Change unused result columns to NULL for subquery %d:\n",
7839 sqlite3TreeViewSelect(0, p
, 0);
7844 zSavedAuthContext
= pParse
->zAuthContext
;
7845 pParse
->zAuthContext
= pItem
->zName
;
7847 /* Generate byte-code to implement the subquery tag-select-0480
7849 if( fromClauseTermCanBeCoroutine(pParse
, pTabList
, i
, p
->selFlags
) ){
7850 /* Implement a co-routine that will return a single row of the result
7851 ** set on each invocation. tag-select-0482
7853 int addrTop
= sqlite3VdbeCurrentAddr(v
)+1;
7855 pSubq
->regReturn
= ++pParse
->nMem
;
7856 sqlite3VdbeAddOp3(v
, OP_InitCoroutine
, pSubq
->regReturn
, 0, addrTop
);
7857 VdbeComment((v
, "%!S", pItem
));
7858 pSubq
->addrFillSub
= addrTop
;
7859 sqlite3SelectDestInit(&dest
, SRT_Coroutine
, pSubq
->regReturn
);
7860 ExplainQueryPlan((pParse
, 1, "CO-ROUTINE %!S", pItem
));
7861 sqlite3Select(pParse
, pSub
, &dest
);
7862 pItem
->pSTab
->nRowLogEst
= pSub
->nSelectRow
;
7863 pItem
->fg
.viaCoroutine
= 1;
7864 pSubq
->regResult
= dest
.iSdst
;
7865 sqlite3VdbeEndCoroutine(v
, pSubq
->regReturn
);
7866 VdbeComment((v
, "end %!S", pItem
));
7867 sqlite3VdbeJumpHere(v
, addrTop
-1);
7868 sqlite3ClearTempRegCache(pParse
);
7869 }else if( pItem
->fg
.isCte
&& pItem
->u2
.pCteUse
->addrM9e
>0 ){
7870 /* This is a CTE for which materialization code has already been
7871 ** generated. Invoke the subroutine to compute the materialization,
7872 ** then make the pItem->iCursor be a copy of the ephemeral table that
7873 ** holds the result of the materialization. tag-select-0484 */
7874 CteUse
*pCteUse
= pItem
->u2
.pCteUse
;
7875 sqlite3VdbeAddOp2(v
, OP_Gosub
, pCteUse
->regRtn
, pCteUse
->addrM9e
);
7876 if( pItem
->iCursor
!=pCteUse
->iCur
){
7877 sqlite3VdbeAddOp2(v
, OP_OpenDup
, pItem
->iCursor
, pCteUse
->iCur
);
7878 VdbeComment((v
, "%!S", pItem
));
7880 pSub
->nSelectRow
= pCteUse
->nRowEst
;
7881 }else if( (pPrior
= isSelfJoinView(pTabList
, pItem
, 0, i
))!=0 ){
7882 /* This view has already been materialized by a prior entry in
7883 ** this same FROM clause. Reuse it. tag-select-0486 */
7884 Subquery
*pPriorSubq
;
7885 assert( pPrior
->fg
.isSubquery
);
7886 pPriorSubq
= pPrior
->u4
.pSubq
;
7887 assert( pPriorSubq
!=0 );
7888 if( pPriorSubq
->addrFillSub
){
7889 sqlite3VdbeAddOp2(v
, OP_Gosub
, pPriorSubq
->regReturn
,
7890 pPriorSubq
->addrFillSub
);
7892 sqlite3VdbeAddOp2(v
, OP_OpenDup
, pItem
->iCursor
, pPrior
->iCursor
);
7893 pSub
->nSelectRow
= pPriorSubq
->pSelect
->nSelectRow
;
7895 /* Materialize the view. If the view is not correlated, generate a
7896 ** subroutine to do the materialization so that subsequent uses of
7897 ** the same view can reuse the materialization. tag-select-0488 */
7900 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
7904 pSubq
->regReturn
= ++pParse
->nMem
;
7905 topAddr
= sqlite3VdbeAddOp0(v
, OP_Goto
);
7906 pSubq
->addrFillSub
= topAddr
+1;
7907 pItem
->fg
.isMaterialized
= 1;
7908 if( pItem
->fg
.isCorrelated
==0 ){
7909 /* If the subquery is not correlated and if we are not inside of
7910 ** a trigger, then we only need to compute the value of the subquery
7912 onceAddr
= sqlite3VdbeAddOp0(v
, OP_Once
); VdbeCoverage(v
);
7913 VdbeComment((v
, "materialize %!S", pItem
));
7915 VdbeNoopComment((v
, "materialize %!S", pItem
));
7917 sqlite3SelectDestInit(&dest
, SRT_EphemTab
, pItem
->iCursor
);
7919 ExplainQueryPlan2(addrExplain
, (pParse
, 1, "MATERIALIZE %!S", pItem
));
7920 sqlite3Select(pParse
, pSub
, &dest
);
7921 pItem
->pSTab
->nRowLogEst
= pSub
->nSelectRow
;
7922 if( onceAddr
) sqlite3VdbeJumpHere(v
, onceAddr
);
7923 sqlite3VdbeAddOp2(v
, OP_Return
, pSubq
->regReturn
, topAddr
+1);
7924 VdbeComment((v
, "end %!S", pItem
));
7925 sqlite3VdbeScanStatusRange(v
, addrExplain
, addrExplain
, -1);
7926 sqlite3VdbeJumpHere(v
, topAddr
);
7927 sqlite3ClearTempRegCache(pParse
);
7928 if( pItem
->fg
.isCte
&& pItem
->fg
.isCorrelated
==0 ){
7929 CteUse
*pCteUse
= pItem
->u2
.pCteUse
;
7930 pCteUse
->addrM9e
= pSubq
->addrFillSub
;
7931 pCteUse
->regRtn
= pSubq
->regReturn
;
7932 pCteUse
->iCur
= pItem
->iCursor
;
7933 pCteUse
->nRowEst
= pSub
->nSelectRow
;
7936 if( db
->mallocFailed
) goto select_end
;
7937 pParse
->nHeight
-= sqlite3SelectExprHeight(p
);
7938 pParse
->zAuthContext
= zSavedAuthContext
;
7942 /* Various elements of the SELECT copied into local variables for
7946 pGroupBy
= p
->pGroupBy
;
7947 pHaving
= p
->pHaving
;
7948 sDistinct
.isTnct
= (p
->selFlags
& SF_Distinct
)!=0;
7950 #if TREETRACE_ENABLED
7951 if( sqlite3TreeTrace
& 0x8000 ){
7952 TREETRACE(0x8000,pParse
,p
,("After all FROM-clause analysis:\n"));
7953 sqlite3TreeViewSelect(0, p
, 0);
7959 ** If the query is DISTINCT with an ORDER BY but is not an aggregate, and
7960 ** if the select-list is the same as the ORDER BY list, then this query
7961 ** can be rewritten as a GROUP BY. In other words, this:
7963 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
7965 ** is transformed to:
7967 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
7969 ** The second form is preferred as a single index (or temp-table) may be
7970 ** used for both the ORDER BY and DISTINCT processing. As originally
7971 ** written the query must use a temp-table for at least one of the ORDER
7972 ** BY and DISTINCT, and an index or separate temp-table for the other.
7974 if( (p
->selFlags
& (SF_Distinct
|SF_Aggregate
))==SF_Distinct
7975 && sqlite3ExprListCompare(sSort
.pOrderBy
, pEList
, -1)==0
7976 && OptimizationEnabled(db
, SQLITE_GroupByOrder
)
7977 #ifndef SQLITE_OMIT_WINDOWFUNC
7981 p
->selFlags
&= ~SF_Distinct
;
7982 pGroupBy
= p
->pGroupBy
= sqlite3ExprListDup(db
, pEList
, 0);
7984 for(i
=0; i
<pGroupBy
->nExpr
; i
++){
7985 pGroupBy
->a
[i
].u
.x
.iOrderByCol
= i
+1;
7988 p
->selFlags
|= SF_Aggregate
;
7989 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
7990 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
7991 ** original setting of the SF_Distinct flag, not the current setting */
7992 assert( sDistinct
.isTnct
);
7993 sDistinct
.isTnct
= 2;
7995 #if TREETRACE_ENABLED
7996 if( sqlite3TreeTrace
& 0x20000 ){
7997 TREETRACE(0x20000,pParse
,p
,("Transform DISTINCT into GROUP BY:\n"));
7998 sqlite3TreeViewSelect(0, p
, 0);
8003 /* If there is an ORDER BY clause, then create an ephemeral index to
8004 ** do the sorting. But this sorting ephemeral index might end up
8005 ** being unused if the data can be extracted in pre-sorted order.
8006 ** If that is the case, then the OP_OpenEphemeral instruction will be
8007 ** changed to an OP_Noop once we figure out that the sorting index is
8008 ** not needed. The sSort.addrSortIndex variable is used to facilitate
8009 ** that change. tag-select-0600
8011 if( sSort
.pOrderBy
){
8013 pKeyInfo
= sqlite3KeyInfoFromExprList(
8014 pParse
, sSort
.pOrderBy
, 0, pEList
->nExpr
);
8015 sSort
.iECursor
= pParse
->nTab
++;
8016 sSort
.addrSortIndex
=
8017 sqlite3VdbeAddOp4(v
, OP_OpenEphemeral
,
8018 sSort
.iECursor
, sSort
.pOrderBy
->nExpr
+1+pEList
->nExpr
, 0,
8019 (char*)pKeyInfo
, P4_KEYINFO
8022 sSort
.addrSortIndex
= -1;
8025 /* If the output is destined for a temporary table, open that table.
8028 if( pDest
->eDest
==SRT_EphemTab
){
8029 sqlite3VdbeAddOp2(v
, OP_OpenEphemeral
, pDest
->iSDParm
, pEList
->nExpr
);
8030 if( p
->selFlags
& SF_NestedFrom
){
8031 /* Delete or NULL-out result columns that will never be used */
8033 for(ii
=pEList
->nExpr
-1; ii
>0 && pEList
->a
[ii
].fg
.bUsed
==0; ii
--){
8034 sqlite3ExprDelete(db
, pEList
->a
[ii
].pExpr
);
8035 sqlite3DbFree(db
, pEList
->a
[ii
].zEName
);
8038 for(ii
=0; ii
<pEList
->nExpr
; ii
++){
8039 if( pEList
->a
[ii
].fg
.bUsed
==0 ) pEList
->a
[ii
].pExpr
->op
= TK_NULL
;
8044 /* Set the limiter. tag-select-0650
8046 iEnd
= sqlite3VdbeMakeLabel(pParse
);
8047 if( (p
->selFlags
& SF_FixedLimit
)==0 ){
8048 p
->nSelectRow
= 320; /* 4 billion rows */
8050 if( p
->pLimit
) computeLimitRegisters(pParse
, p
, iEnd
);
8051 if( p
->iLimit
==0 && sSort
.addrSortIndex
>=0 ){
8052 sqlite3VdbeChangeOpcode(v
, sSort
.addrSortIndex
, OP_SorterOpen
);
8053 sSort
.sortFlags
|= SORTFLAG_UseSorter
;
8056 /* Open an ephemeral index to use for the distinct set. tag-select-0680
8058 if( p
->selFlags
& SF_Distinct
){
8059 sDistinct
.tabTnct
= pParse
->nTab
++;
8060 sDistinct
.addrTnct
= sqlite3VdbeAddOp4(v
, OP_OpenEphemeral
,
8061 sDistinct
.tabTnct
, 0, 0,
8062 (char*)sqlite3KeyInfoFromExprList(pParse
, p
->pEList
,0,0),
8064 sqlite3VdbeChangeP5(v
, BTREE_UNORDERED
);
8065 sDistinct
.eTnctType
= WHERE_DISTINCT_UNORDERED
;
8067 sDistinct
.eTnctType
= WHERE_DISTINCT_NOOP
;
8070 if( !isAgg
&& pGroupBy
==0 ){
8071 /* No aggregate functions and no GROUP BY clause. tag-select-0700 */
8072 u16 wctrlFlags
= (sDistinct
.isTnct
? WHERE_WANT_DISTINCT
: 0)
8073 | (p
->selFlags
& SF_FixedLimit
);
8074 #ifndef SQLITE_OMIT_WINDOWFUNC
8075 Window
*pWin
= p
->pWin
; /* Main window object (or NULL) */
8077 sqlite3WindowCodeInit(pParse
, p
);
8080 assert( WHERE_USE_LIMIT
==SF_FixedLimit
);
8083 /* Begin the database scan. */
8084 TREETRACE(0x2,pParse
,p
,("WhereBegin\n"));
8085 pWInfo
= sqlite3WhereBegin(pParse
, pTabList
, pWhere
, sSort
.pOrderBy
,
8086 p
->pEList
, p
, wctrlFlags
, p
->nSelectRow
);
8087 if( pWInfo
==0 ) goto select_end
;
8088 if( sqlite3WhereOutputRowCount(pWInfo
) < p
->nSelectRow
){
8089 p
->nSelectRow
= sqlite3WhereOutputRowCount(pWInfo
);
8091 if( sDistinct
.isTnct
&& sqlite3WhereIsDistinct(pWInfo
) ){
8092 sDistinct
.eTnctType
= sqlite3WhereIsDistinct(pWInfo
);
8094 if( sSort
.pOrderBy
){
8095 sSort
.nOBSat
= sqlite3WhereIsOrdered(pWInfo
);
8096 sSort
.labelOBLopt
= sqlite3WhereOrderByLimitOptLabel(pWInfo
);
8097 if( sSort
.nOBSat
==sSort
.pOrderBy
->nExpr
){
8101 TREETRACE(0x2,pParse
,p
,("WhereBegin returns\n"));
8103 /* If sorting index that was created by a prior OP_OpenEphemeral
8104 ** instruction ended up not being needed, then change the OP_OpenEphemeral
8107 if( sSort
.addrSortIndex
>=0 && sSort
.pOrderBy
==0 ){
8108 sqlite3VdbeChangeToNoop(v
, sSort
.addrSortIndex
);
8111 assert( p
->pEList
==pEList
);
8112 #ifndef SQLITE_OMIT_WINDOWFUNC
8114 int addrGosub
= sqlite3VdbeMakeLabel(pParse
);
8115 int iCont
= sqlite3VdbeMakeLabel(pParse
);
8116 int iBreak
= sqlite3VdbeMakeLabel(pParse
);
8117 int regGosub
= ++pParse
->nMem
;
8119 sqlite3WindowCodeStep(pParse
, p
, pWInfo
, regGosub
, addrGosub
);
8121 sqlite3VdbeAddOp2(v
, OP_Goto
, 0, iBreak
);
8122 sqlite3VdbeResolveLabel(v
, addrGosub
);
8123 VdbeNoopComment((v
, "inner-loop subroutine"));
8124 sSort
.labelOBLopt
= 0;
8125 selectInnerLoop(pParse
, p
, -1, &sSort
, &sDistinct
, pDest
, iCont
, iBreak
);
8126 sqlite3VdbeResolveLabel(v
, iCont
);
8127 sqlite3VdbeAddOp1(v
, OP_Return
, regGosub
);
8128 VdbeComment((v
, "end inner-loop subroutine"));
8129 sqlite3VdbeResolveLabel(v
, iBreak
);
8131 #endif /* SQLITE_OMIT_WINDOWFUNC */
8133 /* Use the standard inner loop. */
8134 selectInnerLoop(pParse
, p
, -1, &sSort
, &sDistinct
, pDest
,
8135 sqlite3WhereContinueLabel(pWInfo
),
8136 sqlite3WhereBreakLabel(pWInfo
));
8138 /* End the database scan loop.
8140 TREETRACE(0x2,pParse
,p
,("WhereEnd\n"));
8141 sqlite3WhereEnd(pWInfo
);
8144 /* This case is for when there exist aggregate functions or a GROUP BY
8145 ** clause or both. tag-select-0800 */
8146 NameContext sNC
; /* Name context for processing aggregate information */
8147 int iAMem
; /* First Mem address for storing current GROUP BY */
8148 int iBMem
; /* First Mem address for previous GROUP BY */
8149 int iUseFlag
; /* Mem address holding flag indicating that at least
8150 ** one row of the input to the aggregator has been
8152 int iAbortFlag
; /* Mem address which causes query abort if positive */
8153 int groupBySort
; /* Rows come from source in GROUP BY order */
8154 int addrEnd
; /* End of processing for this SELECT */
8155 int sortPTab
= 0; /* Pseudotable used to decode sorting results */
8156 int sortOut
= 0; /* Output register from the sorter */
8157 int orderByGrp
= 0; /* True if the GROUP BY and ORDER BY are the same */
8159 /* Remove any and all aliases between the result set and the
8163 int k
; /* Loop counter */
8164 struct ExprList_item
*pItem
; /* For looping over expression in a list */
8166 for(k
=p
->pEList
->nExpr
, pItem
=p
->pEList
->a
; k
>0; k
--, pItem
++){
8167 pItem
->u
.x
.iAlias
= 0;
8169 for(k
=pGroupBy
->nExpr
, pItem
=pGroupBy
->a
; k
>0; k
--, pItem
++){
8170 pItem
->u
.x
.iAlias
= 0;
8172 assert( 66==sqlite3LogEst(100) );
8173 if( p
->nSelectRow
>66 ) p
->nSelectRow
= 66;
8175 /* If there is both a GROUP BY and an ORDER BY clause and they are
8176 ** identical, then it may be possible to disable the ORDER BY clause
8177 ** on the grounds that the GROUP BY will cause elements to come out
8178 ** in the correct order. It also may not - the GROUP BY might use a
8179 ** database index that causes rows to be grouped together as required
8180 ** but not actually sorted. Either way, record the fact that the
8181 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
8183 if( sSort
.pOrderBy
&& pGroupBy
->nExpr
==sSort
.pOrderBy
->nExpr
){
8185 /* The GROUP BY processing doesn't care whether rows are delivered in
8186 ** ASC or DESC order - only that each group is returned contiguously.
8187 ** So set the ASC/DESC flags in the GROUP BY to match those in the
8188 ** ORDER BY to maximize the chances of rows being delivered in an
8189 ** order that makes the ORDER BY redundant. */
8190 for(ii
=0; ii
<pGroupBy
->nExpr
; ii
++){
8192 sortFlags
= sSort
.pOrderBy
->a
[ii
].fg
.sortFlags
& KEYINFO_ORDER_DESC
;
8193 pGroupBy
->a
[ii
].fg
.sortFlags
= sortFlags
;
8195 if( sqlite3ExprListCompare(pGroupBy
, sSort
.pOrderBy
, -1)==0 ){
8200 assert( 0==sqlite3LogEst(1) );
8204 /* Create a label to jump to when we want to abort the query */
8205 addrEnd
= sqlite3VdbeMakeLabel(pParse
);
8207 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
8208 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
8209 ** SELECT statement.
8211 pAggInfo
= sqlite3DbMallocZero(db
, sizeof(*pAggInfo
) );
8213 sqlite3ParserAddCleanup(pParse
, agginfoFree
, pAggInfo
);
8214 testcase( pParse
->earlyCleanup
);
8216 if( db
->mallocFailed
){
8219 pAggInfo
->selId
= p
->selId
;
8221 pAggInfo
->pSelect
= p
;
8223 memset(&sNC
, 0, sizeof(sNC
));
8224 sNC
.pParse
= pParse
;
8225 sNC
.pSrcList
= pTabList
;
8226 sNC
.uNC
.pAggInfo
= pAggInfo
;
8227 VVA_ONLY( sNC
.ncFlags
= NC_UAggInfo
; )
8228 pAggInfo
->nSortingColumn
= pGroupBy
? pGroupBy
->nExpr
: 0;
8229 pAggInfo
->pGroupBy
= pGroupBy
;
8230 sqlite3ExprAnalyzeAggList(&sNC
, pEList
);
8231 sqlite3ExprAnalyzeAggList(&sNC
, sSort
.pOrderBy
);
8234 assert( pWhere
==p
->pWhere
);
8235 assert( pHaving
==p
->pHaving
);
8236 assert( pGroupBy
==p
->pGroupBy
);
8237 havingToWhere(pParse
, p
);
8240 sqlite3ExprAnalyzeAggregates(&sNC
, pHaving
);
8242 pAggInfo
->nAccumulator
= pAggInfo
->nColumn
;
8243 if( p
->pGroupBy
==0 && p
->pHaving
==0 && pAggInfo
->nFunc
==1 ){
8244 minMaxFlag
= minMaxQuery(db
, pAggInfo
->aFunc
[0].pFExpr
, &pMinMaxOrderBy
);
8246 minMaxFlag
= WHERE_ORDERBY_NORMAL
;
8248 analyzeAggFuncArgs(pAggInfo
, &sNC
);
8249 if( db
->mallocFailed
) goto select_end
;
8250 #if TREETRACE_ENABLED
8251 if( sqlite3TreeTrace
& 0x20 ){
8252 TREETRACE(0x20,pParse
,p
,("After aggregate analysis %p:\n", pAggInfo
));
8253 sqlite3TreeViewSelect(0, p
, 0);
8255 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag
);
8256 sqlite3TreeViewExprList(0, pMinMaxOrderBy
, 0, "ORDERBY");
8258 printAggInfo(pAggInfo
);
8263 /* Processing for aggregates with GROUP BY is very different and
8264 ** much more complex than aggregates without a GROUP BY. tag-select-0810
8267 KeyInfo
*pKeyInfo
; /* Keying information for the group by clause */
8268 int addr1
; /* A-vs-B comparison jump */
8269 int addrOutputRow
; /* Start of subroutine that outputs a result row */
8270 int regOutputRow
; /* Return address register for output subroutine */
8271 int addrSetAbort
; /* Set the abort flag and return */
8272 int addrTopOfLoop
; /* Top of the input loop */
8273 int addrSortingIdx
; /* The OP_OpenEphemeral for the sorting index */
8274 int addrReset
; /* Subroutine for resetting the accumulator */
8275 int regReset
; /* Return address register for reset subroutine */
8276 ExprList
*pDistinct
= 0;
8278 int eDist
= WHERE_DISTINCT_NOOP
;
8280 if( pAggInfo
->nFunc
==1
8281 && pAggInfo
->aFunc
[0].iDistinct
>=0
8282 && ALWAYS(pAggInfo
->aFunc
[0].pFExpr
!=0)
8283 && ALWAYS(ExprUseXList(pAggInfo
->aFunc
[0].pFExpr
))
8284 && pAggInfo
->aFunc
[0].pFExpr
->x
.pList
!=0
8286 Expr
*pExpr
= pAggInfo
->aFunc
[0].pFExpr
->x
.pList
->a
[0].pExpr
;
8287 pExpr
= sqlite3ExprDup(db
, pExpr
, 0);
8288 pDistinct
= sqlite3ExprListDup(db
, pGroupBy
, 0);
8289 pDistinct
= sqlite3ExprListAppend(pParse
, pDistinct
, pExpr
);
8290 distFlag
= pDistinct
? (WHERE_WANT_DISTINCT
|WHERE_AGG_DISTINCT
) : 0;
8293 /* If there is a GROUP BY clause we might need a sorting index to
8294 ** implement it. Allocate that sorting index now. If it turns out
8295 ** that we do not need it after all, the OP_SorterOpen instruction
8296 ** will be converted into a Noop.
8298 pAggInfo
->sortingIdx
= pParse
->nTab
++;
8299 pKeyInfo
= sqlite3KeyInfoFromExprList(pParse
, pGroupBy
,
8300 0, pAggInfo
->nColumn
);
8301 addrSortingIdx
= sqlite3VdbeAddOp4(v
, OP_SorterOpen
,
8302 pAggInfo
->sortingIdx
, pAggInfo
->nSortingColumn
,
8303 0, (char*)pKeyInfo
, P4_KEYINFO
);
8305 /* Initialize memory locations used by GROUP BY aggregate processing
8307 iUseFlag
= ++pParse
->nMem
;
8308 iAbortFlag
= ++pParse
->nMem
;
8309 regOutputRow
= ++pParse
->nMem
;
8310 addrOutputRow
= sqlite3VdbeMakeLabel(pParse
);
8311 regReset
= ++pParse
->nMem
;
8312 addrReset
= sqlite3VdbeMakeLabel(pParse
);
8313 iAMem
= pParse
->nMem
+ 1;
8314 pParse
->nMem
+= pGroupBy
->nExpr
;
8315 iBMem
= pParse
->nMem
+ 1;
8316 pParse
->nMem
+= pGroupBy
->nExpr
;
8317 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, iAbortFlag
);
8318 VdbeComment((v
, "clear abort flag"));
8319 sqlite3VdbeAddOp3(v
, OP_Null
, 0, iAMem
, iAMem
+pGroupBy
->nExpr
-1);
8321 /* Begin a loop that will extract all source rows in GROUP BY order.
8322 ** This might involve two separate loops with an OP_Sort in between, or
8323 ** it might be a single loop that uses an index to extract information
8324 ** in the right order to begin with.
8326 sqlite3VdbeAddOp2(v
, OP_Gosub
, regReset
, addrReset
);
8327 TREETRACE(0x2,pParse
,p
,("WhereBegin\n"));
8328 pWInfo
= sqlite3WhereBegin(pParse
, pTabList
, pWhere
, pGroupBy
, pDistinct
,
8329 p
, (sDistinct
.isTnct
==2 ? WHERE_DISTINCTBY
: WHERE_GROUPBY
)
8330 | (orderByGrp
? WHERE_SORTBYGROUP
: 0) | distFlag
, 0
8333 sqlite3ExprListDelete(db
, pDistinct
);
8336 if( pParse
->pIdxEpr
){
8337 optimizeAggregateUseOfIndexedExpr(pParse
, p
, pAggInfo
, &sNC
);
8339 assignAggregateRegisters(pParse
, pAggInfo
);
8340 eDist
= sqlite3WhereIsDistinct(pWInfo
);
8341 TREETRACE(0x2,pParse
,p
,("WhereBegin returns\n"));
8342 if( sqlite3WhereIsOrdered(pWInfo
)==pGroupBy
->nExpr
){
8343 /* The optimizer is able to deliver rows in group by order so
8344 ** we do not have to sort. The OP_OpenEphemeral table will be
8345 ** cancelled later because we still need to use the pKeyInfo
8349 /* Rows are coming out in undetermined order. We have to push
8350 ** each row into a sorting index, terminate the first loop,
8351 ** then loop over the sorting index in order to get the output
8359 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
8360 int addrExp
; /* Address of OP_Explain instruction */
8362 ExplainQueryPlan2(addrExp
, (pParse
, 0, "USE TEMP B-TREE FOR %s",
8363 (sDistinct
.isTnct
&& (p
->selFlags
&SF_Distinct
)==0) ?
8364 "DISTINCT" : "GROUP BY"
8368 nGroupBy
= pGroupBy
->nExpr
;
8371 for(i
=0; i
<pAggInfo
->nColumn
; i
++){
8372 if( pAggInfo
->aCol
[i
].iSorterColumn
>=j
){
8377 regBase
= sqlite3GetTempRange(pParse
, nCol
);
8378 sqlite3ExprCodeExprList(pParse
, pGroupBy
, regBase
, 0, 0);
8380 pAggInfo
->directMode
= 1;
8381 for(i
=0; i
<pAggInfo
->nColumn
; i
++){
8382 struct AggInfo_col
*pCol
= &pAggInfo
->aCol
[i
];
8383 if( pCol
->iSorterColumn
>=j
){
8384 sqlite3ExprCode(pParse
, pCol
->pCExpr
, j
+ regBase
);
8388 pAggInfo
->directMode
= 0;
8389 regRecord
= sqlite3GetTempReg(pParse
);
8390 sqlite3VdbeScanStatusCounters(v
, addrExp
, 0, sqlite3VdbeCurrentAddr(v
));
8391 sqlite3VdbeAddOp3(v
, OP_MakeRecord
, regBase
, nCol
, regRecord
);
8392 sqlite3VdbeAddOp2(v
, OP_SorterInsert
, pAggInfo
->sortingIdx
, regRecord
);
8393 sqlite3VdbeScanStatusRange(v
, addrExp
, sqlite3VdbeCurrentAddr(v
)-2, -1);
8394 sqlite3ReleaseTempReg(pParse
, regRecord
);
8395 sqlite3ReleaseTempRange(pParse
, regBase
, nCol
);
8396 TREETRACE(0x2,pParse
,p
,("WhereEnd\n"));
8397 sqlite3WhereEnd(pWInfo
);
8398 pAggInfo
->sortingIdxPTab
= sortPTab
= pParse
->nTab
++;
8399 sortOut
= sqlite3GetTempReg(pParse
);
8400 sqlite3VdbeScanStatusCounters(v
, addrExp
, sqlite3VdbeCurrentAddr(v
), 0);
8401 sqlite3VdbeAddOp3(v
, OP_OpenPseudo
, sortPTab
, sortOut
, nCol
);
8402 sqlite3VdbeAddOp2(v
, OP_SorterSort
, pAggInfo
->sortingIdx
, addrEnd
);
8403 VdbeComment((v
, "GROUP BY sort")); VdbeCoverage(v
);
8404 pAggInfo
->useSortingIdx
= 1;
8405 sqlite3VdbeScanStatusRange(v
, addrExp
, -1, sortPTab
);
8406 sqlite3VdbeScanStatusRange(v
, addrExp
, -1, pAggInfo
->sortingIdx
);
8409 /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
8410 ** that are indexed (and that were previously identified and tagged
8411 ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions
8412 ** must now be converted into a TK_AGG_COLUMN node so that the value
8413 ** is correctly pulled from the index rather than being recomputed. */
8414 if( pParse
->pIdxEpr
){
8415 aggregateConvertIndexedExprRefToColumn(pAggInfo
);
8416 #if TREETRACE_ENABLED
8417 if( sqlite3TreeTrace
& 0x20 ){
8418 TREETRACE(0x20, pParse
, p
,
8419 ("AggInfo function expressions converted to reference index\n"));
8420 sqlite3TreeViewSelect(0, p
, 0);
8421 printAggInfo(pAggInfo
);
8426 /* If the index or temporary table used by the GROUP BY sort
8427 ** will naturally deliver rows in the order required by the ORDER BY
8428 ** clause, cancel the ephemeral table open coded earlier.
8430 ** This is an optimization - the correct answer should result regardless.
8431 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
8432 ** disable this optimization for testing purposes. */
8433 if( orderByGrp
&& OptimizationEnabled(db
, SQLITE_GroupByOrder
)
8434 && (groupBySort
|| sqlite3WhereIsSorted(pWInfo
))
8437 sqlite3VdbeChangeToNoop(v
, sSort
.addrSortIndex
);
8440 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
8441 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
8442 ** Then compare the current GROUP BY terms against the GROUP BY terms
8443 ** from the previous row currently stored in a0, a1, a2...
8445 addrTopOfLoop
= sqlite3VdbeCurrentAddr(v
);
8447 sqlite3VdbeAddOp3(v
, OP_SorterData
, pAggInfo
->sortingIdx
,
8450 for(j
=0; j
<pGroupBy
->nExpr
; j
++){
8451 int iOrderByCol
= pGroupBy
->a
[j
].u
.x
.iOrderByCol
;
8454 sqlite3VdbeAddOp3(v
, OP_Column
, sortPTab
, j
, iBMem
+j
);
8456 pAggInfo
->directMode
= 1;
8457 sqlite3ExprCode(pParse
, pGroupBy
->a
[j
].pExpr
, iBMem
+j
);
8461 Expr
*pX
= p
->pEList
->a
[iOrderByCol
-1].pExpr
;
8462 Expr
*pBase
= sqlite3ExprSkipCollateAndLikely(pX
);
8463 if( ALWAYS(pBase
!=0)
8464 && pBase
->op
!=TK_AGG_COLUMN
8465 && pBase
->op
!=TK_REGISTER
8467 sqlite3ExprToRegister(pX
, iAMem
+j
);
8471 sqlite3VdbeAddOp4(v
, OP_Compare
, iAMem
, iBMem
, pGroupBy
->nExpr
,
8472 (char*)sqlite3KeyInfoRef(pKeyInfo
), P4_KEYINFO
);
8473 addr1
= sqlite3VdbeCurrentAddr(v
);
8474 sqlite3VdbeAddOp3(v
, OP_Jump
, addr1
+1, 0, addr1
+1); VdbeCoverage(v
);
8476 /* Generate code that runs whenever the GROUP BY changes.
8477 ** Changes in the GROUP BY are detected by the previous code
8478 ** block. If there were no changes, this block is skipped.
8480 ** This code copies current group by terms in b0,b1,b2,...
8481 ** over to a0,a1,a2. It then calls the output subroutine
8482 ** and resets the aggregate accumulator registers in preparation
8483 ** for the next GROUP BY batch.
8485 sqlite3VdbeAddOp2(v
, OP_Gosub
, regOutputRow
, addrOutputRow
);
8486 VdbeComment((v
, "output one row"));
8487 sqlite3ExprCodeMove(pParse
, iBMem
, iAMem
, pGroupBy
->nExpr
);
8488 sqlite3VdbeAddOp2(v
, OP_IfPos
, iAbortFlag
, addrEnd
); VdbeCoverage(v
);
8489 VdbeComment((v
, "check abort flag"));
8490 sqlite3VdbeAddOp2(v
, OP_Gosub
, regReset
, addrReset
);
8491 VdbeComment((v
, "reset accumulator"));
8493 /* Update the aggregate accumulators based on the content of
8496 sqlite3VdbeJumpHere(v
, addr1
);
8497 updateAccumulator(pParse
, iUseFlag
, pAggInfo
, eDist
);
8498 sqlite3VdbeAddOp2(v
, OP_Integer
, 1, iUseFlag
);
8499 VdbeComment((v
, "indicate data in accumulator"));
8504 sqlite3VdbeAddOp2(v
, OP_SorterNext
, pAggInfo
->sortingIdx
,addrTopOfLoop
);
8507 TREETRACE(0x2,pParse
,p
,("WhereEnd\n"));
8508 sqlite3WhereEnd(pWInfo
);
8509 sqlite3VdbeChangeToNoop(v
, addrSortingIdx
);
8511 sqlite3ExprListDelete(db
, pDistinct
);
8513 /* Output the final row of result
8515 sqlite3VdbeAddOp2(v
, OP_Gosub
, regOutputRow
, addrOutputRow
);
8516 VdbeComment((v
, "output final row"));
8518 /* Jump over the subroutines
8520 sqlite3VdbeGoto(v
, addrEnd
);
8522 /* Generate a subroutine that outputs a single row of the result
8523 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
8524 ** is less than or equal to zero, the subroutine is a no-op. If
8525 ** the processing calls for the query to abort, this subroutine
8526 ** increments the iAbortFlag memory location before returning in
8527 ** order to signal the caller to abort.
8529 addrSetAbort
= sqlite3VdbeCurrentAddr(v
);
8530 sqlite3VdbeAddOp2(v
, OP_Integer
, 1, iAbortFlag
);
8531 VdbeComment((v
, "set abort flag"));
8532 sqlite3VdbeAddOp1(v
, OP_Return
, regOutputRow
);
8533 sqlite3VdbeResolveLabel(v
, addrOutputRow
);
8534 addrOutputRow
= sqlite3VdbeCurrentAddr(v
);
8535 sqlite3VdbeAddOp2(v
, OP_IfPos
, iUseFlag
, addrOutputRow
+2);
8537 VdbeComment((v
, "Groupby result generator entry point"));
8538 sqlite3VdbeAddOp1(v
, OP_Return
, regOutputRow
);
8539 finalizeAggFunctions(pParse
, pAggInfo
);
8540 sqlite3ExprIfFalse(pParse
, pHaving
, addrOutputRow
+1, SQLITE_JUMPIFNULL
);
8541 selectInnerLoop(pParse
, p
, -1, &sSort
,
8543 addrOutputRow
+1, addrSetAbort
);
8544 sqlite3VdbeAddOp1(v
, OP_Return
, regOutputRow
);
8545 VdbeComment((v
, "end groupby result generator"));
8547 /* Generate a subroutine that will reset the group-by accumulator
8549 sqlite3VdbeResolveLabel(v
, addrReset
);
8550 resetAccumulator(pParse
, pAggInfo
);
8551 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, iUseFlag
);
8552 VdbeComment((v
, "indicate accumulator empty"));
8553 sqlite3VdbeAddOp1(v
, OP_Return
, regReset
);
8555 if( distFlag
!=0 && eDist
!=WHERE_DISTINCT_NOOP
){
8556 struct AggInfo_func
*pF
= &pAggInfo
->aFunc
[0];
8557 fixDistinctOpenEph(pParse
, eDist
, pF
->iDistinct
, pF
->iDistAddr
);
8559 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
8561 /* Aggregate functions without GROUP BY. tag-select-0820 */
8563 if( (pTab
= isSimpleCount(p
, pAggInfo
))!=0 ){
8566 ** If isSimpleCount() returns a pointer to a Table structure, then
8567 ** the SQL statement is of the form:
8569 ** SELECT count(*) FROM <tbl>
8571 ** where the Table structure returned represents table <tbl>.
8573 ** This statement is so common that it is optimized specially. The
8574 ** OP_Count instruction is executed either on the intkey table that
8575 ** contains the data for table <tbl> or on one of its indexes. It
8576 ** is better to execute the op on an index, as indexes are almost
8577 ** always spread across less pages than their corresponding tables.
8579 const int iDb
= sqlite3SchemaToIndex(pParse
->db
, pTab
->pSchema
);
8580 const int iCsr
= pParse
->nTab
++; /* Cursor to scan b-tree */
8581 Index
*pIdx
; /* Iterator variable */
8582 KeyInfo
*pKeyInfo
= 0; /* Keyinfo for scanned index */
8583 Index
*pBest
= 0; /* Best index found so far */
8584 Pgno iRoot
= pTab
->tnum
; /* Root page of scanned b-tree */
8586 sqlite3CodeVerifySchema(pParse
, iDb
);
8587 sqlite3TableLock(pParse
, iDb
, pTab
->tnum
, 0, pTab
->zName
);
8589 /* Search for the index that has the lowest scan cost.
8591 ** (2011-04-15) Do not do a full scan of an unordered index.
8593 ** (2013-10-03) Do not count the entries in a partial index.
8595 ** In practice the KeyInfo structure will not be used. It is only
8596 ** passed to keep OP_OpenRead happy.
8598 if( !HasRowid(pTab
) ) pBest
= sqlite3PrimaryKeyIndex(pTab
);
8599 if( !p
->pSrc
->a
[0].fg
.notIndexed
){
8600 for(pIdx
=pTab
->pIndex
; pIdx
; pIdx
=pIdx
->pNext
){
8601 if( pIdx
->bUnordered
==0
8602 && pIdx
->szIdxRow
<pTab
->szTabRow
8603 && pIdx
->pPartIdxWhere
==0
8604 && (!pBest
|| pIdx
->szIdxRow
<pBest
->szIdxRow
)
8611 iRoot
= pBest
->tnum
;
8612 pKeyInfo
= sqlite3KeyInfoOfIndex(pParse
, pBest
);
8615 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
8616 sqlite3VdbeAddOp4Int(v
, OP_OpenRead
, iCsr
, (int)iRoot
, iDb
, 1);
8618 sqlite3VdbeChangeP4(v
, -1, (char *)pKeyInfo
, P4_KEYINFO
);
8620 assignAggregateRegisters(pParse
, pAggInfo
);
8621 sqlite3VdbeAddOp2(v
, OP_Count
, iCsr
, AggInfoFuncReg(pAggInfo
,0));
8622 sqlite3VdbeAddOp1(v
, OP_Close
, iCsr
);
8623 explainSimpleCount(pParse
, pTab
, pBest
);
8625 /* The general case of an aggregate query without GROUP BY
8626 ** tag-select-0822 */
8627 int regAcc
= 0; /* "populate accumulators" flag */
8628 ExprList
*pDistinct
= 0;
8632 /* If there are accumulator registers but no min() or max() functions
8633 ** without FILTER clauses, allocate register regAcc. Register regAcc
8634 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
8635 ** The code generated by updateAccumulator() uses this to ensure
8636 ** that the accumulator registers are (a) updated only once if
8637 ** there are no min() or max functions or (b) always updated for the
8638 ** first row visited by the aggregate, so that they are updated at
8639 ** least once even if the FILTER clause means the min() or max()
8640 ** function visits zero rows. */
8641 if( pAggInfo
->nAccumulator
){
8642 for(i
=0; i
<pAggInfo
->nFunc
; i
++){
8643 if( ExprHasProperty(pAggInfo
->aFunc
[i
].pFExpr
, EP_WinFunc
) ){
8646 if( pAggInfo
->aFunc
[i
].pFunc
->funcFlags
&SQLITE_FUNC_NEEDCOLL
){
8650 if( i
==pAggInfo
->nFunc
){
8651 regAcc
= ++pParse
->nMem
;
8652 sqlite3VdbeAddOp2(v
, OP_Integer
, 0, regAcc
);
8654 }else if( pAggInfo
->nFunc
==1 && pAggInfo
->aFunc
[0].iDistinct
>=0 ){
8655 assert( ExprUseXList(pAggInfo
->aFunc
[0].pFExpr
) );
8656 pDistinct
= pAggInfo
->aFunc
[0].pFExpr
->x
.pList
;
8657 distFlag
= pDistinct
? (WHERE_WANT_DISTINCT
|WHERE_AGG_DISTINCT
) : 0;
8659 assignAggregateRegisters(pParse
, pAggInfo
);
8661 /* This case runs if the aggregate has no GROUP BY clause. The
8662 ** processing is much simpler since there is only a single row
8665 assert( p
->pGroupBy
==0 );
8666 resetAccumulator(pParse
, pAggInfo
);
8668 /* If this query is a candidate for the min/max optimization, then
8669 ** minMaxFlag will have been previously set to either
8670 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
8671 ** be an appropriate ORDER BY expression for the optimization.
8673 assert( minMaxFlag
==WHERE_ORDERBY_NORMAL
|| pMinMaxOrderBy
!=0 );
8674 assert( pMinMaxOrderBy
==0 || pMinMaxOrderBy
->nExpr
==1 );
8676 TREETRACE(0x2,pParse
,p
,("WhereBegin\n"));
8677 pWInfo
= sqlite3WhereBegin(pParse
, pTabList
, pWhere
, pMinMaxOrderBy
,
8678 pDistinct
, p
, minMaxFlag
|distFlag
, 0);
8682 TREETRACE(0x2,pParse
,p
,("WhereBegin returns\n"));
8683 eDist
= sqlite3WhereIsDistinct(pWInfo
);
8684 updateAccumulator(pParse
, regAcc
, pAggInfo
, eDist
);
8685 if( eDist
!=WHERE_DISTINCT_NOOP
){
8686 struct AggInfo_func
*pF
= pAggInfo
->aFunc
;
8688 fixDistinctOpenEph(pParse
, eDist
, pF
->iDistinct
, pF
->iDistAddr
);
8692 if( regAcc
) sqlite3VdbeAddOp2(v
, OP_Integer
, 1, regAcc
);
8694 sqlite3WhereMinMaxOptEarlyOut(v
, pWInfo
);
8696 TREETRACE(0x2,pParse
,p
,("WhereEnd\n"));
8697 sqlite3WhereEnd(pWInfo
);
8698 finalizeAggFunctions(pParse
, pAggInfo
);
8702 sqlite3ExprIfFalse(pParse
, pHaving
, addrEnd
, SQLITE_JUMPIFNULL
);
8703 selectInnerLoop(pParse
, p
, -1, 0, 0,
8704 pDest
, addrEnd
, addrEnd
);
8706 sqlite3VdbeResolveLabel(v
, addrEnd
);
8708 } /* endif aggregate query */
8710 if( sDistinct
.eTnctType
==WHERE_DISTINCT_UNORDERED
){
8711 explainTempTable(pParse
, "DISTINCT");
8714 /* If there is an ORDER BY clause, then we need to sort the results
8715 ** and send them to the callback one by one. tag-select-0900
8717 if( sSort
.pOrderBy
){
8718 assert( p
->pEList
==pEList
);
8719 generateSortTail(pParse
, p
, &sSort
, pEList
->nExpr
, pDest
);
8722 /* Jump here to skip this query
8724 sqlite3VdbeResolveLabel(v
, iEnd
);
8726 /* The SELECT has been coded. If there is an error in the Parse structure,
8727 ** set the return code to 1. Otherwise 0. */
8728 rc
= (pParse
->nErr
>0);
8730 /* Control jumps to here if an error is encountered above, or upon
8731 ** successful coding of the SELECT.
8734 assert( db
->mallocFailed
==0 || db
->mallocFailed
==1 );
8735 assert( db
->mallocFailed
==0 || pParse
->nErr
!=0 );
8736 sqlite3ExprListDelete(db
, pMinMaxOrderBy
);
8738 /* Internal self-checks. tag-select-1000 */
8739 if( pAggInfo
&& !db
->mallocFailed
){
8740 #if TREETRACE_ENABLED
8741 if( sqlite3TreeTrace
& 0x20 ){
8742 TREETRACE(0x20,pParse
,p
,("Finished with AggInfo\n"));
8743 printAggInfo(pAggInfo
);
8746 for(i
=0; i
<pAggInfo
->nColumn
; i
++){
8747 Expr
*pExpr
= pAggInfo
->aCol
[i
].pCExpr
;
8748 if( pExpr
==0 ) continue;
8749 assert( pExpr
->pAggInfo
==pAggInfo
);
8750 assert( pExpr
->iAgg
==i
);
8752 for(i
=0; i
<pAggInfo
->nFunc
; i
++){
8753 Expr
*pExpr
= pAggInfo
->aFunc
[i
].pFExpr
;
8755 assert( pExpr
->pAggInfo
==pAggInfo
);
8756 assert( pExpr
->iAgg
==i
);
8761 #if TREETRACE_ENABLED
8762 TREETRACE(0x1,pParse
,p
,("end processing\n"));
8763 if( (sqlite3TreeTrace
& 0x40000)!=0 && ExplainQueryPlanParent(pParse
)==0 ){
8764 sqlite3TreeViewSelect(0, p
, 0);
8767 ExplainQueryPlanPop(pParse
);