|
- diff -Npur sqlite-version-3.32.2/src/expr.c sqlite-version-3.32.2-patched/src/expr.c
- --- sqlite-version-3.32.2/src/expr.c 2020-06-04 20:58:43.000000000 +0800
- +++ sqlite-version-3.32.2-patched/src/expr.c 2020-07-08 10:00:47.367088648 +0800
- @@ -3813,6 +3813,7 @@ expr_code_doover:
- AggInfo *pAggInfo = pExpr->pAggInfo;
- struct AggInfo_col *pCol;
- assert( pAggInfo!=0 );
- + assert( AggInfoValid(pAggInfo) );
- assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
- pCol = &pAggInfo->aCol[pExpr->iAgg];
- if( !pAggInfo->directMode ){
- @@ -4121,6 +4122,7 @@ expr_code_doover:
- assert( !ExprHasProperty(pExpr, EP_IntValue) );
- sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
- }else{
- + assert( AggInfoValid(pInfo) );
- return pInfo->aFunc[pExpr->iAgg].iMem;
- }
- break;
- @@ -5658,13 +5660,7 @@ struct SrcCount {
- ** Count the number of references to columns.
- */
- static int exprSrcCount(Walker *pWalker, Expr *pExpr){
- - /* There was once a NEVER() on the second term on the grounds that
- - ** sqlite3FunctionUsesThisSrc() was always called before
- - ** sqlite3ExprAnalyzeAggregates() and so the TK_COLUMNs have not yet
- - ** been converted into TK_AGG_COLUMN. But this is no longer true due
- - ** to window functions - sqlite3WindowRewrite() may now indirectly call
- - ** FunctionUsesThisSrc() when creating a new sub-select. */
- - if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
- + if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
- int i;
- struct SrcCount *p = pWalker->u.pSrcCount;
- SrcList *pSrc = p->pSrc;
- diff -Npur sqlite-version-3.32.2/src/global.c sqlite-version-3.32.2-patched/src/global.c
- --- sqlite-version-3.32.2/src/global.c 2020-06-04 20:58:43.000000000 +0800
- +++ sqlite-version-3.32.2-patched/src/global.c 2020-07-08 10:00:47.367088648 +0800
- @@ -300,6 +300,11 @@ sqlite3_uint64 sqlite3NProfileCnt = 0;
- int sqlite3PendingByte = 0x40000000;
- #endif
-
- +/*
- +** Flags for select tracing and the ".selecttrace" macro of the CLI
- +*/
- +/**/ u32 sqlite3SelectTrace = 0;
- +
- #include "opcodes.h"
- /*
- ** Properties of opcodes. The OPFLG_INITIALIZER macro is
- diff -Npur sqlite-version-3.32.2/src/resolve.c sqlite-version-3.32.2-patched/src/resolve.c
- --- sqlite-version-3.32.2/src/resolve.c 2020-06-04 20:58:43.000000000 +0800
- +++ sqlite-version-3.32.2-patched/src/resolve.c 2020-07-08 10:00:47.367088648 +0800
- @@ -1715,6 +1715,14 @@ static int resolveSelectStep(Walker *pWa
- return WRC_Abort;
- }
- }
- + }else if( p->pWin && ALWAYS( (p->selFlags & SF_WinRewrite)==0 ) ){
- + sqlite3WindowRewrite(pParse, p);
- +#if SELECTTRACE_ENABLED
- + if( (sqlite3SelectTrace & 0x108)!=0 ){
- + SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- }
- #endif
-
- diff -Npur sqlite-version-3.32.2/src/select.c sqlite-version-3.32.2-patched/src/select.c
- --- sqlite-version-3.32.2/src/select.c 2020-06-04 20:58:43.000000000 +0800
- +++ sqlite-version-3.32.2-patched/src/select.c 2020-07-08 10:00:50.899152517 +0800
- @@ -15,20 +15,6 @@
- #include "sqliteInt.h"
-
- /*
- -** Trace output macros
- -*/
- -#if SELECTTRACE_ENABLED
- -/***/ int sqlite3SelectTrace = 0;
- -# define SELECTTRACE(K,P,S,X) \
- - if(sqlite3SelectTrace&(K)) \
- - sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
- - sqlite3DebugPrintf X
- -#else
- -# define SELECTTRACE(K,P,S,X)
- -#endif
- -
- -
- -/*
- ** An instance of the following object is used to record information about
- ** how to process the DISTINCT keyword, to simplify passing that information
- ** into the selectInnerLoop() routine.
- @@ -2717,9 +2703,7 @@ static int multiSelect(
- selectOpName(p->op)));
- rc = sqlite3Select(pParse, p, &uniondest);
- testcase( rc!=SQLITE_OK );
- - /* Query flattening in sqlite3Select() might refill p->pOrderBy.
- - ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
- - sqlite3ExprListDelete(db, p->pOrderBy);
- + assert( p->pOrderBy==0 );
- pDelete = p->pPrior;
- p->pPrior = pPrior;
- p->pOrderBy = 0;
- @@ -4105,7 +4089,7 @@ static int flattenSubquery(
- ** We look at every expression in the outer query and every place we see
- ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
- */
- - if( pSub->pOrderBy ){
- + if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
- /* At this point, any non-zero iOrderByCol values indicate that the
- ** ORDER BY column expression is identical to the iOrderByCol'th
- ** expression returned by SELECT statement pSub. Since these values
- @@ -4426,11 +4410,14 @@ static int pushDownWhereTerms(
- ){
- Expr *pNew;
- int nChng = 0;
- + Select *pSel;
- if( pWhere==0 ) return 0;
- if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
-
- #ifndef SQLITE_OMIT_WINDOWFUNC
- - if( pSubq->pWin ) return 0; /* restriction (6) */
- + for(pSel=pSubq; pSel; pSel=pSel->pPrior){
- + if( pSel->pWin ) return 0; /* restriction (6) */
- + }
- #endif
-
- #ifdef SQLITE_DEBUG
- @@ -5766,6 +5753,9 @@ int sqlite3Select(
- }
- if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
- memset(&sAggInfo, 0, sizeof(sAggInfo));
- +#ifdef SQLITE_DEBUG
- + sAggInfo.iAggMagic = SQLITE_AGGMAGIC_VALID;
- +#endif
- #if SELECTTRACE_ENABLED
- SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
- if( sqlite3SelectTrace & 0x100 ){
- @@ -5787,6 +5777,7 @@ int sqlite3Select(
- sqlite3ExprListDelete(db, p->pOrderBy);
- p->pOrderBy = 0;
- p->selFlags &= ~SF_Distinct;
- + p->selFlags |= SF_NoopOrderBy;
- }
- sqlite3SelectPrep(pParse, p, 0);
- if( pParse->nErr || db->mallocFailed ){
- @@ -5804,19 +5795,6 @@ int sqlite3Select(
- generateColumnNames(pParse, p);
- }
-
- -#ifndef SQLITE_OMIT_WINDOWFUNC
- - rc = sqlite3WindowRewrite(pParse, p);
- - if( rc ){
- - assert( db->mallocFailed || pParse->nErr>0 );
- - goto select_end;
- - }
- -#if SELECTTRACE_ENABLED
- - if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){
- - SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
- - sqlite3TreeViewSelect(0, p, 0);
- - }
- -#endif
- -#endif /* SQLITE_OMIT_WINDOWFUNC */
- pTabList = p->pSrc;
- isAgg = (p->selFlags & SF_Aggregate)!=0;
- memset(&sSort, 0, sizeof(sSort));
- @@ -6144,7 +6122,7 @@ int sqlite3Select(
- if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
- && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
- #ifndef SQLITE_OMIT_WINDOWFUNC
- - && p->pWin==0
- + && ALWAYS(p->pWin==0)
- #endif
- ){
- p->selFlags &= ~SF_Distinct;
- @@ -6791,6 +6769,14 @@ int sqlite3Select(
- select_end:
- sqlite3ExprListDelete(db, pMinMaxOrderBy);
- sqlite3DbFree(db, sAggInfo.aCol);
- +#ifdef SQLITE_DEBUG
- + for(i=0; i<sAggInfo.nFunc; i++){
- + assert( sAggInfo.aFunc[i].pExpr!=0 );
- + assert( sAggInfo.aFunc[i].pExpr->pAggInfo==&sAggInfo );
- + sAggInfo.aFunc[i].pExpr->pAggInfo = 0;
- + }
- + sAggInfo.iAggMagic = 0;
- +#endif
- sqlite3DbFree(db, sAggInfo.aFunc);
- #if SELECTTRACE_ENABLED
- SELECTTRACE(0x1,pParse,p,("end processing\n"));
- diff -Npur sqlite-version-3.32.2/src/select.c.orig sqlite-version-3.32.2-patched/src/select.c.orig
- --- sqlite-version-3.32.2/src/select.c.orig 1970-01-01 08:00:00.000000000 +0800
- +++ sqlite-version-3.32.2-patched/src/select.c.orig 2020-07-08 10:00:47.367088648 +0800
- @@ -0,0 +1,6790 @@
- +/*
- +** 2001 September 15
- +**
- +** The author disclaims copyright to this source code. In place of
- +** a legal notice, here is a blessing:
- +**
- +** May you do good and not evil.
- +** May you find forgiveness for yourself and forgive others.
- +** May you share freely, never taking more than you give.
- +**
- +*************************************************************************
- +** This file contains C code routines that are called by the parser
- +** to handle SELECT statements in SQLite.
- +*/
- +#include "sqliteInt.h"
- +
- +/*
- +** An instance of the following object is used to record information about
- +** how to process the DISTINCT keyword, to simplify passing that information
- +** into the selectInnerLoop() routine.
- +*/
- +typedef struct DistinctCtx DistinctCtx;
- +struct DistinctCtx {
- + u8 isTnct; /* True if the DISTINCT keyword is present */
- + u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
- + int tabTnct; /* Ephemeral table used for DISTINCT processing */
- + int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
- +};
- +
- +/*
- +** An instance of the following object is used to record information about
- +** the ORDER BY (or GROUP BY) clause of query is being coded.
- +**
- +** The aDefer[] array is used by the sorter-references optimization. For
- +** example, assuming there is no index that can be used for the ORDER BY,
- +** for the query:
- +**
- +** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
- +**
- +** it may be more efficient to add just the "a" values to the sorter, and
- +** retrieve the associated "bigblob" values directly from table t1 as the
- +** 10 smallest "a" values are extracted from the sorter.
- +**
- +** When the sorter-reference optimization is used, there is one entry in the
- +** aDefer[] array for each database table that may be read as values are
- +** extracted from the sorter.
- +*/
- +typedef struct SortCtx SortCtx;
- +struct SortCtx {
- + ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
- + int nOBSat; /* Number of ORDER BY terms satisfied by indices */
- + int iECursor; /* Cursor number for the sorter */
- + int regReturn; /* Register holding block-output return address */
- + int labelBkOut; /* Start label for the block-output subroutine */
- + int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
- + int labelDone; /* Jump here when done, ex: LIMIT reached */
- + int labelOBLopt; /* Jump here when sorter is full */
- + u8 sortFlags; /* Zero or more SORTFLAG_* bits */
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + u8 nDefer; /* Number of valid entries in aDefer[] */
- + struct DeferredCsr {
- + Table *pTab; /* Table definition */
- + int iCsr; /* Cursor number for table */
- + int nKey; /* Number of PK columns for table pTab (>=1) */
- + } aDefer[4];
- +#endif
- + struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
- +};
- +#define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
- +
- +/*
- +** Delete all the content of a Select structure. Deallocate the structure
- +** itself depending on the value of bFree
- +**
- +** If bFree==1, call sqlite3DbFree() on the p object.
- +** If bFree==0, Leave the first Select object unfreed
- +*/
- +static void clearSelect(sqlite3 *db, Select *p, int bFree){
- + while( p ){
- + Select *pPrior = p->pPrior;
- + sqlite3ExprListDelete(db, p->pEList);
- + sqlite3SrcListDelete(db, p->pSrc);
- + sqlite3ExprDelete(db, p->pWhere);
- + sqlite3ExprListDelete(db, p->pGroupBy);
- + sqlite3ExprDelete(db, p->pHaving);
- + sqlite3ExprListDelete(db, p->pOrderBy);
- + sqlite3ExprDelete(db, p->pLimit);
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
- + sqlite3WindowListDelete(db, p->pWinDefn);
- + }
- +#endif
- + if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
- + if( bFree ) sqlite3DbFreeNN(db, p);
- + p = pPrior;
- + bFree = 1;
- + }
- +}
- +
- +/*
- +** Initialize a SelectDest structure.
- +*/
- +void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
- + pDest->eDest = (u8)eDest;
- + pDest->iSDParm = iParm;
- + pDest->zAffSdst = 0;
- + pDest->iSdst = 0;
- + pDest->nSdst = 0;
- +}
- +
- +
- +/*
- +** Allocate a new Select structure and return a pointer to that
- +** structure.
- +*/
- +Select *sqlite3SelectNew(
- + Parse *pParse, /* Parsing context */
- + ExprList *pEList, /* which columns to include in the result */
- + SrcList *pSrc, /* the FROM clause -- which tables to scan */
- + Expr *pWhere, /* the WHERE clause */
- + ExprList *pGroupBy, /* the GROUP BY clause */
- + Expr *pHaving, /* the HAVING clause */
- + ExprList *pOrderBy, /* the ORDER BY clause */
- + u32 selFlags, /* Flag parameters, such as SF_Distinct */
- + Expr *pLimit /* LIMIT value. NULL means not used */
- +){
- + Select *pNew;
- + Select standin;
- + pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
- + if( pNew==0 ){
- + assert( pParse->db->mallocFailed );
- + pNew = &standin;
- + }
- + if( pEList==0 ){
- + pEList = sqlite3ExprListAppend(pParse, 0,
- + sqlite3Expr(pParse->db,TK_ASTERISK,0));
- + }
- + pNew->pEList = pEList;
- + pNew->op = TK_SELECT;
- + pNew->selFlags = selFlags;
- + pNew->iLimit = 0;
- + pNew->iOffset = 0;
- + pNew->selId = ++pParse->nSelect;
- + pNew->addrOpenEphm[0] = -1;
- + pNew->addrOpenEphm[1] = -1;
- + pNew->nSelectRow = 0;
- + if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
- + pNew->pSrc = pSrc;
- + pNew->pWhere = pWhere;
- + pNew->pGroupBy = pGroupBy;
- + pNew->pHaving = pHaving;
- + pNew->pOrderBy = pOrderBy;
- + pNew->pPrior = 0;
- + pNew->pNext = 0;
- + pNew->pLimit = pLimit;
- + pNew->pWith = 0;
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + pNew->pWin = 0;
- + pNew->pWinDefn = 0;
- +#endif
- + if( pParse->db->mallocFailed ) {
- + clearSelect(pParse->db, pNew, pNew!=&standin);
- + pNew = 0;
- + }else{
- + assert( pNew->pSrc!=0 || pParse->nErr>0 );
- + }
- + assert( pNew!=&standin );
- + return pNew;
- +}
- +
- +
- +/*
- +** Delete the given Select structure and all of its substructures.
- +*/
- +void sqlite3SelectDelete(sqlite3 *db, Select *p){
- + if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1);
- +}
- +
- +/*
- +** Delete all the substructure for p, but keep p allocated. Redefine
- +** p to be a single SELECT where every column of the result set has a
- +** value of NULL.
- +*/
- +void sqlite3SelectReset(Parse *pParse, Select *p){
- + if( ALWAYS(p) ){
- + clearSelect(pParse->db, p, 0);
- + memset(&p->iLimit, 0, sizeof(Select) - offsetof(Select,iLimit));
- + p->pEList = sqlite3ExprListAppend(pParse, 0,
- + sqlite3ExprAlloc(pParse->db,TK_NULL,0,0));
- + p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(SrcList));
- + }
- +}
- +
- +/*
- +** Return a pointer to the right-most SELECT statement in a compound.
- +*/
- +static Select *findRightmost(Select *p){
- + while( p->pNext ) p = p->pNext;
- + return p;
- +}
- +
- +/*
- +** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
- +** type of join. Return an integer constant that expresses that type
- +** in terms of the following bit values:
- +**
- +** JT_INNER
- +** JT_CROSS
- +** JT_OUTER
- +** JT_NATURAL
- +** JT_LEFT
- +** JT_RIGHT
- +**
- +** A full outer join is the combination of JT_LEFT and JT_RIGHT.
- +**
- +** If an illegal or unsupported join type is seen, then still return
- +** a join type, but put an error in the pParse structure.
- +*/
- +int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
- + int jointype = 0;
- + Token *apAll[3];
- + Token *p;
- + /* 0123456789 123456789 123456789 123 */
- + static const char zKeyText[] = "naturaleftouterightfullinnercross";
- + static const struct {
- + u8 i; /* Beginning of keyword text in zKeyText[] */
- + u8 nChar; /* Length of the keyword in characters */
- + u8 code; /* Join type mask */
- + } aKeyword[] = {
- + /* natural */ { 0, 7, JT_NATURAL },
- + /* left */ { 6, 4, JT_LEFT|JT_OUTER },
- + /* outer */ { 10, 5, JT_OUTER },
- + /* right */ { 14, 5, JT_RIGHT|JT_OUTER },
- + /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
- + /* inner */ { 23, 5, JT_INNER },
- + /* cross */ { 28, 5, JT_INNER|JT_CROSS },
- + };
- + int i, j;
- + apAll[0] = pA;
- + apAll[1] = pB;
- + apAll[2] = pC;
- + for(i=0; i<3 && apAll[i]; i++){
- + p = apAll[i];
- + for(j=0; j<ArraySize(aKeyword); j++){
- + if( p->n==aKeyword[j].nChar
- + && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
- + jointype |= aKeyword[j].code;
- + break;
- + }
- + }
- + testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
- + if( j>=ArraySize(aKeyword) ){
- + jointype |= JT_ERROR;
- + break;
- + }
- + }
- + if(
- + (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
- + (jointype & JT_ERROR)!=0
- + ){
- + const char *zSp = " ";
- + assert( pB!=0 );
- + if( pC==0 ){ zSp++; }
- + sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
- + "%T %T%s%T", pA, pB, zSp, pC);
- + jointype = JT_INNER;
- + }else if( (jointype & JT_OUTER)!=0
- + && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
- + sqlite3ErrorMsg(pParse,
- + "RIGHT and FULL OUTER JOINs are not currently supported");
- + jointype = JT_INNER;
- + }
- + return jointype;
- +}
- +
- +/*
- +** Return the index of a column in a table. Return -1 if the column
- +** is not contained in the table.
- +*/
- +static int columnIndex(Table *pTab, const char *zCol){
- + int i;
- + for(i=0; i<pTab->nCol; i++){
- + if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
- + }
- + return -1;
- +}
- +
- +/*
- +** Search the first N tables in pSrc, from left to right, looking for a
- +** table that has a column named zCol.
- +**
- +** When found, set *piTab and *piCol to the table index and column index
- +** of the matching column and return TRUE.
- +**
- +** If not found, return FALSE.
- +*/
- +static int tableAndColumnIndex(
- + SrcList *pSrc, /* Array of tables to search */
- + int N, /* Number of tables in pSrc->a[] to search */
- + const char *zCol, /* Name of the column we are looking for */
- + int *piTab, /* Write index of pSrc->a[] here */
- + int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
- + int bIgnoreHidden /* True to ignore hidden columns */
- +){
- + int i; /* For looping over tables in pSrc */
- + int iCol; /* Index of column matching zCol */
- +
- + assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */
- + for(i=0; i<N; i++){
- + iCol = columnIndex(pSrc->a[i].pTab, zCol);
- + if( iCol>=0
- + && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
- + ){
- + if( piTab ){
- + *piTab = i;
- + *piCol = iCol;
- + }
- + return 1;
- + }
- + }
- + return 0;
- +}
- +
- +/*
- +** This function is used to add terms implied by JOIN syntax to the
- +** WHERE clause expression of a SELECT statement. The new term, which
- +** is ANDed with the existing WHERE clause, is of the form:
- +**
- +** (tab1.col1 = tab2.col2)
- +**
- +** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
- +** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
- +** column iColRight of tab2.
- +*/
- +static void addWhereTerm(
- + Parse *pParse, /* Parsing context */
- + SrcList *pSrc, /* List of tables in FROM clause */
- + int iLeft, /* Index of first table to join in pSrc */
- + int iColLeft, /* Index of column in first table */
- + int iRight, /* Index of second table in pSrc */
- + int iColRight, /* Index of column in second table */
- + int isOuterJoin, /* True if this is an OUTER join */
- + Expr **ppWhere /* IN/OUT: The WHERE clause to add to */
- +){
- + sqlite3 *db = pParse->db;
- + Expr *pE1;
- + Expr *pE2;
- + Expr *pEq;
- +
- + assert( iLeft<iRight );
- + assert( pSrc->nSrc>iRight );
- + assert( pSrc->a[iLeft].pTab );
- + assert( pSrc->a[iRight].pTab );
- +
- + pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
- + pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
- +
- + pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
- + if( pEq && isOuterJoin ){
- + ExprSetProperty(pEq, EP_FromJoin);
- + assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
- + ExprSetVVAProperty(pEq, EP_NoReduce);
- + pEq->iRightJoinTable = (i16)pE2->iTable;
- + }
- + *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq);
- +}
- +
- +/*
- +** Set the EP_FromJoin property on all terms of the given expression.
- +** And set the Expr.iRightJoinTable to iTable for every term in the
- +** expression.
- +**
- +** The EP_FromJoin property is used on terms of an expression to tell
- +** the LEFT OUTER JOIN processing logic that this term is part of the
- +** join restriction specified in the ON or USING clause and not a part
- +** of the more general WHERE clause. These terms are moved over to the
- +** WHERE clause during join processing but we need to remember that they
- +** originated in the ON or USING clause.
- +**
- +** The Expr.iRightJoinTable tells the WHERE clause processing that the
- +** expression depends on table iRightJoinTable even if that table is not
- +** explicitly mentioned in the expression. That information is needed
- +** for cases like this:
- +**
- +** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
- +**
- +** The where clause needs to defer the handling of the t1.x=5
- +** term until after the t2 loop of the join. In that way, a
- +** NULL t2 row will be inserted whenever t1.x!=5. If we do not
- +** defer the handling of t1.x=5, it will be processed immediately
- +** after the t1 loop and rows with t1.x!=5 will never appear in
- +** the output, which is incorrect.
- +*/
- +void sqlite3SetJoinExpr(Expr *p, int iTable){
- + while( p ){
- + ExprSetProperty(p, EP_FromJoin);
- + assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
- + ExprSetVVAProperty(p, EP_NoReduce);
- + p->iRightJoinTable = (i16)iTable;
- + if( p->op==TK_FUNCTION && p->x.pList ){
- + int i;
- + for(i=0; i<p->x.pList->nExpr; i++){
- + sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable);
- + }
- + }
- + sqlite3SetJoinExpr(p->pLeft, iTable);
- + p = p->pRight;
- + }
- +}
- +
- +/* Undo the work of sqlite3SetJoinExpr(). In the expression p, convert every
- +** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
- +** an ordinary term that omits the EP_FromJoin mark.
- +**
- +** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
- +*/
- +static void unsetJoinExpr(Expr *p, int iTable){
- + while( p ){
- + if( ExprHasProperty(p, EP_FromJoin)
- + && (iTable<0 || p->iRightJoinTable==iTable) ){
- + ExprClearProperty(p, EP_FromJoin);
- + }
- + if( p->op==TK_FUNCTION && p->x.pList ){
- + int i;
- + for(i=0; i<p->x.pList->nExpr; i++){
- + unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
- + }
- + }
- + unsetJoinExpr(p->pLeft, iTable);
- + p = p->pRight;
- + }
- +}
- +
- +/*
- +** This routine processes the join information for a SELECT statement.
- +** ON and USING clauses are converted into extra terms of the WHERE clause.
- +** NATURAL joins also create extra WHERE clause terms.
- +**
- +** The terms of a FROM clause are contained in the Select.pSrc structure.
- +** The left most table is the first entry in Select.pSrc. The right-most
- +** table is the last entry. The join operator is held in the entry to
- +** the left. Thus entry 0 contains the join operator for the join between
- +** entries 0 and 1. Any ON or USING clauses associated with the join are
- +** also attached to the left entry.
- +**
- +** This routine returns the number of errors encountered.
- +*/
- +static int sqliteProcessJoin(Parse *pParse, Select *p){
- + SrcList *pSrc; /* All tables in the FROM clause */
- + int i, j; /* Loop counters */
- + struct SrcList_item *pLeft; /* Left table being joined */
- + struct SrcList_item *pRight; /* Right table being joined */
- +
- + pSrc = p->pSrc;
- + pLeft = &pSrc->a[0];
- + pRight = &pLeft[1];
- + for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
- + Table *pRightTab = pRight->pTab;
- + int isOuter;
- +
- + if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
- + isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
- +
- + /* When the NATURAL keyword is present, add WHERE clause terms for
- + ** every column that the two tables have in common.
- + */
- + if( pRight->fg.jointype & JT_NATURAL ){
- + if( pRight->pOn || pRight->pUsing ){
- + sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
- + "an ON or USING clause", 0);
- + return 1;
- + }
- + for(j=0; j<pRightTab->nCol; j++){
- + char *zName; /* Name of column in the right table */
- + int iLeft; /* Matching left table */
- + int iLeftCol; /* Matching column in the left table */
- +
- + if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
- + zName = pRightTab->aCol[j].zName;
- + if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol, 1) ){
- + addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
- + isOuter, &p->pWhere);
- + }
- + }
- + }
- +
- + /* Disallow both ON and USING clauses in the same join
- + */
- + if( pRight->pOn && pRight->pUsing ){
- + sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
- + "clauses in the same join");
- + return 1;
- + }
- +
- + /* Add the ON clause to the end of the WHERE clause, connected by
- + ** an AND operator.
- + */
- + if( pRight->pOn ){
- + if( isOuter ) sqlite3SetJoinExpr(pRight->pOn, pRight->iCursor);
- + p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->pOn);
- + pRight->pOn = 0;
- + }
- +
- + /* Create extra terms on the WHERE clause for each column named
- + ** in the USING clause. Example: If the two tables to be joined are
- + ** A and B and the USING clause names X, Y, and Z, then add this
- + ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
- + ** Report an error if any column mentioned in the USING clause is
- + ** not contained in both tables to be joined.
- + */
- + if( pRight->pUsing ){
- + IdList *pList = pRight->pUsing;
- + for(j=0; j<pList->nId; j++){
- + char *zName; /* Name of the term in the USING clause */
- + int iLeft; /* Table on the left with matching column name */
- + int iLeftCol; /* Column number of matching column on the left */
- + int iRightCol; /* Column number of matching column on the right */
- +
- + zName = pList->a[j].zName;
- + iRightCol = columnIndex(pRightTab, zName);
- + if( iRightCol<0
- + || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol, 0)
- + ){
- + sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
- + "not present in both tables", zName);
- + return 1;
- + }
- + addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
- + isOuter, &p->pWhere);
- + }
- + }
- + }
- + return 0;
- +}
- +
- +/*
- +** An instance of this object holds information (beyond pParse and pSelect)
- +** needed to load the next result row that is to be added to the sorter.
- +*/
- +typedef struct RowLoadInfo RowLoadInfo;
- +struct RowLoadInfo {
- + int regResult; /* Store results in array of registers here */
- + u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + ExprList *pExtra; /* Extra columns needed by sorter refs */
- + int regExtraResult; /* Where to load the extra columns */
- +#endif
- +};
- +
- +/*
- +** This routine does the work of loading query data into an array of
- +** registers so that it can be added to the sorter.
- +*/
- +static void innerLoopLoadRow(
- + Parse *pParse, /* Statement under construction */
- + Select *pSelect, /* The query being coded */
- + RowLoadInfo *pInfo /* Info needed to complete the row load */
- +){
- + sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
- + 0, pInfo->ecelFlags);
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + if( pInfo->pExtra ){
- + sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
- + sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
- + }
- +#endif
- +}
- +
- +/*
- +** Code the OP_MakeRecord instruction that generates the entry to be
- +** added into the sorter.
- +**
- +** Return the register in which the result is stored.
- +*/
- +static int makeSorterRecord(
- + Parse *pParse,
- + SortCtx *pSort,
- + Select *pSelect,
- + int regBase,
- + int nBase
- +){
- + int nOBSat = pSort->nOBSat;
- + Vdbe *v = pParse->pVdbe;
- + int regOut = ++pParse->nMem;
- + if( pSort->pDeferredRowLoad ){
- + innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
- + }
- + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
- + return regOut;
- +}
- +
- +/*
- +** Generate code that will push the record in registers regData
- +** through regData+nData-1 onto the sorter.
- +*/
- +static void pushOntoSorter(
- + Parse *pParse, /* Parser context */
- + SortCtx *pSort, /* Information about the ORDER BY clause */
- + Select *pSelect, /* The whole SELECT statement */
- + int regData, /* First register holding data to be sorted */
- + int regOrigData, /* First register holding data before packing */
- + int nData, /* Number of elements in the regData data array */
- + int nPrefixReg /* No. of reg prior to regData available for use */
- +){
- + Vdbe *v = pParse->pVdbe; /* Stmt under construction */
- + int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
- + int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
- + int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
- + int regBase; /* Regs for sorter record */
- + int regRecord = 0; /* Assembled sorter record */
- + int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
- + int op; /* Opcode to add sorter record to sorter */
- + int iLimit; /* LIMIT counter */
- + int iSkip = 0; /* End of the sorter insert loop */
- +
- + assert( bSeq==0 || bSeq==1 );
- +
- + /* Three cases:
- + ** (1) The data to be sorted has already been packed into a Record
- + ** by a prior OP_MakeRecord. In this case nData==1 and regData
- + ** will be completely unrelated to regOrigData.
- + ** (2) All output columns are included in the sort record. In that
- + ** case regData==regOrigData.
- + ** (3) Some output columns are omitted from the sort record due to
- + ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
- + ** SQLITE_ECEL_OMITREF optimization, or due to the
- + ** SortCtx.pDeferredRowLoad optimiation. In any of these cases
- + ** regOrigData is 0 to prevent this routine from trying to copy
- + ** values that might not yet exist.
- + */
- + assert( nData==1 || regData==regOrigData || regOrigData==0 );
- +
- + if( nPrefixReg ){
- + assert( nPrefixReg==nExpr+bSeq );
- + regBase = regData - nPrefixReg;
- + }else{
- + regBase = pParse->nMem + 1;
- + pParse->nMem += nBase;
- + }
- + assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
- + iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
- + pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
- + sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
- + SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
- + if( bSeq ){
- + sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
- + }
- + if( nPrefixReg==0 && nData>0 ){
- + sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
- + }
- + if( nOBSat>0 ){
- + int regPrevKey; /* The first nOBSat columns of the previous row */
- + int addrFirst; /* Address of the OP_IfNot opcode */
- + int addrJmp; /* Address of the OP_Jump opcode */
- + VdbeOp *pOp; /* Opcode that opens the sorter */
- + int nKey; /* Number of sorting key columns, including OP_Sequence */
- + KeyInfo *pKI; /* Original KeyInfo on the sorter table */
- +
- + regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
- + regPrevKey = pParse->nMem+1;
- + pParse->nMem += pSort->nOBSat;
- + nKey = nExpr - pSort->nOBSat + bSeq;
- + if( bSeq ){
- + addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
- + }else{
- + addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
- + }
- + VdbeCoverage(v);
- + sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
- + pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
- + if( pParse->db->mallocFailed ) return;
- + pOp->p2 = nKey + nData;
- + pKI = pOp->p4.pKeyInfo;
- + memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
- + sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
- + testcase( pKI->nAllField > pKI->nKeyField+2 );
- + pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
- + pKI->nAllField-pKI->nKeyField-1);
- + pOp = 0; /* Ensure pOp not used after sqltie3VdbeAddOp3() */
- + addrJmp = sqlite3VdbeCurrentAddr(v);
- + sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
- + pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
- + pSort->regReturn = ++pParse->nMem;
- + sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
- + sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
- + if( iLimit ){
- + sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
- + VdbeCoverage(v);
- + }
- + sqlite3VdbeJumpHere(v, addrFirst);
- + sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
- + sqlite3VdbeJumpHere(v, addrJmp);
- + }
- + if( iLimit ){
- + /* At this point the values for the new sorter entry are stored
- + ** in an array of registers. They need to be composed into a record
- + ** and inserted into the sorter if either (a) there are currently
- + ** less than LIMIT+OFFSET items or (b) the new record is smaller than
- + ** the largest record currently in the sorter. If (b) is true and there
- + ** are already LIMIT+OFFSET items in the sorter, delete the largest
- + ** entry before inserting the new one. This way there are never more
- + ** than LIMIT+OFFSET items in the sorter.
- + **
- + ** If the new record does not need to be inserted into the sorter,
- + ** jump to the next iteration of the loop. If the pSort->labelOBLopt
- + ** value is not zero, then it is a label of where to jump. Otherwise,
- + ** just bypass the row insert logic. See the header comment on the
- + ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
- + */
- + int iCsr = pSort->iECursor;
- + sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
- + VdbeCoverage(v);
- + sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
- + iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
- + iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
- + VdbeCoverage(v);
- + sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
- + }
- + if( regRecord==0 ){
- + regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
- + }
- + if( pSort->sortFlags & SORTFLAG_UseSorter ){
- + op = OP_SorterInsert;
- + }else{
- + op = OP_IdxInsert;
- + }
- + sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
- + regBase+nOBSat, nBase-nOBSat);
- + if( iSkip ){
- + sqlite3VdbeChangeP2(v, iSkip,
- + pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
- + }
- +}
- +
- +/*
- +** Add code to implement the OFFSET
- +*/
- +static void codeOffset(
- + Vdbe *v, /* Generate code into this VM */
- + int iOffset, /* Register holding the offset counter */
- + int iContinue /* Jump here to skip the current record */
- +){
- + if( iOffset>0 ){
- + sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v);
- + VdbeComment((v, "OFFSET"));
- + }
- +}
- +
- +/*
- +** Add code that will check to make sure the N registers starting at iMem
- +** form a distinct entry. iTab is a sorting index that holds previously
- +** seen combinations of the N values. A new entry is made in iTab
- +** if the current N values are new.
- +**
- +** A jump to addrRepeat is made and the N+1 values are popped from the
- +** stack if the top N elements are not distinct.
- +*/
- +static void codeDistinct(
- + Parse *pParse, /* Parsing and code generating context */
- + int iTab, /* A sorting index used to test for distinctness */
- + int addrRepeat, /* Jump to here if not distinct */
- + int N, /* Number of elements */
- + int iMem /* First element */
- +){
- + Vdbe *v;
- + int r1;
- +
- + v = pParse->pVdbe;
- + r1 = sqlite3GetTempReg(pParse);
- + sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
- + sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
- + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
- + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
- + sqlite3ReleaseTempReg(pParse, r1);
- +}
- +
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- +/*
- +** This function is called as part of inner-loop generation for a SELECT
- +** statement with an ORDER BY that is not optimized by an index. It
- +** determines the expressions, if any, that the sorter-reference
- +** optimization should be used for. The sorter-reference optimization
- +** is used for SELECT queries like:
- +**
- +** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
- +**
- +** If the optimization is used for expression "bigblob", then instead of
- +** storing values read from that column in the sorter records, the PK of
- +** the row from table t1 is stored instead. Then, as records are extracted from
- +** the sorter to return to the user, the required value of bigblob is
- +** retrieved directly from table t1. If the values are very large, this
- +** can be more efficient than storing them directly in the sorter records.
- +**
- +** The ExprList_item.bSorterRef flag is set for each expression in pEList
- +** for which the sorter-reference optimization should be enabled.
- +** Additionally, the pSort->aDefer[] array is populated with entries
- +** for all cursors required to evaluate all selected expressions. Finally.
- +** output variable (*ppExtra) is set to an expression list containing
- +** expressions for all extra PK values that should be stored in the
- +** sorter records.
- +*/
- +static void selectExprDefer(
- + Parse *pParse, /* Leave any error here */
- + SortCtx *pSort, /* Sorter context */
- + ExprList *pEList, /* Expressions destined for sorter */
- + ExprList **ppExtra /* Expressions to append to sorter record */
- +){
- + int i;
- + int nDefer = 0;
- + ExprList *pExtra = 0;
- + for(i=0; i<pEList->nExpr; i++){
- + struct ExprList_item *pItem = &pEList->a[i];
- + if( pItem->u.x.iOrderByCol==0 ){
- + Expr *pExpr = pItem->pExpr;
- + Table *pTab = pExpr->y.pTab;
- + if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
- + && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
- + ){
- + int j;
- + for(j=0; j<nDefer; j++){
- + if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
- + }
- + if( j==nDefer ){
- + if( nDefer==ArraySize(pSort->aDefer) ){
- + continue;
- + }else{
- + int nKey = 1;
- + int k;
- + Index *pPk = 0;
- + if( !HasRowid(pTab) ){
- + pPk = sqlite3PrimaryKeyIndex(pTab);
- + nKey = pPk->nKeyCol;
- + }
- + for(k=0; k<nKey; k++){
- + Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
- + if( pNew ){
- + pNew->iTable = pExpr->iTable;
- + pNew->y.pTab = pExpr->y.pTab;
- + pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
- + pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
- + }
- + }
- + pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
- + pSort->aDefer[nDefer].iCsr = pExpr->iTable;
- + pSort->aDefer[nDefer].nKey = nKey;
- + nDefer++;
- + }
- + }
- + pItem->bSorterRef = 1;
- + }
- + }
- + }
- + pSort->nDefer = (u8)nDefer;
- + *ppExtra = pExtra;
- +}
- +#endif
- +
- +/*
- +** This routine generates the code for the inside of the inner loop
- +** of a SELECT.
- +**
- +** If srcTab is negative, then the p->pEList expressions
- +** are evaluated in order to get the data for this row. If srcTab is
- +** zero or more, then data is pulled from srcTab and p->pEList is used only
- +** to get the number of columns and the collation sequence for each column.
- +*/
- +static void selectInnerLoop(
- + Parse *pParse, /* The parser context */
- + Select *p, /* The complete select statement being coded */
- + int srcTab, /* Pull data from this table if non-negative */
- + SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
- + DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
- + SelectDest *pDest, /* How to dispose of the results */
- + int iContinue, /* Jump here to continue with next row */
- + int iBreak /* Jump here to break out of the inner loop */
- +){
- + Vdbe *v = pParse->pVdbe;
- + int i;
- + int hasDistinct; /* True if the DISTINCT keyword is present */
- + int eDest = pDest->eDest; /* How to dispose of results */
- + int iParm = pDest->iSDParm; /* First argument to disposal method */
- + int nResultCol; /* Number of result columns */
- + int nPrefixReg = 0; /* Number of extra registers before regResult */
- + RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
- +
- + /* Usually, regResult is the first cell in an array of memory cells
- + ** containing the current result row. In this case regOrig is set to the
- + ** same value. However, if the results are being sent to the sorter, the
- + ** values for any expressions that are also part of the sort-key are omitted
- + ** from this array. In this case regOrig is set to zero. */
- + int regResult; /* Start of memory holding current results */
- + int regOrig; /* Start of memory holding full result (or 0) */
- +
- + assert( v );
- + assert( p->pEList!=0 );
- + hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
- + if( pSort && pSort->pOrderBy==0 ) pSort = 0;
- + if( pSort==0 && !hasDistinct ){
- + assert( iContinue!=0 );
- + codeOffset(v, p->iOffset, iContinue);
- + }
- +
- + /* Pull the requested columns.
- + */
- + nResultCol = p->pEList->nExpr;
- +
- + if( pDest->iSdst==0 ){
- + if( pSort ){
- + nPrefixReg = pSort->pOrderBy->nExpr;
- + if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
- + pParse->nMem += nPrefixReg;
- + }
- + pDest->iSdst = pParse->nMem+1;
- + pParse->nMem += nResultCol;
- + }else if( pDest->iSdst+nResultCol > pParse->nMem ){
- + /* This is an error condition that can result, for example, when a SELECT
- + ** on the right-hand side of an INSERT contains more result columns than
- + ** there are columns in the table on the left. The error will be caught
- + ** and reported later. But we need to make sure enough memory is allocated
- + ** to avoid other spurious errors in the meantime. */
- + pParse->nMem += nResultCol;
- + }
- + pDest->nSdst = nResultCol;
- + regOrig = regResult = pDest->iSdst;
- + if( srcTab>=0 ){
- + for(i=0; i<nResultCol; i++){
- + sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
- + VdbeComment((v, "%s", p->pEList->a[i].zEName));
- + }
- + }else if( eDest!=SRT_Exists ){
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + ExprList *pExtra = 0;
- +#endif
- + /* If the destination is an EXISTS(...) expression, the actual
- + ** values returned by the SELECT are not required.
- + */
- + u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
- + ExprList *pEList;
- + if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
- + ecelFlags = SQLITE_ECEL_DUP;
- + }else{
- + ecelFlags = 0;
- + }
- + if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
- + /* For each expression in p->pEList that is a copy of an expression in
- + ** the ORDER BY clause (pSort->pOrderBy), set the associated
- + ** iOrderByCol value to one more than the index of the ORDER BY
- + ** expression within the sort-key that pushOntoSorter() will generate.
- + ** This allows the p->pEList field to be omitted from the sorted record,
- + ** saving space and CPU cycles. */
- + ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
- +
- + for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
- + int j;
- + if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
- + p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
- + }
- + }
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + selectExprDefer(pParse, pSort, p->pEList, &pExtra);
- + if( pExtra && pParse->db->mallocFailed==0 ){
- + /* If there are any extra PK columns to add to the sorter records,
- + ** allocate extra memory cells and adjust the OpenEphemeral
- + ** instruction to account for the larger records. This is only
- + ** required if there are one or more WITHOUT ROWID tables with
- + ** composite primary keys in the SortCtx.aDefer[] array. */
- + VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
- + pOp->p2 += (pExtra->nExpr - pSort->nDefer);
- + pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
- + pParse->nMem += pExtra->nExpr;
- + }
- +#endif
- +
- + /* Adjust nResultCol to account for columns that are omitted
- + ** from the sorter by the optimizations in this branch */
- + pEList = p->pEList;
- + for(i=0; i<pEList->nExpr; i++){
- + if( pEList->a[i].u.x.iOrderByCol>0
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + || pEList->a[i].bSorterRef
- +#endif
- + ){
- + nResultCol--;
- + regOrig = 0;
- + }
- + }
- +
- + testcase( regOrig );
- + testcase( eDest==SRT_Set );
- + testcase( eDest==SRT_Mem );
- + testcase( eDest==SRT_Coroutine );
- + testcase( eDest==SRT_Output );
- + assert( eDest==SRT_Set || eDest==SRT_Mem
- + || eDest==SRT_Coroutine || eDest==SRT_Output );
- + }
- + sRowLoadInfo.regResult = regResult;
- + sRowLoadInfo.ecelFlags = ecelFlags;
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + sRowLoadInfo.pExtra = pExtra;
- + sRowLoadInfo.regExtraResult = regResult + nResultCol;
- + if( pExtra ) nResultCol += pExtra->nExpr;
- +#endif
- + if( p->iLimit
- + && (ecelFlags & SQLITE_ECEL_OMITREF)!=0
- + && nPrefixReg>0
- + ){
- + assert( pSort!=0 );
- + assert( hasDistinct==0 );
- + pSort->pDeferredRowLoad = &sRowLoadInfo;
- + regOrig = 0;
- + }else{
- + innerLoopLoadRow(pParse, p, &sRowLoadInfo);
- + }
- + }
- +
- + /* If the DISTINCT keyword was present on the SELECT statement
- + ** and this row has been seen before, then do not make this row
- + ** part of the result.
- + */
- + if( hasDistinct ){
- + switch( pDistinct->eTnctType ){
- + case WHERE_DISTINCT_ORDERED: {
- + VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
- + int iJump; /* Jump destination */
- + int regPrev; /* Previous row content */
- +
- + /* Allocate space for the previous row */
- + regPrev = pParse->nMem+1;
- + pParse->nMem += nResultCol;
- +
- + /* Change the OP_OpenEphemeral coded earlier to an OP_Null
- + ** sets the MEM_Cleared bit on the first register of the
- + ** previous value. This will cause the OP_Ne below to always
- + ** fail on the first iteration of the loop even if the first
- + ** row is all NULLs.
- + */
- + sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
- + pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
- + pOp->opcode = OP_Null;
- + pOp->p1 = 1;
- + pOp->p2 = regPrev;
- + pOp = 0; /* Ensure pOp is not used after sqlite3VdbeAddOp() */
- +
- + iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
- + for(i=0; i<nResultCol; i++){
- + CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
- + if( i<nResultCol-1 ){
- + sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
- + VdbeCoverage(v);
- + }else{
- + sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
- + VdbeCoverage(v);
- + }
- + sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
- + sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
- + }
- + assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
- + sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
- + break;
- + }
- +
- + case WHERE_DISTINCT_UNIQUE: {
- + sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
- + break;
- + }
- +
- + default: {
- + assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
- + codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
- + regResult);
- + break;
- + }
- + }
- + if( pSort==0 ){
- + codeOffset(v, p->iOffset, iContinue);
- + }
- + }
- +
- + switch( eDest ){
- + /* In this mode, write each query result to the key of the temporary
- + ** table iParm.
- + */
- +#ifndef SQLITE_OMIT_COMPOUND_SELECT
- + case SRT_Union: {
- + int r1;
- + r1 = sqlite3GetTempReg(pParse);
- + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
- + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
- + sqlite3ReleaseTempReg(pParse, r1);
- + break;
- + }
- +
- + /* Construct a record from the query result, but instead of
- + ** saving that record, use it as a key to delete elements from
- + ** the temporary table iParm.
- + */
- + case SRT_Except: {
- + sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
- + break;
- + }
- +#endif /* SQLITE_OMIT_COMPOUND_SELECT */
- +
- + /* Store the result as data using a unique key.
- + */
- + case SRT_Fifo:
- + case SRT_DistFifo:
- + case SRT_Table:
- + case SRT_EphemTab: {
- + int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
- + testcase( eDest==SRT_Table );
- + testcase( eDest==SRT_EphemTab );
- + testcase( eDest==SRT_Fifo );
- + testcase( eDest==SRT_DistFifo );
- + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
- +#ifndef SQLITE_OMIT_CTE
- + if( eDest==SRT_DistFifo ){
- + /* If the destination is DistFifo, then cursor (iParm+1) is open
- + ** on an ephemeral index. If the current row is already present
- + ** in the index, do not write it to the output. If not, add the
- + ** current row to the index and proceed with writing it to the
- + ** output table as well. */
- + int addr = sqlite3VdbeCurrentAddr(v) + 4;
- + sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
- + VdbeCoverage(v);
- + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
- + assert( pSort==0 );
- + }
- +#endif
- + if( pSort ){
- + assert( regResult==regOrig );
- + pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
- + }else{
- + int r2 = sqlite3GetTempReg(pParse);
- + sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
- + sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
- + sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
- + sqlite3ReleaseTempReg(pParse, r2);
- + }
- + sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
- + break;
- + }
- +
- +#ifndef SQLITE_OMIT_SUBQUERY
- + /* If we are creating a set for an "expr IN (SELECT ...)" construct,
- + ** then there should be a single item on the stack. Write this
- + ** item into the set table with bogus data.
- + */
- + case SRT_Set: {
- + if( pSort ){
- + /* At first glance you would think we could optimize out the
- + ** ORDER BY in this case since the order of entries in the set
- + ** does not matter. But there might be a LIMIT clause, in which
- + ** case the order does matter */
- + pushOntoSorter(
- + pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
- + }else{
- + int r1 = sqlite3GetTempReg(pParse);
- + assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
- + sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol,
- + r1, pDest->zAffSdst, nResultCol);
- + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
- + sqlite3ReleaseTempReg(pParse, r1);
- + }
- + break;
- + }
- +
- + /* If any row exist in the result set, record that fact and abort.
- + */
- + case SRT_Exists: {
- + sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
- + /* The LIMIT clause will terminate the loop for us */
- + break;
- + }
- +
- + /* If this is a scalar select that is part of an expression, then
- + ** store the results in the appropriate memory cell or array of
- + ** memory cells and break out of the scan loop.
- + */
- + case SRT_Mem: {
- + if( pSort ){
- + assert( nResultCol<=pDest->nSdst );
- + pushOntoSorter(
- + pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
- + }else{
- + assert( nResultCol==pDest->nSdst );
- + assert( regResult==iParm );
- + /* The LIMIT clause will jump out of the loop for us */
- + }
- + break;
- + }
- +#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
- +
- + case SRT_Coroutine: /* Send data to a co-routine */
- + case SRT_Output: { /* Return the results */
- + testcase( eDest==SRT_Coroutine );
- + testcase( eDest==SRT_Output );
- + if( pSort ){
- + pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
- + nPrefixReg);
- + }else if( eDest==SRT_Coroutine ){
- + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
- + }else{
- + sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
- + }
- + break;
- + }
- +
- +#ifndef SQLITE_OMIT_CTE
- + /* Write the results into a priority queue that is order according to
- + ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
- + ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
- + ** pSO->nExpr columns, then make sure all keys are unique by adding a
- + ** final OP_Sequence column. The last column is the record as a blob.
- + */
- + case SRT_DistQueue:
- + case SRT_Queue: {
- + int nKey;
- + int r1, r2, r3;
- + int addrTest = 0;
- + ExprList *pSO;
- + pSO = pDest->pOrderBy;
- + assert( pSO );
- + nKey = pSO->nExpr;
- + r1 = sqlite3GetTempReg(pParse);
- + r2 = sqlite3GetTempRange(pParse, nKey+2);
- + r3 = r2+nKey+1;
- + if( eDest==SRT_DistQueue ){
- + /* If the destination is DistQueue, then cursor (iParm+1) is open
- + ** on a second ephemeral index that holds all values every previously
- + ** added to the queue. */
- + addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
- + regResult, nResultCol);
- + VdbeCoverage(v);
- + }
- + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
- + if( eDest==SRT_DistQueue ){
- + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
- + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
- + }
- + for(i=0; i<nKey; i++){
- + sqlite3VdbeAddOp2(v, OP_SCopy,
- + regResult + pSO->a[i].u.x.iOrderByCol - 1,
- + r2+i);
- + }
- + sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
- + sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
- + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2);
- + if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
- + sqlite3ReleaseTempReg(pParse, r1);
- + sqlite3ReleaseTempRange(pParse, r2, nKey+2);
- + break;
- + }
- +#endif /* SQLITE_OMIT_CTE */
- +
- +
- +
- +#if !defined(SQLITE_OMIT_TRIGGER)
- + /* Discard the results. This is used for SELECT statements inside
- + ** the body of a TRIGGER. The purpose of such selects is to call
- + ** user-defined functions that have side effects. We do not care
- + ** about the actual results of the select.
- + */
- + default: {
- + assert( eDest==SRT_Discard );
- + break;
- + }
- +#endif
- + }
- +
- + /* Jump to the end of the loop if the LIMIT is reached. Except, if
- + ** there is a sorter, in which case the sorter has already limited
- + ** the output for us.
- + */
- + if( pSort==0 && p->iLimit ){
- + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
- + }
- +}
- +
- +/*
- +** Allocate a KeyInfo object sufficient for an index of N key columns and
- +** X extra columns.
- +*/
- +KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
- + int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
- + KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
- + if( p ){
- + p->aSortFlags = (u8*)&p->aColl[N+X];
- + p->nKeyField = (u16)N;
- + p->nAllField = (u16)(N+X);
- + p->enc = ENC(db);
- + p->db = db;
- + p->nRef = 1;
- + memset(&p[1], 0, nExtra);
- + }else{
- + sqlite3OomFault(db);
- + }
- + return p;
- +}
- +
- +/*
- +** Deallocate a KeyInfo object
- +*/
- +void sqlite3KeyInfoUnref(KeyInfo *p){
- + if( p ){
- + assert( p->nRef>0 );
- + p->nRef--;
- + if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
- + }
- +}
- +
- +/*
- +** Make a new pointer to a KeyInfo object
- +*/
- +KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
- + if( p ){
- + assert( p->nRef>0 );
- + p->nRef++;
- + }
- + return p;
- +}
- +
- +#ifdef SQLITE_DEBUG
- +/*
- +** Return TRUE if a KeyInfo object can be change. The KeyInfo object
- +** can only be changed if this is just a single reference to the object.
- +**
- +** This routine is used only inside of assert() statements.
- +*/
- +int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
- +#endif /* SQLITE_DEBUG */
- +
- +/*
- +** Given an expression list, generate a KeyInfo structure that records
- +** the collating sequence for each expression in that expression list.
- +**
- +** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
- +** KeyInfo structure is appropriate for initializing a virtual index to
- +** implement that clause. If the ExprList is the result set of a SELECT
- +** then the KeyInfo structure is appropriate for initializing a virtual
- +** index to implement a DISTINCT test.
- +**
- +** Space to hold the KeyInfo structure is obtained from malloc. The calling
- +** function is responsible for seeing that this structure is eventually
- +** freed.
- +*/
- +KeyInfo *sqlite3KeyInfoFromExprList(
- + Parse *pParse, /* Parsing context */
- + ExprList *pList, /* Form the KeyInfo object from this ExprList */
- + int iStart, /* Begin with this column of pList */
- + int nExtra /* Add this many extra columns to the end */
- +){
- + int nExpr;
- + KeyInfo *pInfo;
- + struct ExprList_item *pItem;
- + sqlite3 *db = pParse->db;
- + int i;
- +
- + nExpr = pList->nExpr;
- + pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
- + if( pInfo ){
- + assert( sqlite3KeyInfoIsWriteable(pInfo) );
- + for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
- + pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
- + pInfo->aSortFlags[i-iStart] = pItem->sortFlags;
- + }
- + }
- + return pInfo;
- +}
- +
- +/*
- +** Name of the connection operator, used for error messages.
- +*/
- +static const char *selectOpName(int id){
- + char *z;
- + switch( id ){
- + case TK_ALL: z = "UNION ALL"; break;
- + case TK_INTERSECT: z = "INTERSECT"; break;
- + case TK_EXCEPT: z = "EXCEPT"; break;
- + default: z = "UNION"; break;
- + }
- + return z;
- +}
- +
- +#ifndef SQLITE_OMIT_EXPLAIN
- +/*
- +** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
- +** is a no-op. Otherwise, it adds a single row of output to the EQP result,
- +** where the caption is of the form:
- +**
- +** "USE TEMP B-TREE FOR xxx"
- +**
- +** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
- +** is determined by the zUsage argument.
- +*/
- +static void explainTempTable(Parse *pParse, const char *zUsage){
- + ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
- +}
- +
- +/*
- +** Assign expression b to lvalue a. A second, no-op, version of this macro
- +** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
- +** in sqlite3Select() to assign values to structure member variables that
- +** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
- +** code with #ifndef directives.
- +*/
- +# define explainSetInteger(a, b) a = b
- +
- +#else
- +/* No-op versions of the explainXXX() functions and macros. */
- +# define explainTempTable(y,z)
- +# define explainSetInteger(y,z)
- +#endif
- +
- +
- +/*
- +** If the inner loop was generated using a non-null pOrderBy argument,
- +** then the results were placed in a sorter. After the loop is terminated
- +** we need to run the sorter and output the results. The following
- +** routine generates the code needed to do that.
- +*/
- +static void generateSortTail(
- + Parse *pParse, /* Parsing context */
- + Select *p, /* The SELECT statement */
- + SortCtx *pSort, /* Information on the ORDER BY clause */
- + int nColumn, /* Number of columns of data */
- + SelectDest *pDest /* Write the sorted results here */
- +){
- + Vdbe *v = pParse->pVdbe; /* The prepared statement */
- + int addrBreak = pSort->labelDone; /* Jump here to exit loop */
- + int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
- + int addr; /* Top of output loop. Jump for Next. */
- + int addrOnce = 0;
- + int iTab;
- + ExprList *pOrderBy = pSort->pOrderBy;
- + int eDest = pDest->eDest;
- + int iParm = pDest->iSDParm;
- + int regRow;
- + int regRowid;
- + int iCol;
- + int nKey; /* Number of key columns in sorter record */
- + int iSortTab; /* Sorter cursor to read from */
- + int i;
- + int bSeq; /* True if sorter record includes seq. no. */
- + int nRefKey = 0;
- + struct ExprList_item *aOutEx = p->pEList->a;
- +
- + assert( addrBreak<0 );
- + if( pSort->labelBkOut ){
- + sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
- + sqlite3VdbeGoto(v, addrBreak);
- + sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
- + }
- +
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + /* Open any cursors needed for sorter-reference expressions */
- + for(i=0; i<pSort->nDefer; i++){
- + Table *pTab = pSort->aDefer[i].pTab;
- + int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
- + sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
- + nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
- + }
- +#endif
- +
- + iTab = pSort->iECursor;
- + if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
- + regRowid = 0;
- + regRow = pDest->iSdst;
- + }else{
- + regRowid = sqlite3GetTempReg(pParse);
- + if( eDest==SRT_EphemTab || eDest==SRT_Table ){
- + regRow = sqlite3GetTempReg(pParse);
- + nColumn = 0;
- + }else{
- + regRow = sqlite3GetTempRange(pParse, nColumn);
- + }
- + }
- + nKey = pOrderBy->nExpr - pSort->nOBSat;
- + if( pSort->sortFlags & SORTFLAG_UseSorter ){
- + int regSortOut = ++pParse->nMem;
- + iSortTab = pParse->nTab++;
- + if( pSort->labelBkOut ){
- + addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
- + }
- + sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut,
- + nKey+1+nColumn+nRefKey);
- + if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
- + addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
- + VdbeCoverage(v);
- + codeOffset(v, p->iOffset, addrContinue);
- + sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
- + bSeq = 0;
- + }else{
- + addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
- + codeOffset(v, p->iOffset, addrContinue);
- + iSortTab = iTab;
- + bSeq = 1;
- + }
- + for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + if( aOutEx[i].bSorterRef ) continue;
- +#endif
- + if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
- + }
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + if( pSort->nDefer ){
- + int iKey = iCol+1;
- + int regKey = sqlite3GetTempRange(pParse, nRefKey);
- +
- + for(i=0; i<pSort->nDefer; i++){
- + int iCsr = pSort->aDefer[i].iCsr;
- + Table *pTab = pSort->aDefer[i].pTab;
- + int nKey = pSort->aDefer[i].nKey;
- +
- + sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
- + if( HasRowid(pTab) ){
- + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
- + sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr,
- + sqlite3VdbeCurrentAddr(v)+1, regKey);
- + }else{
- + int k;
- + int iJmp;
- + assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
- + for(k=0; k<nKey; k++){
- + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
- + }
- + iJmp = sqlite3VdbeCurrentAddr(v);
- + sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
- + sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
- + sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
- + }
- + }
- + sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
- + }
- +#endif
- + for(i=nColumn-1; i>=0; i--){
- +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
- + if( aOutEx[i].bSorterRef ){
- + sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
- + }else
- +#endif
- + {
- + int iRead;
- + if( aOutEx[i].u.x.iOrderByCol ){
- + iRead = aOutEx[i].u.x.iOrderByCol-1;
- + }else{
- + iRead = iCol--;
- + }
- + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
- + VdbeComment((v, "%s", aOutEx[i].zEName));
- + }
- + }
- + switch( eDest ){
- + case SRT_Table:
- + case SRT_EphemTab: {
- + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq, regRow);
- + sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
- + sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
- + sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
- + break;
- + }
- +#ifndef SQLITE_OMIT_SUBQUERY
- + case SRT_Set: {
- + assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
- + sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
- + pDest->zAffSdst, nColumn);
- + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn);
- + break;
- + }
- + case SRT_Mem: {
- + /* The LIMIT clause will terminate the loop for us */
- + break;
- + }
- +#endif
- + default: {
- + assert( eDest==SRT_Output || eDest==SRT_Coroutine );
- + testcase( eDest==SRT_Output );
- + testcase( eDest==SRT_Coroutine );
- + if( eDest==SRT_Output ){
- + sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
- + }else{
- + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
- + }
- + break;
- + }
- + }
- + if( regRowid ){
- + if( eDest==SRT_Set ){
- + sqlite3ReleaseTempRange(pParse, regRow, nColumn);
- + }else{
- + sqlite3ReleaseTempReg(pParse, regRow);
- + }
- + sqlite3ReleaseTempReg(pParse, regRowid);
- + }
- + /* The bottom of the loop
- + */
- + sqlite3VdbeResolveLabel(v, addrContinue);
- + if( pSort->sortFlags & SORTFLAG_UseSorter ){
- + sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
- + }else{
- + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
- + }
- + if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
- + sqlite3VdbeResolveLabel(v, addrBreak);
- +}
- +
- +/*
- +** Return a pointer to a string containing the 'declaration type' of the
- +** expression pExpr. The string may be treated as static by the caller.
- +**
- +** Also try to estimate the size of the returned value and return that
- +** result in *pEstWidth.
- +**
- +** The declaration type is the exact datatype definition extracted from the
- +** original CREATE TABLE statement if the expression is a column. The
- +** declaration type for a ROWID field is INTEGER. Exactly when an expression
- +** is considered a column can be complex in the presence of subqueries. The
- +** result-set expression in all of the following SELECT statements is
- +** considered a column by this function.
- +**
- +** SELECT col FROM tbl;
- +** SELECT (SELECT col FROM tbl;
- +** SELECT (SELECT col FROM tbl);
- +** SELECT abc FROM (SELECT col AS abc FROM tbl);
- +**
- +** The declaration type for any expression other than a column is NULL.
- +**
- +** This routine has either 3 or 6 parameters depending on whether or not
- +** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
- +*/
- +#ifdef SQLITE_ENABLE_COLUMN_METADATA
- +# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
- +#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
- +# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
- +#endif
- +static const char *columnTypeImpl(
- + NameContext *pNC,
- +#ifndef SQLITE_ENABLE_COLUMN_METADATA
- + Expr *pExpr
- +#else
- + Expr *pExpr,
- + const char **pzOrigDb,
- + const char **pzOrigTab,
- + const char **pzOrigCol
- +#endif
- +){
- + char const *zType = 0;
- + int j;
- +#ifdef SQLITE_ENABLE_COLUMN_METADATA
- + char const *zOrigDb = 0;
- + char const *zOrigTab = 0;
- + char const *zOrigCol = 0;
- +#endif
- +
- + assert( pExpr!=0 );
- + assert( pNC->pSrcList!=0 );
- + switch( pExpr->op ){
- + case TK_COLUMN: {
- + /* The expression is a column. Locate the table the column is being
- + ** extracted from in NameContext.pSrcList. This table may be real
- + ** database table or a subquery.
- + */
- + Table *pTab = 0; /* Table structure column is extracted from */
- + Select *pS = 0; /* Select the column is extracted from */
- + int iCol = pExpr->iColumn; /* Index of column in pTab */
- + while( pNC && !pTab ){
- + SrcList *pTabList = pNC->pSrcList;
- + for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
- + if( j<pTabList->nSrc ){
- + pTab = pTabList->a[j].pTab;
- + pS = pTabList->a[j].pSelect;
- + }else{
- + pNC = pNC->pNext;
- + }
- + }
- +
- + if( pTab==0 ){
- + /* At one time, code such as "SELECT new.x" within a trigger would
- + ** cause this condition to run. Since then, we have restructured how
- + ** trigger code is generated and so this condition is no longer
- + ** possible. However, it can still be true for statements like
- + ** the following:
- + **
- + ** CREATE TABLE t1(col INTEGER);
- + ** SELECT (SELECT t1.col) FROM FROM t1;
- + **
- + ** when columnType() is called on the expression "t1.col" in the
- + ** sub-select. In this case, set the column type to NULL, even
- + ** though it should really be "INTEGER".
- + **
- + ** This is not a problem, as the column type of "t1.col" is never
- + ** used. When columnType() is called on the expression
- + ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
- + ** branch below. */
- + break;
- + }
- +
- + assert( pTab && pExpr->y.pTab==pTab );
- + if( pS ){
- + /* The "table" is actually a sub-select or a view in the FROM clause
- + ** of the SELECT statement. Return the declaration type and origin
- + ** data for the result-set column of the sub-select.
- + */
- + if( iCol>=0 && iCol<pS->pEList->nExpr ){
- + /* If iCol is less than zero, then the expression requests the
- + ** rowid of the sub-select or view. This expression is legal (see
- + ** test case misc2.2.2) - it always evaluates to NULL.
- + */
- + NameContext sNC;
- + Expr *p = pS->pEList->a[iCol].pExpr;
- + sNC.pSrcList = pS->pSrc;
- + sNC.pNext = pNC;
- + sNC.pParse = pNC->pParse;
- + zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol);
- + }
- + }else{
- + /* A real table or a CTE table */
- + assert( !pS );
- +#ifdef SQLITE_ENABLE_COLUMN_METADATA
- + if( iCol<0 ) iCol = pTab->iPKey;
- + assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
- + if( iCol<0 ){
- + zType = "INTEGER";
- + zOrigCol = "rowid";
- + }else{
- + zOrigCol = pTab->aCol[iCol].zName;
- + zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
- + }
- + zOrigTab = pTab->zName;
- + if( pNC->pParse && pTab->pSchema ){
- + int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
- + zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
- + }
- +#else
- + assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
- + if( iCol<0 ){
- + zType = "INTEGER";
- + }else{
- + zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
- + }
- +#endif
- + }
- + break;
- + }
- +#ifndef SQLITE_OMIT_SUBQUERY
- + case TK_SELECT: {
- + /* The expression is a sub-select. Return the declaration type and
- + ** origin info for the single column in the result set of the SELECT
- + ** statement.
- + */
- + NameContext sNC;
- + Select *pS = pExpr->x.pSelect;
- + Expr *p = pS->pEList->a[0].pExpr;
- + assert( ExprHasProperty(pExpr, EP_xIsSelect) );
- + sNC.pSrcList = pS->pSrc;
- + sNC.pNext = pNC;
- + sNC.pParse = pNC->pParse;
- + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
- + break;
- + }
- +#endif
- + }
- +
- +#ifdef SQLITE_ENABLE_COLUMN_METADATA
- + if( pzOrigDb ){
- + assert( pzOrigTab && pzOrigCol );
- + *pzOrigDb = zOrigDb;
- + *pzOrigTab = zOrigTab;
- + *pzOrigCol = zOrigCol;
- + }
- +#endif
- + return zType;
- +}
- +
- +/*
- +** Generate code that will tell the VDBE the declaration types of columns
- +** in the result set.
- +*/
- +static void generateColumnTypes(
- + Parse *pParse, /* Parser context */
- + SrcList *pTabList, /* List of tables */
- + ExprList *pEList /* Expressions defining the result set */
- +){
- +#ifndef SQLITE_OMIT_DECLTYPE
- + Vdbe *v = pParse->pVdbe;
- + int i;
- + NameContext sNC;
- + sNC.pSrcList = pTabList;
- + sNC.pParse = pParse;
- + sNC.pNext = 0;
- + for(i=0; i<pEList->nExpr; i++){
- + Expr *p = pEList->a[i].pExpr;
- + const char *zType;
- +#ifdef SQLITE_ENABLE_COLUMN_METADATA
- + const char *zOrigDb = 0;
- + const char *zOrigTab = 0;
- + const char *zOrigCol = 0;
- + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
- +
- + /* The vdbe must make its own copy of the column-type and other
- + ** column specific strings, in case the schema is reset before this
- + ** virtual machine is deleted.
- + */
- + sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
- + sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
- + sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
- +#else
- + zType = columnType(&sNC, p, 0, 0, 0);
- +#endif
- + sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
- + }
- +#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
- +}
- +
- +
- +/*
- +** Compute the column names for a SELECT statement.
- +**
- +** The only guarantee that SQLite makes about column names is that if the
- +** column has an AS clause assigning it a name, that will be the name used.
- +** That is the only documented guarantee. However, countless applications
- +** developed over the years have made baseless assumptions about column names
- +** and will break if those assumptions changes. Hence, use extreme caution
- +** when modifying this routine to avoid breaking legacy.
- +**
- +** See Also: sqlite3ColumnsFromExprList()
- +**
- +** The PRAGMA short_column_names and PRAGMA full_column_names settings are
- +** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
- +** applications should operate this way. Nevertheless, we need to support the
- +** other modes for legacy:
- +**
- +** short=OFF, full=OFF: Column name is the text of the expression has it
- +** originally appears in the SELECT statement. In
- +** other words, the zSpan of the result expression.
- +**
- +** short=ON, full=OFF: (This is the default setting). If the result
- +** refers directly to a table column, then the
- +** result column name is just the table column
- +** name: COLUMN. Otherwise use zSpan.
- +**
- +** full=ON, short=ANY: If the result refers directly to a table column,
- +** then the result column name with the table name
- +** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
- +*/
- +static void generateColumnNames(
- + Parse *pParse, /* Parser context */
- + Select *pSelect /* Generate column names for this SELECT statement */
- +){
- + Vdbe *v = pParse->pVdbe;
- + int i;
- + Table *pTab;
- + SrcList *pTabList;
- + ExprList *pEList;
- + sqlite3 *db = pParse->db;
- + int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
- + int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
- +
- +#ifndef SQLITE_OMIT_EXPLAIN
- + /* If this is an EXPLAIN, skip this step */
- + if( pParse->explain ){
- + return;
- + }
- +#endif
- +
- + if( pParse->colNamesSet ) return;
- + /* Column names are determined by the left-most term of a compound select */
- + while( pSelect->pPrior ) pSelect = pSelect->pPrior;
- + SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
- + pTabList = pSelect->pSrc;
- + pEList = pSelect->pEList;
- + assert( v!=0 );
- + assert( pTabList!=0 );
- + pParse->colNamesSet = 1;
- + fullName = (db->flags & SQLITE_FullColNames)!=0;
- + srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
- + sqlite3VdbeSetNumCols(v, pEList->nExpr);
- + for(i=0; i<pEList->nExpr; i++){
- + Expr *p = pEList->a[i].pExpr;
- +
- + assert( p!=0 );
- + assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
- + assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */
- + if( pEList->a[i].zEName && pEList->a[i].eEName==ENAME_NAME ){
- + /* An AS clause always takes first priority */
- + char *zName = pEList->a[i].zEName;
- + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
- + }else if( srcName && p->op==TK_COLUMN ){
- + char *zCol;
- + int iCol = p->iColumn;
- + pTab = p->y.pTab;
- + assert( pTab!=0 );
- + if( iCol<0 ) iCol = pTab->iPKey;
- + assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
- + if( iCol<0 ){
- + zCol = "rowid";
- + }else{
- + zCol = pTab->aCol[iCol].zName;
- + }
- + if( fullName ){
- + char *zName = 0;
- + zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
- + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
- + }else{
- + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
- + }
- + }else{
- + const char *z = pEList->a[i].zEName;
- + z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
- + sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
- + }
- + }
- + generateColumnTypes(pParse, pTabList, pEList);
- +}
- +
- +/*
- +** Given an expression list (which is really the list of expressions
- +** that form the result set of a SELECT statement) compute appropriate
- +** column names for a table that would hold the expression list.
- +**
- +** All column names will be unique.
- +**
- +** Only the column names are computed. Column.zType, Column.zColl,
- +** and other fields of Column are zeroed.
- +**
- +** Return SQLITE_OK on success. If a memory allocation error occurs,
- +** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
- +**
- +** The only guarantee that SQLite makes about column names is that if the
- +** column has an AS clause assigning it a name, that will be the name used.
- +** That is the only documented guarantee. However, countless applications
- +** developed over the years have made baseless assumptions about column names
- +** and will break if those assumptions changes. Hence, use extreme caution
- +** when modifying this routine to avoid breaking legacy.
- +**
- +** See Also: generateColumnNames()
- +*/
- +int sqlite3ColumnsFromExprList(
- + Parse *pParse, /* Parsing context */
- + ExprList *pEList, /* Expr list from which to derive column names */
- + i16 *pnCol, /* Write the number of columns here */
- + Column **paCol /* Write the new column list here */
- +){
- + sqlite3 *db = pParse->db; /* Database connection */
- + int i, j; /* Loop counters */
- + u32 cnt; /* Index added to make the name unique */
- + Column *aCol, *pCol; /* For looping over result columns */
- + int nCol; /* Number of columns in the result set */
- + char *zName; /* Column name */
- + int nName; /* Size of name in zName[] */
- + Hash ht; /* Hash table of column names */
- +
- + sqlite3HashInit(&ht);
- + if( pEList ){
- + nCol = pEList->nExpr;
- + aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
- + testcase( aCol==0 );
- + if( nCol>32767 ) nCol = 32767;
- + }else{
- + nCol = 0;
- + aCol = 0;
- + }
- + assert( nCol==(i16)nCol );
- + *pnCol = nCol;
- + *paCol = aCol;
- +
- + for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
- + /* Get an appropriate name for the column
- + */
- + if( (zName = pEList->a[i].zEName)!=0 && pEList->a[i].eEName==ENAME_NAME ){
- + /* If the column contains an "AS <name>" phrase, use <name> as the name */
- + }else{
- + Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pEList->a[i].pExpr);
- + while( pColExpr->op==TK_DOT ){
- + pColExpr = pColExpr->pRight;
- + assert( pColExpr!=0 );
- + }
- + if( pColExpr->op==TK_COLUMN ){
- + /* For columns use the column name name */
- + int iCol = pColExpr->iColumn;
- + Table *pTab = pColExpr->y.pTab;
- + assert( pTab!=0 );
- + if( iCol<0 ) iCol = pTab->iPKey;
- + zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
- + }else if( pColExpr->op==TK_ID ){
- + assert( !ExprHasProperty(pColExpr, EP_IntValue) );
- + zName = pColExpr->u.zToken;
- + }else{
- + /* Use the original text of the column expression as its name */
- + zName = pEList->a[i].zEName;
- + }
- + }
- + if( zName && !sqlite3IsTrueOrFalse(zName) ){
- + zName = sqlite3DbStrDup(db, zName);
- + }else{
- + zName = sqlite3MPrintf(db,"column%d",i+1);
- + }
- +
- + /* Make sure the column name is unique. If the name is not unique,
- + ** append an integer to the name so that it becomes unique.
- + */
- + cnt = 0;
- + while( zName && sqlite3HashFind(&ht, zName)!=0 ){
- + nName = sqlite3Strlen30(zName);
- + if( nName>0 ){
- + for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
- + if( zName[j]==':' ) nName = j;
- + }
- + zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
- + if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
- + }
- + pCol->zName = zName;
- + pCol->hName = sqlite3StrIHash(zName);
- + sqlite3ColumnPropertiesFromName(0, pCol);
- + if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
- + sqlite3OomFault(db);
- + }
- + }
- + sqlite3HashClear(&ht);
- + if( db->mallocFailed ){
- + for(j=0; j<i; j++){
- + sqlite3DbFree(db, aCol[j].zName);
- + }
- + sqlite3DbFree(db, aCol);
- + *paCol = 0;
- + *pnCol = 0;
- + return SQLITE_NOMEM_BKPT;
- + }
- + return SQLITE_OK;
- +}
- +
- +/*
- +** Add type and collation information to a column list based on
- +** a SELECT statement.
- +**
- +** The column list presumably came from selectColumnNamesFromExprList().
- +** The column list has only names, not types or collations. This
- +** routine goes through and adds the types and collations.
- +**
- +** This routine requires that all identifiers in the SELECT
- +** statement be resolved.
- +*/
- +void sqlite3SelectAddColumnTypeAndCollation(
- + Parse *pParse, /* Parsing contexts */
- + Table *pTab, /* Add column type information to this table */
- + Select *pSelect, /* SELECT used to determine types and collations */
- + char aff /* Default affinity for columns */
- +){
- + sqlite3 *db = pParse->db;
- + NameContext sNC;
- + Column *pCol;
- + CollSeq *pColl;
- + int i;
- + Expr *p;
- + struct ExprList_item *a;
- +
- + assert( pSelect!=0 );
- + assert( (pSelect->selFlags & SF_Resolved)!=0 );
- + assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
- + if( db->mallocFailed ) return;
- + memset(&sNC, 0, sizeof(sNC));
- + sNC.pSrcList = pSelect->pSrc;
- + a = pSelect->pEList->a;
- + for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
- + const char *zType;
- + int n, m;
- + p = a[i].pExpr;
- + zType = columnType(&sNC, p, 0, 0, 0);
- + /* pCol->szEst = ... // Column size est for SELECT tables never used */
- + pCol->affinity = sqlite3ExprAffinity(p);
- + if( zType ){
- + m = sqlite3Strlen30(zType);
- + n = sqlite3Strlen30(pCol->zName);
- + pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
- + if( pCol->zName ){
- + memcpy(&pCol->zName[n+1], zType, m+1);
- + pCol->colFlags |= COLFLAG_HASTYPE;
- + }
- + }
- + if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
- + pColl = sqlite3ExprCollSeq(pParse, p);
- + if( pColl && pCol->zColl==0 ){
- + pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
- + }
- + }
- + pTab->szTabRow = 1; /* Any non-zero value works */
- +}
- +
- +/*
- +** Given a SELECT statement, generate a Table structure that describes
- +** the result set of that SELECT.
- +*/
- +Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
- + Table *pTab;
- + sqlite3 *db = pParse->db;
- + u64 savedFlags;
- +
- + savedFlags = db->flags;
- + db->flags &= ~(u64)SQLITE_FullColNames;
- + db->flags |= SQLITE_ShortColNames;
- + sqlite3SelectPrep(pParse, pSelect, 0);
- + db->flags = savedFlags;
- + if( pParse->nErr ) return 0;
- + while( pSelect->pPrior ) pSelect = pSelect->pPrior;
- + pTab = sqlite3DbMallocZero(db, sizeof(Table) );
- + if( pTab==0 ){
- + return 0;
- + }
- + pTab->nTabRef = 1;
- + pTab->zName = 0;
- + pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
- + sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
- + sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect, aff);
- + pTab->iPKey = -1;
- + if( db->mallocFailed ){
- + sqlite3DeleteTable(db, pTab);
- + return 0;
- + }
- + return pTab;
- +}
- +
- +/*
- +** Get a VDBE for the given parser context. Create a new one if necessary.
- +** If an error occurs, return NULL and leave a message in pParse.
- +*/
- +Vdbe *sqlite3GetVdbe(Parse *pParse){
- + if( pParse->pVdbe ){
- + return pParse->pVdbe;
- + }
- + if( pParse->pToplevel==0
- + && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
- + ){
- + pParse->okConstFactor = 1;
- + }
- + return sqlite3VdbeCreate(pParse);
- +}
- +
- +
- +/*
- +** Compute the iLimit and iOffset fields of the SELECT based on the
- +** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
- +** that appear in the original SQL statement after the LIMIT and OFFSET
- +** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
- +** are the integer memory register numbers for counters used to compute
- +** the limit and offset. If there is no limit and/or offset, then
- +** iLimit and iOffset are negative.
- +**
- +** This routine changes the values of iLimit and iOffset only if
- +** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
- +** and iOffset should have been preset to appropriate default values (zero)
- +** prior to calling this routine.
- +**
- +** The iOffset register (if it exists) is initialized to the value
- +** of the OFFSET. The iLimit register is initialized to LIMIT. Register
- +** iOffset+1 is initialized to LIMIT+OFFSET.
- +**
- +** Only if pLimit->pLeft!=0 do the limit registers get
- +** redefined. The UNION ALL operator uses this property to force
- +** the reuse of the same limit and offset registers across multiple
- +** SELECT statements.
- +*/
- +static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
- + Vdbe *v = 0;
- + int iLimit = 0;
- + int iOffset;
- + int n;
- + Expr *pLimit = p->pLimit;
- +
- + if( p->iLimit ) return;
- +
- + /*
- + ** "LIMIT -1" always shows all rows. There is some
- + ** controversy about what the correct behavior should be.
- + ** The current implementation interprets "LIMIT 0" to mean
- + ** no rows.
- + */
- + if( pLimit ){
- + assert( pLimit->op==TK_LIMIT );
- + assert( pLimit->pLeft!=0 );
- + p->iLimit = iLimit = ++pParse->nMem;
- + v = sqlite3GetVdbe(pParse);
- + assert( v!=0 );
- + if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
- + sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
- + VdbeComment((v, "LIMIT counter"));
- + if( n==0 ){
- + sqlite3VdbeGoto(v, iBreak);
- + }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
- + p->nSelectRow = sqlite3LogEst((u64)n);
- + p->selFlags |= SF_FixedLimit;
- + }
- + }else{
- + sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
- + sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
- + VdbeComment((v, "LIMIT counter"));
- + sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
- + }
- + if( pLimit->pRight ){
- + p->iOffset = iOffset = ++pParse->nMem;
- + pParse->nMem++; /* Allocate an extra register for limit+offset */
- + sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
- + sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
- + VdbeComment((v, "OFFSET counter"));
- + sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
- + VdbeComment((v, "LIMIT+OFFSET"));
- + }
- + }
- +}
- +
- +#ifndef SQLITE_OMIT_COMPOUND_SELECT
- +/*
- +** Return the appropriate collating sequence for the iCol-th column of
- +** the result set for the compound-select statement "p". Return NULL if
- +** the column has no default collating sequence.
- +**
- +** The collating sequence for the compound select is taken from the
- +** left-most term of the select that has a collating sequence.
- +*/
- +static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
- + CollSeq *pRet;
- + if( p->pPrior ){
- + pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
- + }else{
- + pRet = 0;
- + }
- + assert( iCol>=0 );
- + /* iCol must be less than p->pEList->nExpr. Otherwise an error would
- + ** have been thrown during name resolution and we would not have gotten
- + ** this far */
- + if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
- + pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
- + }
- + return pRet;
- +}
- +
- +/*
- +** The select statement passed as the second parameter is a compound SELECT
- +** with an ORDER BY clause. This function allocates and returns a KeyInfo
- +** structure suitable for implementing the ORDER BY.
- +**
- +** Space to hold the KeyInfo structure is obtained from malloc. The calling
- +** function is responsible for ensuring that this structure is eventually
- +** freed.
- +*/
- +static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
- + ExprList *pOrderBy = p->pOrderBy;
- + int nOrderBy = p->pOrderBy->nExpr;
- + sqlite3 *db = pParse->db;
- + KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
- + if( pRet ){
- + int i;
- + for(i=0; i<nOrderBy; i++){
- + struct ExprList_item *pItem = &pOrderBy->a[i];
- + Expr *pTerm = pItem->pExpr;
- + CollSeq *pColl;
- +
- + if( pTerm->flags & EP_Collate ){
- + pColl = sqlite3ExprCollSeq(pParse, pTerm);
- + }else{
- + pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
- + if( pColl==0 ) pColl = db->pDfltColl;
- + pOrderBy->a[i].pExpr =
- + sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
- + }
- + assert( sqlite3KeyInfoIsWriteable(pRet) );
- + pRet->aColl[i] = pColl;
- + pRet->aSortFlags[i] = pOrderBy->a[i].sortFlags;
- + }
- + }
- +
- + return pRet;
- +}
- +
- +#ifndef SQLITE_OMIT_CTE
- +/*
- +** This routine generates VDBE code to compute the content of a WITH RECURSIVE
- +** query of the form:
- +**
- +** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
- +** \___________/ \_______________/
- +** p->pPrior p
- +**
- +**
- +** There is exactly one reference to the recursive-table in the FROM clause
- +** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
- +**
- +** The setup-query runs once to generate an initial set of rows that go
- +** into a Queue table. Rows are extracted from the Queue table one by
- +** one. Each row extracted from Queue is output to pDest. Then the single
- +** extracted row (now in the iCurrent table) becomes the content of the
- +** recursive-table for a recursive-query run. The output of the recursive-query
- +** is added back into the Queue table. Then another row is extracted from Queue
- +** and the iteration continues until the Queue table is empty.
- +**
- +** If the compound query operator is UNION then no duplicate rows are ever
- +** inserted into the Queue table. The iDistinct table keeps a copy of all rows
- +** that have ever been inserted into Queue and causes duplicates to be
- +** discarded. If the operator is UNION ALL, then duplicates are allowed.
- +**
- +** If the query has an ORDER BY, then entries in the Queue table are kept in
- +** ORDER BY order and the first entry is extracted for each cycle. Without
- +** an ORDER BY, the Queue table is just a FIFO.
- +**
- +** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
- +** have been output to pDest. A LIMIT of zero means to output no rows and a
- +** negative LIMIT means to output all rows. If there is also an OFFSET clause
- +** with a positive value, then the first OFFSET outputs are discarded rather
- +** than being sent to pDest. The LIMIT count does not begin until after OFFSET
- +** rows have been skipped.
- +*/
- +static void generateWithRecursiveQuery(
- + Parse *pParse, /* Parsing context */
- + Select *p, /* The recursive SELECT to be coded */
- + SelectDest *pDest /* What to do with query results */
- +){
- + SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
- + int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
- + Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
- + Select *pSetup = p->pPrior; /* The setup query */
- + int addrTop; /* Top of the loop */
- + int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
- + int iCurrent = 0; /* The Current table */
- + int regCurrent; /* Register holding Current table */
- + int iQueue; /* The Queue table */
- + int iDistinct = 0; /* To ensure unique results if UNION */
- + int eDest = SRT_Fifo; /* How to write to Queue */
- + SelectDest destQueue; /* SelectDest targetting the Queue table */
- + int i; /* Loop counter */
- + int rc; /* Result code */
- + ExprList *pOrderBy; /* The ORDER BY clause */
- + Expr *pLimit; /* Saved LIMIT and OFFSET */
- + int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
- +
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + if( p->pWin ){
- + sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
- + return;
- + }
- +#endif
- +
- + /* Obtain authorization to do a recursive query */
- + if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
- +
- + /* Process the LIMIT and OFFSET clauses, if they exist */
- + addrBreak = sqlite3VdbeMakeLabel(pParse);
- + p->nSelectRow = 320; /* 4 billion rows */
- + computeLimitRegisters(pParse, p, addrBreak);
- + pLimit = p->pLimit;
- + regLimit = p->iLimit;
- + regOffset = p->iOffset;
- + p->pLimit = 0;
- + p->iLimit = p->iOffset = 0;
- + pOrderBy = p->pOrderBy;
- +
- + /* Locate the cursor number of the Current table */
- + for(i=0; ALWAYS(i<pSrc->nSrc); i++){
- + if( pSrc->a[i].fg.isRecursive ){
- + iCurrent = pSrc->a[i].iCursor;
- + break;
- + }
- + }
- +
- + /* Allocate cursors numbers for Queue and Distinct. The cursor number for
- + ** the Distinct table must be exactly one greater than Queue in order
- + ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
- + iQueue = pParse->nTab++;
- + if( p->op==TK_UNION ){
- + eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
- + iDistinct = pParse->nTab++;
- + }else{
- + eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
- + }
- + sqlite3SelectDestInit(&destQueue, eDest, iQueue);
- +
- + /* Allocate cursors for Current, Queue, and Distinct. */
- + regCurrent = ++pParse->nMem;
- + sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
- + if( pOrderBy ){
- + KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
- + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
- + (char*)pKeyInfo, P4_KEYINFO);
- + destQueue.pOrderBy = pOrderBy;
- + }else{
- + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
- + }
- + VdbeComment((v, "Queue table"));
- + if( iDistinct ){
- + p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
- + p->selFlags |= SF_UsesEphemeral;
- + }
- +
- + /* Detach the ORDER BY clause from the compound SELECT */
- + p->pOrderBy = 0;
- +
- + /* Store the results of the setup-query in Queue. */
- + pSetup->pNext = 0;
- + ExplainQueryPlan((pParse, 1, "SETUP"));
- + rc = sqlite3Select(pParse, pSetup, &destQueue);
- + pSetup->pNext = p;
- + if( rc ) goto end_of_recursive_query;
- +
- + /* Find the next row in the Queue and output that row */
- + addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
- +
- + /* Transfer the next row in Queue over to Current */
- + sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
- + if( pOrderBy ){
- + sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
- + }else{
- + sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
- + }
- + sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
- +
- + /* Output the single row in Current */
- + addrCont = sqlite3VdbeMakeLabel(pParse);
- + codeOffset(v, regOffset, addrCont);
- + selectInnerLoop(pParse, p, iCurrent,
- + 0, 0, pDest, addrCont, addrBreak);
- + if( regLimit ){
- + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
- + VdbeCoverage(v);
- + }
- + sqlite3VdbeResolveLabel(v, addrCont);
- +
- + /* Execute the recursive SELECT taking the single row in Current as
- + ** the value for the recursive-table. Store the results in the Queue.
- + */
- + if( p->selFlags & SF_Aggregate ){
- + sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
- + }else{
- + p->pPrior = 0;
- + ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
- + sqlite3Select(pParse, p, &destQueue);
- + assert( p->pPrior==0 );
- + p->pPrior = pSetup;
- + }
- +
- + /* Keep running the loop until the Queue is empty */
- + sqlite3VdbeGoto(v, addrTop);
- + sqlite3VdbeResolveLabel(v, addrBreak);
- +
- +end_of_recursive_query:
- + sqlite3ExprListDelete(pParse->db, p->pOrderBy);
- + p->pOrderBy = pOrderBy;
- + p->pLimit = pLimit;
- + return;
- +}
- +#endif /* SQLITE_OMIT_CTE */
- +
- +/* Forward references */
- +static int multiSelectOrderBy(
- + Parse *pParse, /* Parsing context */
- + Select *p, /* The right-most of SELECTs to be coded */
- + SelectDest *pDest /* What to do with query results */
- +);
- +
- +/*
- +** Handle the special case of a compound-select that originates from a
- +** VALUES clause. By handling this as a special case, we avoid deep
- +** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
- +** on a VALUES clause.
- +**
- +** Because the Select object originates from a VALUES clause:
- +** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
- +** (2) All terms are UNION ALL
- +** (3) There is no ORDER BY clause
- +**
- +** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
- +** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
- +** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
- +** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
- +*/
- +static int multiSelectValues(
- + Parse *pParse, /* Parsing context */
- + Select *p, /* The right-most of SELECTs to be coded */
- + SelectDest *pDest /* What to do with query results */
- +){
- + int nRow = 1;
- + int rc = 0;
- + int bShowAll = p->pLimit==0;
- + assert( p->selFlags & SF_MultiValue );
- + do{
- + assert( p->selFlags & SF_Values );
- + assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
- + assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + if( p->pWin ) return -1;
- +#endif
- + if( p->pPrior==0 ) break;
- + assert( p->pPrior->pNext==p );
- + p = p->pPrior;
- + nRow += bShowAll;
- + }while(1);
- + ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
- + nRow==1 ? "" : "S"));
- + while( p ){
- + selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
- + if( !bShowAll ) break;
- + p->nSelectRow = nRow;
- + p = p->pNext;
- + }
- + return rc;
- +}
- +
- +/*
- +** This routine is called to process a compound query form from
- +** two or more separate queries using UNION, UNION ALL, EXCEPT, or
- +** INTERSECT
- +**
- +** "p" points to the right-most of the two queries. the query on the
- +** left is p->pPrior. The left query could also be a compound query
- +** in which case this routine will be called recursively.
- +**
- +** The results of the total query are to be written into a destination
- +** of type eDest with parameter iParm.
- +**
- +** Example 1: Consider a three-way compound SQL statement.
- +**
- +** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
- +**
- +** This statement is parsed up as follows:
- +**
- +** SELECT c FROM t3
- +** |
- +** `-----> SELECT b FROM t2
- +** |
- +** `------> SELECT a FROM t1
- +**
- +** The arrows in the diagram above represent the Select.pPrior pointer.
- +** So if this routine is called with p equal to the t3 query, then
- +** pPrior will be the t2 query. p->op will be TK_UNION in this case.
- +**
- +** Notice that because of the way SQLite parses compound SELECTs, the
- +** individual selects always group from left to right.
- +*/
- +static int multiSelect(
- + Parse *pParse, /* Parsing context */
- + Select *p, /* The right-most of SELECTs to be coded */
- + SelectDest *pDest /* What to do with query results */
- +){
- + int rc = SQLITE_OK; /* Success code from a subroutine */
- + Select *pPrior; /* Another SELECT immediately to our left */
- + Vdbe *v; /* Generate code to this VDBE */
- + SelectDest dest; /* Alternative data destination */
- + Select *pDelete = 0; /* Chain of simple selects to delete */
- + sqlite3 *db; /* Database connection */
- +
- + /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
- + ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
- + */
- + assert( p && p->pPrior ); /* Calling function guarantees this much */
- + assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
- + assert( p->selFlags & SF_Compound );
- + db = pParse->db;
- + pPrior = p->pPrior;
- + dest = *pDest;
- + if( pPrior->pOrderBy || pPrior->pLimit ){
- + sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
- + pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
- + rc = 1;
- + goto multi_select_end;
- + }
- +
- + v = sqlite3GetVdbe(pParse);
- + assert( v!=0 ); /* The VDBE already created by calling function */
- +
- + /* Create the destination temporary table if necessary
- + */
- + if( dest.eDest==SRT_EphemTab ){
- + assert( p->pEList );
- + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
- + dest.eDest = SRT_Table;
- + }
- +
- + /* Special handling for a compound-select that originates as a VALUES clause.
- + */
- + if( p->selFlags & SF_MultiValue ){
- + rc = multiSelectValues(pParse, p, &dest);
- + if( rc>=0 ) goto multi_select_end;
- + rc = SQLITE_OK;
- + }
- +
- + /* Make sure all SELECTs in the statement have the same number of elements
- + ** in their result sets.
- + */
- + assert( p->pEList && pPrior->pEList );
- + assert( p->pEList->nExpr==pPrior->pEList->nExpr );
- +
- +#ifndef SQLITE_OMIT_CTE
- + if( p->selFlags & SF_Recursive ){
- + generateWithRecursiveQuery(pParse, p, &dest);
- + }else
- +#endif
- +
- + /* Compound SELECTs that have an ORDER BY clause are handled separately.
- + */
- + if( p->pOrderBy ){
- + return multiSelectOrderBy(pParse, p, pDest);
- + }else{
- +
- +#ifndef SQLITE_OMIT_EXPLAIN
- + if( pPrior->pPrior==0 ){
- + ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
- + ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
- + }
- +#endif
- +
- + /* Generate code for the left and right SELECT statements.
- + */
- + switch( p->op ){
- + case TK_ALL: {
- + int addr = 0;
- + int nLimit;
- + assert( !pPrior->pLimit );
- + pPrior->iLimit = p->iLimit;
- + pPrior->iOffset = p->iOffset;
- + pPrior->pLimit = p->pLimit;
- + rc = sqlite3Select(pParse, pPrior, &dest);
- + p->pLimit = 0;
- + if( rc ){
- + goto multi_select_end;
- + }
- + p->pPrior = 0;
- + p->iLimit = pPrior->iLimit;
- + p->iOffset = pPrior->iOffset;
- + if( p->iLimit ){
- + addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
- + VdbeComment((v, "Jump ahead if LIMIT reached"));
- + if( p->iOffset ){
- + sqlite3VdbeAddOp3(v, OP_OffsetLimit,
- + p->iLimit, p->iOffset+1, p->iOffset);
- + }
- + }
- + ExplainQueryPlan((pParse, 1, "UNION ALL"));
- + rc = sqlite3Select(pParse, p, &dest);
- + testcase( rc!=SQLITE_OK );
- + pDelete = p->pPrior;
- + p->pPrior = pPrior;
- + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
- + if( pPrior->pLimit
- + && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
- + && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
- + ){
- + p->nSelectRow = sqlite3LogEst((u64)nLimit);
- + }
- + if( addr ){
- + sqlite3VdbeJumpHere(v, addr);
- + }
- + break;
- + }
- + case TK_EXCEPT:
- + case TK_UNION: {
- + int unionTab; /* Cursor number of the temp table holding result */
- + u8 op = 0; /* One of the SRT_ operations to apply to self */
- + int priorOp; /* The SRT_ operation to apply to prior selects */
- + Expr *pLimit; /* Saved values of p->nLimit */
- + int addr;
- + SelectDest uniondest;
- +
- + testcase( p->op==TK_EXCEPT );
- + testcase( p->op==TK_UNION );
- + priorOp = SRT_Union;
- + if( dest.eDest==priorOp ){
- + /* We can reuse a temporary table generated by a SELECT to our
- + ** right.
- + */
- + assert( p->pLimit==0 ); /* Not allowed on leftward elements */
- + unionTab = dest.iSDParm;
- + }else{
- + /* We will need to create our own temporary table to hold the
- + ** intermediate results.
- + */
- + unionTab = pParse->nTab++;
- + assert( p->pOrderBy==0 );
- + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
- + assert( p->addrOpenEphm[0] == -1 );
- + p->addrOpenEphm[0] = addr;
- + findRightmost(p)->selFlags |= SF_UsesEphemeral;
- + assert( p->pEList );
- + }
- +
- + /* Code the SELECT statements to our left
- + */
- + assert( !pPrior->pOrderBy );
- + sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
- + rc = sqlite3Select(pParse, pPrior, &uniondest);
- + if( rc ){
- + goto multi_select_end;
- + }
- +
- + /* Code the current SELECT statement
- + */
- + if( p->op==TK_EXCEPT ){
- + op = SRT_Except;
- + }else{
- + assert( p->op==TK_UNION );
- + op = SRT_Union;
- + }
- + p->pPrior = 0;
- + pLimit = p->pLimit;
- + p->pLimit = 0;
- + uniondest.eDest = op;
- + ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
- + selectOpName(p->op)));
- + rc = sqlite3Select(pParse, p, &uniondest);
- + testcase( rc!=SQLITE_OK );
- + /* Query flattening in sqlite3Select() might refill p->pOrderBy.
- + ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
- + sqlite3ExprListDelete(db, p->pOrderBy);
- + pDelete = p->pPrior;
- + p->pPrior = pPrior;
- + p->pOrderBy = 0;
- + if( p->op==TK_UNION ){
- + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
- + }
- + sqlite3ExprDelete(db, p->pLimit);
- + p->pLimit = pLimit;
- + p->iLimit = 0;
- + p->iOffset = 0;
- +
- + /* Convert the data in the temporary table into whatever form
- + ** it is that we currently need.
- + */
- + assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
- + assert( p->pEList || db->mallocFailed );
- + if( dest.eDest!=priorOp && db->mallocFailed==0 ){
- + int iCont, iBreak, iStart;
- + iBreak = sqlite3VdbeMakeLabel(pParse);
- + iCont = sqlite3VdbeMakeLabel(pParse);
- + computeLimitRegisters(pParse, p, iBreak);
- + sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
- + iStart = sqlite3VdbeCurrentAddr(v);
- + selectInnerLoop(pParse, p, unionTab,
- + 0, 0, &dest, iCont, iBreak);
- + sqlite3VdbeResolveLabel(v, iCont);
- + sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
- + sqlite3VdbeResolveLabel(v, iBreak);
- + sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
- + }
- + break;
- + }
- + default: assert( p->op==TK_INTERSECT ); {
- + int tab1, tab2;
- + int iCont, iBreak, iStart;
- + Expr *pLimit;
- + int addr;
- + SelectDest intersectdest;
- + int r1;
- +
- + /* INTERSECT is different from the others since it requires
- + ** two temporary tables. Hence it has its own case. Begin
- + ** by allocating the tables we will need.
- + */
- + tab1 = pParse->nTab++;
- + tab2 = pParse->nTab++;
- + assert( p->pOrderBy==0 );
- +
- + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
- + assert( p->addrOpenEphm[0] == -1 );
- + p->addrOpenEphm[0] = addr;
- + findRightmost(p)->selFlags |= SF_UsesEphemeral;
- + assert( p->pEList );
- +
- + /* Code the SELECTs to our left into temporary table "tab1".
- + */
- + sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
- + rc = sqlite3Select(pParse, pPrior, &intersectdest);
- + if( rc ){
- + goto multi_select_end;
- + }
- +
- + /* Code the current SELECT into temporary table "tab2"
- + */
- + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
- + assert( p->addrOpenEphm[1] == -1 );
- + p->addrOpenEphm[1] = addr;
- + p->pPrior = 0;
- + pLimit = p->pLimit;
- + p->pLimit = 0;
- + intersectdest.iSDParm = tab2;
- + ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
- + selectOpName(p->op)));
- + rc = sqlite3Select(pParse, p, &intersectdest);
- + testcase( rc!=SQLITE_OK );
- + pDelete = p->pPrior;
- + p->pPrior = pPrior;
- + if( p->nSelectRow>pPrior->nSelectRow ){
- + p->nSelectRow = pPrior->nSelectRow;
- + }
- + sqlite3ExprDelete(db, p->pLimit);
- + p->pLimit = pLimit;
- +
- + /* Generate code to take the intersection of the two temporary
- + ** tables.
- + */
- + if( rc ) break;
- + assert( p->pEList );
- + iBreak = sqlite3VdbeMakeLabel(pParse);
- + iCont = sqlite3VdbeMakeLabel(pParse);
- + computeLimitRegisters(pParse, p, iBreak);
- + sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
- + r1 = sqlite3GetTempReg(pParse);
- + iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
- + sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
- + VdbeCoverage(v);
- + sqlite3ReleaseTempReg(pParse, r1);
- + selectInnerLoop(pParse, p, tab1,
- + 0, 0, &dest, iCont, iBreak);
- + sqlite3VdbeResolveLabel(v, iCont);
- + sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
- + sqlite3VdbeResolveLabel(v, iBreak);
- + sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
- + sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
- + break;
- + }
- + }
- +
- + #ifndef SQLITE_OMIT_EXPLAIN
- + if( p->pNext==0 ){
- + ExplainQueryPlanPop(pParse);
- + }
- + #endif
- + }
- + if( pParse->nErr ) goto multi_select_end;
- +
- + /* Compute collating sequences used by
- + ** temporary tables needed to implement the compound select.
- + ** Attach the KeyInfo structure to all temporary tables.
- + **
- + ** This section is run by the right-most SELECT statement only.
- + ** SELECT statements to the left always skip this part. The right-most
- + ** SELECT might also skip this part if it has no ORDER BY clause and
- + ** no temp tables are required.
- + */
- + if( p->selFlags & SF_UsesEphemeral ){
- + int i; /* Loop counter */
- + KeyInfo *pKeyInfo; /* Collating sequence for the result set */
- + Select *pLoop; /* For looping through SELECT statements */
- + CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
- + int nCol; /* Number of columns in result set */
- +
- + assert( p->pNext==0 );
- + nCol = p->pEList->nExpr;
- + pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
- + if( !pKeyInfo ){
- + rc = SQLITE_NOMEM_BKPT;
- + goto multi_select_end;
- + }
- + for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
- + *apColl = multiSelectCollSeq(pParse, p, i);
- + if( 0==*apColl ){
- + *apColl = db->pDfltColl;
- + }
- + }
- +
- + for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
- + for(i=0; i<2; i++){
- + int addr = pLoop->addrOpenEphm[i];
- + if( addr<0 ){
- + /* If [0] is unused then [1] is also unused. So we can
- + ** always safely abort as soon as the first unused slot is found */
- + assert( pLoop->addrOpenEphm[1]<0 );
- + break;
- + }
- + sqlite3VdbeChangeP2(v, addr, nCol);
- + sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
- + P4_KEYINFO);
- + pLoop->addrOpenEphm[i] = -1;
- + }
- + }
- + sqlite3KeyInfoUnref(pKeyInfo);
- + }
- +
- +multi_select_end:
- + pDest->iSdst = dest.iSdst;
- + pDest->nSdst = dest.nSdst;
- + sqlite3SelectDelete(db, pDelete);
- + return rc;
- +}
- +#endif /* SQLITE_OMIT_COMPOUND_SELECT */
- +
- +/*
- +** Error message for when two or more terms of a compound select have different
- +** size result sets.
- +*/
- +void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
- + if( p->selFlags & SF_Values ){
- + sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
- + }else{
- + sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
- + " do not have the same number of result columns", selectOpName(p->op));
- + }
- +}
- +
- +/*
- +** Code an output subroutine for a coroutine implementation of a
- +** SELECT statment.
- +**
- +** The data to be output is contained in pIn->iSdst. There are
- +** pIn->nSdst columns to be output. pDest is where the output should
- +** be sent.
- +**
- +** regReturn is the number of the register holding the subroutine
- +** return address.
- +**
- +** If regPrev>0 then it is the first register in a vector that
- +** records the previous output. mem[regPrev] is a flag that is false
- +** if there has been no previous output. If regPrev>0 then code is
- +** generated to suppress duplicates. pKeyInfo is used for comparing
- +** keys.
- +**
- +** If the LIMIT found in p->iLimit is reached, jump immediately to
- +** iBreak.
- +*/
- +static int generateOutputSubroutine(
- + Parse *pParse, /* Parsing context */
- + Select *p, /* The SELECT statement */
- + SelectDest *pIn, /* Coroutine supplying data */
- + SelectDest *pDest, /* Where to send the data */
- + int regReturn, /* The return address register */
- + int regPrev, /* Previous result register. No uniqueness if 0 */
- + KeyInfo *pKeyInfo, /* For comparing with previous entry */
- + int iBreak /* Jump here if we hit the LIMIT */
- +){
- + Vdbe *v = pParse->pVdbe;
- + int iContinue;
- + int addr;
- +
- + addr = sqlite3VdbeCurrentAddr(v);
- + iContinue = sqlite3VdbeMakeLabel(pParse);
- +
- + /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
- + */
- + if( regPrev ){
- + int addr1, addr2;
- + addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
- + addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
- + (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
- + sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
- + sqlite3VdbeJumpHere(v, addr1);
- + sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
- + sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
- + }
- + if( pParse->db->mallocFailed ) return 0;
- +
- + /* Suppress the first OFFSET entries if there is an OFFSET clause
- + */
- + codeOffset(v, p->iOffset, iContinue);
- +
- + assert( pDest->eDest!=SRT_Exists );
- + assert( pDest->eDest!=SRT_Table );
- + switch( pDest->eDest ){
- + /* Store the result as data using a unique key.
- + */
- + case SRT_EphemTab: {
- + int r1 = sqlite3GetTempReg(pParse);
- + int r2 = sqlite3GetTempReg(pParse);
- + sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
- + sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
- + sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
- + sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
- + sqlite3ReleaseTempReg(pParse, r2);
- + sqlite3ReleaseTempReg(pParse, r1);
- + break;
- + }
- +
- +#ifndef SQLITE_OMIT_SUBQUERY
- + /* If we are creating a set for an "expr IN (SELECT ...)".
- + */
- + case SRT_Set: {
- + int r1;
- + testcase( pIn->nSdst>1 );
- + r1 = sqlite3GetTempReg(pParse);
- + sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
- + r1, pDest->zAffSdst, pIn->nSdst);
- + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
- + pIn->iSdst, pIn->nSdst);
- + sqlite3ReleaseTempReg(pParse, r1);
- + break;
- + }
- +
- + /* If this is a scalar select that is part of an expression, then
- + ** store the results in the appropriate memory cell and break out
- + ** of the scan loop. Note that the select might return multiple columns
- + ** if it is the RHS of a row-value IN operator.
- + */
- + case SRT_Mem: {
- + if( pParse->nErr==0 ){
- + testcase( pIn->nSdst>1 );
- + sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
- + }
- + /* The LIMIT clause will jump out of the loop for us */
- + break;
- + }
- +#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
- +
- + /* The results are stored in a sequence of registers
- + ** starting at pDest->iSdst. Then the co-routine yields.
- + */
- + case SRT_Coroutine: {
- + if( pDest->iSdst==0 ){
- + pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
- + pDest->nSdst = pIn->nSdst;
- + }
- + sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
- + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
- + break;
- + }
- +
- + /* If none of the above, then the result destination must be
- + ** SRT_Output. This routine is never called with any other
- + ** destination other than the ones handled above or SRT_Output.
- + **
- + ** For SRT_Output, results are stored in a sequence of registers.
- + ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
- + ** return the next row of result.
- + */
- + default: {
- + assert( pDest->eDest==SRT_Output );
- + sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
- + break;
- + }
- + }
- +
- + /* Jump to the end of the loop if the LIMIT is reached.
- + */
- + if( p->iLimit ){
- + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
- + }
- +
- + /* Generate the subroutine return
- + */
- + sqlite3VdbeResolveLabel(v, iContinue);
- + sqlite3VdbeAddOp1(v, OP_Return, regReturn);
- +
- + return addr;
- +}
- +
- +/*
- +** Alternative compound select code generator for cases when there
- +** is an ORDER BY clause.
- +**
- +** We assume a query of the following form:
- +**
- +** <selectA> <operator> <selectB> ORDER BY <orderbylist>
- +**
- +** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
- +** is to code both <selectA> and <selectB> with the ORDER BY clause as
- +** co-routines. Then run the co-routines in parallel and merge the results
- +** into the output. In addition to the two coroutines (called selectA and
- +** selectB) there are 7 subroutines:
- +**
- +** outA: Move the output of the selectA coroutine into the output
- +** of the compound query.
- +**
- +** outB: Move the output of the selectB coroutine into the output
- +** of the compound query. (Only generated for UNION and
- +** UNION ALL. EXCEPT and INSERTSECT never output a row that
- +** appears only in B.)
- +**
- +** AltB: Called when there is data from both coroutines and A<B.
- +**
- +** AeqB: Called when there is data from both coroutines and A==B.
- +**
- +** AgtB: Called when there is data from both coroutines and A>B.
- +**
- +** EofA: Called when data is exhausted from selectA.
- +**
- +** EofB: Called when data is exhausted from selectB.
- +**
- +** The implementation of the latter five subroutines depend on which
- +** <operator> is used:
- +**
- +**
- +** UNION ALL UNION EXCEPT INTERSECT
- +** ------------- ----------------- -------------- -----------------
- +** AltB: outA, nextA outA, nextA outA, nextA nextA
- +**
- +** AeqB: outA, nextA nextA nextA outA, nextA
- +**
- +** AgtB: outB, nextB outB, nextB nextB nextB
- +**
- +** EofA: outB, nextB outB, nextB halt halt
- +**
- +** EofB: outA, nextA outA, nextA outA, nextA halt
- +**
- +** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
- +** causes an immediate jump to EofA and an EOF on B following nextB causes
- +** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
- +** following nextX causes a jump to the end of the select processing.
- +**
- +** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
- +** within the output subroutine. The regPrev register set holds the previously
- +** output value. A comparison is made against this value and the output
- +** is skipped if the next results would be the same as the previous.
- +**
- +** The implementation plan is to implement the two coroutines and seven
- +** subroutines first, then put the control logic at the bottom. Like this:
- +**
- +** goto Init
- +** coA: coroutine for left query (A)
- +** coB: coroutine for right query (B)
- +** outA: output one row of A
- +** outB: output one row of B (UNION and UNION ALL only)
- +** EofA: ...
- +** EofB: ...
- +** AltB: ...
- +** AeqB: ...
- +** AgtB: ...
- +** Init: initialize coroutine registers
- +** yield coA
- +** if eof(A) goto EofA
- +** yield coB
- +** if eof(B) goto EofB
- +** Cmpr: Compare A, B
- +** Jump AltB, AeqB, AgtB
- +** End: ...
- +**
- +** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
- +** actually called using Gosub and they do not Return. EofA and EofB loop
- +** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
- +** and AgtB jump to either L2 or to one of EofA or EofB.
- +*/
- +#ifndef SQLITE_OMIT_COMPOUND_SELECT
- +static int multiSelectOrderBy(
- + Parse *pParse, /* Parsing context */
- + Select *p, /* The right-most of SELECTs to be coded */
- + SelectDest *pDest /* What to do with query results */
- +){
- + int i, j; /* Loop counters */
- + Select *pPrior; /* Another SELECT immediately to our left */
- + Vdbe *v; /* Generate code to this VDBE */
- + SelectDest destA; /* Destination for coroutine A */
- + SelectDest destB; /* Destination for coroutine B */
- + int regAddrA; /* Address register for select-A coroutine */
- + int regAddrB; /* Address register for select-B coroutine */
- + int addrSelectA; /* Address of the select-A coroutine */
- + int addrSelectB; /* Address of the select-B coroutine */
- + int regOutA; /* Address register for the output-A subroutine */
- + int regOutB; /* Address register for the output-B subroutine */
- + int addrOutA; /* Address of the output-A subroutine */
- + int addrOutB = 0; /* Address of the output-B subroutine */
- + int addrEofA; /* Address of the select-A-exhausted subroutine */
- + int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
- + int addrEofB; /* Address of the select-B-exhausted subroutine */
- + int addrAltB; /* Address of the A<B subroutine */
- + int addrAeqB; /* Address of the A==B subroutine */
- + int addrAgtB; /* Address of the A>B subroutine */
- + int regLimitA; /* Limit register for select-A */
- + int regLimitB; /* Limit register for select-A */
- + int regPrev; /* A range of registers to hold previous output */
- + int savedLimit; /* Saved value of p->iLimit */
- + int savedOffset; /* Saved value of p->iOffset */
- + int labelCmpr; /* Label for the start of the merge algorithm */
- + int labelEnd; /* Label for the end of the overall SELECT stmt */
- + int addr1; /* Jump instructions that get retargetted */
- + int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
- + KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
- + KeyInfo *pKeyMerge; /* Comparison information for merging rows */
- + sqlite3 *db; /* Database connection */
- + ExprList *pOrderBy; /* The ORDER BY clause */
- + int nOrderBy; /* Number of terms in the ORDER BY clause */
- + int *aPermute; /* Mapping from ORDER BY terms to result set columns */
- +
- + assert( p->pOrderBy!=0 );
- + assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
- + db = pParse->db;
- + v = pParse->pVdbe;
- + assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */
- + labelEnd = sqlite3VdbeMakeLabel(pParse);
- + labelCmpr = sqlite3VdbeMakeLabel(pParse);
- +
- +
- + /* Patch up the ORDER BY clause
- + */
- + op = p->op;
- + pPrior = p->pPrior;
- + assert( pPrior->pOrderBy==0 );
- + pOrderBy = p->pOrderBy;
- + assert( pOrderBy );
- + nOrderBy = pOrderBy->nExpr;
- +
- + /* For operators other than UNION ALL we have to make sure that
- + ** the ORDER BY clause covers every term of the result set. Add
- + ** terms to the ORDER BY clause as necessary.
- + */
- + if( op!=TK_ALL ){
- + for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
- + struct ExprList_item *pItem;
- + for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
- + assert( pItem->u.x.iOrderByCol>0 );
- + if( pItem->u.x.iOrderByCol==i ) break;
- + }
- + if( j==nOrderBy ){
- + Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
- + if( pNew==0 ) return SQLITE_NOMEM_BKPT;
- + pNew->flags |= EP_IntValue;
- + pNew->u.iValue = i;
- + p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
- + if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
- + }
- + }
- + }
- +
- + /* Compute the comparison permutation and keyinfo that is used with
- + ** the permutation used to determine if the next
- + ** row of results comes from selectA or selectB. Also add explicit
- + ** collations to the ORDER BY clause terms so that when the subqueries
- + ** to the right and the left are evaluated, they use the correct
- + ** collation.
- + */
- + aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
- + if( aPermute ){
- + struct ExprList_item *pItem;
- + aPermute[0] = nOrderBy;
- + for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
- + assert( pItem->u.x.iOrderByCol>0 );
- + assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
- + aPermute[i] = pItem->u.x.iOrderByCol - 1;
- + }
- + pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
- + }else{
- + pKeyMerge = 0;
- + }
- +
- + /* Reattach the ORDER BY clause to the query.
- + */
- + p->pOrderBy = pOrderBy;
- + pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
- +
- + /* Allocate a range of temporary registers and the KeyInfo needed
- + ** for the logic that removes duplicate result rows when the
- + ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
- + */
- + if( op==TK_ALL ){
- + regPrev = 0;
- + }else{
- + int nExpr = p->pEList->nExpr;
- + assert( nOrderBy>=nExpr || db->mallocFailed );
- + regPrev = pParse->nMem+1;
- + pParse->nMem += nExpr+1;
- + sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
- + pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
- + if( pKeyDup ){
- + assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
- + for(i=0; i<nExpr; i++){
- + pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
- + pKeyDup->aSortFlags[i] = 0;
- + }
- + }
- + }
- +
- + /* Separate the left and the right query from one another
- + */
- + p->pPrior = 0;
- + pPrior->pNext = 0;
- + sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
- + if( pPrior->pPrior==0 ){
- + sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
- + }
- +
- + /* Compute the limit registers */
- + computeLimitRegisters(pParse, p, labelEnd);
- + if( p->iLimit && op==TK_ALL ){
- + regLimitA = ++pParse->nMem;
- + regLimitB = ++pParse->nMem;
- + sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
- + regLimitA);
- + sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
- + }else{
- + regLimitA = regLimitB = 0;
- + }
- + sqlite3ExprDelete(db, p->pLimit);
- + p->pLimit = 0;
- +
- + regAddrA = ++pParse->nMem;
- + regAddrB = ++pParse->nMem;
- + regOutA = ++pParse->nMem;
- + regOutB = ++pParse->nMem;
- + sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
- + sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
- +
- + ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
- +
- + /* Generate a coroutine to evaluate the SELECT statement to the
- + ** left of the compound operator - the "A" select.
- + */
- + addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
- + addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
- + VdbeComment((v, "left SELECT"));
- + pPrior->iLimit = regLimitA;
- + ExplainQueryPlan((pParse, 1, "LEFT"));
- + sqlite3Select(pParse, pPrior, &destA);
- + sqlite3VdbeEndCoroutine(v, regAddrA);
- + sqlite3VdbeJumpHere(v, addr1);
- +
- + /* Generate a coroutine to evaluate the SELECT statement on
- + ** the right - the "B" select
- + */
- + addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
- + addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
- + VdbeComment((v, "right SELECT"));
- + savedLimit = p->iLimit;
- + savedOffset = p->iOffset;
- + p->iLimit = regLimitB;
- + p->iOffset = 0;
- + ExplainQueryPlan((pParse, 1, "RIGHT"));
- + sqlite3Select(pParse, p, &destB);
- + p->iLimit = savedLimit;
- + p->iOffset = savedOffset;
- + sqlite3VdbeEndCoroutine(v, regAddrB);
- +
- + /* Generate a subroutine that outputs the current row of the A
- + ** select as the next output row of the compound select.
- + */
- + VdbeNoopComment((v, "Output routine for A"));
- + addrOutA = generateOutputSubroutine(pParse,
- + p, &destA, pDest, regOutA,
- + regPrev, pKeyDup, labelEnd);
- +
- + /* Generate a subroutine that outputs the current row of the B
- + ** select as the next output row of the compound select.
- + */
- + if( op==TK_ALL || op==TK_UNION ){
- + VdbeNoopComment((v, "Output routine for B"));
- + addrOutB = generateOutputSubroutine(pParse,
- + p, &destB, pDest, regOutB,
- + regPrev, pKeyDup, labelEnd);
- + }
- + sqlite3KeyInfoUnref(pKeyDup);
- +
- + /* Generate a subroutine to run when the results from select A
- + ** are exhausted and only data in select B remains.
- + */
- + if( op==TK_EXCEPT || op==TK_INTERSECT ){
- + addrEofA_noB = addrEofA = labelEnd;
- + }else{
- + VdbeNoopComment((v, "eof-A subroutine"));
- + addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
- + addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
- + VdbeCoverage(v);
- + sqlite3VdbeGoto(v, addrEofA);
- + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
- + }
- +
- + /* Generate a subroutine to run when the results from select B
- + ** are exhausted and only data in select A remains.
- + */
- + if( op==TK_INTERSECT ){
- + addrEofB = addrEofA;
- + if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
- + }else{
- + VdbeNoopComment((v, "eof-B subroutine"));
- + addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
- + sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
- + sqlite3VdbeGoto(v, addrEofB);
- + }
- +
- + /* Generate code to handle the case of A<B
- + */
- + VdbeNoopComment((v, "A-lt-B subroutine"));
- + addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
- + sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
- + sqlite3VdbeGoto(v, labelCmpr);
- +
- + /* Generate code to handle the case of A==B
- + */
- + if( op==TK_ALL ){
- + addrAeqB = addrAltB;
- + }else if( op==TK_INTERSECT ){
- + addrAeqB = addrAltB;
- + addrAltB++;
- + }else{
- + VdbeNoopComment((v, "A-eq-B subroutine"));
- + addrAeqB =
- + sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
- + sqlite3VdbeGoto(v, labelCmpr);
- + }
- +
- + /* Generate code to handle the case of A>B
- + */
- + VdbeNoopComment((v, "A-gt-B subroutine"));
- + addrAgtB = sqlite3VdbeCurrentAddr(v);
- + if( op==TK_ALL || op==TK_UNION ){
- + sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
- + }
- + sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
- + sqlite3VdbeGoto(v, labelCmpr);
- +
- + /* This code runs once to initialize everything.
- + */
- + sqlite3VdbeJumpHere(v, addr1);
- + sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
- + sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
- +
- + /* Implement the main merge loop
- + */
- + sqlite3VdbeResolveLabel(v, labelCmpr);
- + sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
- + sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
- + (char*)pKeyMerge, P4_KEYINFO);
- + sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
- + sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
- +
- + /* Jump to the this point in order to terminate the query.
- + */
- + sqlite3VdbeResolveLabel(v, labelEnd);
- +
- + /* Reassembly the compound query so that it will be freed correctly
- + ** by the calling function */
- + if( p->pPrior ){
- + sqlite3SelectDelete(db, p->pPrior);
- + }
- + p->pPrior = pPrior;
- + pPrior->pNext = p;
- +
- + /*** TBD: Insert subroutine calls to close cursors on incomplete
- + **** subqueries ****/
- + ExplainQueryPlanPop(pParse);
- + return pParse->nErr!=0;
- +}
- +#endif
- +
- +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
- +
- +/* An instance of the SubstContext object describes an substitution edit
- +** to be performed on a parse tree.
- +**
- +** All references to columns in table iTable are to be replaced by corresponding
- +** expressions in pEList.
- +*/
- +typedef struct SubstContext {
- + Parse *pParse; /* The parsing context */
- + int iTable; /* Replace references to this table */
- + int iNewTable; /* New table number */
- + int isLeftJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
- + ExprList *pEList; /* Replacement expressions */
- +} SubstContext;
- +
- +/* Forward Declarations */
- +static void substExprList(SubstContext*, ExprList*);
- +static void substSelect(SubstContext*, Select*, int);
- +
- +/*
- +** Scan through the expression pExpr. Replace every reference to
- +** a column in table number iTable with a copy of the iColumn-th
- +** entry in pEList. (But leave references to the ROWID column
- +** unchanged.)
- +**
- +** This routine is part of the flattening procedure. A subquery
- +** whose result set is defined by pEList appears as entry in the
- +** FROM clause of a SELECT such that the VDBE cursor assigned to that
- +** FORM clause entry is iTable. This routine makes the necessary
- +** changes to pExpr so that it refers directly to the source table
- +** of the subquery rather the result set of the subquery.
- +*/
- +static Expr *substExpr(
- + SubstContext *pSubst, /* Description of the substitution */
- + Expr *pExpr /* Expr in which substitution occurs */
- +){
- + if( pExpr==0 ) return 0;
- + if( ExprHasProperty(pExpr, EP_FromJoin)
- + && pExpr->iRightJoinTable==pSubst->iTable
- + ){
- + pExpr->iRightJoinTable = pSubst->iNewTable;
- + }
- + if( pExpr->op==TK_COLUMN
- + && pExpr->iTable==pSubst->iTable
- + && !ExprHasProperty(pExpr, EP_FixedCol)
- + ){
- + if( pExpr->iColumn<0 ){
- + pExpr->op = TK_NULL;
- + }else{
- + Expr *pNew;
- + Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
- + Expr ifNullRow;
- + assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
- + assert( pExpr->pRight==0 );
- + if( sqlite3ExprIsVector(pCopy) ){
- + sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
- + }else{
- + sqlite3 *db = pSubst->pParse->db;
- + if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
- + memset(&ifNullRow, 0, sizeof(ifNullRow));
- + ifNullRow.op = TK_IF_NULL_ROW;
- + ifNullRow.pLeft = pCopy;
- + ifNullRow.iTable = pSubst->iNewTable;
- + ifNullRow.flags = EP_Skip;
- + pCopy = &ifNullRow;
- + }
- + testcase( ExprHasProperty(pCopy, EP_Subquery) );
- + pNew = sqlite3ExprDup(db, pCopy, 0);
- + if( pNew && pSubst->isLeftJoin ){
- + ExprSetProperty(pNew, EP_CanBeNull);
- + }
- + if( pNew && ExprHasProperty(pExpr,EP_FromJoin) ){
- + pNew->iRightJoinTable = pExpr->iRightJoinTable;
- + ExprSetProperty(pNew, EP_FromJoin);
- + }
- + sqlite3ExprDelete(db, pExpr);
- + pExpr = pNew;
- +
- + /* Ensure that the expression now has an implicit collation sequence,
- + ** just as it did when it was a column of a view or sub-query. */
- + if( pExpr ){
- + if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
- + CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
- + pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
- + (pColl ? pColl->zName : "BINARY")
- + );
- + }
- + ExprClearProperty(pExpr, EP_Collate);
- + }
- + }
- + }
- + }else{
- + if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
- + pExpr->iTable = pSubst->iNewTable;
- + }
- + pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
- + pExpr->pRight = substExpr(pSubst, pExpr->pRight);
- + if( ExprHasProperty(pExpr, EP_xIsSelect) ){
- + substSelect(pSubst, pExpr->x.pSelect, 1);
- + }else{
- + substExprList(pSubst, pExpr->x.pList);
- + }
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + if( ExprHasProperty(pExpr, EP_WinFunc) ){
- + Window *pWin = pExpr->y.pWin;
- + pWin->pFilter = substExpr(pSubst, pWin->pFilter);
- + substExprList(pSubst, pWin->pPartition);
- + substExprList(pSubst, pWin->pOrderBy);
- + }
- +#endif
- + }
- + return pExpr;
- +}
- +static void substExprList(
- + SubstContext *pSubst, /* Description of the substitution */
- + ExprList *pList /* List to scan and in which to make substitutes */
- +){
- + int i;
- + if( pList==0 ) return;
- + for(i=0; i<pList->nExpr; i++){
- + pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
- + }
- +}
- +static void substSelect(
- + SubstContext *pSubst, /* Description of the substitution */
- + Select *p, /* SELECT statement in which to make substitutions */
- + int doPrior /* Do substitutes on p->pPrior too */
- +){
- + SrcList *pSrc;
- + struct SrcList_item *pItem;
- + int i;
- + if( !p ) return;
- + do{
- + substExprList(pSubst, p->pEList);
- + substExprList(pSubst, p->pGroupBy);
- + substExprList(pSubst, p->pOrderBy);
- + p->pHaving = substExpr(pSubst, p->pHaving);
- + p->pWhere = substExpr(pSubst, p->pWhere);
- + pSrc = p->pSrc;
- + assert( pSrc!=0 );
- + for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
- + substSelect(pSubst, pItem->pSelect, 1);
- + if( pItem->fg.isTabFunc ){
- + substExprList(pSubst, pItem->u1.pFuncArg);
- + }
- + }
- + }while( doPrior && (p = p->pPrior)!=0 );
- +}
- +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
- +
- +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
- +/*
- +** pSelect is a SELECT statement and pSrcItem is one item in the FROM
- +** clause of that SELECT.
- +**
- +** This routine scans the entire SELECT statement and recomputes the
- +** pSrcItem->colUsed mask.
- +*/
- +static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
- + struct SrcList_item *pItem;
- + if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
- + pItem = pWalker->u.pSrcItem;
- + if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
- + if( pExpr->iColumn<0 ) return WRC_Continue;
- + pItem->colUsed |= sqlite3ExprColUsed(pExpr);
- + return WRC_Continue;
- +}
- +static void recomputeColumnsUsed(
- + Select *pSelect, /* The complete SELECT statement */
- + struct SrcList_item *pSrcItem /* Which FROM clause item to recompute */
- +){
- + Walker w;
- + if( NEVER(pSrcItem->pTab==0) ) return;
- + memset(&w, 0, sizeof(w));
- + w.xExprCallback = recomputeColumnsUsedExpr;
- + w.xSelectCallback = sqlite3SelectWalkNoop;
- + w.u.pSrcItem = pSrcItem;
- + pSrcItem->colUsed = 0;
- + sqlite3WalkSelect(&w, pSelect);
- +}
- +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
- +
- +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
- +/*
- +** This routine attempts to flatten subqueries as a performance optimization.
- +** This routine returns 1 if it makes changes and 0 if no flattening occurs.
- +**
- +** To understand the concept of flattening, consider the following
- +** query:
- +**
- +** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
- +**
- +** The default way of implementing this query is to execute the
- +** subquery first and store the results in a temporary table, then
- +** run the outer query on that temporary table. This requires two
- +** passes over the data. Furthermore, because the temporary table
- +** has no indices, the WHERE clause on the outer query cannot be
- +** optimized.
- +**
- +** This routine attempts to rewrite queries such as the above into
- +** a single flat select, like this:
- +**
- +** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
- +**
- +** The code generated for this simplification gives the same result
- +** but only has to scan the data once. And because indices might
- +** exist on the table t1, a complete scan of the data might be
- +** avoided.
- +**
- +** Flattening is subject to the following constraints:
- +**
- +** (**) We no longer attempt to flatten aggregate subqueries. Was:
- +** The subquery and the outer query cannot both be aggregates.
- +**
- +** (**) We no longer attempt to flatten aggregate subqueries. Was:
- +** (2) If the subquery is an aggregate then
- +** (2a) the outer query must not be a join and
- +** (2b) the outer query must not use subqueries
- +** other than the one FROM-clause subquery that is a candidate
- +** for flattening. (This is due to ticket [2f7170d73bf9abf80]
- +** from 2015-02-09.)
- +**
- +** (3) If the subquery is the right operand of a LEFT JOIN then
- +** (3a) the subquery may not be a join and
- +** (3b) the FROM clause of the subquery may not contain a virtual
- +** table and
- +** (3c) the outer query may not be an aggregate.
- +** (3d) the outer query may not be DISTINCT.
- +**
- +** (4) The subquery can not be DISTINCT.
- +**
- +** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
- +** sub-queries that were excluded from this optimization. Restriction
- +** (4) has since been expanded to exclude all DISTINCT subqueries.
- +**
- +** (**) We no longer attempt to flatten aggregate subqueries. Was:
- +** If the subquery is aggregate, the outer query may not be DISTINCT.
- +**
- +** (7) The subquery must have a FROM clause. TODO: For subqueries without
- +** A FROM clause, consider adding a FROM clause with the special
- +** table sqlite_once that consists of a single row containing a
- +** single NULL.
- +**
- +** (8) If the subquery uses LIMIT then the outer query may not be a join.
- +**
- +** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
- +**
- +** (**) Restriction (10) was removed from the code on 2005-02-05 but we
- +** accidently carried the comment forward until 2014-09-15. Original
- +** constraint: "If the subquery is aggregate then the outer query
- +** may not use LIMIT."
- +**
- +** (11) The subquery and the outer query may not both have ORDER BY clauses.
- +**
- +** (**) Not implemented. Subsumed into restriction (3). Was previously
- +** a separate restriction deriving from ticket #350.
- +**
- +** (13) The subquery and outer query may not both use LIMIT.
- +**
- +** (14) The subquery may not use OFFSET.
- +**
- +** (15) If the outer query is part of a compound select, then the
- +** subquery may not use LIMIT.
- +** (See ticket #2339 and ticket [02a8e81d44]).
- +**
- +** (16) If the outer query is aggregate, then the subquery may not
- +** use ORDER BY. (Ticket #2942) This used to not matter
- +** until we introduced the group_concat() function.
- +**
- +** (17) If the subquery is a compound select, then
- +** (17a) all compound operators must be a UNION ALL, and
- +** (17b) no terms within the subquery compound may be aggregate
- +** or DISTINCT, and
- +** (17c) every term within the subquery compound must have a FROM clause
- +** (17d) the outer query may not be
- +** (17d1) aggregate, or
- +** (17d2) DISTINCT, or
- +** (17d3) a join.
- +** (17e) the subquery may not contain window functions
- +**
- +** The parent and sub-query may contain WHERE clauses. Subject to
- +** rules (11), (13) and (14), they may also contain ORDER BY,
- +** LIMIT and OFFSET clauses. The subquery cannot use any compound
- +** operator other than UNION ALL because all the other compound
- +** operators have an implied DISTINCT which is disallowed by
- +** restriction (4).
- +**
- +** Also, each component of the sub-query must return the same number
- +** of result columns. This is actually a requirement for any compound
- +** SELECT statement, but all the code here does is make sure that no
- +** such (illegal) sub-query is flattened. The caller will detect the
- +** syntax error and return a detailed message.
- +**
- +** (18) If the sub-query is a compound select, then all terms of the
- +** ORDER BY clause of the parent must be simple references to
- +** columns of the sub-query.
- +**
- +** (19) If the subquery uses LIMIT then the outer query may not
- +** have a WHERE clause.
- +**
- +** (20) If the sub-query is a compound select, then it must not use
- +** an ORDER BY clause. Ticket #3773. We could relax this constraint
- +** somewhat by saying that the terms of the ORDER BY clause must
- +** appear as unmodified result columns in the outer query. But we
- +** have other optimizations in mind to deal with that case.
- +**
- +** (21) If the subquery uses LIMIT then the outer query may not be
- +** DISTINCT. (See ticket [752e1646fc]).
- +**
- +** (22) The subquery may not be a recursive CTE.
- +**
- +** (**) Subsumed into restriction (17d3). Was: If the outer query is
- +** a recursive CTE, then the sub-query may not be a compound query.
- +** This restriction is because transforming the
- +** parent to a compound query confuses the code that handles
- +** recursive queries in multiSelect().
- +**
- +** (**) We no longer attempt to flatten aggregate subqueries. Was:
- +** The subquery may not be an aggregate that uses the built-in min() or
- +** or max() functions. (Without this restriction, a query like:
- +** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
- +** return the value X for which Y was maximal.)
- +**
- +** (25) If either the subquery or the parent query contains a window
- +** function in the select list or ORDER BY clause, flattening
- +** is not attempted.
- +**
- +**
- +** In this routine, the "p" parameter is a pointer to the outer query.
- +** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
- +** uses aggregates.
- +**
- +** If flattening is not attempted, this routine is a no-op and returns 0.
- +** If flattening is attempted this routine returns 1.
- +**
- +** All of the expression analysis must occur on both the outer query and
- +** the subquery before this routine runs.
- +*/
- +static int flattenSubquery(
- + Parse *pParse, /* Parsing context */
- + Select *p, /* The parent or outer SELECT statement */
- + int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
- + int isAgg /* True if outer SELECT uses aggregate functions */
- +){
- + const char *zSavedAuthContext = pParse->zAuthContext;
- + Select *pParent; /* Current UNION ALL term of the other query */
- + Select *pSub; /* The inner query or "subquery" */
- + Select *pSub1; /* Pointer to the rightmost select in sub-query */
- + SrcList *pSrc; /* The FROM clause of the outer query */
- + SrcList *pSubSrc; /* The FROM clause of the subquery */
- + int iParent; /* VDBE cursor number of the pSub result set temp table */
- + int iNewParent = -1;/* Replacement table for iParent */
- + int isLeftJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
- + int i; /* Loop counter */
- + Expr *pWhere; /* The WHERE clause */
- + struct SrcList_item *pSubitem; /* The subquery */
- + sqlite3 *db = pParse->db;
- +
- + /* Check to see if flattening is permitted. Return 0 if not.
- + */
- + assert( p!=0 );
- + assert( p->pPrior==0 );
- + if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
- + pSrc = p->pSrc;
- + assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
- + pSubitem = &pSrc->a[iFrom];
- + iParent = pSubitem->iCursor;
- + pSub = pSubitem->pSelect;
- + assert( pSub!=0 );
- +
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
- +#endif
- +
- + pSubSrc = pSub->pSrc;
- + assert( pSubSrc );
- + /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
- + ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
- + ** because they could be computed at compile-time. But when LIMIT and OFFSET
- + ** became arbitrary expressions, we were forced to add restrictions (13)
- + ** and (14). */
- + if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
- + if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
- + if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
- + return 0; /* Restriction (15) */
- + }
- + if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
- + if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
- + if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
- + return 0; /* Restrictions (8)(9) */
- + }
- + if( p->pOrderBy && pSub->pOrderBy ){
- + return 0; /* Restriction (11) */
- + }
- + if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
- + if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
- + if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
- + return 0; /* Restriction (21) */
- + }
- + if( pSub->selFlags & (SF_Recursive) ){
- + return 0; /* Restrictions (22) */
- + }
- +
- + /*
- + ** If the subquery is the right operand of a LEFT JOIN, then the
- + ** subquery may not be a join itself (3a). Example of why this is not
- + ** allowed:
- + **
- + ** t1 LEFT OUTER JOIN (t2 JOIN t3)
- + **
- + ** If we flatten the above, we would get
- + **
- + ** (t1 LEFT OUTER JOIN t2) JOIN t3
- + **
- + ** which is not at all the same thing.
- + **
- + ** If the subquery is the right operand of a LEFT JOIN, then the outer
- + ** query cannot be an aggregate. (3c) This is an artifact of the way
- + ** aggregates are processed - there is no mechanism to determine if
- + ** the LEFT JOIN table should be all-NULL.
- + **
- + ** See also tickets #306, #350, and #3300.
- + */
- + if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
- + isLeftJoin = 1;
- + if( pSubSrc->nSrc>1 /* (3a) */
- + || isAgg /* (3b) */
- + || IsVirtual(pSubSrc->a[0].pTab) /* (3c) */
- + || (p->selFlags & SF_Distinct)!=0 /* (3d) */
- + ){
- + return 0;
- + }
- + }
- +#ifdef SQLITE_EXTRA_IFNULLROW
- + else if( iFrom>0 && !isAgg ){
- + /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
- + ** every reference to any result column from subquery in a join, even
- + ** though they are not necessary. This will stress-test the OP_IfNullRow
- + ** opcode. */
- + isLeftJoin = -1;
- + }
- +#endif
- +
- + /* Restriction (17): If the sub-query is a compound SELECT, then it must
- + ** use only the UNION ALL operator. And none of the simple select queries
- + ** that make up the compound SELECT are allowed to be aggregate or distinct
- + ** queries.
- + */
- + if( pSub->pPrior ){
- + if( pSub->pOrderBy ){
- + return 0; /* Restriction (20) */
- + }
- + if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
- + return 0; /* (17d1), (17d2), or (17d3) */
- + }
- + for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
- + testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
- + testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
- + assert( pSub->pSrc!=0 );
- + assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
- + if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
- + || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
- + || pSub1->pSrc->nSrc<1 /* (17c) */
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + || pSub1->pWin /* (17e) */
- +#endif
- + ){
- + return 0;
- + }
- + testcase( pSub1->pSrc->nSrc>1 );
- + }
- +
- + /* Restriction (18). */
- + if( p->pOrderBy ){
- + int ii;
- + for(ii=0; ii<p->pOrderBy->nExpr; ii++){
- + if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
- + }
- + }
- + }
- +
- + /* Ex-restriction (23):
- + ** The only way that the recursive part of a CTE can contain a compound
- + ** subquery is for the subquery to be one term of a join. But if the
- + ** subquery is a join, then the flattening has already been stopped by
- + ** restriction (17d3)
- + */
- + assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
- +
- + /***** If we reach this point, flattening is permitted. *****/
- + SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
- + pSub->selId, pSub, iFrom));
- +
- + /* Authorize the subquery */
- + pParse->zAuthContext = pSubitem->zName;
- + TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
- + testcase( i==SQLITE_DENY );
- + pParse->zAuthContext = zSavedAuthContext;
- +
- + /* If the sub-query is a compound SELECT statement, then (by restrictions
- + ** 17 and 18 above) it must be a UNION ALL and the parent query must
- + ** be of the form:
- + **
- + ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
- + **
- + ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
- + ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
- + ** OFFSET clauses and joins them to the left-hand-side of the original
- + ** using UNION ALL operators. In this case N is the number of simple
- + ** select statements in the compound sub-query.
- + **
- + ** Example:
- + **
- + ** SELECT a+1 FROM (
- + ** SELECT x FROM tab
- + ** UNION ALL
- + ** SELECT y FROM tab
- + ** UNION ALL
- + ** SELECT abs(z*2) FROM tab2
- + ** ) WHERE a!=5 ORDER BY 1
- + **
- + ** Transformed into:
- + **
- + ** SELECT x+1 FROM tab WHERE x+1!=5
- + ** UNION ALL
- + ** SELECT y+1 FROM tab WHERE y+1!=5
- + ** UNION ALL
- + ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
- + ** ORDER BY 1
- + **
- + ** We call this the "compound-subquery flattening".
- + */
- + for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
- + Select *pNew;
- + ExprList *pOrderBy = p->pOrderBy;
- + Expr *pLimit = p->pLimit;
- + Select *pPrior = p->pPrior;
- + p->pOrderBy = 0;
- + p->pSrc = 0;
- + p->pPrior = 0;
- + p->pLimit = 0;
- + pNew = sqlite3SelectDup(db, p, 0);
- + p->pLimit = pLimit;
- + p->pOrderBy = pOrderBy;
- + p->pSrc = pSrc;
- + p->op = TK_ALL;
- + if( pNew==0 ){
- + p->pPrior = pPrior;
- + }else{
- + pNew->pPrior = pPrior;
- + if( pPrior ) pPrior->pNext = pNew;
- + pNew->pNext = p;
- + p->pPrior = pNew;
- + SELECTTRACE(2,pParse,p,("compound-subquery flattener"
- + " creates %u as peer\n",pNew->selId));
- + }
- + if( db->mallocFailed ) return 1;
- + }
- +
- + /* Begin flattening the iFrom-th entry of the FROM clause
- + ** in the outer query.
- + */
- + pSub = pSub1 = pSubitem->pSelect;
- +
- + /* Delete the transient table structure associated with the
- + ** subquery
- + */
- + sqlite3DbFree(db, pSubitem->zDatabase);
- + sqlite3DbFree(db, pSubitem->zName);
- + sqlite3DbFree(db, pSubitem->zAlias);
- + pSubitem->zDatabase = 0;
- + pSubitem->zName = 0;
- + pSubitem->zAlias = 0;
- + pSubitem->pSelect = 0;
- +
- + /* Defer deleting the Table object associated with the
- + ** subquery until code generation is
- + ** complete, since there may still exist Expr.pTab entries that
- + ** refer to the subquery even after flattening. Ticket #3346.
- + **
- + ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
- + */
- + if( ALWAYS(pSubitem->pTab!=0) ){
- + Table *pTabToDel = pSubitem->pTab;
- + if( pTabToDel->nTabRef==1 ){
- + Parse *pToplevel = sqlite3ParseToplevel(pParse);
- + pTabToDel->pNextZombie = pToplevel->pZombieTab;
- + pToplevel->pZombieTab = pTabToDel;
- + }else{
- + pTabToDel->nTabRef--;
- + }
- + pSubitem->pTab = 0;
- + }
- +
- + /* The following loop runs once for each term in a compound-subquery
- + ** flattening (as described above). If we are doing a different kind
- + ** of flattening - a flattening other than a compound-subquery flattening -
- + ** then this loop only runs once.
- + **
- + ** This loop moves all of the FROM elements of the subquery into the
- + ** the FROM clause of the outer query. Before doing this, remember
- + ** the cursor number for the original outer query FROM element in
- + ** iParent. The iParent cursor will never be used. Subsequent code
- + ** will scan expressions looking for iParent references and replace
- + ** those references with expressions that resolve to the subquery FROM
- + ** elements we are now copying in.
- + */
- + for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
- + int nSubSrc;
- + u8 jointype = 0;
- + assert( pSub!=0 );
- + pSubSrc = pSub->pSrc; /* FROM clause of subquery */
- + nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
- + pSrc = pParent->pSrc; /* FROM clause of the outer query */
- +
- + if( pSrc ){
- + assert( pParent==p ); /* First time through the loop */
- + jointype = pSubitem->fg.jointype;
- + }else{
- + assert( pParent!=p ); /* 2nd and subsequent times through the loop */
- + pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
- + if( pSrc==0 ) break;
- + pParent->pSrc = pSrc;
- + }
- +
- + /* The subquery uses a single slot of the FROM clause of the outer
- + ** query. If the subquery has more than one element in its FROM clause,
- + ** then expand the outer query to make space for it to hold all elements
- + ** of the subquery.
- + **
- + ** Example:
- + **
- + ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
- + **
- + ** The outer query has 3 slots in its FROM clause. One slot of the
- + ** outer query (the middle slot) is used by the subquery. The next
- + ** block of code will expand the outer query FROM clause to 4 slots.
- + ** The middle slot is expanded to two slots in order to make space
- + ** for the two elements in the FROM clause of the subquery.
- + */
- + if( nSubSrc>1 ){
- + pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
- + if( pSrc==0 ) break;
- + pParent->pSrc = pSrc;
- + }
- +
- + /* Transfer the FROM clause terms from the subquery into the
- + ** outer query.
- + */
- + for(i=0; i<nSubSrc; i++){
- + sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
- + assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
- + pSrc->a[i+iFrom] = pSubSrc->a[i];
- + iNewParent = pSubSrc->a[i].iCursor;
- + memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
- + }
- + pSrc->a[iFrom].fg.jointype = jointype;
- +
- + /* Now begin substituting subquery result set expressions for
- + ** references to the iParent in the outer query.
- + **
- + ** Example:
- + **
- + ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
- + ** \ \_____________ subquery __________/ /
- + ** \_____________________ outer query ______________________________/
- + **
- + ** We look at every expression in the outer query and every place we see
- + ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
- + */
- + if( pSub->pOrderBy ){
- + /* At this point, any non-zero iOrderByCol values indicate that the
- + ** ORDER BY column expression is identical to the iOrderByCol'th
- + ** expression returned by SELECT statement pSub. Since these values
- + ** do not necessarily correspond to columns in SELECT statement pParent,
- + ** zero them before transfering the ORDER BY clause.
- + **
- + ** Not doing this may cause an error if a subsequent call to this
- + ** function attempts to flatten a compound sub-query into pParent
- + ** (the only way this can happen is if the compound sub-query is
- + ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
- + ExprList *pOrderBy = pSub->pOrderBy;
- + for(i=0; i<pOrderBy->nExpr; i++){
- + pOrderBy->a[i].u.x.iOrderByCol = 0;
- + }
- + assert( pParent->pOrderBy==0 );
- + pParent->pOrderBy = pOrderBy;
- + pSub->pOrderBy = 0;
- + }
- + pWhere = pSub->pWhere;
- + pSub->pWhere = 0;
- + if( isLeftJoin>0 ){
- + sqlite3SetJoinExpr(pWhere, iNewParent);
- + }
- + pParent->pWhere = sqlite3ExprAnd(pParse, pWhere, pParent->pWhere);
- + if( db->mallocFailed==0 ){
- + SubstContext x;
- + x.pParse = pParse;
- + x.iTable = iParent;
- + x.iNewTable = iNewParent;
- + x.isLeftJoin = isLeftJoin;
- + x.pEList = pSub->pEList;
- + substSelect(&x, pParent, 0);
- + }
- +
- + /* The flattened query is a compound if either the inner or the
- + ** outer query is a compound. */
- + pParent->selFlags |= pSub->selFlags & SF_Compound;
- + assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
- +
- + /*
- + ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
- + **
- + ** One is tempted to try to add a and b to combine the limits. But this
- + ** does not work if either limit is negative.
- + */
- + if( pSub->pLimit ){
- + pParent->pLimit = pSub->pLimit;
- + pSub->pLimit = 0;
- + }
- +
- + /* Recompute the SrcList_item.colUsed masks for the flattened
- + ** tables. */
- + for(i=0; i<nSubSrc; i++){
- + recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
- + }
- + }
- +
- + /* Finially, delete what is left of the subquery and return
- + ** success.
- + */
- + sqlite3SelectDelete(db, pSub1);
- +
- +#if SELECTTRACE_ENABLED
- + if( sqlite3SelectTrace & 0x100 ){
- + SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- +
- + return 1;
- +}
- +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
- +
- +/*
- +** A structure to keep track of all of the column values that are fixed to
- +** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
- +*/
- +typedef struct WhereConst WhereConst;
- +struct WhereConst {
- + Parse *pParse; /* Parsing context */
- + int nConst; /* Number for COLUMN=CONSTANT terms */
- + int nChng; /* Number of times a constant is propagated */
- + Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
- +};
- +
- +/*
- +** Add a new entry to the pConst object. Except, do not add duplicate
- +** pColumn entires. Also, do not add if doing so would not be appropriate.
- +**
- +** The caller guarantees the pColumn is a column and pValue is a constant.
- +** This routine has to do some additional checks before completing the
- +** insert.
- +*/
- +static void constInsert(
- + WhereConst *pConst, /* The WhereConst into which we are inserting */
- + Expr *pColumn, /* The COLUMN part of the constraint */
- + Expr *pValue, /* The VALUE part of the constraint */
- + Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
- +){
- + int i;
- + assert( pColumn->op==TK_COLUMN );
- + assert( sqlite3ExprIsConstant(pValue) );
- +
- + if( ExprHasProperty(pColumn, EP_FixedCol) ) return;
- + if( sqlite3ExprAffinity(pValue)!=0 ) return;
- + if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
- + return;
- + }
- +
- + /* 2018-10-25 ticket [cf5ed20f]
- + ** Make sure the same pColumn is not inserted more than once */
- + for(i=0; i<pConst->nConst; i++){
- + const Expr *pE2 = pConst->apExpr[i*2];
- + assert( pE2->op==TK_COLUMN );
- + if( pE2->iTable==pColumn->iTable
- + && pE2->iColumn==pColumn->iColumn
- + ){
- + return; /* Already present. Return without doing anything. */
- + }
- + }
- +
- + pConst->nConst++;
- + pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
- + pConst->nConst*2*sizeof(Expr*));
- + if( pConst->apExpr==0 ){
- + pConst->nConst = 0;
- + }else{
- + pConst->apExpr[pConst->nConst*2-2] = pColumn;
- + pConst->apExpr[pConst->nConst*2-1] = pValue;
- + }
- +}
- +
- +/*
- +** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
- +** is a constant expression and where the term must be true because it
- +** is part of the AND-connected terms of the expression. For each term
- +** found, add it to the pConst structure.
- +*/
- +static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
- + Expr *pRight, *pLeft;
- + if( pExpr==0 ) return;
- + if( ExprHasProperty(pExpr, EP_FromJoin) ) return;
- + if( pExpr->op==TK_AND ){
- + findConstInWhere(pConst, pExpr->pRight);
- + findConstInWhere(pConst, pExpr->pLeft);
- + return;
- + }
- + if( pExpr->op!=TK_EQ ) return;
- + pRight = pExpr->pRight;
- + pLeft = pExpr->pLeft;
- + assert( pRight!=0 );
- + assert( pLeft!=0 );
- + if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
- + constInsert(pConst,pRight,pLeft,pExpr);
- + }
- + if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
- + constInsert(pConst,pLeft,pRight,pExpr);
- + }
- +}
- +
- +/*
- +** This is a Walker expression callback. pExpr is a candidate expression
- +** to be replaced by a value. If pExpr is equivalent to one of the
- +** columns named in pWalker->u.pConst, then overwrite it with its
- +** corresponding value.
- +*/
- +static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
- + int i;
- + WhereConst *pConst;
- + if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
- + if( ExprHasProperty(pExpr, EP_FixedCol|EP_FromJoin) ){
- + testcase( ExprHasProperty(pExpr, EP_FixedCol) );
- + testcase( ExprHasProperty(pExpr, EP_FromJoin) );
- + return WRC_Continue;
- + }
- + pConst = pWalker->u.pConst;
- + for(i=0; i<pConst->nConst; i++){
- + Expr *pColumn = pConst->apExpr[i*2];
- + if( pColumn==pExpr ) continue;
- + if( pColumn->iTable!=pExpr->iTable ) continue;
- + if( pColumn->iColumn!=pExpr->iColumn ) continue;
- + /* A match is found. Add the EP_FixedCol property */
- + pConst->nChng++;
- + ExprClearProperty(pExpr, EP_Leaf);
- + ExprSetProperty(pExpr, EP_FixedCol);
- + assert( pExpr->pLeft==0 );
- + pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
- + break;
- + }
- + return WRC_Prune;
- +}
- +
- +/*
- +** The WHERE-clause constant propagation optimization.
- +**
- +** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
- +** CONSTANT=COLUMN that are top-level AND-connected terms that are not
- +** part of a ON clause from a LEFT JOIN, then throughout the query
- +** replace all other occurrences of COLUMN with CONSTANT.
- +**
- +** For example, the query:
- +**
- +** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
- +**
- +** Is transformed into
- +**
- +** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
- +**
- +** Return true if any transformations where made and false if not.
- +**
- +** Implementation note: Constant propagation is tricky due to affinity
- +** and collating sequence interactions. Consider this example:
- +**
- +** CREATE TABLE t1(a INT,b TEXT);
- +** INSERT INTO t1 VALUES(123,'0123');
- +** SELECT * FROM t1 WHERE a=123 AND b=a;
- +** SELECT * FROM t1 WHERE a=123 AND b=123;
- +**
- +** The two SELECT statements above should return different answers. b=a
- +** is alway true because the comparison uses numeric affinity, but b=123
- +** is false because it uses text affinity and '0123' is not the same as '123'.
- +** To work around this, the expression tree is not actually changed from
- +** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
- +** and the "123" value is hung off of the pLeft pointer. Code generator
- +** routines know to generate the constant "123" instead of looking up the
- +** column value. Also, to avoid collation problems, this optimization is
- +** only attempted if the "a=123" term uses the default BINARY collation.
- +*/
- +static int propagateConstants(
- + Parse *pParse, /* The parsing context */
- + Select *p /* The query in which to propagate constants */
- +){
- + WhereConst x;
- + Walker w;
- + int nChng = 0;
- + x.pParse = pParse;
- + do{
- + x.nConst = 0;
- + x.nChng = 0;
- + x.apExpr = 0;
- + findConstInWhere(&x, p->pWhere);
- + if( x.nConst ){
- + memset(&w, 0, sizeof(w));
- + w.pParse = pParse;
- + w.xExprCallback = propagateConstantExprRewrite;
- + w.xSelectCallback = sqlite3SelectWalkNoop;
- + w.xSelectCallback2 = 0;
- + w.walkerDepth = 0;
- + w.u.pConst = &x;
- + sqlite3WalkExpr(&w, p->pWhere);
- + sqlite3DbFree(x.pParse->db, x.apExpr);
- + nChng += x.nChng;
- + }
- + }while( x.nChng );
- + return nChng;
- +}
- +
- +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
- +/*
- +** Make copies of relevant WHERE clause terms of the outer query into
- +** the WHERE clause of subquery. Example:
- +**
- +** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
- +**
- +** Transformed into:
- +**
- +** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
- +** WHERE x=5 AND y=10;
- +**
- +** The hope is that the terms added to the inner query will make it more
- +** efficient.
- +**
- +** Do not attempt this optimization if:
- +**
- +** (1) (** This restriction was removed on 2017-09-29. We used to
- +** disallow this optimization for aggregate subqueries, but now
- +** it is allowed by putting the extra terms on the HAVING clause.
- +** The added HAVING clause is pointless if the subquery lacks
- +** a GROUP BY clause. But such a HAVING clause is also harmless
- +** so there does not appear to be any reason to add extra logic
- +** to suppress it. **)
- +**
- +** (2) The inner query is the recursive part of a common table expression.
- +**
- +** (3) The inner query has a LIMIT clause (since the changes to the WHERE
- +** clause would change the meaning of the LIMIT).
- +**
- +** (4) The inner query is the right operand of a LEFT JOIN and the
- +** expression to be pushed down does not come from the ON clause
- +** on that LEFT JOIN.
- +**
- +** (5) The WHERE clause expression originates in the ON or USING clause
- +** of a LEFT JOIN where iCursor is not the right-hand table of that
- +** left join. An example:
- +**
- +** SELECT *
- +** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
- +** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
- +** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
- +**
- +** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
- +** But if the (b2=2) term were to be pushed down into the bb subquery,
- +** then the (1,1,NULL) row would be suppressed.
- +**
- +** (6) The inner query features one or more window-functions (since
- +** changes to the WHERE clause of the inner query could change the
- +** window over which window functions are calculated).
- +**
- +** Return 0 if no changes are made and non-zero if one or more WHERE clause
- +** terms are duplicated into the subquery.
- +*/
- +static int pushDownWhereTerms(
- + Parse *pParse, /* Parse context (for malloc() and error reporting) */
- + Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
- + Expr *pWhere, /* The WHERE clause of the outer query */
- + int iCursor, /* Cursor number of the subquery */
- + int isLeftJoin /* True if pSubq is the right term of a LEFT JOIN */
- +){
- + Expr *pNew;
- + int nChng = 0;
- + Select *pSel;
- + if( pWhere==0 ) return 0;
- + if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
- +
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + for(pSel=pSubq; pSel; pSel=pSel->pPrior){
- + if( pSel->pWin ) return 0; /* restriction (6) */
- + }
- +#endif
- +
- +#ifdef SQLITE_DEBUG
- + /* Only the first term of a compound can have a WITH clause. But make
- + ** sure no other terms are marked SF_Recursive in case something changes
- + ** in the future.
- + */
- + {
- + Select *pX;
- + for(pX=pSubq; pX; pX=pX->pPrior){
- + assert( (pX->selFlags & (SF_Recursive))==0 );
- + }
- + }
- +#endif
- +
- + if( pSubq->pLimit!=0 ){
- + return 0; /* restriction (3) */
- + }
- + while( pWhere->op==TK_AND ){
- + nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
- + iCursor, isLeftJoin);
- + pWhere = pWhere->pLeft;
- + }
- + if( isLeftJoin
- + && (ExprHasProperty(pWhere,EP_FromJoin)==0
- + || pWhere->iRightJoinTable!=iCursor)
- + ){
- + return 0; /* restriction (4) */
- + }
- + if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
- + return 0; /* restriction (5) */
- + }
- + if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
- + nChng++;
- + while( pSubq ){
- + SubstContext x;
- + pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
- + unsetJoinExpr(pNew, -1);
- + x.pParse = pParse;
- + x.iTable = iCursor;
- + x.iNewTable = iCursor;
- + x.isLeftJoin = 0;
- + x.pEList = pSubq->pEList;
- + pNew = substExpr(&x, pNew);
- + if( pSubq->selFlags & SF_Aggregate ){
- + pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
- + }else{
- + pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
- + }
- + pSubq = pSubq->pPrior;
- + }
- + }
- + return nChng;
- +}
- +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
- +
- +/*
- +** The pFunc is the only aggregate function in the query. Check to see
- +** if the query is a candidate for the min/max optimization.
- +**
- +** If the query is a candidate for the min/max optimization, then set
- +** *ppMinMax to be an ORDER BY clause to be used for the optimization
- +** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
- +** whether pFunc is a min() or max() function.
- +**
- +** If the query is not a candidate for the min/max optimization, return
- +** WHERE_ORDERBY_NORMAL (which must be zero).
- +**
- +** This routine must be called after aggregate functions have been
- +** located but before their arguments have been subjected to aggregate
- +** analysis.
- +*/
- +static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
- + int eRet = WHERE_ORDERBY_NORMAL; /* Return value */
- + ExprList *pEList = pFunc->x.pList; /* Arguments to agg function */
- + const char *zFunc; /* Name of aggregate function pFunc */
- + ExprList *pOrderBy;
- + u8 sortFlags = 0;
- +
- + assert( *ppMinMax==0 );
- + assert( pFunc->op==TK_AGG_FUNCTION );
- + assert( !IsWindowFunc(pFunc) );
- + if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_WinFunc) ){
- + return eRet;
- + }
- + zFunc = pFunc->u.zToken;
- + if( sqlite3StrICmp(zFunc, "min")==0 ){
- + eRet = WHERE_ORDERBY_MIN;
- + if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
- + sortFlags = KEYINFO_ORDER_BIGNULL;
- + }
- + }else if( sqlite3StrICmp(zFunc, "max")==0 ){
- + eRet = WHERE_ORDERBY_MAX;
- + sortFlags = KEYINFO_ORDER_DESC;
- + }else{
- + return eRet;
- + }
- + *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
- + assert( pOrderBy!=0 || db->mallocFailed );
- + if( pOrderBy ) pOrderBy->a[0].sortFlags = sortFlags;
- + return eRet;
- +}
- +
- +/*
- +** The select statement passed as the first argument is an aggregate query.
- +** The second argument is the associated aggregate-info object. This
- +** function tests if the SELECT is of the form:
- +**
- +** SELECT count(*) FROM <tbl>
- +**
- +** where table is a database table, not a sub-select or view. If the query
- +** does match this pattern, then a pointer to the Table object representing
- +** <tbl> is returned. Otherwise, 0 is returned.
- +*/
- +static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
- + Table *pTab;
- + Expr *pExpr;
- +
- + assert( !p->pGroupBy );
- +
- + if( p->pWhere || p->pEList->nExpr!=1
- + || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
- + ){
- + return 0;
- + }
- + pTab = p->pSrc->a[0].pTab;
- + pExpr = p->pEList->a[0].pExpr;
- + assert( pTab && !pTab->pSelect && pExpr );
- +
- + if( IsVirtual(pTab) ) return 0;
- + if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
- + if( NEVER(pAggInfo->nFunc==0) ) return 0;
- + if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
- + if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0;
- +
- + return pTab;
- +}
- +
- +/*
- +** If the source-list item passed as an argument was augmented with an
- +** INDEXED BY clause, then try to locate the specified index. If there
- +** was such a clause and the named index cannot be found, return
- +** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
- +** pFrom->pIndex and return SQLITE_OK.
- +*/
- +int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
- + if( pFrom->pTab && pFrom->fg.isIndexedBy ){
- + Table *pTab = pFrom->pTab;
- + char *zIndexedBy = pFrom->u1.zIndexedBy;
- + Index *pIdx;
- + for(pIdx=pTab->pIndex;
- + pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
- + pIdx=pIdx->pNext
- + );
- + if( !pIdx ){
- + sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
- + pParse->checkSchema = 1;
- + return SQLITE_ERROR;
- + }
- + pFrom->pIBIndex = pIdx;
- + }
- + return SQLITE_OK;
- +}
- +/*
- +** Detect compound SELECT statements that use an ORDER BY clause with
- +** an alternative collating sequence.
- +**
- +** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
- +**
- +** These are rewritten as a subquery:
- +**
- +** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
- +** ORDER BY ... COLLATE ...
- +**
- +** This transformation is necessary because the multiSelectOrderBy() routine
- +** above that generates the code for a compound SELECT with an ORDER BY clause
- +** uses a merge algorithm that requires the same collating sequence on the
- +** result columns as on the ORDER BY clause. See ticket
- +** http://www.sqlite.org/src/info/6709574d2a
- +**
- +** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
- +** The UNION ALL operator works fine with multiSelectOrderBy() even when
- +** there are COLLATE terms in the ORDER BY.
- +*/
- +static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
- + int i;
- + Select *pNew;
- + Select *pX;
- + sqlite3 *db;
- + struct ExprList_item *a;
- + SrcList *pNewSrc;
- + Parse *pParse;
- + Token dummy;
- +
- + if( p->pPrior==0 ) return WRC_Continue;
- + if( p->pOrderBy==0 ) return WRC_Continue;
- + for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
- + if( pX==0 ) return WRC_Continue;
- + a = p->pOrderBy->a;
- + for(i=p->pOrderBy->nExpr-1; i>=0; i--){
- + if( a[i].pExpr->flags & EP_Collate ) break;
- + }
- + if( i<0 ) return WRC_Continue;
- +
- + /* If we reach this point, that means the transformation is required. */
- +
- + pParse = pWalker->pParse;
- + db = pParse->db;
- + pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
- + if( pNew==0 ) return WRC_Abort;
- + memset(&dummy, 0, sizeof(dummy));
- + pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
- + if( pNewSrc==0 ) return WRC_Abort;
- + *pNew = *p;
- + p->pSrc = pNewSrc;
- + p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
- + p->op = TK_SELECT;
- + p->pWhere = 0;
- + pNew->pGroupBy = 0;
- + pNew->pHaving = 0;
- + pNew->pOrderBy = 0;
- + p->pPrior = 0;
- + p->pNext = 0;
- + p->pWith = 0;
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + p->pWinDefn = 0;
- +#endif
- + p->selFlags &= ~SF_Compound;
- + assert( (p->selFlags & SF_Converted)==0 );
- + p->selFlags |= SF_Converted;
- + assert( pNew->pPrior!=0 );
- + pNew->pPrior->pNext = pNew;
- + pNew->pLimit = 0;
- + return WRC_Continue;
- +}
- +
- +/*
- +** Check to see if the FROM clause term pFrom has table-valued function
- +** arguments. If it does, leave an error message in pParse and return
- +** non-zero, since pFrom is not allowed to be a table-valued function.
- +*/
- +static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
- + if( pFrom->fg.isTabFunc ){
- + sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
- + return 1;
- + }
- + return 0;
- +}
- +
- +#ifndef SQLITE_OMIT_CTE
- +/*
- +** Argument pWith (which may be NULL) points to a linked list of nested
- +** WITH contexts, from inner to outermost. If the table identified by
- +** FROM clause element pItem is really a common-table-expression (CTE)
- +** then return a pointer to the CTE definition for that table. Otherwise
- +** return NULL.
- +**
- +** If a non-NULL value is returned, set *ppContext to point to the With
- +** object that the returned CTE belongs to.
- +*/
- +static struct Cte *searchWith(
- + With *pWith, /* Current innermost WITH clause */
- + struct SrcList_item *pItem, /* FROM clause element to resolve */
- + With **ppContext /* OUT: WITH clause return value belongs to */
- +){
- + const char *zName;
- + if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
- + With *p;
- + for(p=pWith; p; p=p->pOuter){
- + int i;
- + for(i=0; i<p->nCte; i++){
- + if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
- + *ppContext = p;
- + return &p->a[i];
- + }
- + }
- + }
- + }
- + return 0;
- +}
- +
- +/* The code generator maintains a stack of active WITH clauses
- +** with the inner-most WITH clause being at the top of the stack.
- +**
- +** This routine pushes the WITH clause passed as the second argument
- +** onto the top of the stack. If argument bFree is true, then this
- +** WITH clause will never be popped from the stack. In this case it
- +** should be freed along with the Parse object. In other cases, when
- +** bFree==0, the With object will be freed along with the SELECT
- +** statement with which it is associated.
- +*/
- +void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
- + assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
- + if( pWith ){
- + assert( pParse->pWith!=pWith );
- + pWith->pOuter = pParse->pWith;
- + pParse->pWith = pWith;
- + if( bFree ) pParse->pWithToFree = pWith;
- + }
- +}
- +
- +/*
- +** This function checks if argument pFrom refers to a CTE declared by
- +** a WITH clause on the stack currently maintained by the parser. And,
- +** if currently processing a CTE expression, if it is a recursive
- +** reference to the current CTE.
- +**
- +** If pFrom falls into either of the two categories above, pFrom->pTab
- +** and other fields are populated accordingly. The caller should check
- +** (pFrom->pTab!=0) to determine whether or not a successful match
- +** was found.
- +**
- +** Whether or not a match is found, SQLITE_OK is returned if no error
- +** occurs. If an error does occur, an error message is stored in the
- +** parser and some error code other than SQLITE_OK returned.
- +*/
- +static int withExpand(
- + Walker *pWalker,
- + struct SrcList_item *pFrom
- +){
- + Parse *pParse = pWalker->pParse;
- + sqlite3 *db = pParse->db;
- + struct Cte *pCte; /* Matched CTE (or NULL if no match) */
- + With *pWith; /* WITH clause that pCte belongs to */
- +
- + assert( pFrom->pTab==0 );
- + if( pParse->nErr ){
- + return SQLITE_ERROR;
- + }
- +
- + pCte = searchWith(pParse->pWith, pFrom, &pWith);
- + if( pCte ){
- + Table *pTab;
- + ExprList *pEList;
- + Select *pSel;
- + Select *pLeft; /* Left-most SELECT statement */
- + int bMayRecursive; /* True if compound joined by UNION [ALL] */
- + With *pSavedWith; /* Initial value of pParse->pWith */
- +
- + /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
- + ** recursive reference to CTE pCte. Leave an error in pParse and return
- + ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
- + ** In this case, proceed. */
- + if( pCte->zCteErr ){
- + sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
- + return SQLITE_ERROR;
- + }
- + if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR;
- +
- + assert( pFrom->pTab==0 );
- + pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
- + if( pTab==0 ) return WRC_Abort;
- + pTab->nTabRef = 1;
- + pTab->zName = sqlite3DbStrDup(db, pCte->zName);
- + pTab->iPKey = -1;
- + pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
- + pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
- + pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
- + if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
- + assert( pFrom->pSelect );
- +
- + /* Check if this is a recursive CTE. */
- + pSel = pFrom->pSelect;
- + bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
- + if( bMayRecursive ){
- + int i;
- + SrcList *pSrc = pFrom->pSelect->pSrc;
- + for(i=0; i<pSrc->nSrc; i++){
- + struct SrcList_item *pItem = &pSrc->a[i];
- + if( pItem->zDatabase==0
- + && pItem->zName!=0
- + && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
- + ){
- + pItem->pTab = pTab;
- + pItem->fg.isRecursive = 1;
- + pTab->nTabRef++;
- + pSel->selFlags |= SF_Recursive;
- + }
- + }
- + }
- +
- + /* Only one recursive reference is permitted. */
- + if( pTab->nTabRef>2 ){
- + sqlite3ErrorMsg(
- + pParse, "multiple references to recursive table: %s", pCte->zName
- + );
- + return SQLITE_ERROR;
- + }
- + assert( pTab->nTabRef==1 ||
- + ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
- +
- + pCte->zCteErr = "circular reference: %s";
- + pSavedWith = pParse->pWith;
- + pParse->pWith = pWith;
- + if( bMayRecursive ){
- + Select *pPrior = pSel->pPrior;
- + assert( pPrior->pWith==0 );
- + pPrior->pWith = pSel->pWith;
- + sqlite3WalkSelect(pWalker, pPrior);
- + pPrior->pWith = 0;
- + }else{
- + sqlite3WalkSelect(pWalker, pSel);
- + }
- + pParse->pWith = pWith;
- +
- + for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
- + pEList = pLeft->pEList;
- + if( pCte->pCols ){
- + if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
- + sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
- + pCte->zName, pEList->nExpr, pCte->pCols->nExpr
- + );
- + pParse->pWith = pSavedWith;
- + return SQLITE_ERROR;
- + }
- + pEList = pCte->pCols;
- + }
- +
- + sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
- + if( bMayRecursive ){
- + if( pSel->selFlags & SF_Recursive ){
- + pCte->zCteErr = "multiple recursive references: %s";
- + }else{
- + pCte->zCteErr = "recursive reference in a subquery: %s";
- + }
- + sqlite3WalkSelect(pWalker, pSel);
- + }
- + pCte->zCteErr = 0;
- + pParse->pWith = pSavedWith;
- + }
- +
- + return SQLITE_OK;
- +}
- +#endif
- +
- +#ifndef SQLITE_OMIT_CTE
- +/*
- +** If the SELECT passed as the second argument has an associated WITH
- +** clause, pop it from the stack stored as part of the Parse object.
- +**
- +** This function is used as the xSelectCallback2() callback by
- +** sqlite3SelectExpand() when walking a SELECT tree to resolve table
- +** names and other FROM clause elements.
- +*/
- +static void selectPopWith(Walker *pWalker, Select *p){
- + Parse *pParse = pWalker->pParse;
- + if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
- + With *pWith = findRightmost(p)->pWith;
- + if( pWith!=0 ){
- + assert( pParse->pWith==pWith || pParse->nErr );
- + pParse->pWith = pWith->pOuter;
- + }
- + }
- +}
- +#else
- +#define selectPopWith 0
- +#endif
- +
- +/*
- +** The SrcList_item structure passed as the second argument represents a
- +** sub-query in the FROM clause of a SELECT statement. This function
- +** allocates and populates the SrcList_item.pTab object. If successful,
- +** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
- +** SQLITE_NOMEM.
- +*/
- +int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
- + Select *pSel = pFrom->pSelect;
- + Table *pTab;
- +
- + assert( pSel );
- + pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
- + if( pTab==0 ) return SQLITE_NOMEM;
- + pTab->nTabRef = 1;
- + if( pFrom->zAlias ){
- + pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
- + }else{
- + pTab->zName = sqlite3MPrintf(pParse->db, "subquery_%u", pSel->selId);
- + }
- + while( pSel->pPrior ){ pSel = pSel->pPrior; }
- + sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
- + pTab->iPKey = -1;
- + pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
- + pTab->tabFlags |= TF_Ephemeral;
- +
- + return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
- +}
- +
- +/*
- +** This routine is a Walker callback for "expanding" a SELECT statement.
- +** "Expanding" means to do the following:
- +**
- +** (1) Make sure VDBE cursor numbers have been assigned to every
- +** element of the FROM clause.
- +**
- +** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
- +** defines FROM clause. When views appear in the FROM clause,
- +** fill pTabList->a[].pSelect with a copy of the SELECT statement
- +** that implements the view. A copy is made of the view's SELECT
- +** statement so that we can freely modify or delete that statement
- +** without worrying about messing up the persistent representation
- +** of the view.
- +**
- +** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
- +** on joins and the ON and USING clause of joins.
- +**
- +** (4) Scan the list of columns in the result set (pEList) looking
- +** for instances of the "*" operator or the TABLE.* operator.
- +** If found, expand each "*" to be every column in every table
- +** and TABLE.* to be every column in TABLE.
- +**
- +*/
- +static int selectExpander(Walker *pWalker, Select *p){
- + Parse *pParse = pWalker->pParse;
- + int i, j, k;
- + SrcList *pTabList;
- + ExprList *pEList;
- + struct SrcList_item *pFrom;
- + sqlite3 *db = pParse->db;
- + Expr *pE, *pRight, *pExpr;
- + u16 selFlags = p->selFlags;
- + u32 elistFlags = 0;
- +
- + p->selFlags |= SF_Expanded;
- + if( db->mallocFailed ){
- + return WRC_Abort;
- + }
- + assert( p->pSrc!=0 );
- + if( (selFlags & SF_Expanded)!=0 ){
- + return WRC_Prune;
- + }
- + if( pWalker->eCode ){
- + /* Renumber selId because it has been copied from a view */
- + p->selId = ++pParse->nSelect;
- + }
- + pTabList = p->pSrc;
- + pEList = p->pEList;
- + sqlite3WithPush(pParse, p->pWith, 0);
- +
- + /* Make sure cursor numbers have been assigned to all entries in
- + ** the FROM clause of the SELECT statement.
- + */
- + sqlite3SrcListAssignCursors(pParse, pTabList);
- +
- + /* Look up every table named in the FROM clause of the select. If
- + ** an entry of the FROM clause is a subquery instead of a table or view,
- + ** then create a transient table structure to describe the subquery.
- + */
- + for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
- + Table *pTab;
- + assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
- + if( pFrom->fg.isRecursive ) continue;
- + assert( pFrom->pTab==0 );
- +#ifndef SQLITE_OMIT_CTE
- + if( withExpand(pWalker, pFrom) ) return WRC_Abort;
- + if( pFrom->pTab ) {} else
- +#endif
- + if( pFrom->zName==0 ){
- +#ifndef SQLITE_OMIT_SUBQUERY
- + Select *pSel = pFrom->pSelect;
- + /* A sub-query in the FROM clause of a SELECT */
- + assert( pSel!=0 );
- + assert( pFrom->pTab==0 );
- + if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
- + if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
- +#endif
- + }else{
- + /* An ordinary table or view name in the FROM clause */
- + assert( pFrom->pTab==0 );
- + pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
- + if( pTab==0 ) return WRC_Abort;
- + if( pTab->nTabRef>=0xffff ){
- + sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
- + pTab->zName);
- + pFrom->pTab = 0;
- + return WRC_Abort;
- + }
- + pTab->nTabRef++;
- + if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
- + return WRC_Abort;
- + }
- +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
- + if( IsVirtual(pTab) || pTab->pSelect ){
- + i16 nCol;
- + u8 eCodeOrig = pWalker->eCode;
- + if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
- + assert( pFrom->pSelect==0 );
- + if( pTab->pSelect && (db->flags & SQLITE_EnableView)==0 ){
- + sqlite3ErrorMsg(pParse, "access to view \"%s\" prohibited",
- + pTab->zName);
- + }
- +#ifndef SQLITE_OMIT_VIRTUALTABLE
- + if( IsVirtual(pTab)
- + && pFrom->fg.fromDDL
- + && ALWAYS(pTab->pVTable!=0)
- + && pTab->pVTable->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
- + ){
- + sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
- + pTab->zName);
- + }
- +#endif
- + pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
- + nCol = pTab->nCol;
- + pTab->nCol = -1;
- + pWalker->eCode = 1; /* Turn on Select.selId renumbering */
- + sqlite3WalkSelect(pWalker, pFrom->pSelect);
- + pWalker->eCode = eCodeOrig;
- + pTab->nCol = nCol;
- + }
- +#endif
- + }
- +
- + /* Locate the index named by the INDEXED BY clause, if any. */
- + if( sqlite3IndexedByLookup(pParse, pFrom) ){
- + return WRC_Abort;
- + }
- + }
- +
- + /* Process NATURAL keywords, and ON and USING clauses of joins.
- + */
- + if( pParse->nErr || db->mallocFailed || sqliteProcessJoin(pParse, p) ){
- + return WRC_Abort;
- + }
- +
- + /* For every "*" that occurs in the column list, insert the names of
- + ** all columns in all tables. And for every TABLE.* insert the names
- + ** of all columns in TABLE. The parser inserted a special expression
- + ** with the TK_ASTERISK operator for each "*" that it found in the column
- + ** list. The following code just has to locate the TK_ASTERISK
- + ** expressions and expand each one to the list of all columns in
- + ** all tables.
- + **
- + ** The first loop just checks to see if there are any "*" operators
- + ** that need expanding.
- + */
- + for(k=0; k<pEList->nExpr; k++){
- + pE = pEList->a[k].pExpr;
- + if( pE->op==TK_ASTERISK ) break;
- + assert( pE->op!=TK_DOT || pE->pRight!=0 );
- + assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
- + if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
- + elistFlags |= pE->flags;
- + }
- + if( k<pEList->nExpr ){
- + /*
- + ** If we get here it means the result set contains one or more "*"
- + ** operators that need to be expanded. Loop through each expression
- + ** in the result set and expand them one by one.
- + */
- + struct ExprList_item *a = pEList->a;
- + ExprList *pNew = 0;
- + int flags = pParse->db->flags;
- + int longNames = (flags & SQLITE_FullColNames)!=0
- + && (flags & SQLITE_ShortColNames)==0;
- +
- + for(k=0; k<pEList->nExpr; k++){
- + pE = a[k].pExpr;
- + elistFlags |= pE->flags;
- + pRight = pE->pRight;
- + assert( pE->op!=TK_DOT || pRight!=0 );
- + if( pE->op!=TK_ASTERISK
- + && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
- + ){
- + /* This particular expression does not need to be expanded.
- + */
- + pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
- + if( pNew ){
- + pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
- + pNew->a[pNew->nExpr-1].eEName = a[k].eEName;
- + a[k].zEName = 0;
- + }
- + a[k].pExpr = 0;
- + }else{
- + /* This expression is a "*" or a "TABLE.*" and needs to be
- + ** expanded. */
- + int tableSeen = 0; /* Set to 1 when TABLE matches */
- + char *zTName = 0; /* text of name of TABLE */
- + if( pE->op==TK_DOT ){
- + assert( pE->pLeft!=0 );
- + assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
- + zTName = pE->pLeft->u.zToken;
- + }
- + for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
- + Table *pTab = pFrom->pTab;
- + Select *pSub = pFrom->pSelect;
- + char *zTabName = pFrom->zAlias;
- + const char *zSchemaName = 0;
- + int iDb;
- + if( zTabName==0 ){
- + zTabName = pTab->zName;
- + }
- + if( db->mallocFailed ) break;
- + if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
- + pSub = 0;
- + if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
- + continue;
- + }
- + iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
- + zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
- + }
- + for(j=0; j<pTab->nCol; j++){
- + char *zName = pTab->aCol[j].zName;
- + char *zColname; /* The computed column name */
- + char *zToFree; /* Malloced string that needs to be freed */
- + Token sColname; /* Computed column name as a token */
- +
- + assert( zName );
- + if( zTName && pSub
- + && sqlite3MatchEName(&pSub->pEList->a[j], 0, zTName, 0)==0
- + ){
- + continue;
- + }
- +
- + /* If a column is marked as 'hidden', omit it from the expanded
- + ** result-set list unless the SELECT has the SF_IncludeHidden
- + ** bit set.
- + */
- + if( (p->selFlags & SF_IncludeHidden)==0
- + && IsHiddenColumn(&pTab->aCol[j])
- + ){
- + continue;
- + }
- + tableSeen = 1;
- +
- + if( i>0 && zTName==0 ){
- + if( (pFrom->fg.jointype & JT_NATURAL)!=0
- + && tableAndColumnIndex(pTabList, i, zName, 0, 0, 1)
- + ){
- + /* In a NATURAL join, omit the join columns from the
- + ** table to the right of the join */
- + continue;
- + }
- + if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
- + /* In a join with a USING clause, omit columns in the
- + ** using clause from the table on the right. */
- + continue;
- + }
- + }
- + pRight = sqlite3Expr(db, TK_ID, zName);
- + zColname = zName;
- + zToFree = 0;
- + if( longNames || pTabList->nSrc>1 ){
- + Expr *pLeft;
- + pLeft = sqlite3Expr(db, TK_ID, zTabName);
- + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
- + if( zSchemaName ){
- + pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
- + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr);
- + }
- + if( longNames ){
- + zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
- + zToFree = zColname;
- + }
- + }else{
- + pExpr = pRight;
- + }
- + pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
- + sqlite3TokenInit(&sColname, zColname);
- + sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
- + if( pNew && (p->selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT ){
- + struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
- + sqlite3DbFree(db, pX->zEName);
- + if( pSub ){
- + pX->zEName = sqlite3DbStrDup(db, pSub->pEList->a[j].zEName);
- + testcase( pX->zEName==0 );
- + }else{
- + pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
- + zSchemaName, zTabName, zColname);
- + testcase( pX->zEName==0 );
- + }
- + pX->eEName = ENAME_TAB;
- + }
- + sqlite3DbFree(db, zToFree);
- + }
- + }
- + if( !tableSeen ){
- + if( zTName ){
- + sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
- + }else{
- + sqlite3ErrorMsg(pParse, "no tables specified");
- + }
- + }
- + }
- + }
- + sqlite3ExprListDelete(db, pEList);
- + p->pEList = pNew;
- + }
- + if( p->pEList ){
- + if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
- + sqlite3ErrorMsg(pParse, "too many columns in result set");
- + return WRC_Abort;
- + }
- + if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
- + p->selFlags |= SF_ComplexResult;
- + }
- + }
- + return WRC_Continue;
- +}
- +
- +#if SQLITE_DEBUG
- +/*
- +** Always assert. This xSelectCallback2 implementation proves that the
- +** xSelectCallback2 is never invoked.
- +*/
- +void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
- + UNUSED_PARAMETER2(NotUsed, NotUsed2);
- + assert( 0 );
- +}
- +#endif
- +/*
- +** This routine "expands" a SELECT statement and all of its subqueries.
- +** For additional information on what it means to "expand" a SELECT
- +** statement, see the comment on the selectExpand worker callback above.
- +**
- +** Expanding a SELECT statement is the first step in processing a
- +** SELECT statement. The SELECT statement must be expanded before
- +** name resolution is performed.
- +**
- +** If anything goes wrong, an error message is written into pParse.
- +** The calling function can detect the problem by looking at pParse->nErr
- +** and/or pParse->db->mallocFailed.
- +*/
- +static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
- + Walker w;
- + w.xExprCallback = sqlite3ExprWalkNoop;
- + w.pParse = pParse;
- + if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
- + w.xSelectCallback = convertCompoundSelectToSubquery;
- + w.xSelectCallback2 = 0;
- + sqlite3WalkSelect(&w, pSelect);
- + }
- + w.xSelectCallback = selectExpander;
- + w.xSelectCallback2 = selectPopWith;
- + w.eCode = 0;
- + sqlite3WalkSelect(&w, pSelect);
- +}
- +
- +
- +#ifndef SQLITE_OMIT_SUBQUERY
- +/*
- +** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
- +** interface.
- +**
- +** For each FROM-clause subquery, add Column.zType and Column.zColl
- +** information to the Table structure that represents the result set
- +** of that subquery.
- +**
- +** The Table structure that represents the result set was constructed
- +** by selectExpander() but the type and collation information was omitted
- +** at that point because identifiers had not yet been resolved. This
- +** routine is called after identifier resolution.
- +*/
- +static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
- + Parse *pParse;
- + int i;
- + SrcList *pTabList;
- + struct SrcList_item *pFrom;
- +
- + assert( p->selFlags & SF_Resolved );
- + if( p->selFlags & SF_HasTypeInfo ) return;
- + p->selFlags |= SF_HasTypeInfo;
- + pParse = pWalker->pParse;
- + pTabList = p->pSrc;
- + for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
- + Table *pTab = pFrom->pTab;
- + assert( pTab!=0 );
- + if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
- + /* A sub-query in the FROM clause of a SELECT */
- + Select *pSel = pFrom->pSelect;
- + if( pSel ){
- + while( pSel->pPrior ) pSel = pSel->pPrior;
- + sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel,
- + SQLITE_AFF_NONE);
- + }
- + }
- + }
- +}
- +#endif
- +
- +
- +/*
- +** This routine adds datatype and collating sequence information to
- +** the Table structures of all FROM-clause subqueries in a
- +** SELECT statement.
- +**
- +** Use this routine after name resolution.
- +*/
- +static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
- +#ifndef SQLITE_OMIT_SUBQUERY
- + Walker w;
- + w.xSelectCallback = sqlite3SelectWalkNoop;
- + w.xSelectCallback2 = selectAddSubqueryTypeInfo;
- + w.xExprCallback = sqlite3ExprWalkNoop;
- + w.pParse = pParse;
- + sqlite3WalkSelect(&w, pSelect);
- +#endif
- +}
- +
- +
- +/*
- +** This routine sets up a SELECT statement for processing. The
- +** following is accomplished:
- +**
- +** * VDBE Cursor numbers are assigned to all FROM-clause terms.
- +** * Ephemeral Table objects are created for all FROM-clause subqueries.
- +** * ON and USING clauses are shifted into WHERE statements
- +** * Wildcards "*" and "TABLE.*" in result sets are expanded.
- +** * Identifiers in expression are matched to tables.
- +**
- +** This routine acts recursively on all subqueries within the SELECT.
- +*/
- +void sqlite3SelectPrep(
- + Parse *pParse, /* The parser context */
- + Select *p, /* The SELECT statement being coded. */
- + NameContext *pOuterNC /* Name context for container */
- +){
- + assert( p!=0 || pParse->db->mallocFailed );
- + if( pParse->db->mallocFailed ) return;
- + if( p->selFlags & SF_HasTypeInfo ) return;
- + sqlite3SelectExpand(pParse, p);
- + if( pParse->nErr || pParse->db->mallocFailed ) return;
- + sqlite3ResolveSelectNames(pParse, p, pOuterNC);
- + if( pParse->nErr || pParse->db->mallocFailed ) return;
- + sqlite3SelectAddTypeInfo(pParse, p);
- +}
- +
- +/*
- +** Reset the aggregate accumulator.
- +**
- +** The aggregate accumulator is a set of memory cells that hold
- +** intermediate results while calculating an aggregate. This
- +** routine generates code that stores NULLs in all of those memory
- +** cells.
- +*/
- +static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
- + Vdbe *v = pParse->pVdbe;
- + int i;
- + struct AggInfo_func *pFunc;
- + int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
- + if( nReg==0 ) return;
- + if( pParse->nErr ) return;
- +#ifdef SQLITE_DEBUG
- + /* Verify that all AggInfo registers are within the range specified by
- + ** AggInfo.mnReg..AggInfo.mxReg */
- + assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
- + for(i=0; i<pAggInfo->nColumn; i++){
- + assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
- + && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
- + }
- + for(i=0; i<pAggInfo->nFunc; i++){
- + assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
- + && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
- + }
- +#endif
- + sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
- + for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
- + if( pFunc->iDistinct>=0 ){
- + Expr *pE = pFunc->pExpr;
- + assert( !ExprHasProperty(pE, EP_xIsSelect) );
- + if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
- + sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
- + "argument");
- + pFunc->iDistinct = -1;
- + }else{
- + KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
- + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
- + (char*)pKeyInfo, P4_KEYINFO);
- + }
- + }
- + }
- +}
- +
- +/*
- +** Invoke the OP_AggFinalize opcode for every aggregate function
- +** in the AggInfo structure.
- +*/
- +static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
- + Vdbe *v = pParse->pVdbe;
- + int i;
- + struct AggInfo_func *pF;
- + for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
- + ExprList *pList = pF->pExpr->x.pList;
- + assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
- + sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
- + sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
- + }
- +}
- +
- +
- +/*
- +** Update the accumulator memory cells for an aggregate based on
- +** the current cursor position.
- +**
- +** If regAcc is non-zero and there are no min() or max() aggregates
- +** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
- +** registers if register regAcc contains 0. The caller will take care
- +** of setting and clearing regAcc.
- +*/
- +static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
- + Vdbe *v = pParse->pVdbe;
- + int i;
- + int regHit = 0;
- + int addrHitTest = 0;
- + struct AggInfo_func *pF;
- + struct AggInfo_col *pC;
- +
- + pAggInfo->directMode = 1;
- + for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
- + int nArg;
- + int addrNext = 0;
- + int regAgg;
- + ExprList *pList = pF->pExpr->x.pList;
- + assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
- + assert( !IsWindowFunc(pF->pExpr) );
- + if( ExprHasProperty(pF->pExpr, EP_WinFunc) ){
- + Expr *pFilter = pF->pExpr->y.pWin->pFilter;
- + if( pAggInfo->nAccumulator
- + && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
- + ){
- + if( regHit==0 ) regHit = ++pParse->nMem;
- + /* If this is the first row of the group (regAcc==0), clear the
- + ** "magnet" register regHit so that the accumulator registers
- + ** are populated if the FILTER clause jumps over the the
- + ** invocation of min() or max() altogether. Or, if this is not
- + ** the first row (regAcc==1), set the magnet register so that the
- + ** accumulators are not populated unless the min()/max() is invoked and
- + ** indicates that they should be. */
- + sqlite3VdbeAddOp2(v, OP_Copy, regAcc, regHit);
- + }
- + addrNext = sqlite3VdbeMakeLabel(pParse);
- + sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL);
- + }
- + if( pList ){
- + nArg = pList->nExpr;
- + regAgg = sqlite3GetTempRange(pParse, nArg);
- + sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
- + }else{
- + nArg = 0;
- + regAgg = 0;
- + }
- + if( pF->iDistinct>=0 ){
- + if( addrNext==0 ){
- + addrNext = sqlite3VdbeMakeLabel(pParse);
- + }
- + testcase( nArg==0 ); /* Error condition */
- + testcase( nArg>1 ); /* Also an error */
- + codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
- + }
- + if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
- + CollSeq *pColl = 0;
- + struct ExprList_item *pItem;
- + int j;
- + assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
- + for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
- + pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
- + }
- + if( !pColl ){
- + pColl = pParse->db->pDfltColl;
- + }
- + if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
- + sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
- + }
- + sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
- + sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
- + sqlite3VdbeChangeP5(v, (u8)nArg);
- + sqlite3ReleaseTempRange(pParse, regAgg, nArg);
- + if( addrNext ){
- + sqlite3VdbeResolveLabel(v, addrNext);
- + }
- + }
- + if( regHit==0 && pAggInfo->nAccumulator ){
- + regHit = regAcc;
- + }
- + if( regHit ){
- + addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
- + }
- + for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
- + sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
- + }
- +
- + pAggInfo->directMode = 0;
- + if( addrHitTest ){
- + sqlite3VdbeJumpHereOrPopInst(v, addrHitTest);
- + }
- +}
- +
- +/*
- +** Add a single OP_Explain instruction to the VDBE to explain a simple
- +** count(*) query ("SELECT count(*) FROM pTab").
- +*/
- +#ifndef SQLITE_OMIT_EXPLAIN
- +static void explainSimpleCount(
- + Parse *pParse, /* Parse context */
- + Table *pTab, /* Table being queried */
- + Index *pIdx /* Index used to optimize scan, or NULL */
- +){
- + if( pParse->explain==2 ){
- + int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
- + sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
- + pTab->zName,
- + bCover ? " USING COVERING INDEX " : "",
- + bCover ? pIdx->zName : ""
- + );
- + }
- +}
- +#else
- +# define explainSimpleCount(a,b,c)
- +#endif
- +
- +/*
- +** sqlite3WalkExpr() callback used by havingToWhere().
- +**
- +** If the node passed to the callback is a TK_AND node, return
- +** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
- +**
- +** Otherwise, return WRC_Prune. In this case, also check if the
- +** sub-expression matches the criteria for being moved to the WHERE
- +** clause. If so, add it to the WHERE clause and replace the sub-expression
- +** within the HAVING expression with a constant "1".
- +*/
- +static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
- + if( pExpr->op!=TK_AND ){
- + Select *pS = pWalker->u.pSelect;
- + if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
- + sqlite3 *db = pWalker->pParse->db;
- + Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1");
- + if( pNew ){
- + Expr *pWhere = pS->pWhere;
- + SWAP(Expr, *pNew, *pExpr);
- + pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
- + pS->pWhere = pNew;
- + pWalker->eCode = 1;
- + }
- + }
- + return WRC_Prune;
- + }
- + return WRC_Continue;
- +}
- +
- +/*
- +** Transfer eligible terms from the HAVING clause of a query, which is
- +** processed after grouping, to the WHERE clause, which is processed before
- +** grouping. For example, the query:
- +**
- +** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
- +**
- +** can be rewritten as:
- +**
- +** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
- +**
- +** A term of the HAVING expression is eligible for transfer if it consists
- +** entirely of constants and expressions that are also GROUP BY terms that
- +** use the "BINARY" collation sequence.
- +*/
- +static void havingToWhere(Parse *pParse, Select *p){
- + Walker sWalker;
- + memset(&sWalker, 0, sizeof(sWalker));
- + sWalker.pParse = pParse;
- + sWalker.xExprCallback = havingToWhereExprCb;
- + sWalker.u.pSelect = p;
- + sqlite3WalkExpr(&sWalker, p->pHaving);
- +#if SELECTTRACE_ENABLED
- + if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
- + SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- +}
- +
- +/*
- +** Check to see if the pThis entry of pTabList is a self-join of a prior view.
- +** If it is, then return the SrcList_item for the prior view. If it is not,
- +** then return 0.
- +*/
- +static struct SrcList_item *isSelfJoinView(
- + SrcList *pTabList, /* Search for self-joins in this FROM clause */
- + struct SrcList_item *pThis /* Search for prior reference to this subquery */
- +){
- + struct SrcList_item *pItem;
- + for(pItem = pTabList->a; pItem<pThis; pItem++){
- + Select *pS1;
- + if( pItem->pSelect==0 ) continue;
- + if( pItem->fg.viaCoroutine ) continue;
- + if( pItem->zName==0 ) continue;
- + assert( pItem->pTab!=0 );
- + assert( pThis->pTab!=0 );
- + if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
- + if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
- + pS1 = pItem->pSelect;
- + if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
- + /* The query flattener left two different CTE tables with identical
- + ** names in the same FROM clause. */
- + continue;
- + }
- + if( sqlite3ExprCompare(0, pThis->pSelect->pWhere, pS1->pWhere, -1)
- + || sqlite3ExprCompare(0, pThis->pSelect->pHaving, pS1->pHaving, -1)
- + ){
- + /* The view was modified by some other optimization such as
- + ** pushDownWhereTerms() */
- + continue;
- + }
- + return pItem;
- + }
- + return 0;
- +}
- +
- +#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
- +/*
- +** Attempt to transform a query of the form
- +**
- +** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
- +**
- +** Into this:
- +**
- +** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
- +**
- +** The transformation only works if all of the following are true:
- +**
- +** * The subquery is a UNION ALL of two or more terms
- +** * The subquery does not have a LIMIT clause
- +** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
- +** * The outer query is a simple count(*) with no WHERE clause or other
- +** extraneous syntax.
- +**
- +** Return TRUE if the optimization is undertaken.
- +*/
- +static int countOfViewOptimization(Parse *pParse, Select *p){
- + Select *pSub, *pPrior;
- + Expr *pExpr;
- + Expr *pCount;
- + sqlite3 *db;
- + if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
- + if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
- + if( p->pWhere ) return 0;
- + if( p->pGroupBy ) return 0;
- + pExpr = p->pEList->a[0].pExpr;
- + if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
- + if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
- + if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
- + if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
- + pSub = p->pSrc->a[0].pSelect;
- + if( pSub==0 ) return 0; /* The FROM is a subquery */
- + if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
- + do{
- + if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
- + if( pSub->pWhere ) return 0; /* No WHERE clause */
- + if( pSub->pLimit ) return 0; /* No LIMIT clause */
- + if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
- + pSub = pSub->pPrior; /* Repeat over compound */
- + }while( pSub );
- +
- + /* If we reach this point then it is OK to perform the transformation */
- +
- + db = pParse->db;
- + pCount = pExpr;
- + pExpr = 0;
- + pSub = p->pSrc->a[0].pSelect;
- + p->pSrc->a[0].pSelect = 0;
- + sqlite3SrcListDelete(db, p->pSrc);
- + p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
- + while( pSub ){
- + Expr *pTerm;
- + pPrior = pSub->pPrior;
- + pSub->pPrior = 0;
- + pSub->pNext = 0;
- + pSub->selFlags |= SF_Aggregate;
- + pSub->selFlags &= ~SF_Compound;
- + pSub->nSelectRow = 0;
- + sqlite3ExprListDelete(db, pSub->pEList);
- + pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
- + pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
- + pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
- + sqlite3PExprAddSelect(pParse, pTerm, pSub);
- + if( pExpr==0 ){
- + pExpr = pTerm;
- + }else{
- + pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
- + }
- + pSub = pPrior;
- + }
- + p->pEList->a[0].pExpr = pExpr;
- + p->selFlags &= ~SF_Aggregate;
- +
- +#if SELECTTRACE_ENABLED
- + if( sqlite3SelectTrace & 0x400 ){
- + SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- + return 1;
- +}
- +#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
- +
- +/*
- +** Generate code for the SELECT statement given in the p argument.
- +**
- +** The results are returned according to the SelectDest structure.
- +** See comments in sqliteInt.h for further information.
- +**
- +** This routine returns the number of errors. If any errors are
- +** encountered, then an appropriate error message is left in
- +** pParse->zErrMsg.
- +**
- +** This routine does NOT free the Select structure passed in. The
- +** calling function needs to do that.
- +*/
- +int sqlite3Select(
- + Parse *pParse, /* The parser context */
- + Select *p, /* The SELECT statement being coded. */
- + SelectDest *pDest /* What to do with the query results */
- +){
- + int i, j; /* Loop counters */
- + WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
- + Vdbe *v; /* The virtual machine under construction */
- + int isAgg; /* True for select lists like "count(*)" */
- + ExprList *pEList = 0; /* List of columns to extract. */
- + SrcList *pTabList; /* List of tables to select from */
- + Expr *pWhere; /* The WHERE clause. May be NULL */
- + ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
- + Expr *pHaving; /* The HAVING clause. May be NULL */
- + int rc = 1; /* Value to return from this function */
- + DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
- + SortCtx sSort; /* Info on how to code the ORDER BY clause */
- + AggInfo sAggInfo; /* Information used by aggregate queries */
- + int iEnd; /* Address of the end of the query */
- + sqlite3 *db; /* The database connection */
- + ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
- + u8 minMaxFlag; /* Flag for min/max queries */
- +
- + db = pParse->db;
- + v = sqlite3GetVdbe(pParse);
- + if( p==0 || db->mallocFailed || pParse->nErr ){
- + return 1;
- + }
- + if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
- + memset(&sAggInfo, 0, sizeof(sAggInfo));
- +#ifdef SQLITE_DEBUG
- + sAggInfo.iAggMagic = SQLITE_AGGMAGIC_VALID;
- +#endif
- +#if SELECTTRACE_ENABLED
- + SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
- + if( sqlite3SelectTrace & 0x100 ){
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- +
- + assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
- + assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
- + assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
- + assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
- + if( IgnorableOrderby(pDest) ){
- + assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
- + pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
- + pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo ||
- + pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
- + /* If ORDER BY makes no difference in the output then neither does
- + ** DISTINCT so it can be removed too. */
- + sqlite3ExprListDelete(db, p->pOrderBy);
- + p->pOrderBy = 0;
- + p->selFlags &= ~SF_Distinct;
- + }
- + sqlite3SelectPrep(pParse, p, 0);
- + if( pParse->nErr || db->mallocFailed ){
- + goto select_end;
- + }
- + assert( p->pEList!=0 );
- +#if SELECTTRACE_ENABLED
- + if( sqlite3SelectTrace & 0x104 ){
- + SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- +
- + if( pDest->eDest==SRT_Output ){
- + generateColumnNames(pParse, p);
- + }
- +
- + pTabList = p->pSrc;
- + isAgg = (p->selFlags & SF_Aggregate)!=0;
- + memset(&sSort, 0, sizeof(sSort));
- + sSort.pOrderBy = p->pOrderBy;
- +
- + /* Try to various optimizations (flattening subqueries, and strength
- + ** reduction of join operators) in the FROM clause up into the main query
- + */
- +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
- + for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
- + struct SrcList_item *pItem = &pTabList->a[i];
- + Select *pSub = pItem->pSelect;
- + Table *pTab = pItem->pTab;
- +
- + /* Convert LEFT JOIN into JOIN if there are terms of the right table
- + ** of the LEFT JOIN used in the WHERE clause.
- + */
- + if( (pItem->fg.jointype & JT_LEFT)!=0
- + && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
- + && OptimizationEnabled(db, SQLITE_SimplifyJoin)
- + ){
- + SELECTTRACE(0x100,pParse,p,
- + ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
- + pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
- + unsetJoinExpr(p->pWhere, pItem->iCursor);
- + }
- +
- + /* No futher action if this term of the FROM clause is no a subquery */
- + if( pSub==0 ) continue;
- +
- + /* Catch mismatch in the declared columns of a view and the number of
- + ** columns in the SELECT on the RHS */
- + if( pTab->nCol!=pSub->pEList->nExpr ){
- + sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
- + pTab->nCol, pTab->zName, pSub->pEList->nExpr);
- + goto select_end;
- + }
- +
- + /* Do not try to flatten an aggregate subquery.
- + **
- + ** Flattening an aggregate subquery is only possible if the outer query
- + ** is not a join. But if the outer query is not a join, then the subquery
- + ** will be implemented as a co-routine and there is no advantage to
- + ** flattening in that case.
- + */
- + if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
- + assert( pSub->pGroupBy==0 );
- +
- + /* If the outer query contains a "complex" result set (that is,
- + ** if the result set of the outer query uses functions or subqueries)
- + ** and if the subquery contains an ORDER BY clause and if
- + ** it will be implemented as a co-routine, then do not flatten. This
- + ** restriction allows SQL constructs like this:
- + **
- + ** SELECT expensive_function(x)
- + ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
- + **
- + ** The expensive_function() is only computed on the 10 rows that
- + ** are output, rather than every row of the table.
- + **
- + ** The requirement that the outer query have a complex result set
- + ** means that flattening does occur on simpler SQL constraints without
- + ** the expensive_function() like:
- + **
- + ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
- + */
- + if( pSub->pOrderBy!=0
- + && i==0
- + && (p->selFlags & SF_ComplexResult)!=0
- + && (pTabList->nSrc==1
- + || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
- + ){
- + continue;
- + }
- +
- + if( flattenSubquery(pParse, p, i, isAgg) ){
- + if( pParse->nErr ) goto select_end;
- + /* This subquery can be absorbed into its parent. */
- + i = -1;
- + }
- + pTabList = p->pSrc;
- + if( db->mallocFailed ) goto select_end;
- + if( !IgnorableOrderby(pDest) ){
- + sSort.pOrderBy = p->pOrderBy;
- + }
- + }
- +#endif
- +
- +#ifndef SQLITE_OMIT_COMPOUND_SELECT
- + /* Handle compound SELECT statements using the separate multiSelect()
- + ** procedure.
- + */
- + if( p->pPrior ){
- + rc = multiSelect(pParse, p, pDest);
- +#if SELECTTRACE_ENABLED
- + SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
- + if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- + if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
- + return rc;
- + }
- +#endif
- +
- + /* Do the WHERE-clause constant propagation optimization if this is
- + ** a join. No need to speed time on this operation for non-join queries
- + ** as the equivalent optimization will be handled by query planner in
- + ** sqlite3WhereBegin().
- + */
- + if( pTabList->nSrc>1
- + && OptimizationEnabled(db, SQLITE_PropagateConst)
- + && propagateConstants(pParse, p)
- + ){
- +#if SELECTTRACE_ENABLED
- + if( sqlite3SelectTrace & 0x100 ){
- + SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- + }else{
- + SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
- + }
- +
- +#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
- + if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
- + && countOfViewOptimization(pParse, p)
- + ){
- + if( db->mallocFailed ) goto select_end;
- + pEList = p->pEList;
- + pTabList = p->pSrc;
- + }
- +#endif
- +
- + /* For each term in the FROM clause, do two things:
- + ** (1) Authorized unreferenced tables
- + ** (2) Generate code for all sub-queries
- + */
- + for(i=0; i<pTabList->nSrc; i++){
- + struct SrcList_item *pItem = &pTabList->a[i];
- + SelectDest dest;
- + Select *pSub;
- +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
- + const char *zSavedAuthContext;
- +#endif
- +
- + /* Issue SQLITE_READ authorizations with a fake column name for any
- + ** tables that are referenced but from which no values are extracted.
- + ** Examples of where these kinds of null SQLITE_READ authorizations
- + ** would occur:
- + **
- + ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
- + ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
- + **
- + ** The fake column name is an empty string. It is possible for a table to
- + ** have a column named by the empty string, in which case there is no way to
- + ** distinguish between an unreferenced table and an actual reference to the
- + ** "" column. The original design was for the fake column name to be a NULL,
- + ** which would be unambiguous. But legacy authorization callbacks might
- + ** assume the column name is non-NULL and segfault. The use of an empty
- + ** string for the fake column name seems safer.
- + */
- + if( pItem->colUsed==0 && pItem->zName!=0 ){
- + sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
- + }
- +
- +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
- + /* Generate code for all sub-queries in the FROM clause
- + */
- + pSub = pItem->pSelect;
- + if( pSub==0 ) continue;
- +
- + /* The code for a subquery should only be generated once, though it is
- + ** technically harmless for it to be generated multiple times. The
- + ** following assert() will detect if something changes to cause
- + ** the same subquery to be coded multiple times, as a signal to the
- + ** developers to try to optimize the situation.
- + **
- + ** Update 2019-07-24:
- + ** See ticket https://sqlite.org/src/tktview/c52b09c7f38903b1311cec40.
- + ** The dbsqlfuzz fuzzer found a case where the same subquery gets
- + ** coded twice. So this assert() now becomes a testcase(). It should
- + ** be very rare, though.
- + */
- + testcase( pItem->addrFillSub!=0 );
- +
- + /* Increment Parse.nHeight by the height of the largest expression
- + ** tree referred to by this, the parent select. The child select
- + ** may contain expression trees of at most
- + ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
- + ** more conservative than necessary, but much easier than enforcing
- + ** an exact limit.
- + */
- + pParse->nHeight += sqlite3SelectExprHeight(p);
- +
- + /* Make copies of constant WHERE-clause terms in the outer query down
- + ** inside the subquery. This can help the subquery to run more efficiently.
- + */
- + if( OptimizationEnabled(db, SQLITE_PushDown)
- + && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
- + (pItem->fg.jointype & JT_OUTER)!=0)
- + ){
- +#if SELECTTRACE_ENABLED
- + if( sqlite3SelectTrace & 0x100 ){
- + SELECTTRACE(0x100,pParse,p,
- + ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- + }else{
- + SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
- + }
- +
- + zSavedAuthContext = pParse->zAuthContext;
- + pParse->zAuthContext = pItem->zName;
- +
- + /* Generate code to implement the subquery
- + **
- + ** The subquery is implemented as a co-routine if the subquery is
- + ** guaranteed to be the outer loop (so that it does not need to be
- + ** computed more than once)
- + **
- + ** TODO: Are there other reasons beside (1) to use a co-routine
- + ** implementation?
- + */
- + if( i==0
- + && (pTabList->nSrc==1
- + || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */
- + ){
- + /* Implement a co-routine that will return a single row of the result
- + ** set on each invocation.
- + */
- + int addrTop = sqlite3VdbeCurrentAddr(v)+1;
- +
- + pItem->regReturn = ++pParse->nMem;
- + sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
- + VdbeComment((v, "%s", pItem->pTab->zName));
- + pItem->addrFillSub = addrTop;
- + sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
- + ExplainQueryPlan((pParse, 1, "CO-ROUTINE %u", pSub->selId));
- + sqlite3Select(pParse, pSub, &dest);
- + pItem->pTab->nRowLogEst = pSub->nSelectRow;
- + pItem->fg.viaCoroutine = 1;
- + pItem->regResult = dest.iSdst;
- + sqlite3VdbeEndCoroutine(v, pItem->regReturn);
- + sqlite3VdbeJumpHere(v, addrTop-1);
- + sqlite3ClearTempRegCache(pParse);
- + }else{
- + /* Generate a subroutine that will fill an ephemeral table with
- + ** the content of this subquery. pItem->addrFillSub will point
- + ** to the address of the generated subroutine. pItem->regReturn
- + ** is a register allocated to hold the subroutine return address
- + */
- + int topAddr;
- + int onceAddr = 0;
- + int retAddr;
- + struct SrcList_item *pPrior;
- +
- + testcase( pItem->addrFillSub==0 ); /* Ticket c52b09c7f38903b1311 */
- + pItem->regReturn = ++pParse->nMem;
- + topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
- + pItem->addrFillSub = topAddr+1;
- + if( pItem->fg.isCorrelated==0 ){
- + /* If the subquery is not correlated and if we are not inside of
- + ** a trigger, then we only need to compute the value of the subquery
- + ** once. */
- + onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
- + VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
- + }else{
- + VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
- + }
- + pPrior = isSelfJoinView(pTabList, pItem);
- + if( pPrior ){
- + sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
- + assert( pPrior->pSelect!=0 );
- + pSub->nSelectRow = pPrior->pSelect->nSelectRow;
- + }else{
- + sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
- + ExplainQueryPlan((pParse, 1, "MATERIALIZE %u", pSub->selId));
- + sqlite3Select(pParse, pSub, &dest);
- + }
- + pItem->pTab->nRowLogEst = pSub->nSelectRow;
- + if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
- + retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
- + VdbeComment((v, "end %s", pItem->pTab->zName));
- + sqlite3VdbeChangeP1(v, topAddr, retAddr);
- + sqlite3ClearTempRegCache(pParse);
- + }
- + if( db->mallocFailed ) goto select_end;
- + pParse->nHeight -= sqlite3SelectExprHeight(p);
- + pParse->zAuthContext = zSavedAuthContext;
- +#endif
- + }
- +
- + /* Various elements of the SELECT copied into local variables for
- + ** convenience */
- + pEList = p->pEList;
- + pWhere = p->pWhere;
- + pGroupBy = p->pGroupBy;
- + pHaving = p->pHaving;
- + sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
- +
- +#if SELECTTRACE_ENABLED
- + if( sqlite3SelectTrace & 0x400 ){
- + SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- +
- + /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
- + ** if the select-list is the same as the ORDER BY list, then this query
- + ** can be rewritten as a GROUP BY. In other words, this:
- + **
- + ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
- + **
- + ** is transformed to:
- + **
- + ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
- + **
- + ** The second form is preferred as a single index (or temp-table) may be
- + ** used for both the ORDER BY and DISTINCT processing. As originally
- + ** written the query must use a temp-table for at least one of the ORDER
- + ** BY and DISTINCT, and an index or separate temp-table for the other.
- + */
- + if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
- + && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + && ALWAYS(p->pWin==0)
- +#endif
- + ){
- + p->selFlags &= ~SF_Distinct;
- + pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
- + p->selFlags |= SF_Aggregate;
- + /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
- + ** the sDistinct.isTnct is still set. Hence, isTnct represents the
- + ** original setting of the SF_Distinct flag, not the current setting */
- + assert( sDistinct.isTnct );
- +
- +#if SELECTTRACE_ENABLED
- + if( sqlite3SelectTrace & 0x400 ){
- + SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- + }
- +
- + /* If there is an ORDER BY clause, then create an ephemeral index to
- + ** do the sorting. But this sorting ephemeral index might end up
- + ** being unused if the data can be extracted in pre-sorted order.
- + ** If that is the case, then the OP_OpenEphemeral instruction will be
- + ** changed to an OP_Noop once we figure out that the sorting index is
- + ** not needed. The sSort.addrSortIndex variable is used to facilitate
- + ** that change.
- + */
- + if( sSort.pOrderBy ){
- + KeyInfo *pKeyInfo;
- + pKeyInfo = sqlite3KeyInfoFromExprList(
- + pParse, sSort.pOrderBy, 0, pEList->nExpr);
- + sSort.iECursor = pParse->nTab++;
- + sSort.addrSortIndex =
- + sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
- + sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
- + (char*)pKeyInfo, P4_KEYINFO
- + );
- + }else{
- + sSort.addrSortIndex = -1;
- + }
- +
- + /* If the output is destined for a temporary table, open that table.
- + */
- + if( pDest->eDest==SRT_EphemTab ){
- + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
- + }
- +
- + /* Set the limiter.
- + */
- + iEnd = sqlite3VdbeMakeLabel(pParse);
- + if( (p->selFlags & SF_FixedLimit)==0 ){
- + p->nSelectRow = 320; /* 4 billion rows */
- + }
- + computeLimitRegisters(pParse, p, iEnd);
- + if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
- + sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
- + sSort.sortFlags |= SORTFLAG_UseSorter;
- + }
- +
- + /* Open an ephemeral index to use for the distinct set.
- + */
- + if( p->selFlags & SF_Distinct ){
- + sDistinct.tabTnct = pParse->nTab++;
- + sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
- + sDistinct.tabTnct, 0, 0,
- + (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
- + P4_KEYINFO);
- + sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
- + sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
- + }else{
- + sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
- + }
- +
- + if( !isAgg && pGroupBy==0 ){
- + /* No aggregate functions and no GROUP BY clause */
- + u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0)
- + | (p->selFlags & SF_FixedLimit);
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + Window *pWin = p->pWin; /* Master window object (or NULL) */
- + if( pWin ){
- + sqlite3WindowCodeInit(pParse, p);
- + }
- +#endif
- + assert( WHERE_USE_LIMIT==SF_FixedLimit );
- +
- +
- + /* Begin the database scan. */
- + SELECTTRACE(1,pParse,p,("WhereBegin\n"));
- + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
- + p->pEList, wctrlFlags, p->nSelectRow);
- + if( pWInfo==0 ) goto select_end;
- + if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
- + p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
- + }
- + if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
- + sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
- + }
- + if( sSort.pOrderBy ){
- + sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
- + sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
- + if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
- + sSort.pOrderBy = 0;
- + }
- + }
- +
- + /* If sorting index that was created by a prior OP_OpenEphemeral
- + ** instruction ended up not being needed, then change the OP_OpenEphemeral
- + ** into an OP_Noop.
- + */
- + if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
- + sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
- + }
- +
- + assert( p->pEList==pEList );
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + if( pWin ){
- + int addrGosub = sqlite3VdbeMakeLabel(pParse);
- + int iCont = sqlite3VdbeMakeLabel(pParse);
- + int iBreak = sqlite3VdbeMakeLabel(pParse);
- + int regGosub = ++pParse->nMem;
- +
- + sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
- +
- + sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
- + sqlite3VdbeResolveLabel(v, addrGosub);
- + VdbeNoopComment((v, "inner-loop subroutine"));
- + sSort.labelOBLopt = 0;
- + selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
- + sqlite3VdbeResolveLabel(v, iCont);
- + sqlite3VdbeAddOp1(v, OP_Return, regGosub);
- + VdbeComment((v, "end inner-loop subroutine"));
- + sqlite3VdbeResolveLabel(v, iBreak);
- + }else
- +#endif /* SQLITE_OMIT_WINDOWFUNC */
- + {
- + /* Use the standard inner loop. */
- + selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
- + sqlite3WhereContinueLabel(pWInfo),
- + sqlite3WhereBreakLabel(pWInfo));
- +
- + /* End the database scan loop.
- + */
- + sqlite3WhereEnd(pWInfo);
- + }
- + }else{
- + /* This case when there exist aggregate functions or a GROUP BY clause
- + ** or both */
- + NameContext sNC; /* Name context for processing aggregate information */
- + int iAMem; /* First Mem address for storing current GROUP BY */
- + int iBMem; /* First Mem address for previous GROUP BY */
- + int iUseFlag; /* Mem address holding flag indicating that at least
- + ** one row of the input to the aggregator has been
- + ** processed */
- + int iAbortFlag; /* Mem address which causes query abort if positive */
- + int groupBySort; /* Rows come from source in GROUP BY order */
- + int addrEnd; /* End of processing for this SELECT */
- + int sortPTab = 0; /* Pseudotable used to decode sorting results */
- + int sortOut = 0; /* Output register from the sorter */
- + int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
- +
- + /* Remove any and all aliases between the result set and the
- + ** GROUP BY clause.
- + */
- + if( pGroupBy ){
- + int k; /* Loop counter */
- + struct ExprList_item *pItem; /* For looping over expression in a list */
- +
- + for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
- + pItem->u.x.iAlias = 0;
- + }
- + for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
- + pItem->u.x.iAlias = 0;
- + }
- + assert( 66==sqlite3LogEst(100) );
- + if( p->nSelectRow>66 ) p->nSelectRow = 66;
- +
- + /* If there is both a GROUP BY and an ORDER BY clause and they are
- + ** identical, then it may be possible to disable the ORDER BY clause
- + ** on the grounds that the GROUP BY will cause elements to come out
- + ** in the correct order. It also may not - the GROUP BY might use a
- + ** database index that causes rows to be grouped together as required
- + ** but not actually sorted. Either way, record the fact that the
- + ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
- + ** variable. */
- + if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
- + int ii;
- + /* The GROUP BY processing doesn't care whether rows are delivered in
- + ** ASC or DESC order - only that each group is returned contiguously.
- + ** So set the ASC/DESC flags in the GROUP BY to match those in the
- + ** ORDER BY to maximize the chances of rows being delivered in an
- + ** order that makes the ORDER BY redundant. */
- + for(ii=0; ii<pGroupBy->nExpr; ii++){
- + u8 sortFlags = sSort.pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_DESC;
- + pGroupBy->a[ii].sortFlags = sortFlags;
- + }
- + if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
- + orderByGrp = 1;
- + }
- + }
- + }else{
- + assert( 0==sqlite3LogEst(1) );
- + p->nSelectRow = 0;
- + }
- +
- + /* Create a label to jump to when we want to abort the query */
- + addrEnd = sqlite3VdbeMakeLabel(pParse);
- +
- + /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
- + ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
- + ** SELECT statement.
- + */
- + memset(&sNC, 0, sizeof(sNC));
- + sNC.pParse = pParse;
- + sNC.pSrcList = pTabList;
- + sNC.uNC.pAggInfo = &sAggInfo;
- + VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
- + sAggInfo.mnReg = pParse->nMem+1;
- + sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
- + sAggInfo.pGroupBy = pGroupBy;
- + sqlite3ExprAnalyzeAggList(&sNC, pEList);
- + sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
- + if( pHaving ){
- + if( pGroupBy ){
- + assert( pWhere==p->pWhere );
- + assert( pHaving==p->pHaving );
- + assert( pGroupBy==p->pGroupBy );
- + havingToWhere(pParse, p);
- + pWhere = p->pWhere;
- + }
- + sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
- + }
- + sAggInfo.nAccumulator = sAggInfo.nColumn;
- + if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
- + minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
- + }else{
- + minMaxFlag = WHERE_ORDERBY_NORMAL;
- + }
- + for(i=0; i<sAggInfo.nFunc; i++){
- + Expr *pExpr = sAggInfo.aFunc[i].pExpr;
- + assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
- + sNC.ncFlags |= NC_InAggFunc;
- + sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + assert( !IsWindowFunc(pExpr) );
- + if( ExprHasProperty(pExpr, EP_WinFunc) ){
- + sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
- + }
- +#endif
- + sNC.ncFlags &= ~NC_InAggFunc;
- + }
- + sAggInfo.mxReg = pParse->nMem;
- + if( db->mallocFailed ) goto select_end;
- +#if SELECTTRACE_ENABLED
- + if( sqlite3SelectTrace & 0x400 ){
- + int ii;
- + SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", &sAggInfo));
- + sqlite3TreeViewSelect(0, p, 0);
- + for(ii=0; ii<sAggInfo.nColumn; ii++){
- + sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
- + ii, sAggInfo.aCol[ii].iMem);
- + sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0);
- + }
- + for(ii=0; ii<sAggInfo.nFunc; ii++){
- + sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
- + ii, sAggInfo.aFunc[ii].iMem);
- + sqlite3TreeViewExpr(0, sAggInfo.aFunc[ii].pExpr, 0);
- + }
- + }
- +#endif
- +
- +
- + /* Processing for aggregates with GROUP BY is very different and
- + ** much more complex than aggregates without a GROUP BY.
- + */
- + if( pGroupBy ){
- + KeyInfo *pKeyInfo; /* Keying information for the group by clause */
- + int addr1; /* A-vs-B comparision jump */
- + int addrOutputRow; /* Start of subroutine that outputs a result row */
- + int regOutputRow; /* Return address register for output subroutine */
- + int addrSetAbort; /* Set the abort flag and return */
- + int addrTopOfLoop; /* Top of the input loop */
- + int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
- + int addrReset; /* Subroutine for resetting the accumulator */
- + int regReset; /* Return address register for reset subroutine */
- +
- + /* If there is a GROUP BY clause we might need a sorting index to
- + ** implement it. Allocate that sorting index now. If it turns out
- + ** that we do not need it after all, the OP_SorterOpen instruction
- + ** will be converted into a Noop.
- + */
- + sAggInfo.sortingIdx = pParse->nTab++;
- + pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pGroupBy,0,sAggInfo.nColumn);
- + addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
- + sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
- + 0, (char*)pKeyInfo, P4_KEYINFO);
- +
- + /* Initialize memory locations used by GROUP BY aggregate processing
- + */
- + iUseFlag = ++pParse->nMem;
- + iAbortFlag = ++pParse->nMem;
- + regOutputRow = ++pParse->nMem;
- + addrOutputRow = sqlite3VdbeMakeLabel(pParse);
- + regReset = ++pParse->nMem;
- + addrReset = sqlite3VdbeMakeLabel(pParse);
- + iAMem = pParse->nMem + 1;
- + pParse->nMem += pGroupBy->nExpr;
- + iBMem = pParse->nMem + 1;
- + pParse->nMem += pGroupBy->nExpr;
- + sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
- + VdbeComment((v, "clear abort flag"));
- + sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
- +
- + /* Begin a loop that will extract all source rows in GROUP BY order.
- + ** This might involve two separate loops with an OP_Sort in between, or
- + ** it might be a single loop that uses an index to extract information
- + ** in the right order to begin with.
- + */
- + sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
- + SELECTTRACE(1,pParse,p,("WhereBegin\n"));
- + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
- + WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
- + );
- + if( pWInfo==0 ) goto select_end;
- + if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
- + /* The optimizer is able to deliver rows in group by order so
- + ** we do not have to sort. The OP_OpenEphemeral table will be
- + ** cancelled later because we still need to use the pKeyInfo
- + */
- + groupBySort = 0;
- + }else{
- + /* Rows are coming out in undetermined order. We have to push
- + ** each row into a sorting index, terminate the first loop,
- + ** then loop over the sorting index in order to get the output
- + ** in sorted order
- + */
- + int regBase;
- + int regRecord;
- + int nCol;
- + int nGroupBy;
- +
- + explainTempTable(pParse,
- + (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
- + "DISTINCT" : "GROUP BY");
- +
- + groupBySort = 1;
- + nGroupBy = pGroupBy->nExpr;
- + nCol = nGroupBy;
- + j = nGroupBy;
- + for(i=0; i<sAggInfo.nColumn; i++){
- + if( sAggInfo.aCol[i].iSorterColumn>=j ){
- + nCol++;
- + j++;
- + }
- + }
- + regBase = sqlite3GetTempRange(pParse, nCol);
- + sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
- + j = nGroupBy;
- + for(i=0; i<sAggInfo.nColumn; i++){
- + struct AggInfo_col *pCol = &sAggInfo.aCol[i];
- + if( pCol->iSorterColumn>=j ){
- + int r1 = j + regBase;
- + sqlite3ExprCodeGetColumnOfTable(v,
- + pCol->pTab, pCol->iTable, pCol->iColumn, r1);
- + j++;
- + }
- + }
- + regRecord = sqlite3GetTempReg(pParse);
- + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
- + sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
- + sqlite3ReleaseTempReg(pParse, regRecord);
- + sqlite3ReleaseTempRange(pParse, regBase, nCol);
- + sqlite3WhereEnd(pWInfo);
- + sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
- + sortOut = sqlite3GetTempReg(pParse);
- + sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
- + sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
- + VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
- + sAggInfo.useSortingIdx = 1;
- + }
- +
- + /* If the index or temporary table used by the GROUP BY sort
- + ** will naturally deliver rows in the order required by the ORDER BY
- + ** clause, cancel the ephemeral table open coded earlier.
- + **
- + ** This is an optimization - the correct answer should result regardless.
- + ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
- + ** disable this optimization for testing purposes. */
- + if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
- + && (groupBySort || sqlite3WhereIsSorted(pWInfo))
- + ){
- + sSort.pOrderBy = 0;
- + sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
- + }
- +
- + /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
- + ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
- + ** Then compare the current GROUP BY terms against the GROUP BY terms
- + ** from the previous row currently stored in a0, a1, a2...
- + */
- + addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
- + if( groupBySort ){
- + sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
- + sortOut, sortPTab);
- + }
- + for(j=0; j<pGroupBy->nExpr; j++){
- + if( groupBySort ){
- + sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
- + }else{
- + sAggInfo.directMode = 1;
- + sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
- + }
- + }
- + sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
- + (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
- + addr1 = sqlite3VdbeCurrentAddr(v);
- + sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v);
- +
- + /* Generate code that runs whenever the GROUP BY changes.
- + ** Changes in the GROUP BY are detected by the previous code
- + ** block. If there were no changes, this block is skipped.
- + **
- + ** This code copies current group by terms in b0,b1,b2,...
- + ** over to a0,a1,a2. It then calls the output subroutine
- + ** and resets the aggregate accumulator registers in preparation
- + ** for the next GROUP BY batch.
- + */
- + sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
- + sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
- + VdbeComment((v, "output one row"));
- + sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
- + VdbeComment((v, "check abort flag"));
- + sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
- + VdbeComment((v, "reset accumulator"));
- +
- + /* Update the aggregate accumulators based on the content of
- + ** the current row
- + */
- + sqlite3VdbeJumpHere(v, addr1);
- + updateAccumulator(pParse, iUseFlag, &sAggInfo);
- + sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
- + VdbeComment((v, "indicate data in accumulator"));
- +
- + /* End of the loop
- + */
- + if( groupBySort ){
- + sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
- + VdbeCoverage(v);
- + }else{
- + sqlite3WhereEnd(pWInfo);
- + sqlite3VdbeChangeToNoop(v, addrSortingIdx);
- + }
- +
- + /* Output the final row of result
- + */
- + sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
- + VdbeComment((v, "output final row"));
- +
- + /* Jump over the subroutines
- + */
- + sqlite3VdbeGoto(v, addrEnd);
- +
- + /* Generate a subroutine that outputs a single row of the result
- + ** set. This subroutine first looks at the iUseFlag. If iUseFlag
- + ** is less than or equal to zero, the subroutine is a no-op. If
- + ** the processing calls for the query to abort, this subroutine
- + ** increments the iAbortFlag memory location before returning in
- + ** order to signal the caller to abort.
- + */
- + addrSetAbort = sqlite3VdbeCurrentAddr(v);
- + sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
- + VdbeComment((v, "set abort flag"));
- + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
- + sqlite3VdbeResolveLabel(v, addrOutputRow);
- + addrOutputRow = sqlite3VdbeCurrentAddr(v);
- + sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
- + VdbeCoverage(v);
- + VdbeComment((v, "Groupby result generator entry point"));
- + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
- + finalizeAggFunctions(pParse, &sAggInfo);
- + sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
- + selectInnerLoop(pParse, p, -1, &sSort,
- + &sDistinct, pDest,
- + addrOutputRow+1, addrSetAbort);
- + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
- + VdbeComment((v, "end groupby result generator"));
- +
- + /* Generate a subroutine that will reset the group-by accumulator
- + */
- + sqlite3VdbeResolveLabel(v, addrReset);
- + resetAccumulator(pParse, &sAggInfo);
- + sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
- + VdbeComment((v, "indicate accumulator empty"));
- + sqlite3VdbeAddOp1(v, OP_Return, regReset);
- +
- + } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
- + else {
- + Table *pTab;
- + if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
- + /* If isSimpleCount() returns a pointer to a Table structure, then
- + ** the SQL statement is of the form:
- + **
- + ** SELECT count(*) FROM <tbl>
- + **
- + ** where the Table structure returned represents table <tbl>.
- + **
- + ** This statement is so common that it is optimized specially. The
- + ** OP_Count instruction is executed either on the intkey table that
- + ** contains the data for table <tbl> or on one of its indexes. It
- + ** is better to execute the op on an index, as indexes are almost
- + ** always spread across less pages than their corresponding tables.
- + */
- + const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
- + const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
- + Index *pIdx; /* Iterator variable */
- + KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
- + Index *pBest = 0; /* Best index found so far */
- + int iRoot = pTab->tnum; /* Root page of scanned b-tree */
- +
- + sqlite3CodeVerifySchema(pParse, iDb);
- + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
- +
- + /* Search for the index that has the lowest scan cost.
- + **
- + ** (2011-04-15) Do not do a full scan of an unordered index.
- + **
- + ** (2013-10-03) Do not count the entries in a partial index.
- + **
- + ** In practice the KeyInfo structure will not be used. It is only
- + ** passed to keep OP_OpenRead happy.
- + */
- + if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
- + if( !p->pSrc->a[0].fg.notIndexed ){
- + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
- + if( pIdx->bUnordered==0
- + && pIdx->szIdxRow<pTab->szTabRow
- + && pIdx->pPartIdxWhere==0
- + && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
- + ){
- + pBest = pIdx;
- + }
- + }
- + }
- + if( pBest ){
- + iRoot = pBest->tnum;
- + pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
- + }
- +
- + /* Open a read-only cursor, execute the OP_Count, close the cursor. */
- + sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
- + if( pKeyInfo ){
- + sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
- + }
- + sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
- + sqlite3VdbeAddOp1(v, OP_Close, iCsr);
- + explainSimpleCount(pParse, pTab, pBest);
- + }else{
- + int regAcc = 0; /* "populate accumulators" flag */
- +
- + /* If there are accumulator registers but no min() or max() functions
- + ** without FILTER clauses, allocate register regAcc. Register regAcc
- + ** will contain 0 the first time the inner loop runs, and 1 thereafter.
- + ** The code generated by updateAccumulator() uses this to ensure
- + ** that the accumulator registers are (a) updated only once if
- + ** there are no min() or max functions or (b) always updated for the
- + ** first row visited by the aggregate, so that they are updated at
- + ** least once even if the FILTER clause means the min() or max()
- + ** function visits zero rows. */
- + if( sAggInfo.nAccumulator ){
- + for(i=0; i<sAggInfo.nFunc; i++){
- + if( ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_WinFunc) ) continue;
- + if( sAggInfo.aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ) break;
- + }
- + if( i==sAggInfo.nFunc ){
- + regAcc = ++pParse->nMem;
- + sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc);
- + }
- + }
- +
- + /* This case runs if the aggregate has no GROUP BY clause. The
- + ** processing is much simpler since there is only a single row
- + ** of output.
- + */
- + assert( p->pGroupBy==0 );
- + resetAccumulator(pParse, &sAggInfo);
- +
- + /* If this query is a candidate for the min/max optimization, then
- + ** minMaxFlag will have been previously set to either
- + ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
- + ** be an appropriate ORDER BY expression for the optimization.
- + */
- + assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
- + assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
- +
- + SELECTTRACE(1,pParse,p,("WhereBegin\n"));
- + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
- + 0, minMaxFlag, 0);
- + if( pWInfo==0 ){
- + goto select_end;
- + }
- + updateAccumulator(pParse, regAcc, &sAggInfo);
- + if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
- + if( sqlite3WhereIsOrdered(pWInfo)>0 ){
- + sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
- + VdbeComment((v, "%s() by index",
- + (minMaxFlag==WHERE_ORDERBY_MIN?"min":"max")));
- + }
- + sqlite3WhereEnd(pWInfo);
- + finalizeAggFunctions(pParse, &sAggInfo);
- + }
- +
- + sSort.pOrderBy = 0;
- + sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
- + selectInnerLoop(pParse, p, -1, 0, 0,
- + pDest, addrEnd, addrEnd);
- + }
- + sqlite3VdbeResolveLabel(v, addrEnd);
- +
- + } /* endif aggregate query */
- +
- + if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
- + explainTempTable(pParse, "DISTINCT");
- + }
- +
- + /* If there is an ORDER BY clause, then we need to sort the results
- + ** and send them to the callback one by one.
- + */
- + if( sSort.pOrderBy ){
- + explainTempTable(pParse,
- + sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
- + assert( p->pEList==pEList );
- + generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
- + }
- +
- + /* Jump here to skip this query
- + */
- + sqlite3VdbeResolveLabel(v, iEnd);
- +
- + /* The SELECT has been coded. If there is an error in the Parse structure,
- + ** set the return code to 1. Otherwise 0. */
- + rc = (pParse->nErr>0);
- +
- + /* Control jumps to here if an error is encountered above, or upon
- + ** successful coding of the SELECT.
- + */
- +select_end:
- + sqlite3ExprListDelete(db, pMinMaxOrderBy);
- + sqlite3DbFree(db, sAggInfo.aCol);
- +#ifdef SQLITE_DEBUG
- + for(i=0; i<sAggInfo.nFunc; i++){
- + assert( sAggInfo.aFunc[i].pExpr!=0 );
- + assert( sAggInfo.aFunc[i].pExpr->pAggInfo==&sAggInfo );
- + sAggInfo.aFunc[i].pExpr->pAggInfo = 0;
- + }
- + sAggInfo.iAggMagic = 0;
- +#endif
- + sqlite3DbFree(db, sAggInfo.aFunc);
- +#if SELECTTRACE_ENABLED
- + SELECTTRACE(0x1,pParse,p,("end processing\n"));
- + if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
- + sqlite3TreeViewSelect(0, p, 0);
- + }
- +#endif
- + ExplainQueryPlanPop(pParse);
- + return rc;
- +}
- diff -Npur sqlite-version-3.32.2/src/sqliteInt.h sqlite-version-3.32.2-patched/src/sqliteInt.h
- --- sqlite-version-3.32.2/src/sqliteInt.h 2020-06-04 20:58:43.000000000 +0800
- +++ sqlite-version-3.32.2-patched/src/sqliteInt.h 2020-07-08 10:00:50.899152517 +0800
- @@ -976,7 +976,12 @@ typedef INT16_TYPE LogEst;
- */
- #if defined(SQLITE_ENABLE_SELECTTRACE)
- # define SELECTTRACE_ENABLED 1
- +# define SELECTTRACE(K,P,S,X) \
- + if(sqlite3SelectTrace&(K)) \
- + sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
- + sqlite3DebugPrintf X
- #else
- +# define SELECTTRACE(K,P,S,X)
- # define SELECTTRACE_ENABLED 0
- #endif
-
- @@ -2523,9 +2528,24 @@ struct AggInfo {
- int iDistinct; /* Ephemeral table used to enforce DISTINCT */
- } *aFunc;
- int nFunc; /* Number of entries in aFunc[] */
- +#ifdef SQLITE_DEBUG
- + u32 iAggMagic; /* Sanity checking constant */
- +#endif
- };
-
- /*
- +** Allowed values for AggInfo.iAggMagic
- +*/
- +#define SQLITE_AGGMAGIC_VALID 0x05cadade
- +
- +/*
- +** True if the AggInfo object is valid. Used inside of assert() only.
- +*/
- +#ifdef SQLITE_DEBUG
- +# define AggInfoValid(P) ((P)->iAggMagic==SQLITE_AGGMAGIC_VALID)
- +#endif
- +
- +/*
- ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
- ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
- ** than 32767 we have to make it 32-bit. 16-bit is preferred because
- @@ -3105,6 +3125,7 @@ struct Select {
- #define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
- #define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
- #define SF_View 0x0200000 /* SELECT statement is a view */
- +#define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */
-
- /*
- ** The results of a SELECT can be distributed in several ways, as defined
- @@ -4546,10 +4567,11 @@ extern const unsigned char sqlite3UpperT
- extern const unsigned char sqlite3CtypeMap[];
- extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
- extern FuncDefHash sqlite3BuiltinFunctions;
- +extern u32 sqlite3SelectTrace;
- #ifndef SQLITE_OMIT_WSD
- extern int sqlite3PendingByte;
- #endif
- -#endif
- +#endif /* !defined(SQLITE_AMALGAMATION) */
- #ifdef VDBE_PROFILE
- extern sqlite3_uint64 sqlite3NProfileCnt;
- #endif
- diff -Npur sqlite-version-3.32.2/src/sqliteInt.h.orig sqlite-version-3.32.2-patched/src/sqliteInt.h.orig
- --- sqlite-version-3.32.2/src/sqliteInt.h.orig 1970-01-01 08:00:00.000000000 +0800
- +++ sqlite-version-3.32.2-patched/src/sqliteInt.h.orig 2020-07-08 10:00:47.367088648 +0800
- @@ -0,0 +1,4977 @@
- +/*
- +** 2001 September 15
- +**
- +** The author disclaims copyright to this source code. In place of
- +** a legal notice, here is a blessing:
- +**
- +** May you do good and not evil.
- +** May you find forgiveness for yourself and forgive others.
- +** May you share freely, never taking more than you give.
- +**
- +*************************************************************************
- +** Internal interface definitions for SQLite.
- +**
- +*/
- +#ifndef SQLITEINT_H
- +#define SQLITEINT_H
- +
- +/* Special Comments:
- +**
- +** Some comments have special meaning to the tools that measure test
- +** coverage:
- +**
- +** NO_TEST - The branches on this line are not
- +** measured by branch coverage. This is
- +** used on lines of code that actually
- +** implement parts of coverage testing.
- +**
- +** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
- +** and the correct answer is still obtained,
- +** though perhaps more slowly.
- +**
- +** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
- +** and the correct answer is still obtained,
- +** though perhaps more slowly.
- +**
- +** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
- +** that would be harmless and undetectable
- +** if it did occur.
- +**
- +** In all cases, the special comment must be enclosed in the usual
- +** slash-asterisk...asterisk-slash comment marks, with no spaces between the
- +** asterisks and the comment text.
- +*/
- +
- +/*
- +** Make sure the Tcl calling convention macro is defined. This macro is
- +** only used by test code and Tcl integration code.
- +*/
- +#ifndef SQLITE_TCLAPI
- +# define SQLITE_TCLAPI
- +#endif
- +
- +/*
- +** Include the header file used to customize the compiler options for MSVC.
- +** This should be done first so that it can successfully prevent spurious
- +** compiler warnings due to subsequent content in this file and other files
- +** that are included by this file.
- +*/
- +#include "msvc.h"
- +
- +/*
- +** Special setup for VxWorks
- +*/
- +#include "vxworks.h"
- +
- +/*
- +** These #defines should enable >2GB file support on POSIX if the
- +** underlying operating system supports it. If the OS lacks
- +** large file support, or if the OS is windows, these should be no-ops.
- +**
- +** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
- +** system #includes. Hence, this block of code must be the very first
- +** code in all source files.
- +**
- +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
- +** on the compiler command line. This is necessary if you are compiling
- +** on a recent machine (ex: Red Hat 7.2) but you want your code to work
- +** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
- +** without this option, LFS is enable. But LFS does not exist in the kernel
- +** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
- +** portability you should omit LFS.
- +**
- +** The previous paragraph was written in 2005. (This paragraph is written
- +** on 2008-11-28.) These days, all Linux kernels support large files, so
- +** you should probably leave LFS enabled. But some embedded platforms might
- +** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
- +**
- +** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
- +*/
- +#ifndef SQLITE_DISABLE_LFS
- +# define _LARGE_FILE 1
- +# ifndef _FILE_OFFSET_BITS
- +# define _FILE_OFFSET_BITS 64
- +# endif
- +# define _LARGEFILE_SOURCE 1
- +#endif
- +
- +/* The GCC_VERSION and MSVC_VERSION macros are used to
- +** conditionally include optimizations for each of these compilers. A
- +** value of 0 means that compiler is not being used. The
- +** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
- +** optimizations, and hence set all compiler macros to 0
- +**
- +** There was once also a CLANG_VERSION macro. However, we learn that the
- +** version numbers in clang are for "marketing" only and are inconsistent
- +** and unreliable. Fortunately, all versions of clang also recognize the
- +** gcc version numbers and have reasonable settings for gcc version numbers,
- +** so the GCC_VERSION macro will be set to a correct non-zero value even
- +** when compiling with clang.
- +*/
- +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
- +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
- +#else
- +# define GCC_VERSION 0
- +#endif
- +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
- +# define MSVC_VERSION _MSC_VER
- +#else
- +# define MSVC_VERSION 0
- +#endif
- +
- +/* Needed for various definitions... */
- +#if defined(__GNUC__) && !defined(_GNU_SOURCE)
- +# define _GNU_SOURCE
- +#endif
- +
- +#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
- +# define _BSD_SOURCE
- +#endif
- +
- +/*
- +** For MinGW, check to see if we can include the header file containing its
- +** version information, among other things. Normally, this internal MinGW
- +** header file would [only] be included automatically by other MinGW header
- +** files; however, the contained version information is now required by this
- +** header file to work around binary compatibility issues (see below) and
- +** this is the only known way to reliably obtain it. This entire #if block
- +** would be completely unnecessary if there was any other way of detecting
- +** MinGW via their preprocessor (e.g. if they customized their GCC to define
- +** some MinGW-specific macros). When compiling for MinGW, either the
- +** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
- +** defined; otherwise, detection of conditions specific to MinGW will be
- +** disabled.
- +*/
- +#if defined(_HAVE_MINGW_H)
- +# include "mingw.h"
- +#elif defined(_HAVE__MINGW_H)
- +# include "_mingw.h"
- +#endif
- +
- +/*
- +** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
- +** define is required to maintain binary compatibility with the MSVC runtime
- +** library in use (e.g. for Windows XP).
- +*/
- +#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
- + defined(_WIN32) && !defined(_WIN64) && \
- + defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
- + defined(__MSVCRT__)
- +# define _USE_32BIT_TIME_T
- +#endif
- +
- +/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
- +** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
- +** MinGW.
- +*/
- +#include "sqlite3.h"
- +
- +/*
- +** Include the configuration header output by 'configure' if we're using the
- +** autoconf-based build
- +*/
- +#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
- +#include "config.h"
- +#define SQLITECONFIG_H 1
- +#endif
- +
- +#include "sqliteLimit.h"
- +
- +/* Disable nuisance warnings on Borland compilers */
- +#if defined(__BORLANDC__)
- +#pragma warn -rch /* unreachable code */
- +#pragma warn -ccc /* Condition is always true or false */
- +#pragma warn -aus /* Assigned value is never used */
- +#pragma warn -csu /* Comparing signed and unsigned */
- +#pragma warn -spa /* Suspicious pointer arithmetic */
- +#endif
- +
- +/*
- +** WAL mode depends on atomic aligned 32-bit loads and stores in a few
- +** places. The following macros try to make this explicit.
- +*/
- +#ifndef __has_feature
- +# define __has_feature(x) 0 /* compatibility with non-clang compilers */
- +#endif
- +#if GCC_VERSION>=4007000 || __has_feature(c_atomic)
- +# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED)
- +# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)
- +#else
- +# define AtomicLoad(PTR) (*(PTR))
- +# define AtomicStore(PTR,VAL) (*(PTR) = (VAL))
- +#endif
- +
- +/*
- +** Include standard header files as necessary
- +*/
- +#ifdef HAVE_STDINT_H
- +#include <stdint.h>
- +#endif
- +#ifdef HAVE_INTTYPES_H
- +#include <inttypes.h>
- +#endif
- +
- +/*
- +** The following macros are used to cast pointers to integers and
- +** integers to pointers. The way you do this varies from one compiler
- +** to the next, so we have developed the following set of #if statements
- +** to generate appropriate macros for a wide range of compilers.
- +**
- +** The correct "ANSI" way to do this is to use the intptr_t type.
- +** Unfortunately, that typedef is not available on all compilers, or
- +** if it is available, it requires an #include of specific headers
- +** that vary from one machine to the next.
- +**
- +** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
- +** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
- +** So we have to define the macros in different ways depending on the
- +** compiler.
- +*/
- +#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
- +# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
- +# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
- +#elif defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
- +# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
- +# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
- +#elif !defined(__GNUC__) /* Works for compilers other than LLVM */
- +# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
- +# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
- +#else /* Generates a warning - but it always works */
- +# define SQLITE_INT_TO_PTR(X) ((void*)(X))
- +# define SQLITE_PTR_TO_INT(X) ((int)(X))
- +#endif
- +
- +/*
- +** A macro to hint to the compiler that a function should not be
- +** inlined.
- +*/
- +#if defined(__GNUC__)
- +# define SQLITE_NOINLINE __attribute__((noinline))
- +#elif defined(_MSC_VER) && _MSC_VER>=1310
- +# define SQLITE_NOINLINE __declspec(noinline)
- +#else
- +# define SQLITE_NOINLINE
- +#endif
- +
- +/*
- +** Make sure that the compiler intrinsics we desire are enabled when
- +** compiling with an appropriate version of MSVC unless prevented by
- +** the SQLITE_DISABLE_INTRINSIC define.
- +*/
- +#if !defined(SQLITE_DISABLE_INTRINSIC)
- +# if defined(_MSC_VER) && _MSC_VER>=1400
- +# if !defined(_WIN32_WCE)
- +# include <intrin.h>
- +# pragma intrinsic(_byteswap_ushort)
- +# pragma intrinsic(_byteswap_ulong)
- +# pragma intrinsic(_byteswap_uint64)
- +# pragma intrinsic(_ReadWriteBarrier)
- +# else
- +# include <cmnintrin.h>
- +# endif
- +# endif
- +#endif
- +
- +/*
- +** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
- +** 0 means mutexes are permanently disable and the library is never
- +** threadsafe. 1 means the library is serialized which is the highest
- +** level of threadsafety. 2 means the library is multithreaded - multiple
- +** threads can use SQLite as long as no two threads try to use the same
- +** database connection at the same time.
- +**
- +** Older versions of SQLite used an optional THREADSAFE macro.
- +** We support that for legacy.
- +**
- +** To ensure that the correct value of "THREADSAFE" is reported when querying
- +** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
- +** logic is partially replicated in ctime.c. If it is updated here, it should
- +** also be updated there.
- +*/
- +#if !defined(SQLITE_THREADSAFE)
- +# if defined(THREADSAFE)
- +# define SQLITE_THREADSAFE THREADSAFE
- +# else
- +# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
- +# endif
- +#endif
- +
- +/*
- +** Powersafe overwrite is on by default. But can be turned off using
- +** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
- +*/
- +#ifndef SQLITE_POWERSAFE_OVERWRITE
- +# define SQLITE_POWERSAFE_OVERWRITE 1
- +#endif
- +
- +/*
- +** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
- +** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
- +** which case memory allocation statistics are disabled by default.
- +*/
- +#if !defined(SQLITE_DEFAULT_MEMSTATUS)
- +# define SQLITE_DEFAULT_MEMSTATUS 1
- +#endif
- +
- +/*
- +** Exactly one of the following macros must be defined in order to
- +** specify which memory allocation subsystem to use.
- +**
- +** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
- +** SQLITE_WIN32_MALLOC // Use Win32 native heap API
- +** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
- +** SQLITE_MEMDEBUG // Debugging version of system malloc()
- +**
- +** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
- +** assert() macro is enabled, each call into the Win32 native heap subsystem
- +** will cause HeapValidate to be called. If heap validation should fail, an
- +** assertion will be triggered.
- +**
- +** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
- +** the default.
- +*/
- +#if defined(SQLITE_SYSTEM_MALLOC) \
- + + defined(SQLITE_WIN32_MALLOC) \
- + + defined(SQLITE_ZERO_MALLOC) \
- + + defined(SQLITE_MEMDEBUG)>1
- +# error "Two or more of the following compile-time configuration options\
- + are defined but at most one is allowed:\
- + SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
- + SQLITE_ZERO_MALLOC"
- +#endif
- +#if defined(SQLITE_SYSTEM_MALLOC) \
- + + defined(SQLITE_WIN32_MALLOC) \
- + + defined(SQLITE_ZERO_MALLOC) \
- + + defined(SQLITE_MEMDEBUG)==0
- +# define SQLITE_SYSTEM_MALLOC 1
- +#endif
- +
- +/*
- +** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
- +** sizes of memory allocations below this value where possible.
- +*/
- +#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
- +# define SQLITE_MALLOC_SOFT_LIMIT 1024
- +#endif
- +
- +/*
- +** We need to define _XOPEN_SOURCE as follows in order to enable
- +** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
- +** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
- +** it.
- +*/
- +#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
- +# define _XOPEN_SOURCE 600
- +#endif
- +
- +/*
- +** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
- +** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
- +** make it true by defining or undefining NDEBUG.
- +**
- +** Setting NDEBUG makes the code smaller and faster by disabling the
- +** assert() statements in the code. So we want the default action
- +** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
- +** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
- +** feature.
- +*/
- +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
- +# define NDEBUG 1
- +#endif
- +#if defined(NDEBUG) && defined(SQLITE_DEBUG)
- +# undef NDEBUG
- +#endif
- +
- +/*
- +** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
- +*/
- +#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
- +# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
- +#endif
- +
- +/*
- +** The testcase() macro is used to aid in coverage testing. When
- +** doing coverage testing, the condition inside the argument to
- +** testcase() must be evaluated both true and false in order to
- +** get full branch coverage. The testcase() macro is inserted
- +** to help ensure adequate test coverage in places where simple
- +** condition/decision coverage is inadequate. For example, testcase()
- +** can be used to make sure boundary values are tested. For
- +** bitmask tests, testcase() can be used to make sure each bit
- +** is significant and used at least once. On switch statements
- +** where multiple cases go to the same block of code, testcase()
- +** can insure that all cases are evaluated.
- +**
- +*/
- +#ifdef SQLITE_COVERAGE_TEST
- + void sqlite3Coverage(int);
- +# define testcase(X) if( X ){ sqlite3Coverage(__LINE__); }
- +#else
- +# define testcase(X)
- +#endif
- +
- +/*
- +** The TESTONLY macro is used to enclose variable declarations or
- +** other bits of code that are needed to support the arguments
- +** within testcase() and assert() macros.
- +*/
- +#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
- +# define TESTONLY(X) X
- +#else
- +# define TESTONLY(X)
- +#endif
- +
- +/*
- +** Sometimes we need a small amount of code such as a variable initialization
- +** to setup for a later assert() statement. We do not want this code to
- +** appear when assert() is disabled. The following macro is therefore
- +** used to contain that setup code. The "VVA" acronym stands for
- +** "Verification, Validation, and Accreditation". In other words, the
- +** code within VVA_ONLY() will only run during verification processes.
- +*/
- +#ifndef NDEBUG
- +# define VVA_ONLY(X) X
- +#else
- +# define VVA_ONLY(X)
- +#endif
- +
- +/*
- +** The ALWAYS and NEVER macros surround boolean expressions which
- +** are intended to always be true or false, respectively. Such
- +** expressions could be omitted from the code completely. But they
- +** are included in a few cases in order to enhance the resilience
- +** of SQLite to unexpected behavior - to make the code "self-healing"
- +** or "ductile" rather than being "brittle" and crashing at the first
- +** hint of unplanned behavior.
- +**
- +** In other words, ALWAYS and NEVER are added for defensive code.
- +**
- +** When doing coverage testing ALWAYS and NEVER are hard-coded to
- +** be true and false so that the unreachable code they specify will
- +** not be counted as untested code.
- +*/
- +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
- +# define ALWAYS(X) (1)
- +# define NEVER(X) (0)
- +#elif !defined(NDEBUG)
- +# define ALWAYS(X) ((X)?1:(assert(0),0))
- +# define NEVER(X) ((X)?(assert(0),1):0)
- +#else
- +# define ALWAYS(X) (X)
- +# define NEVER(X) (X)
- +#endif
- +
- +/*
- +** The harmless(X) macro indicates that expression X is usually false
- +** but can be true without causing any problems, but we don't know of
- +** any way to cause X to be true.
- +**
- +** In debugging and testing builds, this macro will abort if X is ever
- +** true. In this way, developers are alerted to a possible test case
- +** that causes X to be true. If a harmless macro ever fails, that is
- +** an opportunity to change the macro into a testcase() and add a new
- +** test case to the test suite.
- +**
- +** For normal production builds, harmless(X) is a no-op, since it does
- +** not matter whether expression X is true or false.
- +*/
- +#ifdef SQLITE_DEBUG
- +# define harmless(X) assert(!(X));
- +#else
- +# define harmless(X)
- +#endif
- +
- +/*
- +** Some conditionals are optimizations only. In other words, if the
- +** conditionals are replaced with a constant 1 (true) or 0 (false) then
- +** the correct answer is still obtained, though perhaps not as quickly.
- +**
- +** The following macros mark these optimizations conditionals.
- +*/
- +#if defined(SQLITE_MUTATION_TEST)
- +# define OK_IF_ALWAYS_TRUE(X) (1)
- +# define OK_IF_ALWAYS_FALSE(X) (0)
- +#else
- +# define OK_IF_ALWAYS_TRUE(X) (X)
- +# define OK_IF_ALWAYS_FALSE(X) (X)
- +#endif
- +
- +/*
- +** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
- +** defined. We need to defend against those failures when testing with
- +** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
- +** during a normal build. The following macro can be used to disable tests
- +** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
- +*/
- +#if defined(SQLITE_TEST_REALLOC_STRESS)
- +# define ONLY_IF_REALLOC_STRESS(X) (X)
- +#elif !defined(NDEBUG)
- +# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0)
- +#else
- +# define ONLY_IF_REALLOC_STRESS(X) (0)
- +#endif
- +
- +/*
- +** Declarations used for tracing the operating system interfaces.
- +*/
- +#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
- + (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
- + extern int sqlite3OSTrace;
- +# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
- +# define SQLITE_HAVE_OS_TRACE
- +#else
- +# define OSTRACE(X)
- +# undef SQLITE_HAVE_OS_TRACE
- +#endif
- +
- +/*
- +** Is the sqlite3ErrName() function needed in the build? Currently,
- +** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
- +** OSTRACE is enabled), and by several "test*.c" files (which are
- +** compiled using SQLITE_TEST).
- +*/
- +#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
- + (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
- +# define SQLITE_NEED_ERR_NAME
- +#else
- +# undef SQLITE_NEED_ERR_NAME
- +#endif
- +
- +/*
- +** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
- +*/
- +#ifdef SQLITE_OMIT_EXPLAIN
- +# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
- +#endif
- +
- +/*
- +** Return true (non-zero) if the input is an integer that is too large
- +** to fit in 32-bits. This macro is used inside of various testcase()
- +** macros to verify that we have tested SQLite for large-file support.
- +*/
- +#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
- +
- +/*
- +** The macro unlikely() is a hint that surrounds a boolean
- +** expression that is usually false. Macro likely() surrounds
- +** a boolean expression that is usually true. These hints could,
- +** in theory, be used by the compiler to generate better code, but
- +** currently they are just comments for human readers.
- +*/
- +#define likely(X) (X)
- +#define unlikely(X) (X)
- +
- +#include "hash.h"
- +#include "parse.h"
- +#include <stdio.h>
- +#include <stdlib.h>
- +#include <string.h>
- +#include <assert.h>
- +#include <stddef.h>
- +
- +/*
- +** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
- +** This allows better measurements of where memcpy() is used when running
- +** cachegrind. But this macro version of memcpy() is very slow so it
- +** should not be used in production. This is a performance measurement
- +** hack only.
- +*/
- +#ifdef SQLITE_INLINE_MEMCPY
- +# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
- + int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
- +#endif
- +
- +/*
- +** If compiling for a processor that lacks floating point support,
- +** substitute integer for floating-point
- +*/
- +#ifdef SQLITE_OMIT_FLOATING_POINT
- +# define double sqlite_int64
- +# define float sqlite_int64
- +# define LONGDOUBLE_TYPE sqlite_int64
- +# ifndef SQLITE_BIG_DBL
- +# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
- +# endif
- +# define SQLITE_OMIT_DATETIME_FUNCS 1
- +# define SQLITE_OMIT_TRACE 1
- +# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
- +# undef SQLITE_HAVE_ISNAN
- +#endif
- +#ifndef SQLITE_BIG_DBL
- +# define SQLITE_BIG_DBL (1e99)
- +#endif
- +
- +/*
- +** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
- +** afterward. Having this macro allows us to cause the C compiler
- +** to omit code used by TEMP tables without messy #ifndef statements.
- +*/
- +#ifdef SQLITE_OMIT_TEMPDB
- +#define OMIT_TEMPDB 1
- +#else
- +#define OMIT_TEMPDB 0
- +#endif
- +
- +/*
- +** The "file format" number is an integer that is incremented whenever
- +** the VDBE-level file format changes. The following macros define the
- +** the default file format for new databases and the maximum file format
- +** that the library can read.
- +*/
- +#define SQLITE_MAX_FILE_FORMAT 4
- +#ifndef SQLITE_DEFAULT_FILE_FORMAT
- +# define SQLITE_DEFAULT_FILE_FORMAT 4
- +#endif
- +
- +/*
- +** Determine whether triggers are recursive by default. This can be
- +** changed at run-time using a pragma.
- +*/
- +#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
- +# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
- +#endif
- +
- +/*
- +** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
- +** on the command-line
- +*/
- +#ifndef SQLITE_TEMP_STORE
- +# define SQLITE_TEMP_STORE 1
- +#endif
- +
- +/*
- +** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
- +** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
- +** to zero.
- +*/
- +#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
- +# undef SQLITE_MAX_WORKER_THREADS
- +# define SQLITE_MAX_WORKER_THREADS 0
- +#endif
- +#ifndef SQLITE_MAX_WORKER_THREADS
- +# define SQLITE_MAX_WORKER_THREADS 8
- +#endif
- +#ifndef SQLITE_DEFAULT_WORKER_THREADS
- +# define SQLITE_DEFAULT_WORKER_THREADS 0
- +#endif
- +#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS
- +# undef SQLITE_MAX_WORKER_THREADS
- +# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
- +#endif
- +
- +/*
- +** The default initial allocation for the pagecache when using separate
- +** pagecaches for each database connection. A positive number is the
- +** number of pages. A negative number N translations means that a buffer
- +** of -1024*N bytes is allocated and used for as many pages as it will hold.
- +**
- +** The default value of "20" was choosen to minimize the run-time of the
- +** speedtest1 test program with options: --shrink-memory --reprepare
- +*/
- +#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
- +# define SQLITE_DEFAULT_PCACHE_INITSZ 20
- +#endif
- +
- +/*
- +** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
- +*/
- +#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
- +# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
- +#endif
- +
- +/*
- +** The compile-time options SQLITE_MMAP_READWRITE and
- +** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
- +** You must choose one or the other (or neither) but not both.
- +*/
- +#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
- +#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
- +#endif
- +
- +/*
- +** GCC does not define the offsetof() macro so we'll have to do it
- +** ourselves.
- +*/
- +#ifndef offsetof
- +#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
- +#endif
- +
- +/*
- +** Macros to compute minimum and maximum of two numbers.
- +*/
- +#ifndef MIN
- +# define MIN(A,B) ((A)<(B)?(A):(B))
- +#endif
- +#ifndef MAX
- +# define MAX(A,B) ((A)>(B)?(A):(B))
- +#endif
- +
- +/*
- +** Swap two objects of type TYPE.
- +*/
- +#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
- +
- +/*
- +** Check to see if this machine uses EBCDIC. (Yes, believe it or
- +** not, there are still machines out there that use EBCDIC.)
- +*/
- +#if 'A' == '\301'
- +# define SQLITE_EBCDIC 1
- +#else
- +# define SQLITE_ASCII 1
- +#endif
- +
- +/*
- +** Integers of known sizes. These typedefs might change for architectures
- +** where the sizes very. Preprocessor macros are available so that the
- +** types can be conveniently redefined at compile-type. Like this:
- +**
- +** cc '-DUINTPTR_TYPE=long long int' ...
- +*/
- +#ifndef UINT32_TYPE
- +# ifdef HAVE_UINT32_T
- +# define UINT32_TYPE uint32_t
- +# else
- +# define UINT32_TYPE unsigned int
- +# endif
- +#endif
- +#ifndef UINT16_TYPE
- +# ifdef HAVE_UINT16_T
- +# define UINT16_TYPE uint16_t
- +# else
- +# define UINT16_TYPE unsigned short int
- +# endif
- +#endif
- +#ifndef INT16_TYPE
- +# ifdef HAVE_INT16_T
- +# define INT16_TYPE int16_t
- +# else
- +# define INT16_TYPE short int
- +# endif
- +#endif
- +#ifndef UINT8_TYPE
- +# ifdef HAVE_UINT8_T
- +# define UINT8_TYPE uint8_t
- +# else
- +# define UINT8_TYPE unsigned char
- +# endif
- +#endif
- +#ifndef INT8_TYPE
- +# ifdef HAVE_INT8_T
- +# define INT8_TYPE int8_t
- +# else
- +# define INT8_TYPE signed char
- +# endif
- +#endif
- +#ifndef LONGDOUBLE_TYPE
- +# define LONGDOUBLE_TYPE long double
- +#endif
- +typedef sqlite_int64 i64; /* 8-byte signed integer */
- +typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
- +typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
- +typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
- +typedef INT16_TYPE i16; /* 2-byte signed integer */
- +typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
- +typedef INT8_TYPE i8; /* 1-byte signed integer */
- +
- +/*
- +** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
- +** that can be stored in a u32 without loss of data. The value
- +** is 0x00000000ffffffff. But because of quirks of some compilers, we
- +** have to specify the value in the less intuitive manner shown:
- +*/
- +#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
- +
- +/*
- +** The datatype used to store estimates of the number of rows in a
- +** table or index. This is an unsigned integer type. For 99.9% of
- +** the world, a 32-bit integer is sufficient. But a 64-bit integer
- +** can be used at compile-time if desired.
- +*/
- +#ifdef SQLITE_64BIT_STATS
- + typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
- +#else
- + typedef u32 tRowcnt; /* 32-bit is the default */
- +#endif
- +
- +/*
- +** Estimated quantities used for query planning are stored as 16-bit
- +** logarithms. For quantity X, the value stored is 10*log2(X). This
- +** gives a possible range of values of approximately 1.0e986 to 1e-986.
- +** But the allowed values are "grainy". Not every value is representable.
- +** For example, quantities 16 and 17 are both represented by a LogEst
- +** of 40. However, since LogEst quantities are suppose to be estimates,
- +** not exact values, this imprecision is not a problem.
- +**
- +** "LogEst" is short for "Logarithmic Estimate".
- +**
- +** Examples:
- +** 1 -> 0 20 -> 43 10000 -> 132
- +** 2 -> 10 25 -> 46 25000 -> 146
- +** 3 -> 16 100 -> 66 1000000 -> 199
- +** 4 -> 20 1000 -> 99 1048576 -> 200
- +** 10 -> 33 1024 -> 100 4294967296 -> 320
- +**
- +** The LogEst can be negative to indicate fractional values.
- +** Examples:
- +**
- +** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
- +*/
- +typedef INT16_TYPE LogEst;
- +
- +/*
- +** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
- +*/
- +#ifndef SQLITE_PTRSIZE
- +# if defined(__SIZEOF_POINTER__)
- +# define SQLITE_PTRSIZE __SIZEOF_POINTER__
- +# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
- + defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
- + (defined(__TOS_AIX__) && !defined(__64BIT__))
- +# define SQLITE_PTRSIZE 4
- +# else
- +# define SQLITE_PTRSIZE 8
- +# endif
- +#endif
- +
- +/* The uptr type is an unsigned integer large enough to hold a pointer
- +*/
- +#if defined(HAVE_STDINT_H)
- + typedef uintptr_t uptr;
- +#elif SQLITE_PTRSIZE==4
- + typedef u32 uptr;
- +#else
- + typedef u64 uptr;
- +#endif
- +
- +/*
- +** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
- +** something between S (inclusive) and E (exclusive).
- +**
- +** In other words, S is a buffer and E is a pointer to the first byte after
- +** the end of buffer S. This macro returns true if P points to something
- +** contained within the buffer S.
- +*/
- +#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
- +
- +
- +/*
- +** Macros to determine whether the machine is big or little endian,
- +** and whether or not that determination is run-time or compile-time.
- +**
- +** For best performance, an attempt is made to guess at the byte-order
- +** using C-preprocessor macros. If that is unsuccessful, or if
- +** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
- +** at run-time.
- +*/
- +#ifndef SQLITE_BYTEORDER
- +# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
- + defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
- + defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
- + defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
- +# define SQLITE_BYTEORDER 1234
- +# elif defined(sparc) || defined(__ppc__) || \
- + defined(__ARMEB__) || defined(__AARCH64EB__)
- +# define SQLITE_BYTEORDER 4321
- +# else
- +# define SQLITE_BYTEORDER 0
- +# endif
- +#endif
- +#if SQLITE_BYTEORDER==4321
- +# define SQLITE_BIGENDIAN 1
- +# define SQLITE_LITTLEENDIAN 0
- +# define SQLITE_UTF16NATIVE SQLITE_UTF16BE
- +#elif SQLITE_BYTEORDER==1234
- +# define SQLITE_BIGENDIAN 0
- +# define SQLITE_LITTLEENDIAN 1
- +# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
- +#else
- +# ifdef SQLITE_AMALGAMATION
- + const int sqlite3one = 1;
- +# else
- + extern const int sqlite3one;
- +# endif
- +# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
- +# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
- +# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
- +#endif
- +
- +/*
- +** Constants for the largest and smallest possible 64-bit signed integers.
- +** These macros are designed to work correctly on both 32-bit and 64-bit
- +** compilers.
- +*/
- +#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
- +#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
- +
- +/*
- +** Round up a number to the next larger multiple of 8. This is used
- +** to force 8-byte alignment on 64-bit architectures.
- +*/
- +#define ROUND8(x) (((x)+7)&~7)
- +
- +/*
- +** Round down to the nearest multiple of 8
- +*/
- +#define ROUNDDOWN8(x) ((x)&~7)
- +
- +/*
- +** Assert that the pointer X is aligned to an 8-byte boundary. This
- +** macro is used only within assert() to verify that the code gets
- +** all alignment restrictions correct.
- +**
- +** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
- +** underlying malloc() implementation might return us 4-byte aligned
- +** pointers. In that case, only verify 4-byte alignment.
- +*/
- +#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
- +# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
- +#else
- +# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
- +#endif
- +
- +/*
- +** Disable MMAP on platforms where it is known to not work
- +*/
- +#if defined(__OpenBSD__) || defined(__QNXNTO__)
- +# undef SQLITE_MAX_MMAP_SIZE
- +# define SQLITE_MAX_MMAP_SIZE 0
- +#endif
- +
- +/*
- +** Default maximum size of memory used by memory-mapped I/O in the VFS
- +*/
- +#ifdef __APPLE__
- +# include <TargetConditionals.h>
- +#endif
- +#ifndef SQLITE_MAX_MMAP_SIZE
- +# if defined(__linux__) \
- + || defined(_WIN32) \
- + || (defined(__APPLE__) && defined(__MACH__)) \
- + || defined(__sun) \
- + || defined(__FreeBSD__) \
- + || defined(__DragonFly__)
- +# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
- +# else
- +# define SQLITE_MAX_MMAP_SIZE 0
- +# endif
- +#endif
- +
- +/*
- +** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
- +** default MMAP_SIZE is specified at compile-time, make sure that it does
- +** not exceed the maximum mmap size.
- +*/
- +#ifndef SQLITE_DEFAULT_MMAP_SIZE
- +# define SQLITE_DEFAULT_MMAP_SIZE 0
- +#endif
- +#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
- +# undef SQLITE_DEFAULT_MMAP_SIZE
- +# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
- +#endif
- +
- +/*
- +** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
- +** the Select query generator tracing logic is turned on.
- +*/
- +#if defined(SQLITE_ENABLE_SELECTTRACE)
- +# define SELECTTRACE_ENABLED 1
- +# define SELECTTRACE(K,P,S,X) \
- + if(sqlite3SelectTrace&(K)) \
- + sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
- + sqlite3DebugPrintf X
- +#else
- +# define SELECTTRACE(K,P,S,X)
- +# define SELECTTRACE_ENABLED 0
- +#endif
- +
- +/*
- +** An instance of the following structure is used to store the busy-handler
- +** callback for a given sqlite handle.
- +**
- +** The sqlite.busyHandler member of the sqlite struct contains the busy
- +** callback for the database handle. Each pager opened via the sqlite
- +** handle is passed a pointer to sqlite.busyHandler. The busy-handler
- +** callback is currently invoked only from within pager.c.
- +*/
- +typedef struct BusyHandler BusyHandler;
- +struct BusyHandler {
- + int (*xBusyHandler)(void *,int); /* The busy callback */
- + void *pBusyArg; /* First arg to busy callback */
- + int nBusy; /* Incremented with each busy call */
- +};
- +
- +/*
- +** Name of the master database table. The master database table
- +** is a special table that holds the names and attributes of all
- +** user tables and indices.
- +*/
- +#define MASTER_NAME "sqlite_master"
- +#define TEMP_MASTER_NAME "sqlite_temp_master"
- +
- +/*
- +** The root-page of the master database table.
- +*/
- +#define MASTER_ROOT 1
- +
- +/*
- +** The name of the schema table.
- +*/
- +#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
- +
- +/*
- +** A convenience macro that returns the number of elements in
- +** an array.
- +*/
- +#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
- +
- +/*
- +** Determine if the argument is a power of two
- +*/
- +#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
- +
- +/*
- +** The following value as a destructor means to use sqlite3DbFree().
- +** The sqlite3DbFree() routine requires two parameters instead of the
- +** one parameter that destructors normally want. So we have to introduce
- +** this magic value that the code knows to handle differently. Any
- +** pointer will work here as long as it is distinct from SQLITE_STATIC
- +** and SQLITE_TRANSIENT.
- +*/
- +#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3MallocSize)
- +
- +/*
- +** When SQLITE_OMIT_WSD is defined, it means that the target platform does
- +** not support Writable Static Data (WSD) such as global and static variables.
- +** All variables must either be on the stack or dynamically allocated from
- +** the heap. When WSD is unsupported, the variable declarations scattered
- +** throughout the SQLite code must become constants instead. The SQLITE_WSD
- +** macro is used for this purpose. And instead of referencing the variable
- +** directly, we use its constant as a key to lookup the run-time allocated
- +** buffer that holds real variable. The constant is also the initializer
- +** for the run-time allocated buffer.
- +**
- +** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
- +** macros become no-ops and have zero performance impact.
- +*/
- +#ifdef SQLITE_OMIT_WSD
- + #define SQLITE_WSD const
- + #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
- + #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
- + int sqlite3_wsd_init(int N, int J);
- + void *sqlite3_wsd_find(void *K, int L);
- +#else
- + #define SQLITE_WSD
- + #define GLOBAL(t,v) v
- + #define sqlite3GlobalConfig sqlite3Config
- +#endif
- +
- +/*
- +** The following macros are used to suppress compiler warnings and to
- +** make it clear to human readers when a function parameter is deliberately
- +** left unused within the body of a function. This usually happens when
- +** a function is called via a function pointer. For example the
- +** implementation of an SQL aggregate step callback may not use the
- +** parameter indicating the number of arguments passed to the aggregate,
- +** if it knows that this is enforced elsewhere.
- +**
- +** When a function parameter is not used at all within the body of a function,
- +** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
- +** However, these macros may also be used to suppress warnings related to
- +** parameters that may or may not be used depending on compilation options.
- +** For example those parameters only used in assert() statements. In these
- +** cases the parameters are named as per the usual conventions.
- +*/
- +#define UNUSED_PARAMETER(x) (void)(x)
- +#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
- +
- +/*
- +** Forward references to structures
- +*/
- +typedef struct AggInfo AggInfo;
- +typedef struct AuthContext AuthContext;
- +typedef struct AutoincInfo AutoincInfo;
- +typedef struct Bitvec Bitvec;
- +typedef struct CollSeq CollSeq;
- +typedef struct Column Column;
- +typedef struct Db Db;
- +typedef struct Schema Schema;
- +typedef struct Expr Expr;
- +typedef struct ExprList ExprList;
- +typedef struct FKey FKey;
- +typedef struct FuncDestructor FuncDestructor;
- +typedef struct FuncDef FuncDef;
- +typedef struct FuncDefHash FuncDefHash;
- +typedef struct IdList IdList;
- +typedef struct Index Index;
- +typedef struct IndexSample IndexSample;
- +typedef struct KeyClass KeyClass;
- +typedef struct KeyInfo KeyInfo;
- +typedef struct Lookaside Lookaside;
- +typedef struct LookasideSlot LookasideSlot;
- +typedef struct Module Module;
- +typedef struct NameContext NameContext;
- +typedef struct Parse Parse;
- +typedef struct PreUpdate PreUpdate;
- +typedef struct PrintfArguments PrintfArguments;
- +typedef struct RenameToken RenameToken;
- +typedef struct RowSet RowSet;
- +typedef struct Savepoint Savepoint;
- +typedef struct Select Select;
- +typedef struct SQLiteThread SQLiteThread;
- +typedef struct SelectDest SelectDest;
- +typedef struct SrcList SrcList;
- +typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
- +typedef struct Table Table;
- +typedef struct TableLock TableLock;
- +typedef struct Token Token;
- +typedef struct TreeView TreeView;
- +typedef struct Trigger Trigger;
- +typedef struct TriggerPrg TriggerPrg;
- +typedef struct TriggerStep TriggerStep;
- +typedef struct UnpackedRecord UnpackedRecord;
- +typedef struct Upsert Upsert;
- +typedef struct VTable VTable;
- +typedef struct VtabCtx VtabCtx;
- +typedef struct Walker Walker;
- +typedef struct WhereInfo WhereInfo;
- +typedef struct Window Window;
- +typedef struct With With;
- +
- +
- +/*
- +** The bitmask datatype defined below is used for various optimizations.
- +**
- +** Changing this from a 64-bit to a 32-bit type limits the number of
- +** tables in a join to 32 instead of 64. But it also reduces the size
- +** of the library by 738 bytes on ix86.
- +*/
- +#ifdef SQLITE_BITMASK_TYPE
- + typedef SQLITE_BITMASK_TYPE Bitmask;
- +#else
- + typedef u64 Bitmask;
- +#endif
- +
- +/*
- +** The number of bits in a Bitmask. "BMS" means "BitMask Size".
- +*/
- +#define BMS ((int)(sizeof(Bitmask)*8))
- +
- +/*
- +** A bit in a Bitmask
- +*/
- +#define MASKBIT(n) (((Bitmask)1)<<(n))
- +#define MASKBIT64(n) (((u64)1)<<(n))
- +#define MASKBIT32(n) (((unsigned int)1)<<(n))
- +#define ALLBITS ((Bitmask)-1)
- +
- +/* A VList object records a mapping between parameters/variables/wildcards
- +** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
- +** variable number associated with that parameter. See the format description
- +** on the sqlite3VListAdd() routine for more information. A VList is really
- +** just an array of integers.
- +*/
- +typedef int VList;
- +
- +/*
- +** Defer sourcing vdbe.h and btree.h until after the "u8" and
- +** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
- +** pointer types (i.e. FuncDef) defined above.
- +*/
- +#include "btree.h"
- +#include "vdbe.h"
- +#include "pager.h"
- +#include "pcache.h"
- +#include "os.h"
- +#include "mutex.h"
- +
- +/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
- +** synchronous setting to EXTRA. It is no longer supported.
- +*/
- +#ifdef SQLITE_EXTRA_DURABLE
- +# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
- +# define SQLITE_DEFAULT_SYNCHRONOUS 3
- +#endif
- +
- +/*
- +** Default synchronous levels.
- +**
- +** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
- +** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
- +**
- +** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
- +** OFF 1 0
- +** NORMAL 2 1
- +** FULL 3 2
- +** EXTRA 4 3
- +**
- +** The "PRAGMA synchronous" statement also uses the zero-based numbers.
- +** In other words, the zero-based numbers are used for all external interfaces
- +** and the one-based values are used internally.
- +*/
- +#ifndef SQLITE_DEFAULT_SYNCHRONOUS
- +# define SQLITE_DEFAULT_SYNCHRONOUS 2
- +#endif
- +#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
- +# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
- +#endif
- +
- +/*
- +** Each database file to be accessed by the system is an instance
- +** of the following structure. There are normally two of these structures
- +** in the sqlite.aDb[] array. aDb[0] is the main database file and
- +** aDb[1] is the database file used to hold temporary tables. Additional
- +** databases may be attached.
- +*/
- +struct Db {
- + char *zDbSName; /* Name of this database. (schema name, not filename) */
- + Btree *pBt; /* The B*Tree structure for this database file */
- + u8 safety_level; /* How aggressive at syncing data to disk */
- + u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
- + Schema *pSchema; /* Pointer to database schema (possibly shared) */
- +};
- +
- +/*
- +** An instance of the following structure stores a database schema.
- +**
- +** Most Schema objects are associated with a Btree. The exception is
- +** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
- +** In shared cache mode, a single Schema object can be shared by multiple
- +** Btrees that refer to the same underlying BtShared object.
- +**
- +** Schema objects are automatically deallocated when the last Btree that
- +** references them is destroyed. The TEMP Schema is manually freed by
- +** sqlite3_close().
- +*
- +** A thread must be holding a mutex on the corresponding Btree in order
- +** to access Schema content. This implies that the thread must also be
- +** holding a mutex on the sqlite3 connection pointer that owns the Btree.
- +** For a TEMP Schema, only the connection mutex is required.
- +*/
- +struct Schema {
- + int schema_cookie; /* Database schema version number for this file */
- + int iGeneration; /* Generation counter. Incremented with each change */
- + Hash tblHash; /* All tables indexed by name */
- + Hash idxHash; /* All (named) indices indexed by name */
- + Hash trigHash; /* All triggers indexed by name */
- + Hash fkeyHash; /* All foreign keys by referenced table name */
- + Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
- + u8 file_format; /* Schema format version for this file */
- + u8 enc; /* Text encoding used by this database */
- + u16 schemaFlags; /* Flags associated with this schema */
- + int cache_size; /* Number of pages to use in the cache */
- +};
- +
- +/*
- +** These macros can be used to test, set, or clear bits in the
- +** Db.pSchema->flags field.
- +*/
- +#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
- +#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
- +#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
- +#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
- +
- +/*
- +** Allowed values for the DB.pSchema->flags field.
- +**
- +** The DB_SchemaLoaded flag is set after the database schema has been
- +** read into internal hash tables.
- +**
- +** DB_UnresetViews means that one or more views have column names that
- +** have been filled out. If the schema changes, these column names might
- +** changes and so the view will need to be reset.
- +*/
- +#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
- +#define DB_UnresetViews 0x0002 /* Some views have defined column names */
- +#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
- +
- +/*
- +** The number of different kinds of things that can be limited
- +** using the sqlite3_limit() interface.
- +*/
- +#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
- +
- +/*
- +** Lookaside malloc is a set of fixed-size buffers that can be used
- +** to satisfy small transient memory allocation requests for objects
- +** associated with a particular database connection. The use of
- +** lookaside malloc provides a significant performance enhancement
- +** (approx 10%) by avoiding numerous malloc/free requests while parsing
- +** SQL statements.
- +**
- +** The Lookaside structure holds configuration information about the
- +** lookaside malloc subsystem. Each available memory allocation in
- +** the lookaside subsystem is stored on a linked list of LookasideSlot
- +** objects.
- +**
- +** Lookaside allocations are only allowed for objects that are associated
- +** with a particular database connection. Hence, schema information cannot
- +** be stored in lookaside because in shared cache mode the schema information
- +** is shared by multiple database connections. Therefore, while parsing
- +** schema information, the Lookaside.bEnabled flag is cleared so that
- +** lookaside allocations are not used to construct the schema objects.
- +**
- +** New lookaside allocations are only allowed if bDisable==0. When
- +** bDisable is greater than zero, sz is set to zero which effectively
- +** disables lookaside without adding a new test for the bDisable flag
- +** in a performance-critical path. sz should be set by to szTrue whenever
- +** bDisable changes back to zero.
- +**
- +** Lookaside buffers are initially held on the pInit list. As they are
- +** used and freed, they are added back to the pFree list. New allocations
- +** come off of pFree first, then pInit as a fallback. This dual-list
- +** allows use to compute a high-water mark - the maximum number of allocations
- +** outstanding at any point in the past - by subtracting the number of
- +** allocations on the pInit list from the total number of allocations.
- +**
- +** Enhancement on 2019-12-12: Two-size-lookaside
- +** The default lookaside configuration is 100 slots of 1200 bytes each.
- +** The larger slot sizes are important for performance, but they waste
- +** a lot of space, as most lookaside allocations are less than 128 bytes.
- +** The two-size-lookaside enhancement breaks up the lookaside allocation
- +** into two pools: One of 128-byte slots and the other of the default size
- +** (1200-byte) slots. Allocations are filled from the small-pool first,
- +** failing over to the full-size pool if that does not work. Thus more
- +** lookaside slots are available while also using less memory.
- +** This enhancement can be omitted by compiling with
- +** SQLITE_OMIT_TWOSIZE_LOOKASIDE.
- +*/
- +struct Lookaside {
- + u32 bDisable; /* Only operate the lookaside when zero */
- + u16 sz; /* Size of each buffer in bytes */
- + u16 szTrue; /* True value of sz, even if disabled */
- + u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
- + u32 nSlot; /* Number of lookaside slots allocated */
- + u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
- + LookasideSlot *pInit; /* List of buffers not previously used */
- + LookasideSlot *pFree; /* List of available buffers */
- +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
- + LookasideSlot *pSmallInit; /* List of small buffers not prediously used */
- + LookasideSlot *pSmallFree; /* List of available small buffers */
- + void *pMiddle; /* First byte past end of full-size buffers and
- + ** the first byte of LOOKASIDE_SMALL buffers */
- +#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
- + void *pStart; /* First byte of available memory space */
- + void *pEnd; /* First byte past end of available space */
- +};
- +struct LookasideSlot {
- + LookasideSlot *pNext; /* Next buffer in the list of free buffers */
- +};
- +
- +#define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
- +#define EnableLookaside db->lookaside.bDisable--;\
- + db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
- +
- +/* Size of the smaller allocations in two-size lookside */
- +#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
- +# define LOOKASIDE_SMALL 0
- +#else
- +# define LOOKASIDE_SMALL 128
- +#endif
- +
- +/*
- +** A hash table for built-in function definitions. (Application-defined
- +** functions use a regular table table from hash.h.)
- +**
- +** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
- +** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
- +** macro to compute a hash on the function name.
- +*/
- +#define SQLITE_FUNC_HASH_SZ 23
- +struct FuncDefHash {
- + FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */
- +};
- +#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
- +
- +#ifdef SQLITE_USER_AUTHENTICATION
- +/*
- +** Information held in the "sqlite3" database connection object and used
- +** to manage user authentication.
- +*/
- +typedef struct sqlite3_userauth sqlite3_userauth;
- +struct sqlite3_userauth {
- + u8 authLevel; /* Current authentication level */
- + int nAuthPW; /* Size of the zAuthPW in bytes */
- + char *zAuthPW; /* Password used to authenticate */
- + char *zAuthUser; /* User name used to authenticate */
- +};
- +
- +/* Allowed values for sqlite3_userauth.authLevel */
- +#define UAUTH_Unknown 0 /* Authentication not yet checked */
- +#define UAUTH_Fail 1 /* User authentication failed */
- +#define UAUTH_User 2 /* Authenticated as a normal user */
- +#define UAUTH_Admin 3 /* Authenticated as an administrator */
- +
- +/* Functions used only by user authorization logic */
- +int sqlite3UserAuthTable(const char*);
- +int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
- +void sqlite3UserAuthInit(sqlite3*);
- +void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
- +
- +#endif /* SQLITE_USER_AUTHENTICATION */
- +
- +/*
- +** typedef for the authorization callback function.
- +*/
- +#ifdef SQLITE_USER_AUTHENTICATION
- + typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
- + const char*, const char*);
- +#else
- + typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
- + const char*);
- +#endif
- +
- +#ifndef SQLITE_OMIT_DEPRECATED
- +/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
- +** in the style of sqlite3_trace()
- +*/
- +#define SQLITE_TRACE_LEGACY 0x40 /* Use the legacy xTrace */
- +#define SQLITE_TRACE_XPROFILE 0x80 /* Use the legacy xProfile */
- +#else
- +#define SQLITE_TRACE_LEGACY 0
- +#define SQLITE_TRACE_XPROFILE 0
- +#endif /* SQLITE_OMIT_DEPRECATED */
- +#define SQLITE_TRACE_NONLEGACY_MASK 0x0f /* Normal flags */
- +
- +
- +/*
- +** Each database connection is an instance of the following structure.
- +*/
- +struct sqlite3 {
- + sqlite3_vfs *pVfs; /* OS Interface */
- + struct Vdbe *pVdbe; /* List of active virtual machines */
- + CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
- + sqlite3_mutex *mutex; /* Connection mutex */
- + Db *aDb; /* All backends */
- + int nDb; /* Number of backends currently in use */
- + u32 mDbFlags; /* flags recording internal state */
- + u64 flags; /* flags settable by pragmas. See below */
- + i64 lastRowid; /* ROWID of most recent insert (see above) */
- + i64 szMmap; /* Default mmap_size setting */
- + u32 nSchemaLock; /* Do not reset the schema when non-zero */
- + unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
- + int errCode; /* Most recent error code (SQLITE_*) */
- + int errMask; /* & result codes with this before returning */
- + int iSysErrno; /* Errno value from last system error */
- + u16 dbOptFlags; /* Flags to enable/disable optimizations */
- + u8 enc; /* Text encoding */
- + u8 autoCommit; /* The auto-commit flag. */
- + u8 temp_store; /* 1: file 2: memory 0: default */
- + u8 mallocFailed; /* True if we have seen a malloc failure */
- + u8 bBenignMalloc; /* Do not require OOMs if true */
- + u8 dfltLockMode; /* Default locking-mode for attached dbs */
- + signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
- + u8 suppressErr; /* Do not issue error messages if true */
- + u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
- + u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
- + u8 mTrace; /* zero or more SQLITE_TRACE flags */
- + u8 noSharedCache; /* True if no shared-cache backends */
- + u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
- + int nextPagesize; /* Pagesize after VACUUM if >0 */
- + u32 magic; /* Magic number for detect library misuse */
- + int nChange; /* Value returned by sqlite3_changes() */
- + int nTotalChange; /* Value returned by sqlite3_total_changes() */
- + int aLimit[SQLITE_N_LIMIT]; /* Limits */
- + int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
- + struct sqlite3InitInfo { /* Information used during initialization */
- + int newTnum; /* Rootpage of table being initialized */
- + u8 iDb; /* Which db file is being initialized */
- + u8 busy; /* TRUE if currently initializing */
- + unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
- + unsigned imposterTable : 1; /* Building an imposter table */
- + unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
- + char **azInit; /* "type", "name", and "tbl_name" columns */
- + } init;
- + int nVdbeActive; /* Number of VDBEs currently running */
- + int nVdbeRead; /* Number of active VDBEs that read or write */
- + int nVdbeWrite; /* Number of active VDBEs that read and write */
- + int nVdbeExec; /* Number of nested calls to VdbeExec() */
- + int nVDestroy; /* Number of active OP_VDestroy operations */
- + int nExtension; /* Number of loaded extensions */
- + void **aExtension; /* Array of shared library handles */
- + int (*xTrace)(u32,void*,void*,void*); /* Trace function */
- + void *pTraceArg; /* Argument to the trace function */
- +#ifndef SQLITE_OMIT_DEPRECATED
- + void (*xProfile)(void*,const char*,u64); /* Profiling function */
- + void *pProfileArg; /* Argument to profile function */
- +#endif
- + void *pCommitArg; /* Argument to xCommitCallback() */
- + int (*xCommitCallback)(void*); /* Invoked at every commit. */
- + void *pRollbackArg; /* Argument to xRollbackCallback() */
- + void (*xRollbackCallback)(void*); /* Invoked at every commit. */
- + void *pUpdateArg;
- + void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
- + Parse *pParse; /* Current parse */
- +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
- + void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
- + void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
- + void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
- + );
- + PreUpdate *pPreUpdate; /* Context for active pre-update callback */
- +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
- +#ifndef SQLITE_OMIT_WAL
- + int (*xWalCallback)(void *, sqlite3 *, const char *, int);
- + void *pWalArg;
- +#endif
- + void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
- + void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
- + void *pCollNeededArg;
- + sqlite3_value *pErr; /* Most recent error message */
- + union {
- + volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
- + double notUsed1; /* Spacer */
- + } u1;
- + Lookaside lookaside; /* Lookaside malloc configuration */
- +#ifndef SQLITE_OMIT_AUTHORIZATION
- + sqlite3_xauth xAuth; /* Access authorization function */
- + void *pAuthArg; /* 1st argument to the access auth function */
- +#endif
- +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
- + int (*xProgress)(void *); /* The progress callback */
- + void *pProgressArg; /* Argument to the progress callback */
- + unsigned nProgressOps; /* Number of opcodes for progress callback */
- +#endif
- +#ifndef SQLITE_OMIT_VIRTUALTABLE
- + int nVTrans; /* Allocated size of aVTrans */
- + Hash aModule; /* populated by sqlite3_create_module() */
- + VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
- + VTable **aVTrans; /* Virtual tables with open transactions */
- + VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
- +#endif
- + Hash aFunc; /* Hash table of connection functions */
- + Hash aCollSeq; /* All collating sequences */
- + BusyHandler busyHandler; /* Busy callback */
- + Db aDbStatic[2]; /* Static space for the 2 default backends */
- + Savepoint *pSavepoint; /* List of active savepoints */
- + int nAnalysisLimit; /* Number of index rows to ANALYZE */
- + int busyTimeout; /* Busy handler timeout, in msec */
- + int nSavepoint; /* Number of non-transaction savepoints */
- + int nStatement; /* Number of nested statement-transactions */
- + i64 nDeferredCons; /* Net deferred constraints this transaction. */
- + i64 nDeferredImmCons; /* Net deferred immediate constraints */
- + int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
- +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
- + /* The following variables are all protected by the STATIC_MASTER
- + ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
- + **
- + ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
- + ** unlock so that it can proceed.
- + **
- + ** When X.pBlockingConnection==Y, that means that something that X tried
- + ** tried to do recently failed with an SQLITE_LOCKED error due to locks
- + ** held by Y.
- + */
- + sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
- + sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
- + void *pUnlockArg; /* Argument to xUnlockNotify */
- + void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
- + sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
- +#endif
- +#ifdef SQLITE_USER_AUTHENTICATION
- + sqlite3_userauth auth; /* User authentication information */
- +#endif
- +};
- +
- +/*
- +** A macro to discover the encoding of a database.
- +*/
- +#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
- +#define ENC(db) ((db)->enc)
- +
- +/*
- +** A u64 constant where the lower 32 bits are all zeros. Only the
- +** upper 32 bits are included in the argument. Necessary because some
- +** C-compilers still do not accept LL integer literals.
- +*/
- +#define HI(X) ((u64)(X)<<32)
- +
- +/*
- +** Possible values for the sqlite3.flags.
- +**
- +** Value constraints (enforced via assert()):
- +** SQLITE_FullFSync == PAGER_FULLFSYNC
- +** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
- +** SQLITE_CacheSpill == PAGER_CACHE_SPILL
- +*/
- +#define SQLITE_WriteSchema 0x00000001 /* OK to update SQLITE_MASTER */
- +#define SQLITE_LegacyFileFmt 0x00000002 /* Create new databases in format 1 */
- +#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
- +#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */
- +#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */
- +#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */
- +#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
- +#define SQLITE_TrustedSchema 0x00000080 /* Allow unsafe functions and
- + ** vtabs in the schema definition */
- +#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
- + /* result set is empty */
- +#define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */
- +#define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
- +#define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */
- +#define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */
- +#define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */
- +#define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */
- +#define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */
- +#define SQLITE_LoadExtension 0x00010000 /* Enable load_extension */
- +#define SQLITE_LoadExtFunc 0x00020000 /* Enable load_extension() SQL func */
- +#define SQLITE_EnableTrigger 0x00040000 /* True to enable triggers */
- +#define SQLITE_DeferFKs 0x00080000 /* Defer all FK constraints */
- +#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
- +#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
- +#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
- +#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
- +#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
- +#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
- +#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */
- +#define SQLITE_NoSchemaError 0x08000000 /* Do not report schema parse errors*/
- +#define SQLITE_Defensive 0x10000000 /* Input SQL is likely hostile */
- +#define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
- +#define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
- +#define SQLITE_EnableView 0x80000000 /* Enable the use of views */
- +#define SQLITE_CountRows HI(0x00001) /* Count rows changed by INSERT, */
- + /* DELETE, or UPDATE and return */
- + /* the count using a callback. */
- +
- +/* Flags used only if debugging */
- +#ifdef SQLITE_DEBUG
- +#define SQLITE_SqlTrace HI(0x0100000) /* Debug print SQL as it executes */
- +#define SQLITE_VdbeListing HI(0x0200000) /* Debug listings of VDBE progs */
- +#define SQLITE_VdbeTrace HI(0x0400000) /* True to trace VDBE execution */
- +#define SQLITE_VdbeAddopTrace HI(0x0800000) /* Trace sqlite3VdbeAddOp() calls */
- +#define SQLITE_VdbeEQP HI(0x1000000) /* Debug EXPLAIN QUERY PLAN */
- +#define SQLITE_ParserTrace HI(0x2000000) /* PRAGMA parser_trace=ON */
- +#endif
- +
- +/*
- +** Allowed values for sqlite3.mDbFlags
- +*/
- +#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */
- +#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
- +#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
- +#define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */
- +#define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */
- +#define DBFLAG_InternalFunc 0x0020 /* Allow use of internal functions */
- +#define DBFLAG_EncodingFixed 0x0040 /* No longer possible to change enc. */
- +
- +/*
- +** Bits of the sqlite3.dbOptFlags field that are used by the
- +** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
- +** selectively disable various optimizations.
- +*/
- +#define SQLITE_QueryFlattener 0x0001 /* Query flattening */
- +#define SQLITE_WindowFunc 0x0002 /* Use xInverse for window functions */
- +#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
- +#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
- +#define SQLITE_DistinctOpt 0x0010 /* DISTINCT using indexes */
- +#define SQLITE_CoverIdxScan 0x0020 /* Covering index scans */
- +#define SQLITE_OrderByIdxJoin 0x0040 /* ORDER BY of joins via index */
- +#define SQLITE_Transitive 0x0080 /* Transitive constraints */
- +#define SQLITE_OmitNoopJoin 0x0100 /* Omit unused tables in joins */
- +#define SQLITE_CountOfView 0x0200 /* The count-of-view optimization */
- +#define SQLITE_CursorHints 0x0400 /* Add OP_CursorHint opcodes */
- +#define SQLITE_Stat4 0x0800 /* Use STAT4 data */
- + /* TH3 expects the Stat4 ^^^^^^ value to be 0x0800. Don't change it */
- +#define SQLITE_PushDown 0x1000 /* The push-down optimization */
- +#define SQLITE_SimplifyJoin 0x2000 /* Convert LEFT JOIN to JOIN */
- +#define SQLITE_SkipScan 0x4000 /* Skip-scans */
- +#define SQLITE_PropagateConst 0x8000 /* The constant propagation opt */
- +#define SQLITE_AllOpts 0xffff /* All optimizations */
- +
- +/*
- +** Macros for testing whether or not optimizations are enabled or disabled.
- +*/
- +#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
- +#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
- +
- +/*
- +** Return true if it OK to factor constant expressions into the initialization
- +** code. The argument is a Parse object for the code generator.
- +*/
- +#define ConstFactorOk(P) ((P)->okConstFactor)
- +
- +/*
- +** Possible values for the sqlite.magic field.
- +** The numbers are obtained at random and have no special meaning, other
- +** than being distinct from one another.
- +*/
- +#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
- +#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
- +#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
- +#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
- +#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
- +#define SQLITE_MAGIC_ZOMBIE 0x64cffc7f /* Close with last statement close */
- +
- +/*
- +** Each SQL function is defined by an instance of the following
- +** structure. For global built-in functions (ex: substr(), max(), count())
- +** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
- +** For per-connection application-defined functions, a pointer to this
- +** structure is held in the db->aHash hash table.
- +**
- +** The u.pHash field is used by the global built-ins. The u.pDestructor
- +** field is used by per-connection app-def functions.
- +*/
- +struct FuncDef {
- + i8 nArg; /* Number of arguments. -1 means unlimited */
- + u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
- + void *pUserData; /* User data parameter */
- + FuncDef *pNext; /* Next function with same name */
- + void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
- + void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
- + void (*xValue)(sqlite3_context*); /* Current agg value */
- + void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
- + const char *zName; /* SQL name of the function. */
- + union {
- + FuncDef *pHash; /* Next with a different name but the same hash */
- + FuncDestructor *pDestructor; /* Reference counted destructor function */
- + } u;
- +};
- +
- +/*
- +** This structure encapsulates a user-function destructor callback (as
- +** configured using create_function_v2()) and a reference counter. When
- +** create_function_v2() is called to create a function with a destructor,
- +** a single object of this type is allocated. FuncDestructor.nRef is set to
- +** the number of FuncDef objects created (either 1 or 3, depending on whether
- +** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
- +** member of each of the new FuncDef objects is set to point to the allocated
- +** FuncDestructor.
- +**
- +** Thereafter, when one of the FuncDef objects is deleted, the reference
- +** count on this object is decremented. When it reaches 0, the destructor
- +** is invoked and the FuncDestructor structure freed.
- +*/
- +struct FuncDestructor {
- + int nRef;
- + void (*xDestroy)(void *);
- + void *pUserData;
- +};
- +
- +/*
- +** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
- +** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
- +** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
- +** are assert() statements in the code to verify this.
- +**
- +** Value constraints (enforced via assert()):
- +** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
- +** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
- +** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
- +** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
- +** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API
- +** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS
- +** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
- +*/
- +#define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
- +#define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */
- +#define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
- +#define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */
- +#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
- +#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
- +#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
- +#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
- +/* 0x0200 -- available for reuse */
- +#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
- +#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
- +#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
- +#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
- + ** single query - might change over time */
- +#define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
- +#define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
- +#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
- +#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
- +#define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
- +#define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
- +#define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
- +#define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
- +
- +/* Identifier numbers for each in-line function */
- +#define INLINEFUNC_coalesce 0
- +#define INLINEFUNC_implies_nonnull_row 1
- +#define INLINEFUNC_expr_implies_expr 2
- +#define INLINEFUNC_expr_compare 3
- +#define INLINEFUNC_affinity 4
- +#define INLINEFUNC_iif 5
- +#define INLINEFUNC_unlikely 99 /* Default case */
- +
- +/*
- +** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
- +** used to create the initializers for the FuncDef structures.
- +**
- +** FUNCTION(zName, nArg, iArg, bNC, xFunc)
- +** Used to create a scalar function definition of a function zName
- +** implemented by C function xFunc that accepts nArg arguments. The
- +** value passed as iArg is cast to a (void*) and made available
- +** as the user-data (sqlite3_user_data()) for the function. If
- +** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
- +**
- +** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
- +** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
- +**
- +** SFUNCTION(zName, nArg, iArg, bNC, xFunc)
- +** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
- +** adds the SQLITE_DIRECTONLY flag.
- +**
- +** INLINE_FUNC(zName, nArg, iFuncId, mFlags)
- +** zName is the name of a function that is implemented by in-line
- +** byte code rather than by the usual callbacks. The iFuncId
- +** parameter determines the function id. The mFlags parameter is
- +** optional SQLITE_FUNC_ flags for this function.
- +**
- +** TEST_FUNC(zName, nArg, iFuncId, mFlags)
- +** zName is the name of a test-only function implemented by in-line
- +** byte code rather than by the usual callbacks. The iFuncId
- +** parameter determines the function id. The mFlags parameter is
- +** optional SQLITE_FUNC_ flags for this function.
- +**
- +** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
- +** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
- +** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
- +** and functions like sqlite_version() that can change, but not during
- +** a single query. The iArg is ignored. The user-data is always set
- +** to a NULL pointer. The bNC parameter is not used.
- +**
- +** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
- +** Used for "pure" date/time functions, this macro is like DFUNCTION
- +** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
- +** ignored and the user-data for these functions is set to an
- +** arbitrary non-NULL pointer. The bNC parameter is not used.
- +**
- +** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
- +** Used to create an aggregate function definition implemented by
- +** the C functions xStep and xFinal. The first four parameters
- +** are interpreted in the same way as the first 4 parameters to
- +** FUNCTION().
- +**
- +** WFUNCTION(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
- +** Used to create an aggregate function definition implemented by
- +** the C functions xStep and xFinal. The first four parameters
- +** are interpreted in the same way as the first 4 parameters to
- +** FUNCTION().
- +**
- +** LIKEFUNC(zName, nArg, pArg, flags)
- +** Used to create a scalar function definition of a function zName
- +** that accepts nArg arguments and is implemented by a call to C
- +** function likeFunc. Argument pArg is cast to a (void *) and made
- +** available as the function user-data (sqlite3_user_data()). The
- +** FuncDef.flags variable is set to the value passed as the flags
- +** parameter.
- +*/
- +#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
- + {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
- + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
- +#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
- + {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
- + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
- +#define SFUNCTION(zName, nArg, iArg, bNC, xFunc) \
- + {nArg, SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \
- + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
- +#define INLINE_FUNC(zName, nArg, iArg, mFlags) \
- + {nArg, SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
- + SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
- +#define TEST_FUNC(zName, nArg, iArg, mFlags) \
- + {nArg, SQLITE_UTF8|SQLITE_FUNC_INTERNAL|SQLITE_FUNC_TEST| \
- + SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
- + SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
- +#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
- + {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \
- + 0, 0, xFunc, 0, 0, 0, #zName, {0} }
- +#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \
- + {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
- + (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
- +#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
- + {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
- + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
- +#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
- + {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
- + pArg, 0, xFunc, 0, 0, 0, #zName, }
- +#define LIKEFUNC(zName, nArg, arg, flags) \
- + {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
- + (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
- +#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
- + {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
- + SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
- +#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
- + {nArg, SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
- + 0, 0, xFunc, 0, 0, 0, #zName, {0} }
- +
- +
- +/*
- +** All current savepoints are stored in a linked list starting at
- +** sqlite3.pSavepoint. The first element in the list is the most recently
- +** opened savepoint. Savepoints are added to the list by the vdbe
- +** OP_Savepoint instruction.
- +*/
- +struct Savepoint {
- + char *zName; /* Savepoint name (nul-terminated) */
- + i64 nDeferredCons; /* Number of deferred fk violations */
- + i64 nDeferredImmCons; /* Number of deferred imm fk. */
- + Savepoint *pNext; /* Parent savepoint (if any) */
- +};
- +
- +/*
- +** The following are used as the second parameter to sqlite3Savepoint(),
- +** and as the P1 argument to the OP_Savepoint instruction.
- +*/
- +#define SAVEPOINT_BEGIN 0
- +#define SAVEPOINT_RELEASE 1
- +#define SAVEPOINT_ROLLBACK 2
- +
- +
- +/*
- +** Each SQLite module (virtual table definition) is defined by an
- +** instance of the following structure, stored in the sqlite3.aModule
- +** hash table.
- +*/
- +struct Module {
- + const sqlite3_module *pModule; /* Callback pointers */
- + const char *zName; /* Name passed to create_module() */
- + int nRefModule; /* Number of pointers to this object */
- + void *pAux; /* pAux passed to create_module() */
- + void (*xDestroy)(void *); /* Module destructor function */
- + Table *pEpoTab; /* Eponymous table for this module */
- +};
- +
- +/*
- +** Information about each column of an SQL table is held in an instance
- +** of the Column structure, in the Table.aCol[] array.
- +**
- +** Definitions:
- +**
- +** "table column index" This is the index of the column in the
- +** Table.aCol[] array, and also the index of
- +** the column in the original CREATE TABLE stmt.
- +**
- +** "storage column index" This is the index of the column in the
- +** record BLOB generated by the OP_MakeRecord
- +** opcode. The storage column index is less than
- +** or equal to the table column index. It is
- +** equal if and only if there are no VIRTUAL
- +** columns to the left.
- +*/
- +struct Column {
- + char *zName; /* Name of this column, \000, then the type */
- + Expr *pDflt; /* Default value or GENERATED ALWAYS AS value */
- + char *zColl; /* Collating sequence. If NULL, use the default */
- + u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
- + char affinity; /* One of the SQLITE_AFF_... values */
- + u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */
- + u8 hName; /* Column name hash for faster lookup */
- + u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
- +};
- +
- +/* Allowed values for Column.colFlags:
- +*/
- +#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
- +#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
- +#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
- +#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */
- +#define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
- +#define COLFLAG_VIRTUAL 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */
- +#define COLFLAG_STORED 0x0040 /* GENERATED ALWAYS AS ... STORED */
- +#define COLFLAG_NOTAVAIL 0x0080 /* STORED column not yet calculated */
- +#define COLFLAG_BUSY 0x0100 /* Blocks recursion on GENERATED columns */
- +#define COLFLAG_GENERATED 0x0060 /* Combo: _STORED, _VIRTUAL */
- +#define COLFLAG_NOINSERT 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */
- +
- +/*
- +** A "Collating Sequence" is defined by an instance of the following
- +** structure. Conceptually, a collating sequence consists of a name and
- +** a comparison routine that defines the order of that sequence.
- +**
- +** If CollSeq.xCmp is NULL, it means that the
- +** collating sequence is undefined. Indices built on an undefined
- +** collating sequence may not be read or written.
- +*/
- +struct CollSeq {
- + char *zName; /* Name of the collating sequence, UTF-8 encoded */
- + u8 enc; /* Text encoding handled by xCmp() */
- + void *pUser; /* First argument to xCmp() */
- + int (*xCmp)(void*,int, const void*, int, const void*);
- + void (*xDel)(void*); /* Destructor for pUser */
- +};
- +
- +/*
- +** A sort order can be either ASC or DESC.
- +*/
- +#define SQLITE_SO_ASC 0 /* Sort in ascending order */
- +#define SQLITE_SO_DESC 1 /* Sort in ascending order */
- +#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
- +
- +/*
- +** Column affinity types.
- +**
- +** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
- +** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
- +** the speed a little by numbering the values consecutively.
- +**
- +** But rather than start with 0 or 1, we begin with 'A'. That way,
- +** when multiple affinity types are concatenated into a string and
- +** used as the P4 operand, they will be more readable.
- +**
- +** Note also that the numeric types are grouped together so that testing
- +** for a numeric type is a single comparison. And the BLOB type is first.
- +*/
- +#define SQLITE_AFF_NONE 0x40 /* '@' */
- +#define SQLITE_AFF_BLOB 0x41 /* 'A' */
- +#define SQLITE_AFF_TEXT 0x42 /* 'B' */
- +#define SQLITE_AFF_NUMERIC 0x43 /* 'C' */
- +#define SQLITE_AFF_INTEGER 0x44 /* 'D' */
- +#define SQLITE_AFF_REAL 0x45 /* 'E' */
- +
- +#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
- +
- +/*
- +** The SQLITE_AFF_MASK values masks off the significant bits of an
- +** affinity value.
- +*/
- +#define SQLITE_AFF_MASK 0x47
- +
- +/*
- +** Additional bit values that can be ORed with an affinity without
- +** changing the affinity.
- +**
- +** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
- +** It causes an assert() to fire if either operand to a comparison
- +** operator is NULL. It is added to certain comparison operators to
- +** prove that the operands are always NOT NULL.
- +*/
- +#define SQLITE_KEEPNULL 0x08 /* Used by vector == or <> */
- +#define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */
- +#define SQLITE_STOREP2 0x20 /* Store result in reg[P2] rather than jump */
- +#define SQLITE_NULLEQ 0x80 /* NULL=NULL */
- +#define SQLITE_NOTNULL 0x90 /* Assert that operands are never NULL */
- +
- +/*
- +** An object of this type is created for each virtual table present in
- +** the database schema.
- +**
- +** If the database schema is shared, then there is one instance of this
- +** structure for each database connection (sqlite3*) that uses the shared
- +** schema. This is because each database connection requires its own unique
- +** instance of the sqlite3_vtab* handle used to access the virtual table
- +** implementation. sqlite3_vtab* handles can not be shared between
- +** database connections, even when the rest of the in-memory database
- +** schema is shared, as the implementation often stores the database
- +** connection handle passed to it via the xConnect() or xCreate() method
- +** during initialization internally. This database connection handle may
- +** then be used by the virtual table implementation to access real tables
- +** within the database. So that they appear as part of the callers
- +** transaction, these accesses need to be made via the same database
- +** connection as that used to execute SQL operations on the virtual table.
- +**
- +** All VTable objects that correspond to a single table in a shared
- +** database schema are initially stored in a linked-list pointed to by
- +** the Table.pVTable member variable of the corresponding Table object.
- +** When an sqlite3_prepare() operation is required to access the virtual
- +** table, it searches the list for the VTable that corresponds to the
- +** database connection doing the preparing so as to use the correct
- +** sqlite3_vtab* handle in the compiled query.
- +**
- +** When an in-memory Table object is deleted (for example when the
- +** schema is being reloaded for some reason), the VTable objects are not
- +** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
- +** immediately. Instead, they are moved from the Table.pVTable list to
- +** another linked list headed by the sqlite3.pDisconnect member of the
- +** corresponding sqlite3 structure. They are then deleted/xDisconnected
- +** next time a statement is prepared using said sqlite3*. This is done
- +** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
- +** Refer to comments above function sqlite3VtabUnlockList() for an
- +** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
- +** list without holding the corresponding sqlite3.mutex mutex.
- +**
- +** The memory for objects of this type is always allocated by
- +** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
- +** the first argument.
- +*/
- +struct VTable {
- + sqlite3 *db; /* Database connection associated with this table */
- + Module *pMod; /* Pointer to module implementation */
- + sqlite3_vtab *pVtab; /* Pointer to vtab instance */
- + int nRef; /* Number of pointers to this structure */
- + u8 bConstraint; /* True if constraints are supported */
- + u8 eVtabRisk; /* Riskiness of allowing hacker access */
- + int iSavepoint; /* Depth of the SAVEPOINT stack */
- + VTable *pNext; /* Next in linked list (see above) */
- +};
- +
- +/* Allowed values for VTable.eVtabRisk
- +*/
- +#define SQLITE_VTABRISK_Low 0
- +#define SQLITE_VTABRISK_Normal 1
- +#define SQLITE_VTABRISK_High 2
- +
- +/*
- +** The schema for each SQL table and view is represented in memory
- +** by an instance of the following structure.
- +*/
- +struct Table {
- + char *zName; /* Name of the table or view */
- + Column *aCol; /* Information about each column */
- + Index *pIndex; /* List of SQL indexes on this table. */
- + Select *pSelect; /* NULL for tables. Points to definition if a view. */
- + FKey *pFKey; /* Linked list of all foreign keys in this table */
- + char *zColAff; /* String defining the affinity of each column */
- + ExprList *pCheck; /* All CHECK constraints */
- + /* ... also used as column name list in a VIEW */
- + int tnum; /* Root BTree page for this table */
- + u32 nTabRef; /* Number of pointers to this Table */
- + u32 tabFlags; /* Mask of TF_* values */
- + i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
- + i16 nCol; /* Number of columns in this table */
- + i16 nNVCol; /* Number of columns that are not VIRTUAL */
- + LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
- + LogEst szTabRow; /* Estimated size of each table row in bytes */
- +#ifdef SQLITE_ENABLE_COSTMULT
- + LogEst costMult; /* Cost multiplier for using this table */
- +#endif
- + u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
- +#ifndef SQLITE_OMIT_ALTERTABLE
- + int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
- +#endif
- +#ifndef SQLITE_OMIT_VIRTUALTABLE
- + int nModuleArg; /* Number of arguments to the module */
- + char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */
- + VTable *pVTable; /* List of VTable objects. */
- +#endif
- + Trigger *pTrigger; /* List of triggers stored in pSchema */
- + Schema *pSchema; /* Schema that contains this table */
- + Table *pNextZombie; /* Next on the Parse.pZombieTab list */
- +};
- +
- +/*
- +** Allowed values for Table.tabFlags.
- +**
- +** TF_OOOHidden applies to tables or view that have hidden columns that are
- +** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
- +** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
- +** the TF_OOOHidden attribute would apply in this case. Such tables require
- +** special handling during INSERT processing. The "OOO" means "Out Of Order".
- +**
- +** Constraints:
- +**
- +** TF_HasVirtual == COLFLAG_Virtual
- +** TF_HasStored == COLFLAG_Stored
- +*/
- +#define TF_Readonly 0x0001 /* Read-only system table */
- +#define TF_Ephemeral 0x0002 /* An ephemeral table */
- +#define TF_HasPrimaryKey 0x0004 /* Table has a primary key */
- +#define TF_Autoincrement 0x0008 /* Integer primary key is autoincrement */
- +#define TF_HasStat1 0x0010 /* nRowLogEst set from sqlite_stat1 */
- +#define TF_HasVirtual 0x0020 /* Has one or more VIRTUAL columns */
- +#define TF_HasStored 0x0040 /* Has one or more STORED columns */
- +#define TF_HasGenerated 0x0060 /* Combo: HasVirtual + HasStored */
- +#define TF_WithoutRowid 0x0080 /* No rowid. PRIMARY KEY is the key */
- +#define TF_StatsUsed 0x0100 /* Query planner decisions affected by
- + ** Index.aiRowLogEst[] values */
- +#define TF_NoVisibleRowid 0x0200 /* No user-visible "rowid" column */
- +#define TF_OOOHidden 0x0400 /* Out-of-Order hidden columns */
- +#define TF_HasNotNull 0x0800 /* Contains NOT NULL constraints */
- +#define TF_Shadow 0x1000 /* True for a shadow table */
- +
- +/*
- +** Test to see whether or not a table is a virtual table. This is
- +** done as a macro so that it will be optimized out when virtual
- +** table support is omitted from the build.
- +*/
- +#ifndef SQLITE_OMIT_VIRTUALTABLE
- +# define IsVirtual(X) ((X)->nModuleArg)
- +# define ExprIsVtab(X) \
- + ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->nModuleArg)
- +#else
- +# define IsVirtual(X) 0
- +# define ExprIsVtab(X) 0
- +#endif
- +
- +/*
- +** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
- +** only works for non-virtual tables (ordinary tables and views) and is
- +** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
- +** IsHiddenColumn() macro is general purpose.
- +*/
- +#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
- +# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
- +# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
- +#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
- +# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
- +# define IsOrdinaryHiddenColumn(X) 0
- +#else
- +# define IsHiddenColumn(X) 0
- +# define IsOrdinaryHiddenColumn(X) 0
- +#endif
- +
- +
- +/* Does the table have a rowid */
- +#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
- +#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
- +
- +/*
- +** Each foreign key constraint is an instance of the following structure.
- +**
- +** A foreign key is associated with two tables. The "from" table is
- +** the table that contains the REFERENCES clause that creates the foreign
- +** key. The "to" table is the table that is named in the REFERENCES clause.
- +** Consider this example:
- +**
- +** CREATE TABLE ex1(
- +** a INTEGER PRIMARY KEY,
- +** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
- +** );
- +**
- +** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
- +** Equivalent names:
- +**
- +** from-table == child-table
- +** to-table == parent-table
- +**
- +** Each REFERENCES clause generates an instance of the following structure
- +** which is attached to the from-table. The to-table need not exist when
- +** the from-table is created. The existence of the to-table is not checked.
- +**
- +** The list of all parents for child Table X is held at X.pFKey.
- +**
- +** A list of all children for a table named Z (which might not even exist)
- +** is held in Schema.fkeyHash with a hash key of Z.
- +*/
- +struct FKey {
- + Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
- + FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
- + char *zTo; /* Name of table that the key points to (aka: Parent) */
- + FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
- + FKey *pPrevTo; /* Previous with the same zTo */
- + int nCol; /* Number of columns in this key */
- + /* EV: R-30323-21917 */
- + u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
- + u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
- + Trigger *apTrigger[2];/* Triggers for aAction[] actions */
- + struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
- + int iFrom; /* Index of column in pFrom */
- + char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
- + } aCol[1]; /* One entry for each of nCol columns */
- +};
- +
- +/*
- +** SQLite supports many different ways to resolve a constraint
- +** error. ROLLBACK processing means that a constraint violation
- +** causes the operation in process to fail and for the current transaction
- +** to be rolled back. ABORT processing means the operation in process
- +** fails and any prior changes from that one operation are backed out,
- +** but the transaction is not rolled back. FAIL processing means that
- +** the operation in progress stops and returns an error code. But prior
- +** changes due to the same operation are not backed out and no rollback
- +** occurs. IGNORE means that the particular row that caused the constraint
- +** error is not inserted or updated. Processing continues and no error
- +** is returned. REPLACE means that preexisting database rows that caused
- +** a UNIQUE constraint violation are removed so that the new insert or
- +** update can proceed. Processing continues and no error is reported.
- +**
- +** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
- +** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
- +** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
- +** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
- +** referenced table row is propagated into the row that holds the
- +** foreign key.
- +**
- +** The following symbolic values are used to record which type
- +** of action to take.
- +*/
- +#define OE_None 0 /* There is no constraint to check */
- +#define OE_Rollback 1 /* Fail the operation and rollback the transaction */
- +#define OE_Abort 2 /* Back out changes but do no rollback transaction */
- +#define OE_Fail 3 /* Stop the operation but leave all prior changes */
- +#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
- +#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
- +#define OE_Update 6 /* Process as a DO UPDATE in an upsert */
- +#define OE_Restrict 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
- +#define OE_SetNull 8 /* Set the foreign key value to NULL */
- +#define OE_SetDflt 9 /* Set the foreign key value to its default */
- +#define OE_Cascade 10 /* Cascade the changes */
- +#define OE_Default 11 /* Do whatever the default action is */
- +
- +
- +/*
- +** An instance of the following structure is passed as the first
- +** argument to sqlite3VdbeKeyCompare and is used to control the
- +** comparison of the two index keys.
- +**
- +** Note that aSortOrder[] and aColl[] have nField+1 slots. There
- +** are nField slots for the columns of an index then one extra slot
- +** for the rowid at the end.
- +*/
- +struct KeyInfo {
- + u32 nRef; /* Number of references to this KeyInfo object */
- + u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
- + u16 nKeyField; /* Number of key columns in the index */
- + u16 nAllField; /* Total columns, including key plus others */
- + sqlite3 *db; /* The database connection */
- + u8 *aSortFlags; /* Sort order for each column. */
- + CollSeq *aColl[1]; /* Collating sequence for each term of the key */
- +};
- +
- +/*
- +** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
- +*/
- +#define KEYINFO_ORDER_DESC 0x01 /* DESC sort order */
- +#define KEYINFO_ORDER_BIGNULL 0x02 /* NULL is larger than any other value */
- +
- +/*
- +** This object holds a record which has been parsed out into individual
- +** fields, for the purposes of doing a comparison.
- +**
- +** A record is an object that contains one or more fields of data.
- +** Records are used to store the content of a table row and to store
- +** the key of an index. A blob encoding of a record is created by
- +** the OP_MakeRecord opcode of the VDBE and is disassembled by the
- +** OP_Column opcode.
- +**
- +** An instance of this object serves as a "key" for doing a search on
- +** an index b+tree. The goal of the search is to find the entry that
- +** is closed to the key described by this object. This object might hold
- +** just a prefix of the key. The number of fields is given by
- +** pKeyInfo->nField.
- +**
- +** The r1 and r2 fields are the values to return if this key is less than
- +** or greater than a key in the btree, respectively. These are normally
- +** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
- +** is in DESC order.
- +**
- +** The key comparison functions actually return default_rc when they find
- +** an equals comparison. default_rc can be -1, 0, or +1. If there are
- +** multiple entries in the b-tree with the same key (when only looking
- +** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
- +** cause the search to find the last match, or +1 to cause the search to
- +** find the first match.
- +**
- +** The key comparison functions will set eqSeen to true if they ever
- +** get and equal results when comparing this structure to a b-tree record.
- +** When default_rc!=0, the search might end up on the record immediately
- +** before the first match or immediately after the last match. The
- +** eqSeen field will indicate whether or not an exact match exists in the
- +** b-tree.
- +*/
- +struct UnpackedRecord {
- + KeyInfo *pKeyInfo; /* Collation and sort-order information */
- + Mem *aMem; /* Values */
- + u16 nField; /* Number of entries in apMem[] */
- + i8 default_rc; /* Comparison result if keys are equal */
- + u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
- + i8 r1; /* Value to return if (lhs < rhs) */
- + i8 r2; /* Value to return if (lhs > rhs) */
- + u8 eqSeen; /* True if an equality comparison has been seen */
- +};
- +
- +
- +/*
- +** Each SQL index is represented in memory by an
- +** instance of the following structure.
- +**
- +** The columns of the table that are to be indexed are described
- +** by the aiColumn[] field of this structure. For example, suppose
- +** we have the following table and index:
- +**
- +** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
- +** CREATE INDEX Ex2 ON Ex1(c3,c1);
- +**
- +** In the Table structure describing Ex1, nCol==3 because there are
- +** three columns in the table. In the Index structure describing
- +** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
- +** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
- +** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
- +** The second column to be indexed (c1) has an index of 0 in
- +** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
- +**
- +** The Index.onError field determines whether or not the indexed columns
- +** must be unique and what to do if they are not. When Index.onError=OE_None,
- +** it means this is not a unique index. Otherwise it is a unique index
- +** and the value of Index.onError indicate the which conflict resolution
- +** algorithm to employ whenever an attempt is made to insert a non-unique
- +** element.
- +**
- +** While parsing a CREATE TABLE or CREATE INDEX statement in order to
- +** generate VDBE code (as opposed to parsing one read from an sqlite_master
- +** table as part of parsing an existing database schema), transient instances
- +** of this structure may be created. In this case the Index.tnum variable is
- +** used to store the address of a VDBE instruction, not a database page
- +** number (it cannot - the database page is not allocated until the VDBE
- +** program is executed). See convertToWithoutRowidTable() for details.
- +*/
- +struct Index {
- + char *zName; /* Name of this index */
- + i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
- + LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
- + Table *pTable; /* The SQL table being indexed */
- + char *zColAff; /* String defining the affinity of each column */
- + Index *pNext; /* The next index associated with the same table */
- + Schema *pSchema; /* Schema containing this index */
- + u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
- + const char **azColl; /* Array of collation sequence names for index */
- + Expr *pPartIdxWhere; /* WHERE clause for partial indices */
- + ExprList *aColExpr; /* Column expressions */
- + int tnum; /* DB Page containing root of this index */
- + LogEst szIdxRow; /* Estimated average row size in bytes */
- + u16 nKeyCol; /* Number of columns forming the key */
- + u16 nColumn; /* Number of columns stored in the index */
- + u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
- + unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
- + unsigned bUnordered:1; /* Use this index for == or IN queries only */
- + unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
- + unsigned isResized:1; /* True if resizeIndexObject() has been called */
- + unsigned isCovering:1; /* True if this is a covering index */
- + unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
- + unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
- + unsigned bNoQuery:1; /* Do not use this index to optimize queries */
- + unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
- + unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
- +#ifdef SQLITE_ENABLE_STAT4
- + int nSample; /* Number of elements in aSample[] */
- + int nSampleCol; /* Size of IndexSample.anEq[] and so on */
- + tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
- + IndexSample *aSample; /* Samples of the left-most key */
- + tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
- + tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
- +#endif
- + Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */
- +};
- +
- +/*
- +** Allowed values for Index.idxType
- +*/
- +#define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */
- +#define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
- +#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
- +#define SQLITE_IDXTYPE_IPK 3 /* INTEGER PRIMARY KEY index */
- +
- +/* Return true if index X is a PRIMARY KEY index */
- +#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
- +
- +/* Return true if index X is a UNIQUE index */
- +#define IsUniqueIndex(X) ((X)->onError!=OE_None)
- +
- +/* The Index.aiColumn[] values are normally positive integer. But
- +** there are some negative values that have special meaning:
- +*/
- +#define XN_ROWID (-1) /* Indexed column is the rowid */
- +#define XN_EXPR (-2) /* Indexed column is an expression */
- +
- +/*
- +** Each sample stored in the sqlite_stat4 table is represented in memory
- +** using a structure of this type. See documentation at the top of the
- +** analyze.c source file for additional information.
- +*/
- +struct IndexSample {
- + void *p; /* Pointer to sampled record */
- + int n; /* Size of record in bytes */
- + tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
- + tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
- + tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
- +};
- +
- +/*
- +** Possible values to use within the flags argument to sqlite3GetToken().
- +*/
- +#define SQLITE_TOKEN_QUOTED 0x1 /* Token is a quoted identifier. */
- +#define SQLITE_TOKEN_KEYWORD 0x2 /* Token is a keyword. */
- +
- +/*
- +** Each token coming out of the lexer is an instance of
- +** this structure. Tokens are also used as part of an expression.
- +**
- +** The memory that "z" points to is owned by other objects. Take care
- +** that the owner of the "z" string does not deallocate the string before
- +** the Token goes out of scope! Very often, the "z" points to some place
- +** in the middle of the Parse.zSql text. But it might also point to a
- +** static string.
- +*/
- +struct Token {
- + const char *z; /* Text of the token. Not NULL-terminated! */
- + unsigned int n; /* Number of characters in this token */
- +};
- +
- +/*
- +** An instance of this structure contains information needed to generate
- +** code for a SELECT that contains aggregate functions.
- +**
- +** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
- +** pointer to this structure. The Expr.iAgg field is the index in
- +** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
- +** code for that node.
- +**
- +** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
- +** original Select structure that describes the SELECT statement. These
- +** fields do not need to be freed when deallocating the AggInfo structure.
- +*/
- +struct AggInfo {
- + u8 directMode; /* Direct rendering mode means take data directly
- + ** from source tables rather than from accumulators */
- + u8 useSortingIdx; /* In direct mode, reference the sorting index rather
- + ** than the source table */
- + int sortingIdx; /* Cursor number of the sorting index */
- + int sortingIdxPTab; /* Cursor number of pseudo-table */
- + int nSortingColumn; /* Number of columns in the sorting index */
- + int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */
- + ExprList *pGroupBy; /* The group by clause */
- + struct AggInfo_col { /* For each column used in source tables */
- + Table *pTab; /* Source table */
- + int iTable; /* Cursor number of the source table */
- + int iColumn; /* Column number within the source table */
- + int iSorterColumn; /* Column number in the sorting index */
- + int iMem; /* Memory location that acts as accumulator */
- + Expr *pExpr; /* The original expression */
- + } *aCol;
- + int nColumn; /* Number of used entries in aCol[] */
- + int nAccumulator; /* Number of columns that show through to the output.
- + ** Additional columns are used only as parameters to
- + ** aggregate functions */
- + struct AggInfo_func { /* For each aggregate function */
- + Expr *pExpr; /* Expression encoding the function */
- + FuncDef *pFunc; /* The aggregate function implementation */
- + int iMem; /* Memory location that acts as accumulator */
- + int iDistinct; /* Ephemeral table used to enforce DISTINCT */
- + } *aFunc;
- + int nFunc; /* Number of entries in aFunc[] */
- +#ifdef SQLITE_DEBUG
- + u32 iAggMagic; /* Sanity checking constant */
- +#endif
- +};
- +
- +/*
- +** Allowed values for AggInfo.iAggMagic
- +*/
- +#define SQLITE_AGGMAGIC_VALID 0x05cadade
- +
- +/*
- +** True if the AggInfo object is valid. Used inside of assert() only.
- +*/
- +#ifdef SQLITE_DEBUG
- +# define AggInfoValid(P) ((P)->iAggMagic==SQLITE_AGGMAGIC_VALID)
- +#endif
- +
- +/*
- +** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
- +** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
- +** than 32767 we have to make it 32-bit. 16-bit is preferred because
- +** it uses less memory in the Expr object, which is a big memory user
- +** in systems with lots of prepared statements. And few applications
- +** need more than about 10 or 20 variables. But some extreme users want
- +** to have prepared statements with over 32766 variables, and for them
- +** the option is available (at compile-time).
- +*/
- +#if SQLITE_MAX_VARIABLE_NUMBER<32767
- +typedef i16 ynVar;
- +#else
- +typedef int ynVar;
- +#endif
- +
- +/*
- +** Each node of an expression in the parse tree is an instance
- +** of this structure.
- +**
- +** Expr.op is the opcode. The integer parser token codes are reused
- +** as opcodes here. For example, the parser defines TK_GE to be an integer
- +** code representing the ">=" operator. This same integer code is reused
- +** to represent the greater-than-or-equal-to operator in the expression
- +** tree.
- +**
- +** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
- +** or TK_STRING), then Expr.token contains the text of the SQL literal. If
- +** the expression is a variable (TK_VARIABLE), then Expr.token contains the
- +** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
- +** then Expr.token contains the name of the function.
- +**
- +** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
- +** binary operator. Either or both may be NULL.
- +**
- +** Expr.x.pList is a list of arguments if the expression is an SQL function,
- +** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
- +** Expr.x.pSelect is used if the expression is a sub-select or an expression of
- +** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
- +** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
- +** valid.
- +**
- +** An expression of the form ID or ID.ID refers to a column in a table.
- +** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
- +** the integer cursor number of a VDBE cursor pointing to that table and
- +** Expr.iColumn is the column number for the specific column. If the
- +** expression is used as a result in an aggregate SELECT, then the
- +** value is also stored in the Expr.iAgg column in the aggregate so that
- +** it can be accessed after all aggregates are computed.
- +**
- +** If the expression is an unbound variable marker (a question mark
- +** character '?' in the original SQL) then the Expr.iTable holds the index
- +** number for that variable.
- +**
- +** If the expression is a subquery then Expr.iColumn holds an integer
- +** register number containing the result of the subquery. If the
- +** subquery gives a constant result, then iTable is -1. If the subquery
- +** gives a different answer at different times during statement processing
- +** then iTable is the address of a subroutine that computes the subquery.
- +**
- +** If the Expr is of type OP_Column, and the table it is selecting from
- +** is a disk table or the "old.*" pseudo-table, then pTab points to the
- +** corresponding table definition.
- +**
- +** ALLOCATION NOTES:
- +**
- +** Expr objects can use a lot of memory space in database schema. To
- +** help reduce memory requirements, sometimes an Expr object will be
- +** truncated. And to reduce the number of memory allocations, sometimes
- +** two or more Expr objects will be stored in a single memory allocation,
- +** together with Expr.zToken strings.
- +**
- +** If the EP_Reduced and EP_TokenOnly flags are set when
- +** an Expr object is truncated. When EP_Reduced is set, then all
- +** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
- +** are contained within the same memory allocation. Note, however, that
- +** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
- +** allocated, regardless of whether or not EP_Reduced is set.
- +*/
- +struct Expr {
- + u8 op; /* Operation performed by this node */
- + char affExpr; /* affinity, or RAISE type */
- + u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
- + ** TK_COLUMN: the value of p5 for OP_Column
- + ** TK_AGG_FUNCTION: nesting depth
- + ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
- +#ifdef SQLITE_DEBUG
- + u8 vvaFlags; /* Verification flags. */
- +#endif
- + u32 flags; /* Various flags. EP_* See below */
- + union {
- + char *zToken; /* Token value. Zero terminated and dequoted */
- + int iValue; /* Non-negative integer value if EP_IntValue */
- + } u;
- +
- + /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
- + ** space is allocated for the fields below this point. An attempt to
- + ** access them will result in a segfault or malfunction.
- + *********************************************************************/
- +
- + Expr *pLeft; /* Left subnode */
- + Expr *pRight; /* Right subnode */
- + union {
- + ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
- + Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
- + } x;
- +
- + /* If the EP_Reduced flag is set in the Expr.flags mask, then no
- + ** space is allocated for the fields below this point. An attempt to
- + ** access them will result in a segfault or malfunction.
- + *********************************************************************/
- +
- +#if SQLITE_MAX_EXPR_DEPTH>0
- + int nHeight; /* Height of the tree headed by this node */
- +#endif
- + int iTable; /* TK_COLUMN: cursor number of table holding column
- + ** TK_REGISTER: register number
- + ** TK_TRIGGER: 1 -> new, 0 -> old
- + ** EP_Unlikely: 134217728 times likelihood
- + ** TK_IN: ephemerial table holding RHS
- + ** TK_SELECT_COLUMN: Number of columns on the LHS
- + ** TK_SELECT: 1st register of result vector */
- + ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
- + ** TK_VARIABLE: variable number (always >= 1).
- + ** TK_SELECT_COLUMN: column of the result vector */
- + i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
- + i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
- + AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
- + union {
- + Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
- + ** for a column of an index on an expression */
- + Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
- + struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
- + int iAddr; /* Subroutine entry address */
- + int regReturn; /* Register used to hold return address */
- + } sub;
- + } y;
- +};
- +
- +/*
- +** The following are the meanings of bits in the Expr.flags field.
- +** Value restrictions:
- +**
- +** EP_Agg == NC_HasAgg == SF_HasAgg
- +** EP_Win == NC_HasWin
- +*/
- +#define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */
- +#define EP_Distinct 0x000002 /* Aggregate function with DISTINCT keyword */
- +#define EP_HasFunc 0x000004 /* Contains one or more functions of any kind */
- +#define EP_FixedCol 0x000008 /* TK_Column with a known fixed value */
- +#define EP_Agg 0x000010 /* Contains one or more aggregate functions */
- +#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
- +#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
- +#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
- +#define EP_Collate 0x000100 /* Tree contains a TK_COLLATE operator */
- +#define EP_Commuted 0x000200 /* Comparison operator has been commuted */
- +#define EP_IntValue 0x000400 /* Integer value contained in u.iValue */
- +#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
- +#define EP_Skip 0x001000 /* Operator does not contribute to affinity */
- +#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
- +#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
- +#define EP_Win 0x008000 /* Contains window functions */
- +#define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
- + /* 0x020000 // available for reuse */
- +#define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */
- +#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
- +#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
- +#define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */
- +#define EP_Alias 0x400000 /* Is an alias for a result set column */
- +#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
- +#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
- +#define EP_Subrtn 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
- +#define EP_Quoted 0x4000000 /* TK_ID was originally quoted */
- +#define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */
- +#define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */
- +#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
- +#define EP_FromDDL 0x40000000 /* Originates from sqlite_master */
- + /* 0x80000000 // Available */
- +
- +/*
- +** The EP_Propagate mask is a set of properties that automatically propagate
- +** upwards into parent nodes.
- +*/
- +#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
- +
- +/*
- +** These macros can be used to test, set, or clear bits in the
- +** Expr.flags field.
- +*/
- +#define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
- +#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
- +#define ExprSetProperty(E,P) (E)->flags|=(P)
- +#define ExprClearProperty(E,P) (E)->flags&=~(P)
- +#define ExprAlwaysTrue(E) (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
- +#define ExprAlwaysFalse(E) (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
- +
- +
- +/* Flags for use with Expr.vvaFlags
- +*/
- +#define EP_NoReduce 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
- +#define EP_Immutable 0x02 /* Do not change this Expr node */
- +
- +/* The ExprSetVVAProperty() macro is used for Verification, Validation,
- +** and Accreditation only. It works like ExprSetProperty() during VVA
- +** processes but is a no-op for delivery.
- +*/
- +#ifdef SQLITE_DEBUG
- +# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
- +# define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
- +# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
- +#else
- +# define ExprSetVVAProperty(E,P)
- +# define ExprHasVVAProperty(E,P) 0
- +# define ExprClearVVAProperties(E)
- +#endif
- +
- +/*
- +** Macros to determine the number of bytes required by a normal Expr
- +** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
- +** and an Expr struct with the EP_TokenOnly flag set.
- +*/
- +#define EXPR_FULLSIZE sizeof(Expr) /* Full size */
- +#define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
- +#define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
- +
- +/*
- +** Flags passed to the sqlite3ExprDup() function. See the header comment
- +** above sqlite3ExprDup() for details.
- +*/
- +#define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
- +
- +/*
- +** True if the expression passed as an argument was a function with
- +** an OVER() clause (a window function).
- +*/
- +#ifdef SQLITE_OMIT_WINDOWFUNC
- +# define IsWindowFunc(p) 0
- +#else
- +# define IsWindowFunc(p) ( \
- + ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
- + )
- +#endif
- +
- +/*
- +** A list of expressions. Each expression may optionally have a
- +** name. An expr/name combination can be used in several ways, such
- +** as the list of "expr AS ID" fields following a "SELECT" or in the
- +** list of "ID = expr" items in an UPDATE. A list of expressions can
- +** also be used as the argument to a function, in which case the a.zName
- +** field is not used.
- +**
- +** In order to try to keep memory usage down, the Expr.a.zEName field
- +** is used for multiple purposes:
- +**
- +** eEName Usage
- +** ---------- -------------------------
- +** ENAME_NAME (1) the AS of result set column
- +** (2) COLUMN= of an UPDATE
- +**
- +** ENAME_TAB DB.TABLE.NAME used to resolve names
- +** of subqueries
- +**
- +** ENAME_SPAN Text of the original result set
- +** expression.
- +*/
- +struct ExprList {
- + int nExpr; /* Number of expressions on the list */
- + struct ExprList_item { /* For each expression in the list */
- + Expr *pExpr; /* The parse tree for this expression */
- + char *zEName; /* Token associated with this expression */
- + u8 sortFlags; /* Mask of KEYINFO_ORDER_* flags */
- + unsigned eEName :2; /* Meaning of zEName */
- + unsigned done :1; /* A flag to indicate when processing is finished */
- + unsigned reusable :1; /* Constant expression is reusable */
- + unsigned bSorterRef :1; /* Defer evaluation until after sorting */
- + unsigned bNulls: 1; /* True if explicit "NULLS FIRST/LAST" */
- + union {
- + struct {
- + u16 iOrderByCol; /* For ORDER BY, column number in result set */
- + u16 iAlias; /* Index into Parse.aAlias[] for zName */
- + } x;
- + int iConstExprReg; /* Register in which Expr value is cached */
- + } u;
- + } a[1]; /* One slot for each expression in the list */
- +};
- +
- +/*
- +** Allowed values for Expr.a.eEName
- +*/
- +#define ENAME_NAME 0 /* The AS clause of a result set */
- +#define ENAME_SPAN 1 /* Complete text of the result set expression */
- +#define ENAME_TAB 2 /* "DB.TABLE.NAME" for the result set */
- +
- +/*
- +** An instance of this structure can hold a simple list of identifiers,
- +** such as the list "a,b,c" in the following statements:
- +**
- +** INSERT INTO t(a,b,c) VALUES ...;
- +** CREATE INDEX idx ON t(a,b,c);
- +** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
- +**
- +** The IdList.a.idx field is used when the IdList represents the list of
- +** column names after a table name in an INSERT statement. In the statement
- +**
- +** INSERT INTO t(a,b,c) ...
- +**
- +** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
- +*/
- +struct IdList {
- + struct IdList_item {
- + char *zName; /* Name of the identifier */
- + int idx; /* Index in some Table.aCol[] of a column named zName */
- + } *a;
- + int nId; /* Number of identifiers on the list */
- +};
- +
- +/*
- +** The following structure describes the FROM clause of a SELECT statement.
- +** Each table or subquery in the FROM clause is a separate element of
- +** the SrcList.a[] array.
- +**
- +** With the addition of multiple database support, the following structure
- +** can also be used to describe a particular table such as the table that
- +** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
- +** such a table must be a simple name: ID. But in SQLite, the table can
- +** now be identified by a database name, a dot, then the table name: ID.ID.
- +**
- +** The jointype starts out showing the join type between the current table
- +** and the next table on the list. The parser builds the list this way.
- +** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
- +** jointype expresses the join between the table and the previous table.
- +**
- +** In the colUsed field, the high-order bit (bit 63) is set if the table
- +** contains more than 63 columns and the 64-th or later column is used.
- +*/
- +struct SrcList {
- + int nSrc; /* Number of tables or subqueries in the FROM clause */
- + u32 nAlloc; /* Number of entries allocated in a[] below */
- + struct SrcList_item {
- + Schema *pSchema; /* Schema to which this item is fixed */
- + char *zDatabase; /* Name of database holding this table */
- + char *zName; /* Name of the table */
- + char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
- + Table *pTab; /* An SQL table corresponding to zName */
- + Select *pSelect; /* A SELECT statement used in place of a table name */
- + int addrFillSub; /* Address of subroutine to manifest a subquery */
- + int regReturn; /* Register holding return address of addrFillSub */
- + int regResult; /* Registers holding results of a co-routine */
- + struct {
- + u8 jointype; /* Type of join between this table and the previous */
- + unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
- + unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
- + unsigned isTabFunc :1; /* True if table-valued-function syntax */
- + unsigned isCorrelated :1; /* True if sub-query is correlated */
- + unsigned viaCoroutine :1; /* Implemented as a co-routine */
- + unsigned isRecursive :1; /* True for recursive reference in WITH */
- + unsigned fromDDL :1; /* Comes from sqlite_master */
- + } fg;
- + int iCursor; /* The VDBE cursor number used to access this table */
- + Expr *pOn; /* The ON clause of a join */
- + IdList *pUsing; /* The USING clause of a join */
- + Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
- + union {
- + char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
- + ExprList *pFuncArg; /* Arguments to table-valued-function */
- + } u1;
- + Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
- + } a[1]; /* One entry for each identifier on the list */
- +};
- +
- +/*
- +** Permitted values of the SrcList.a.jointype field
- +*/
- +#define JT_INNER 0x0001 /* Any kind of inner or cross join */
- +#define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */
- +#define JT_NATURAL 0x0004 /* True for a "natural" join */
- +#define JT_LEFT 0x0008 /* Left outer join */
- +#define JT_RIGHT 0x0010 /* Right outer join */
- +#define JT_OUTER 0x0020 /* The "OUTER" keyword is present */
- +#define JT_ERROR 0x0040 /* unknown or unsupported join type */
- +
- +
- +/*
- +** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
- +** and the WhereInfo.wctrlFlags member.
- +**
- +** Value constraints (enforced via assert()):
- +** WHERE_USE_LIMIT == SF_FixedLimit
- +*/
- +#define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
- +#define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
- +#define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
- +#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
- +#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
- +#define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */
- +#define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
- + ** the OR optimization */
- +#define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */
- +#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
- +#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
- +#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
- +#define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */
- +#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
- +#define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */
- + /* 0x2000 not currently used */
- +#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
- + /* 0x8000 not currently used */
- +
- +/* Allowed return values from sqlite3WhereIsDistinct()
- +*/
- +#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
- +#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
- +#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
- +#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
- +
- +/*
- +** A NameContext defines a context in which to resolve table and column
- +** names. The context consists of a list of tables (the pSrcList) field and
- +** a list of named expression (pEList). The named expression list may
- +** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
- +** to the table being operated on by INSERT, UPDATE, or DELETE. The
- +** pEList corresponds to the result set of a SELECT and is NULL for
- +** other statements.
- +**
- +** NameContexts can be nested. When resolving names, the inner-most
- +** context is searched first. If no match is found, the next outer
- +** context is checked. If there is still no match, the next context
- +** is checked. This process continues until either a match is found
- +** or all contexts are check. When a match is found, the nRef member of
- +** the context containing the match is incremented.
- +**
- +** Each subquery gets a new NameContext. The pNext field points to the
- +** NameContext in the parent query. Thus the process of scanning the
- +** NameContext list corresponds to searching through successively outer
- +** subqueries looking for a match.
- +*/
- +struct NameContext {
- + Parse *pParse; /* The parser */
- + SrcList *pSrcList; /* One or more tables used to resolve names */
- + union {
- + ExprList *pEList; /* Optional list of result-set columns */
- + AggInfo *pAggInfo; /* Information about aggregates at this level */
- + Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
- + } uNC;
- + NameContext *pNext; /* Next outer name context. NULL for outermost */
- + int nRef; /* Number of names resolved by this context */
- + int nErr; /* Number of errors encountered while resolving names */
- + int ncFlags; /* Zero or more NC_* flags defined below */
- + Select *pWinSelect; /* SELECT statement for any window functions */
- +};
- +
- +/*
- +** Allowed values for the NameContext, ncFlags field.
- +**
- +** Value constraints (all checked via assert()):
- +** NC_HasAgg == SF_HasAgg == EP_Agg
- +** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
- +** NC_HasWin == EP_Win
- +**
- +*/
- +#define NC_AllowAgg 0x00001 /* Aggregate functions are allowed here */
- +#define NC_PartIdx 0x00002 /* True if resolving a partial index WHERE */
- +#define NC_IsCheck 0x00004 /* True if resolving a CHECK constraint */
- +#define NC_GenCol 0x00008 /* True for a GENERATED ALWAYS AS clause */
- +#define NC_HasAgg 0x00010 /* One or more aggregate functions seen */
- +#define NC_IdxExpr 0x00020 /* True if resolving columns of CREATE INDEX */
- +#define NC_SelfRef 0x0002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */
- +#define NC_VarSelect 0x00040 /* A correlated subquery has been seen */
- +#define NC_UEList 0x00080 /* True if uNC.pEList is used */
- +#define NC_UAggInfo 0x00100 /* True if uNC.pAggInfo is used */
- +#define NC_UUpsert 0x00200 /* True if uNC.pUpsert is used */
- +#define NC_MinMaxAgg 0x01000 /* min/max aggregates seen. See note above */
- +#define NC_Complex 0x02000 /* True if a function or subquery seen */
- +#define NC_AllowWin 0x04000 /* Window functions are allowed here */
- +#define NC_HasWin 0x08000 /* One or more window functions seen */
- +#define NC_IsDDL 0x10000 /* Resolving names in a CREATE statement */
- +#define NC_InAggFunc 0x20000 /* True if analyzing arguments to an agg func */
- +#define NC_FromDDL 0x40000 /* SQL text comes from sqlite_master */
- +
- +/*
- +** An instance of the following object describes a single ON CONFLICT
- +** clause in an upsert.
- +**
- +** The pUpsertTarget field is only set if the ON CONFLICT clause includes
- +** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
- +** conflict-target clause.) The pUpsertTargetWhere is the optional
- +** WHERE clause used to identify partial unique indexes.
- +**
- +** pUpsertSet is the list of column=expr terms of the UPDATE statement.
- +** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
- +** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
- +** WHERE clause is omitted.
- +*/
- +struct Upsert {
- + ExprList *pUpsertTarget; /* Optional description of conflicting index */
- + Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
- + ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
- + Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
- + /* The fields above comprise the parse tree for the upsert clause.
- + ** The fields below are used to transfer information from the INSERT
- + ** processing down into the UPDATE processing while generating code.
- + ** Upsert owns the memory allocated above, but not the memory below. */
- + Index *pUpsertIdx; /* Constraint that pUpsertTarget identifies */
- + SrcList *pUpsertSrc; /* Table to be updated */
- + int regData; /* First register holding array of VALUES */
- + int iDataCur; /* Index of the data cursor */
- + int iIdxCur; /* Index of the first index cursor */
- +};
- +
- +/*
- +** An instance of the following structure contains all information
- +** needed to generate code for a single SELECT statement.
- +**
- +** See the header comment on the computeLimitRegisters() routine for a
- +** detailed description of the meaning of the iLimit and iOffset fields.
- +**
- +** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
- +** These addresses must be stored so that we can go back and fill in
- +** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
- +** the number of columns in P2 can be computed at the same time
- +** as the OP_OpenEphm instruction is coded because not
- +** enough information about the compound query is known at that point.
- +** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
- +** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
- +** sequences for the ORDER BY clause.
- +*/
- +struct Select {
- + u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
- + LogEst nSelectRow; /* Estimated number of result rows */
- + u32 selFlags; /* Various SF_* values */
- + int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
- + u32 selId; /* Unique identifier number for this SELECT */
- + int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
- + ExprList *pEList; /* The fields of the result */
- + SrcList *pSrc; /* The FROM clause */
- + Expr *pWhere; /* The WHERE clause */
- + ExprList *pGroupBy; /* The GROUP BY clause */
- + Expr *pHaving; /* The HAVING clause */
- + ExprList *pOrderBy; /* The ORDER BY clause */
- + Select *pPrior; /* Prior select in a compound select statement */
- + Select *pNext; /* Next select to the left in a compound */
- + Expr *pLimit; /* LIMIT expression. NULL means not used. */
- + With *pWith; /* WITH clause attached to this select. Or NULL. */
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + Window *pWin; /* List of window functions */
- + Window *pWinDefn; /* List of named window definitions */
- +#endif
- +};
- +
- +/*
- +** Allowed values for Select.selFlags. The "SF" prefix stands for
- +** "Select Flag".
- +**
- +** Value constraints (all checked via assert())
- +** SF_HasAgg == NC_HasAgg
- +** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
- +** SF_FixedLimit == WHERE_USE_LIMIT
- +*/
- +#define SF_Distinct 0x0000001 /* Output should be DISTINCT */
- +#define SF_All 0x0000002 /* Includes the ALL keyword */
- +#define SF_Resolved 0x0000004 /* Identifiers have been resolved */
- +#define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */
- +#define SF_HasAgg 0x0000010 /* Contains aggregate functions */
- +#define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */
- +#define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */
- +#define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */
- +#define SF_Compound 0x0000100 /* Part of a compound query */
- +#define SF_Values 0x0000200 /* Synthesized from VALUES clause */
- +#define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */
- +#define SF_NestedFrom 0x0000800 /* Part of a parenthesized FROM clause */
- +#define SF_MinMaxAgg 0x0001000 /* Aggregate containing min() or max() */
- +#define SF_Recursive 0x0002000 /* The recursive part of a recursive CTE */
- +#define SF_FixedLimit 0x0004000 /* nSelectRow set by a constant LIMIT */
- +#define SF_MaybeConvert 0x0008000 /* Need convertCompoundSelectToSubquery() */
- +#define SF_Converted 0x0010000 /* By convertCompoundSelectToSubquery() */
- +#define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */
- +#define SF_ComplexResult 0x0040000 /* Result contains subquery or function */
- +#define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
- +#define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
- +#define SF_View 0x0200000 /* SELECT statement is a view */
- +
- +/*
- +** The results of a SELECT can be distributed in several ways, as defined
- +** by one of the following macros. The "SRT" prefix means "SELECT Result
- +** Type".
- +**
- +** SRT_Union Store results as a key in a temporary index
- +** identified by pDest->iSDParm.
- +**
- +** SRT_Except Remove results from the temporary index pDest->iSDParm.
- +**
- +** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
- +** set is not empty.
- +**
- +** SRT_Discard Throw the results away. This is used by SELECT
- +** statements within triggers whose only purpose is
- +** the side-effects of functions.
- +**
- +** All of the above are free to ignore their ORDER BY clause. Those that
- +** follow must honor the ORDER BY clause.
- +**
- +** SRT_Output Generate a row of output (using the OP_ResultRow
- +** opcode) for each row in the result set.
- +**
- +** SRT_Mem Only valid if the result is a single column.
- +** Store the first column of the first result row
- +** in register pDest->iSDParm then abandon the rest
- +** of the query. This destination implies "LIMIT 1".
- +**
- +** SRT_Set The result must be a single column. Store each
- +** row of result as the key in table pDest->iSDParm.
- +** Apply the affinity pDest->affSdst before storing
- +** results. Used to implement "IN (SELECT ...)".
- +**
- +** SRT_EphemTab Create an temporary table pDest->iSDParm and store
- +** the result there. The cursor is left open after
- +** returning. This is like SRT_Table except that
- +** this destination uses OP_OpenEphemeral to create
- +** the table first.
- +**
- +** SRT_Coroutine Generate a co-routine that returns a new row of
- +** results each time it is invoked. The entry point
- +** of the co-routine is stored in register pDest->iSDParm
- +** and the result row is stored in pDest->nDest registers
- +** starting with pDest->iSdst.
- +**
- +** SRT_Table Store results in temporary table pDest->iSDParm.
- +** SRT_Fifo This is like SRT_EphemTab except that the table
- +** is assumed to already be open. SRT_Fifo has
- +** the additional property of being able to ignore
- +** the ORDER BY clause.
- +**
- +** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
- +** But also use temporary table pDest->iSDParm+1 as
- +** a record of all prior results and ignore any duplicate
- +** rows. Name means: "Distinct Fifo".
- +**
- +** SRT_Queue Store results in priority queue pDest->iSDParm (really
- +** an index). Append a sequence number so that all entries
- +** are distinct.
- +**
- +** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
- +** the same record has never been stored before. The
- +** index at pDest->iSDParm+1 hold all prior stores.
- +*/
- +#define SRT_Union 1 /* Store result as keys in an index */
- +#define SRT_Except 2 /* Remove result from a UNION index */
- +#define SRT_Exists 3 /* Store 1 if the result is not empty */
- +#define SRT_Discard 4 /* Do not save the results anywhere */
- +#define SRT_Fifo 5 /* Store result as data with an automatic rowid */
- +#define SRT_DistFifo 6 /* Like SRT_Fifo, but unique results only */
- +#define SRT_Queue 7 /* Store result in an queue */
- +#define SRT_DistQueue 8 /* Like SRT_Queue, but unique results only */
- +
- +/* The ORDER BY clause is ignored for all of the above */
- +#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
- +
- +#define SRT_Output 9 /* Output each row of result */
- +#define SRT_Mem 10 /* Store result in a memory cell */
- +#define SRT_Set 11 /* Store results as keys in an index */
- +#define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */
- +#define SRT_Coroutine 13 /* Generate a single row of result */
- +#define SRT_Table 14 /* Store result as data with an automatic rowid */
- +
- +/*
- +** An instance of this object describes where to put of the results of
- +** a SELECT statement.
- +*/
- +struct SelectDest {
- + u8 eDest; /* How to dispose of the results. On of SRT_* above. */
- + int iSDParm; /* A parameter used by the eDest disposal method */
- + int iSdst; /* Base register where results are written */
- + int nSdst; /* Number of registers allocated */
- + char *zAffSdst; /* Affinity used when eDest==SRT_Set */
- + ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
- +};
- +
- +/*
- +** During code generation of statements that do inserts into AUTOINCREMENT
- +** tables, the following information is attached to the Table.u.autoInc.p
- +** pointer of each autoincrement table to record some side information that
- +** the code generator needs. We have to keep per-table autoincrement
- +** information in case inserts are done within triggers. Triggers do not
- +** normally coordinate their activities, but we do need to coordinate the
- +** loading and saving of autoincrement information.
- +*/
- +struct AutoincInfo {
- + AutoincInfo *pNext; /* Next info block in a list of them all */
- + Table *pTab; /* Table this info block refers to */
- + int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
- + int regCtr; /* Memory register holding the rowid counter */
- +};
- +
- +/*
- +** At least one instance of the following structure is created for each
- +** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
- +** statement. All such objects are stored in the linked list headed at
- +** Parse.pTriggerPrg and deleted once statement compilation has been
- +** completed.
- +**
- +** A Vdbe sub-program that implements the body and WHEN clause of trigger
- +** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
- +** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
- +** The Parse.pTriggerPrg list never contains two entries with the same
- +** values for both pTrigger and orconf.
- +**
- +** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
- +** accessed (or set to 0 for triggers fired as a result of INSERT
- +** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
- +** a mask of new.* columns used by the program.
- +*/
- +struct TriggerPrg {
- + Trigger *pTrigger; /* Trigger this program was coded from */
- + TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
- + SubProgram *pProgram; /* Program implementing pTrigger/orconf */
- + int orconf; /* Default ON CONFLICT policy */
- + u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
- +};
- +
- +/*
- +** The yDbMask datatype for the bitmask of all attached databases.
- +*/
- +#if SQLITE_MAX_ATTACHED>30
- + typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
- +# define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0)
- +# define DbMaskZero(M) memset((M),0,sizeof(M))
- +# define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7))
- +# define DbMaskAllZero(M) sqlite3DbMaskAllZero(M)
- +# define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0)
- +#else
- + typedef unsigned int yDbMask;
- +# define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0)
- +# define DbMaskZero(M) (M)=0
- +# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
- +# define DbMaskAllZero(M) (M)==0
- +# define DbMaskNonZero(M) (M)!=0
- +#endif
- +
- +/*
- +** An SQL parser context. A copy of this structure is passed through
- +** the parser and down into all the parser action routine in order to
- +** carry around information that is global to the entire parse.
- +**
- +** The structure is divided into two parts. When the parser and code
- +** generate call themselves recursively, the first part of the structure
- +** is constant but the second part is reset at the beginning and end of
- +** each recursion.
- +**
- +** The nTableLock and aTableLock variables are only used if the shared-cache
- +** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
- +** used to store the set of table-locks required by the statement being
- +** compiled. Function sqlite3TableLock() is used to add entries to the
- +** list.
- +*/
- +struct Parse {
- + sqlite3 *db; /* The main database structure */
- + char *zErrMsg; /* An error message */
- + Vdbe *pVdbe; /* An engine for executing database bytecode */
- + int rc; /* Return code from execution */
- + u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
- + u8 checkSchema; /* Causes schema cookie check after an error */
- + u8 nested; /* Number of nested calls to the parser/code generator */
- + u8 nTempReg; /* Number of temporary registers in aTempReg[] */
- + u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
- + u8 mayAbort; /* True if statement may throw an ABORT exception */
- + u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
- + u8 okConstFactor; /* OK to factor out constants */
- + u8 disableLookaside; /* Number of times lookaside has been disabled */
- + u8 disableVtab; /* Disable all virtual tables for this parse */
- + int nRangeReg; /* Size of the temporary register block */
- + int iRangeReg; /* First register in temporary register block */
- + int nErr; /* Number of errors seen */
- + int nTab; /* Number of previously allocated VDBE cursors */
- + int nMem; /* Number of memory cells used so far */
- + int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
- + int iSelfTab; /* Table associated with an index on expr, or negative
- + ** of the base register during check-constraint eval */
- + int nLabel; /* The *negative* of the number of labels used */
- + int nLabelAlloc; /* Number of slots in aLabel */
- + int *aLabel; /* Space to hold the labels */
- + ExprList *pConstExpr;/* Constant expressions */
- + Token constraintName;/* Name of the constraint currently being parsed */
- + yDbMask writeMask; /* Start a write transaction on these databases */
- + yDbMask cookieMask; /* Bitmask of schema verified databases */
- + int regRowid; /* Register holding rowid of CREATE TABLE entry */
- + int regRoot; /* Register holding root page number for new objects */
- + int nMaxArg; /* Max args passed to user function by sub-program */
- + int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
- +#ifndef SQLITE_OMIT_SHARED_CACHE
- + int nTableLock; /* Number of locks in aTableLock */
- + TableLock *aTableLock; /* Required table locks for shared-cache mode */
- +#endif
- + AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
- + Parse *pToplevel; /* Parse structure for main program (or NULL) */
- + Table *pTriggerTab; /* Table triggers are being coded for */
- + Parse *pParentParse; /* Parent parser if this parser is nested */
- + int addrCrTab; /* Address of OP_CreateBtree opcode on CREATE TABLE */
- + u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
- + u32 oldmask; /* Mask of old.* columns referenced */
- + u32 newmask; /* Mask of new.* columns referenced */
- + u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
- + u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
- + u8 disableTriggers; /* True to disable triggers */
- +
- + /**************************************************************************
- + ** Fields above must be initialized to zero. The fields that follow,
- + ** down to the beginning of the recursive section, do not need to be
- + ** initialized as they will be set before being used. The boundary is
- + ** determined by offsetof(Parse,aTempReg).
- + **************************************************************************/
- +
- + int aTempReg[8]; /* Holding area for temporary registers */
- + Token sNameToken; /* Token with unqualified schema object name */
- +
- + /************************************************************************
- + ** Above is constant between recursions. Below is reset before and after
- + ** each recursion. The boundary between these two regions is determined
- + ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
- + ** first field in the recursive region.
- + ************************************************************************/
- +
- + Token sLastToken; /* The last token parsed */
- + ynVar nVar; /* Number of '?' variables seen in the SQL so far */
- + u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
- + u8 explain; /* True if the EXPLAIN flag is found on the query */
- +#if !(defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE))
- + u8 eParseMode; /* PARSE_MODE_XXX constant */
- +#endif
- +#ifndef SQLITE_OMIT_VIRTUALTABLE
- + int nVtabLock; /* Number of virtual tables to lock */
- +#endif
- + int nHeight; /* Expression tree height of current sub-select */
- +#ifndef SQLITE_OMIT_EXPLAIN
- + int addrExplain; /* Address of current OP_Explain opcode */
- +#endif
- + VList *pVList; /* Mapping between variable names and numbers */
- + Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
- + const char *zTail; /* All SQL text past the last semicolon parsed */
- + Table *pNewTable; /* A table being constructed by CREATE TABLE */
- + Index *pNewIndex; /* An index being constructed by CREATE INDEX.
- + ** Also used to hold redundant UNIQUE constraints
- + ** during a RENAME COLUMN */
- + Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
- + const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
- +#ifndef SQLITE_OMIT_VIRTUALTABLE
- + Token sArg; /* Complete text of a module argument */
- + Table **apVtabLock; /* Pointer to virtual tables needing locking */
- +#endif
- + Table *pZombieTab; /* List of Table objects to delete after code gen */
- + TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
- + With *pWith; /* Current WITH clause, or NULL */
- + With *pWithToFree; /* Free this WITH object at the end of the parse */
- +#ifndef SQLITE_OMIT_ALTERTABLE
- + RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
- +#endif
- +};
- +
- +#define PARSE_MODE_NORMAL 0
- +#define PARSE_MODE_DECLARE_VTAB 1
- +#define PARSE_MODE_RENAME 2
- +#define PARSE_MODE_UNMAP 3
- +
- +/*
- +** Sizes and pointers of various parts of the Parse object.
- +*/
- +#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/
- +#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */
- +#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
- +#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */
- +
- +/*
- +** Return true if currently inside an sqlite3_declare_vtab() call.
- +*/
- +#ifdef SQLITE_OMIT_VIRTUALTABLE
- + #define IN_DECLARE_VTAB 0
- +#else
- + #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
- +#endif
- +
- +#if defined(SQLITE_OMIT_ALTERTABLE)
- + #define IN_RENAME_OBJECT 0
- +#else
- + #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
- +#endif
- +
- +#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
- + #define IN_SPECIAL_PARSE 0
- +#else
- + #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
- +#endif
- +
- +/*
- +** An instance of the following structure can be declared on a stack and used
- +** to save the Parse.zAuthContext value so that it can be restored later.
- +*/
- +struct AuthContext {
- + const char *zAuthContext; /* Put saved Parse.zAuthContext here */
- + Parse *pParse; /* The Parse structure */
- +};
- +
- +/*
- +** Bitfield flags for P5 value in various opcodes.
- +**
- +** Value constraints (enforced via assert()):
- +** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
- +** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
- +** OPFLAG_BULKCSR == BTREE_BULKLOAD
- +** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
- +** OPFLAG_FORDELETE == BTREE_FORDELETE
- +** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
- +** OPFLAG_AUXDELETE == BTREE_AUXDELETE
- +*/
- +#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
- + /* Also used in P2 (not P5) of OP_Delete */
- +#define OPFLAG_NOCHNG 0x01 /* OP_VColumn nochange for UPDATE */
- +#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
- +#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
- +#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
- +#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
- +#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
- +#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
- +#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
- +#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
- +#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
- +#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
- +#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
- +#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
- +#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
- +#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
- +#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
- +#define OPFLAG_NOCHNG_MAGIC 0x6d /* OP_MakeRecord: serialtype 10 is ok */
- +
- +/*
- + * Each trigger present in the database schema is stored as an instance of
- + * struct Trigger.
- + *
- + * Pointers to instances of struct Trigger are stored in two ways.
- + * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
- + * database). This allows Trigger structures to be retrieved by name.
- + * 2. All triggers associated with a single table form a linked list, using the
- + * pNext member of struct Trigger. A pointer to the first element of the
- + * linked list is stored as the "pTrigger" member of the associated
- + * struct Table.
- + *
- + * The "step_list" member points to the first element of a linked list
- + * containing the SQL statements specified as the trigger program.
- + */
- +struct Trigger {
- + char *zName; /* The name of the trigger */
- + char *table; /* The table or view to which the trigger applies */
- + u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
- + u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
- + Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
- + IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
- + the <column-list> is stored here */
- + Schema *pSchema; /* Schema containing the trigger */
- + Schema *pTabSchema; /* Schema containing the table */
- + TriggerStep *step_list; /* Link list of trigger program steps */
- + Trigger *pNext; /* Next trigger associated with the table */
- +};
- +
- +/*
- +** A trigger is either a BEFORE or an AFTER trigger. The following constants
- +** determine which.
- +**
- +** If there are multiple triggers, you might of some BEFORE and some AFTER.
- +** In that cases, the constants below can be ORed together.
- +*/
- +#define TRIGGER_BEFORE 1
- +#define TRIGGER_AFTER 2
- +
- +/*
- + * An instance of struct TriggerStep is used to store a single SQL statement
- + * that is a part of a trigger-program.
- + *
- + * Instances of struct TriggerStep are stored in a singly linked list (linked
- + * using the "pNext" member) referenced by the "step_list" member of the
- + * associated struct Trigger instance. The first element of the linked list is
- + * the first step of the trigger-program.
- + *
- + * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
- + * "SELECT" statement. The meanings of the other members is determined by the
- + * value of "op" as follows:
- + *
- + * (op == TK_INSERT)
- + * orconf -> stores the ON CONFLICT algorithm
- + * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
- + * this stores a pointer to the SELECT statement. Otherwise NULL.
- + * zTarget -> Dequoted name of the table to insert into.
- + * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
- + * this stores values to be inserted. Otherwise NULL.
- + * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
- + * statement, then this stores the column-names to be
- + * inserted into.
- + *
- + * (op == TK_DELETE)
- + * zTarget -> Dequoted name of the table to delete from.
- + * pWhere -> The WHERE clause of the DELETE statement if one is specified.
- + * Otherwise NULL.
- + *
- + * (op == TK_UPDATE)
- + * zTarget -> Dequoted name of the table to update.
- + * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
- + * Otherwise NULL.
- + * pExprList -> A list of the columns to update and the expressions to update
- + * them to. See sqlite3Update() documentation of "pChanges"
- + * argument.
- + *
- + */
- +struct TriggerStep {
- + u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
- + u8 orconf; /* OE_Rollback etc. */
- + Trigger *pTrig; /* The trigger that this step is a part of */
- + Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
- + char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
- + Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
- + ExprList *pExprList; /* SET clause for UPDATE */
- + IdList *pIdList; /* Column names for INSERT */
- + Upsert *pUpsert; /* Upsert clauses on an INSERT */
- + char *zSpan; /* Original SQL text of this command */
- + TriggerStep *pNext; /* Next in the link-list */
- + TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
- +};
- +
- +/*
- +** The following structure contains information used by the sqliteFix...
- +** routines as they walk the parse tree to make database references
- +** explicit.
- +*/
- +typedef struct DbFixer DbFixer;
- +struct DbFixer {
- + Parse *pParse; /* The parsing context. Error messages written here */
- + Schema *pSchema; /* Fix items to this schema */
- + u8 bTemp; /* True for TEMP schema entries */
- + const char *zDb; /* Make sure all objects are contained in this database */
- + const char *zType; /* Type of the container - used for error messages */
- + const Token *pName; /* Name of the container - used for error messages */
- +};
- +
- +/*
- +** An objected used to accumulate the text of a string where we
- +** do not necessarily know how big the string will be in the end.
- +*/
- +struct sqlite3_str {
- + sqlite3 *db; /* Optional database for lookaside. Can be NULL */
- + char *zText; /* The string collected so far */
- + u32 nAlloc; /* Amount of space allocated in zText */
- + u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
- + u32 nChar; /* Length of the string so far */
- + u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
- + u8 printfFlags; /* SQLITE_PRINTF flags below */
- +};
- +#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
- +#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */
- +#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */
- +
- +#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
- +
- +
- +/*
- +** A pointer to this structure is used to communicate information
- +** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
- +*/
- +typedef struct {
- + sqlite3 *db; /* The database being initialized */
- + char **pzErrMsg; /* Error message stored here */
- + int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
- + int rc; /* Result code stored here */
- + u32 mInitFlags; /* Flags controlling error messages */
- + u32 nInitRow; /* Number of rows processed */
- +} InitData;
- +
- +/*
- +** Allowed values for mInitFlags
- +*/
- +#define INITFLAG_AlterTable 0x0001 /* This is a reparse after ALTER TABLE */
- +
- +/*
- +** Structure containing global configuration data for the SQLite library.
- +**
- +** This structure also contains some state information.
- +*/
- +struct Sqlite3Config {
- + int bMemstat; /* True to enable memory status */
- + u8 bCoreMutex; /* True to enable core mutexing */
- + u8 bFullMutex; /* True to enable full mutexing */
- + u8 bOpenUri; /* True to interpret filenames as URIs */
- + u8 bUseCis; /* Use covering indices for full-scans */
- + u8 bSmallMalloc; /* Avoid large memory allocations if true */
- + u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
- + int mxStrlen; /* Maximum string length */
- + int neverCorrupt; /* Database is always well-formed */
- + int szLookaside; /* Default lookaside buffer size */
- + int nLookaside; /* Default lookaside buffer count */
- + int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
- + sqlite3_mem_methods m; /* Low-level memory allocation interface */
- + sqlite3_mutex_methods mutex; /* Low-level mutex interface */
- + sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
- + void *pHeap; /* Heap storage space */
- + int nHeap; /* Size of pHeap[] */
- + int mnReq, mxReq; /* Min and max heap requests sizes */
- + sqlite3_int64 szMmap; /* mmap() space per open file */
- + sqlite3_int64 mxMmap; /* Maximum value for szMmap */
- + void *pPage; /* Page cache memory */
- + int szPage; /* Size of each page in pPage[] */
- + int nPage; /* Number of pages in pPage[] */
- + int mxParserStack; /* maximum depth of the parser stack */
- + int sharedCacheEnabled; /* true if shared-cache mode enabled */
- + u32 szPma; /* Maximum Sorter PMA size */
- + /* The above might be initialized to non-zero. The following need to always
- + ** initially be zero, however. */
- + int isInit; /* True after initialization has finished */
- + int inProgress; /* True while initialization in progress */
- + int isMutexInit; /* True after mutexes are initialized */
- + int isMallocInit; /* True after malloc is initialized */
- + int isPCacheInit; /* True after malloc is initialized */
- + int nRefInitMutex; /* Number of users of pInitMutex */
- + sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
- + void (*xLog)(void*,int,const char*); /* Function for logging */
- + void *pLogArg; /* First argument to xLog() */
- +#ifdef SQLITE_ENABLE_SQLLOG
- + void(*xSqllog)(void*,sqlite3*,const char*, int);
- + void *pSqllogArg;
- +#endif
- +#ifdef SQLITE_VDBE_COVERAGE
- + /* The following callback (if not NULL) is invoked on every VDBE branch
- + ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
- + */
- + void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
- + void *pVdbeBranchArg; /* 1st argument */
- +#endif
- +#ifdef SQLITE_ENABLE_DESERIALIZE
- + sqlite3_int64 mxMemdbSize; /* Default max memdb size */
- +#endif
- +#ifndef SQLITE_UNTESTABLE
- + int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
- +#endif
- + int bLocaltimeFault; /* True to fail localtime() calls */
- + int iOnceResetThreshold; /* When to reset OP_Once counters */
- + u32 szSorterRef; /* Min size in bytes to use sorter-refs */
- + unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */
- +};
- +
- +/*
- +** This macro is used inside of assert() statements to indicate that
- +** the assert is only valid on a well-formed database. Instead of:
- +**
- +** assert( X );
- +**
- +** One writes:
- +**
- +** assert( X || CORRUPT_DB );
- +**
- +** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
- +** that the database is definitely corrupt, only that it might be corrupt.
- +** For most test cases, CORRUPT_DB is set to false using a special
- +** sqlite3_test_control(). This enables assert() statements to prove
- +** things that are always true for well-formed databases.
- +*/
- +#define CORRUPT_DB (sqlite3Config.neverCorrupt==0)
- +
- +/*
- +** Context pointer passed down through the tree-walk.
- +*/
- +struct Walker {
- + Parse *pParse; /* Parser context. */
- + int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
- + int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
- + void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
- + int walkerDepth; /* Number of subqueries */
- + u16 eCode; /* A small processing code */
- + union { /* Extra data for callback */
- + NameContext *pNC; /* Naming context */
- + int n; /* A counter */
- + int iCur; /* A cursor number */
- + SrcList *pSrcList; /* FROM clause */
- + struct SrcCount *pSrcCount; /* Counting column references */
- + struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
- + int *aiCol; /* array of column indexes */
- + struct IdxCover *pIdxCover; /* Check for index coverage */
- + struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */
- + ExprList *pGroupBy; /* GROUP BY clause */
- + Select *pSelect; /* HAVING to WHERE clause ctx */
- + struct WindowRewrite *pRewrite; /* Window rewrite context */
- + struct WhereConst *pConst; /* WHERE clause constants */
- + struct RenameCtx *pRename; /* RENAME COLUMN context */
- + struct Table *pTab; /* Table of generated column */
- + struct SrcList_item *pSrcItem; /* A single FROM clause item */
- + } u;
- +};
- +
- +/* Forward declarations */
- +int sqlite3WalkExpr(Walker*, Expr*);
- +int sqlite3WalkExprList(Walker*, ExprList*);
- +int sqlite3WalkSelect(Walker*, Select*);
- +int sqlite3WalkSelectExpr(Walker*, Select*);
- +int sqlite3WalkSelectFrom(Walker*, Select*);
- +int sqlite3ExprWalkNoop(Walker*, Expr*);
- +int sqlite3SelectWalkNoop(Walker*, Select*);
- +int sqlite3SelectWalkFail(Walker*, Select*);
- +int sqlite3WalkerDepthIncrease(Walker*,Select*);
- +void sqlite3WalkerDepthDecrease(Walker*,Select*);
- +
- +#ifdef SQLITE_DEBUG
- +void sqlite3SelectWalkAssert2(Walker*, Select*);
- +#endif
- +
- +/*
- +** Return code from the parse-tree walking primitives and their
- +** callbacks.
- +*/
- +#define WRC_Continue 0 /* Continue down into children */
- +#define WRC_Prune 1 /* Omit children but continue walking siblings */
- +#define WRC_Abort 2 /* Abandon the tree walk */
- +
- +/*
- +** An instance of this structure represents a set of one or more CTEs
- +** (common table expressions) created by a single WITH clause.
- +*/
- +struct With {
- + int nCte; /* Number of CTEs in the WITH clause */
- + With *pOuter; /* Containing WITH clause, or NULL */
- + struct Cte { /* For each CTE in the WITH clause.... */
- + char *zName; /* Name of this CTE */
- + ExprList *pCols; /* List of explicit column names, or NULL */
- + Select *pSelect; /* The definition of this CTE */
- + const char *zCteErr; /* Error message for circular references */
- + } a[1];
- +};
- +
- +#ifdef SQLITE_DEBUG
- +/*
- +** An instance of the TreeView object is used for printing the content of
- +** data structures on sqlite3DebugPrintf() using a tree-like view.
- +*/
- +struct TreeView {
- + int iLevel; /* Which level of the tree we are on */
- + u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
- +};
- +#endif /* SQLITE_DEBUG */
- +
- +/*
- +** This object is used in various ways, most (but not all) related to window
- +** functions.
- +**
- +** (1) A single instance of this structure is attached to the
- +** the Expr.y.pWin field for each window function in an expression tree.
- +** This object holds the information contained in the OVER clause,
- +** plus additional fields used during code generation.
- +**
- +** (2) All window functions in a single SELECT form a linked-list
- +** attached to Select.pWin. The Window.pFunc and Window.pExpr
- +** fields point back to the expression that is the window function.
- +**
- +** (3) The terms of the WINDOW clause of a SELECT are instances of this
- +** object on a linked list attached to Select.pWinDefn.
- +**
- +** (4) For an aggregate function with a FILTER clause, an instance
- +** of this object is stored in Expr.y.pWin with eFrmType set to
- +** TK_FILTER. In this case the only field used is Window.pFilter.
- +**
- +** The uses (1) and (2) are really the same Window object that just happens
- +** to be accessible in two different ways. Use case (3) are separate objects.
- +*/
- +struct Window {
- + char *zName; /* Name of window (may be NULL) */
- + char *zBase; /* Name of base window for chaining (may be NULL) */
- + ExprList *pPartition; /* PARTITION BY clause */
- + ExprList *pOrderBy; /* ORDER BY clause */
- + u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
- + u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
- + u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
- + u8 bImplicitFrame; /* True if frame was implicitly specified */
- + u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
- + Expr *pStart; /* Expression for "<expr> PRECEDING" */
- + Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
- + Window **ppThis; /* Pointer to this object in Select.pWin list */
- + Window *pNextWin; /* Next window function belonging to this SELECT */
- + Expr *pFilter; /* The FILTER expression */
- + FuncDef *pFunc; /* The function */
- + int iEphCsr; /* Partition buffer or Peer buffer */
- + int regAccum; /* Accumulator */
- + int regResult; /* Interim result */
- + int csrApp; /* Function cursor (used by min/max) */
- + int regApp; /* Function register (also used by min/max) */
- + int regPart; /* Array of registers for PARTITION BY values */
- + Expr *pOwner; /* Expression object this window is attached to */
- + int nBufferCol; /* Number of columns in buffer table */
- + int iArgCol; /* Offset of first argument for this function */
- + int regOne; /* Register containing constant value 1 */
- + int regStartRowid;
- + int regEndRowid;
- + u8 bExprArgs; /* Defer evaluation of window function arguments
- + ** due to the SQLITE_SUBTYPE flag */
- +};
- +
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- +void sqlite3WindowDelete(sqlite3*, Window*);
- +void sqlite3WindowUnlinkFromSelect(Window*);
- +void sqlite3WindowListDelete(sqlite3 *db, Window *p);
- +Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
- +void sqlite3WindowAttach(Parse*, Expr*, Window*);
- +void sqlite3WindowLink(Select *pSel, Window *pWin);
- +int sqlite3WindowCompare(Parse*, Window*, Window*, int);
- +void sqlite3WindowCodeInit(Parse*, Select*);
- +void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
- +int sqlite3WindowRewrite(Parse*, Select*);
- +int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
- +void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
- +Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
- +Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
- +void sqlite3WindowFunctions(void);
- +void sqlite3WindowChain(Parse*, Window*, Window*);
- +Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
- +#else
- +# define sqlite3WindowDelete(a,b)
- +# define sqlite3WindowFunctions()
- +# define sqlite3WindowAttach(a,b,c)
- +#endif
- +
- +/*
- +** Assuming zIn points to the first byte of a UTF-8 character,
- +** advance zIn to point to the first byte of the next UTF-8 character.
- +*/
- +#define SQLITE_SKIP_UTF8(zIn) { \
- + if( (*(zIn++))>=0xc0 ){ \
- + while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
- + } \
- +}
- +
- +/*
- +** The SQLITE_*_BKPT macros are substitutes for the error codes with
- +** the same name but without the _BKPT suffix. These macros invoke
- +** routines that report the line-number on which the error originated
- +** using sqlite3_log(). The routines also provide a convenient place
- +** to set a debugger breakpoint.
- +*/
- +int sqlite3ReportError(int iErr, int lineno, const char *zType);
- +int sqlite3CorruptError(int);
- +int sqlite3MisuseError(int);
- +int sqlite3CantopenError(int);
- +#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
- +#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
- +#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
- +#ifdef SQLITE_DEBUG
- + int sqlite3NomemError(int);
- + int sqlite3IoerrnomemError(int);
- +# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
- +# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
- +#else
- +# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
- +# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
- +#endif
- +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
- + int sqlite3CorruptPgnoError(int,Pgno);
- +# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P))
- +#else
- +# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__)
- +#endif
- +
- +/*
- +** FTS3 and FTS4 both require virtual table support
- +*/
- +#if defined(SQLITE_OMIT_VIRTUALTABLE)
- +# undef SQLITE_ENABLE_FTS3
- +# undef SQLITE_ENABLE_FTS4
- +#endif
- +
- +/*
- +** FTS4 is really an extension for FTS3. It is enabled using the
- +** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
- +** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
- +*/
- +#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
- +# define SQLITE_ENABLE_FTS3 1
- +#endif
- +
- +/*
- +** The ctype.h header is needed for non-ASCII systems. It is also
- +** needed by FTS3 when FTS3 is included in the amalgamation.
- +*/
- +#if !defined(SQLITE_ASCII) || \
- + (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
- +# include <ctype.h>
- +#endif
- +
- +/*
- +** The following macros mimic the standard library functions toupper(),
- +** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
- +** sqlite versions only work for ASCII characters, regardless of locale.
- +*/
- +#ifdef SQLITE_ASCII
- +# define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
- +# define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
- +# define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
- +# define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
- +# define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
- +# define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
- +# define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)])
- +# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
- +#else
- +# define sqlite3Toupper(x) toupper((unsigned char)(x))
- +# define sqlite3Isspace(x) isspace((unsigned char)(x))
- +# define sqlite3Isalnum(x) isalnum((unsigned char)(x))
- +# define sqlite3Isalpha(x) isalpha((unsigned char)(x))
- +# define sqlite3Isdigit(x) isdigit((unsigned char)(x))
- +# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
- +# define sqlite3Tolower(x) tolower((unsigned char)(x))
- +# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
- +#endif
- +int sqlite3IsIdChar(u8);
- +
- +/*
- +** Internal function prototypes
- +*/
- +int sqlite3StrICmp(const char*,const char*);
- +int sqlite3Strlen30(const char*);
- +#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
- +char *sqlite3ColumnType(Column*,char*);
- +#define sqlite3StrNICmp sqlite3_strnicmp
- +
- +int sqlite3MallocInit(void);
- +void sqlite3MallocEnd(void);
- +void *sqlite3Malloc(u64);
- +void *sqlite3MallocZero(u64);
- +void *sqlite3DbMallocZero(sqlite3*, u64);
- +void *sqlite3DbMallocRaw(sqlite3*, u64);
- +void *sqlite3DbMallocRawNN(sqlite3*, u64);
- +char *sqlite3DbStrDup(sqlite3*,const char*);
- +char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
- +char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
- +void *sqlite3Realloc(void*, u64);
- +void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
- +void *sqlite3DbRealloc(sqlite3 *, void *, u64);
- +void sqlite3DbFree(sqlite3*, void*);
- +void sqlite3DbFreeNN(sqlite3*, void*);
- +int sqlite3MallocSize(void*);
- +int sqlite3DbMallocSize(sqlite3*, void*);
- +void *sqlite3PageMalloc(int);
- +void sqlite3PageFree(void*);
- +void sqlite3MemSetDefault(void);
- +#ifndef SQLITE_UNTESTABLE
- +void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
- +#endif
- +int sqlite3HeapNearlyFull(void);
- +
- +/*
- +** On systems with ample stack space and that support alloca(), make
- +** use of alloca() to obtain space for large automatic objects. By default,
- +** obtain space from malloc().
- +**
- +** The alloca() routine never returns NULL. This will cause code paths
- +** that deal with sqlite3StackAlloc() failures to be unreachable.
- +*/
- +#ifdef SQLITE_USE_ALLOCA
- +# define sqlite3StackAllocRaw(D,N) alloca(N)
- +# define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
- +# define sqlite3StackFree(D,P)
- +#else
- +# define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
- +# define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
- +# define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
- +#endif
- +
- +/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
- +** are, disable MEMSYS3
- +*/
- +#ifdef SQLITE_ENABLE_MEMSYS5
- +const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
- +#undef SQLITE_ENABLE_MEMSYS3
- +#endif
- +#ifdef SQLITE_ENABLE_MEMSYS3
- +const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
- +#endif
- +
- +
- +#ifndef SQLITE_MUTEX_OMIT
- + sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
- + sqlite3_mutex_methods const *sqlite3NoopMutex(void);
- + sqlite3_mutex *sqlite3MutexAlloc(int);
- + int sqlite3MutexInit(void);
- + int sqlite3MutexEnd(void);
- +#endif
- +#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
- + void sqlite3MemoryBarrier(void);
- +#else
- +# define sqlite3MemoryBarrier()
- +#endif
- +
- +sqlite3_int64 sqlite3StatusValue(int);
- +void sqlite3StatusUp(int, int);
- +void sqlite3StatusDown(int, int);
- +void sqlite3StatusHighwater(int, int);
- +int sqlite3LookasideUsed(sqlite3*,int*);
- +
- +/* Access to mutexes used by sqlite3_status() */
- +sqlite3_mutex *sqlite3Pcache1Mutex(void);
- +sqlite3_mutex *sqlite3MallocMutex(void);
- +
- +#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
- +void sqlite3MutexWarnOnContention(sqlite3_mutex*);
- +#else
- +# define sqlite3MutexWarnOnContention(x)
- +#endif
- +
- +#ifndef SQLITE_OMIT_FLOATING_POINT
- +# define EXP754 (((u64)0x7ff)<<52)
- +# define MAN754 ((((u64)1)<<52)-1)
- +# define IsNaN(X) (((X)&EXP754)==EXP754 && ((X)&MAN754)!=0)
- + int sqlite3IsNaN(double);
- +#else
- +# define IsNaN(X) 0
- +# define sqlite3IsNaN(X) 0
- +#endif
- +
- +/*
- +** An instance of the following structure holds information about SQL
- +** functions arguments that are the parameters to the printf() function.
- +*/
- +struct PrintfArguments {
- + int nArg; /* Total number of arguments */
- + int nUsed; /* Number of arguments used so far */
- + sqlite3_value **apArg; /* The argument values */
- +};
- +
- +char *sqlite3MPrintf(sqlite3*,const char*, ...);
- +char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
- +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
- + void sqlite3DebugPrintf(const char*, ...);
- +#endif
- +#if defined(SQLITE_TEST)
- + void *sqlite3TestTextToPtr(const char*);
- +#endif
- +
- +#if defined(SQLITE_DEBUG)
- + void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
- + void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
- + void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
- + void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
- + void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
- + void sqlite3TreeViewWith(TreeView*, const With*, u8);
- +#ifndef SQLITE_OMIT_WINDOWFUNC
- + void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
- + void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
- +#endif
- +#endif
- +
- +
- +void sqlite3SetString(char **, sqlite3*, const char*);
- +void sqlite3ErrorMsg(Parse*, const char*, ...);
- +int sqlite3ErrorToParser(sqlite3*,int);
- +void sqlite3Dequote(char*);
- +void sqlite3DequoteExpr(Expr*);
- +void sqlite3TokenInit(Token*,char*);
- +int sqlite3KeywordCode(const unsigned char*, int);
- +int sqlite3RunParser(Parse*, const char*, char **);
- +void sqlite3FinishCoding(Parse*);
- +int sqlite3GetTempReg(Parse*);
- +void sqlite3ReleaseTempReg(Parse*,int);
- +int sqlite3GetTempRange(Parse*,int);
- +void sqlite3ReleaseTempRange(Parse*,int,int);
- +void sqlite3ClearTempRegCache(Parse*);
- +#ifdef SQLITE_DEBUG
- +int sqlite3NoTempsInRange(Parse*,int,int);
- +#endif
- +Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
- +Expr *sqlite3Expr(sqlite3*,int,const char*);
- +void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
- +Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
- +void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
- +Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
- +Expr *sqlite3ExprSimplifiedAndOr(Expr*);
- +Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
- +void sqlite3ExprFunctionUsable(Parse*,Expr*,FuncDef*);
- +void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
- +void sqlite3ExprDelete(sqlite3*, Expr*);
- +void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
- +ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
- +ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
- +void sqlite3ExprListSetSortOrder(ExprList*,int,int);
- +void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
- +void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
- +void sqlite3ExprListDelete(sqlite3*, ExprList*);
- +u32 sqlite3ExprListFlags(const ExprList*);
- +int sqlite3IndexHasDuplicateRootPage(Index*);
- +int sqlite3Init(sqlite3*, char**);
- +int sqlite3InitCallback(void*, int, char**, char**);
- +int sqlite3InitOne(sqlite3*, int, char**, u32);
- +void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
- +#ifndef SQLITE_OMIT_VIRTUALTABLE
- +Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
- +#endif
- +void sqlite3ResetAllSchemasOfConnection(sqlite3*);
- +void sqlite3ResetOneSchema(sqlite3*,int);
- +void sqlite3CollapseDatabaseArray(sqlite3*);
- +void sqlite3CommitInternalChanges(sqlite3*);
- +void sqlite3DeleteColumnNames(sqlite3*,Table*);
- +int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
- +void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*,char);
- +Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
- +void sqlite3OpenMasterTable(Parse *, int);
- +Index *sqlite3PrimaryKeyIndex(Table*);
- +i16 sqlite3TableColumnToIndex(Index*, i16);
- +#ifdef SQLITE_OMIT_GENERATED_COLUMNS
- +# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
- +# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
- +#else
- + i16 sqlite3TableColumnToStorage(Table*, i16);
- + i16 sqlite3StorageColumnToTable(Table*, i16);
- +#endif
- +void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
- +#if SQLITE_ENABLE_HIDDEN_COLUMNS
- + void sqlite3ColumnPropertiesFromName(Table*, Column*);
- +#else
- +# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
- +#endif
- +void sqlite3AddColumn(Parse*,Token*,Token*);
- +void sqlite3AddNotNull(Parse*, int);
- +void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
- +void sqlite3AddCheckConstraint(Parse*, Expr*);
- +void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
- +void sqlite3AddCollateType(Parse*, Token*);
- +void sqlite3AddGenerated(Parse*,Expr*,Token*);
- +void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
- +int sqlite3ParseUri(const char*,const char*,unsigned int*,
- + sqlite3_vfs**,char**,char **);
- +#define sqlite3CodecQueryParameters(A,B,C) 0
- +Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
- +
- +#ifdef SQLITE_UNTESTABLE
- +# define sqlite3FaultSim(X) SQLITE_OK
- +#else
- + int sqlite3FaultSim(int);
- +#endif
- +
- +Bitvec *sqlite3BitvecCreate(u32);
- +int sqlite3BitvecTest(Bitvec*, u32);
- +int sqlite3BitvecTestNotNull(Bitvec*, u32);
- +int sqlite3BitvecSet(Bitvec*, u32);
- +void sqlite3BitvecClear(Bitvec*, u32, void*);
- +void sqlite3BitvecDestroy(Bitvec*);
- +u32 sqlite3BitvecSize(Bitvec*);
- +#ifndef SQLITE_UNTESTABLE
- +int sqlite3BitvecBuiltinTest(int,int*);
- +#endif
- +
- +RowSet *sqlite3RowSetInit(sqlite3*);
- +void sqlite3RowSetDelete(void*);
- +void sqlite3RowSetClear(void*);
- +void sqlite3RowSetInsert(RowSet*, i64);
- +int sqlite3RowSetTest(RowSet*, int iBatch, i64);
- +int sqlite3RowSetNext(RowSet*, i64*);
- +
- +void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
- +
- +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
- + int sqlite3ViewGetColumnNames(Parse*,Table*);
- +#else
- +# define sqlite3ViewGetColumnNames(A,B) 0
- +#endif
- +
- +#if SQLITE_MAX_ATTACHED>30
- + int sqlite3DbMaskAllZero(yDbMask);
- +#endif
- +void sqlite3DropTable(Parse*, SrcList*, int, int);
- +void sqlite3CodeDropTable(Parse*, Table*, int, int);
- +void sqlite3DeleteTable(sqlite3*, Table*);
- +void sqlite3FreeIndex(sqlite3*, Index*);
- +#ifndef SQLITE_OMIT_AUTOINCREMENT
- + void sqlite3AutoincrementBegin(Parse *pParse);
- + void sqlite3AutoincrementEnd(Parse *pParse);
- +#else
- +# define sqlite3AutoincrementBegin(X)
- +# define sqlite3AutoincrementEnd(X)
- +#endif
- +void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
- +#ifndef SQLITE_OMIT_GENERATED_COLUMNS
- + void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
- +#endif
- +void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
- +IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
- +int sqlite3IdListIndex(IdList*,const char*);
- +SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
- +SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
- +SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
- + Token*, Select*, Expr*, IdList*);
- +void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
- +void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
- +int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
- +void sqlite3SrcListShiftJoinType(SrcList*);
- +void sqlite3SrcListAssignCursors(Parse*, SrcList*);
- +void sqlite3IdListDelete(sqlite3*, IdList*);
- +void sqlite3SrcListDelete(sqlite3*, SrcList*);
- +Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
- +void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
- + Expr*, int, int, u8);
- +void sqlite3DropIndex(Parse*, SrcList*, int);
- +int sqlite3Select(Parse*, Select*, SelectDest*);
- +Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
- + Expr*,ExprList*,u32,Expr*);
- +void sqlite3SelectDelete(sqlite3*, Select*);
- +void sqlite3SelectReset(Parse*, Select*);
- +Table *sqlite3SrcListLookup(Parse*, SrcList*);
- +int sqlite3IsReadOnly(Parse*, Table*, int);
- +void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
- +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
- +Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
- +#endif
- +void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
- +void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
- + Upsert*);
- +WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
- +void sqlite3WhereEnd(WhereInfo*);
- +LogEst sqlite3WhereOutputRowCount(WhereInfo*);
- +int sqlite3WhereIsDistinct(WhereInfo*);
- +int sqlite3WhereIsOrdered(WhereInfo*);
- +int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
- +int sqlite3WhereIsSorted(WhereInfo*);
- +int sqlite3WhereContinueLabel(WhereInfo*);
- +int sqlite3WhereBreakLabel(WhereInfo*);
- +int sqlite3WhereOkOnePass(WhereInfo*, int*);
- +#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
- +#define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */
- +#define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */
- +int sqlite3WhereUsesDeferredSeek(WhereInfo*);
- +void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
- +int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
- +void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
- +void sqlite3ExprCodeMove(Parse*, int, int, int);
- +void sqlite3ExprCode(Parse*, Expr*, int);
- +#ifndef SQLITE_OMIT_GENERATED_COLUMNS
- +void sqlite3ExprCodeGeneratedColumn(Parse*, Column*, int);
- +#endif
- +void sqlite3ExprCodeCopy(Parse*, Expr*, int);
- +void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
- +int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
- +int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
- +int sqlite3ExprCodeTarget(Parse*, Expr*, int);
- +int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
- +#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
- +#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
- +#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */
- +#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */
- +void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
- +void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
- +void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
- +Table *sqlite3FindTable(sqlite3*,const char*, const char*);
- +#define LOCATE_VIEW 0x01
- +#define LOCATE_NOERR 0x02
- +Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
- +Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
- +Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
- +void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
- +void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
- +void sqlite3Vacuum(Parse*,Token*,Expr*);
- +int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
- +char *sqlite3NameFromToken(sqlite3*, Token*);
- +int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
- +int sqlite3ExprCompareSkip(Expr*, Expr*, int);
- +int sqlite3ExprListCompare(ExprList*, ExprList*, int);
- +int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
- +int sqlite3ExprImpliesNonNullRow(Expr*,int);
- +void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
- +void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
- +int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
- +int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
- +Vdbe *sqlite3GetVdbe(Parse*);
- +#ifndef SQLITE_UNTESTABLE
- +void sqlite3PrngSaveState(void);
- +void sqlite3PrngRestoreState(void);
- +#endif
- +void sqlite3RollbackAll(sqlite3*,int);
- +void sqlite3CodeVerifySchema(Parse*, int);
- +void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
- +void sqlite3BeginTransaction(Parse*, int);
- +void sqlite3EndTransaction(Parse*,int);
- +void sqlite3Savepoint(Parse*, int, Token*);
- +void sqlite3CloseSavepoints(sqlite3 *);
- +void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
- +u32 sqlite3IsTrueOrFalse(const char*);
- +int sqlite3ExprIdToTrueFalse(Expr*);
- +int sqlite3ExprTruthValue(const Expr*);
- +int sqlite3ExprIsConstant(Expr*);
- +int sqlite3ExprIsConstantNotJoin(Expr*);
- +int sqlite3ExprIsConstantOrFunction(Expr*, u8);
- +int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
- +int sqlite3ExprIsTableConstant(Expr*,int);
- +#ifdef SQLITE_ENABLE_CURSOR_HINTS
- +int sqlite3ExprContainsSubquery(Expr*);
- +#endif
- +int sqlite3ExprIsInteger(Expr*, int*);
- +int sqlite3ExprCanBeNull(const Expr*);
- +int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
- +int sqlite3IsRowid(const char*);
- +void sqlite3GenerateRowDelete(
- + Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
- +void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
- +int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
- +void sqlite3ResolvePartIdxLabel(Parse*,int);
- +int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
- +void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
- + u8,u8,int,int*,int*,Upsert*);
- +#ifdef SQLITE_ENABLE_NULL_TRIM
- + void sqlite3SetMakeRecordP5(Vdbe*,Table*);
- +#else
- +# define sqlite3SetMakeRecordP5(A,B)
- +#endif
- +void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
- +int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
- +void sqlite3BeginWriteOperation(Parse*, int, int);
- +void sqlite3MultiWrite(Parse*);
- +void sqlite3MayAbort(Parse*);
- +void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
- +void sqlite3UniqueConstraint(Parse*, int, Index*);
- +void sqlite3RowidConstraint(Parse*, int, Table*);
- +Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
- +ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
- +SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
- +IdList *sqlite3IdListDup(sqlite3*,IdList*);
- +Select *sqlite3SelectDup(sqlite3*,Select*,int);
- +FuncDef *sqlite3FunctionSearch(int,const char*);
- +void sqlite3InsertBuiltinFuncs(FuncDef*,int);
- +FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
- +void sqlite3RegisterBuiltinFunctions(void);
- +void sqlite3RegisterDateTimeFunctions(void);
- +void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
- +int sqlite3SafetyCheckOk(sqlite3*);
- +int sqlite3SafetyCheckSickOrOk(sqlite3*);
- +void sqlite3ChangeCookie(Parse*, int);
- +
- +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
- +void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
- +#endif
- +
- +#ifndef SQLITE_OMIT_TRIGGER
- + void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
- + Expr*,int, int);
- + void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
- + void sqlite3DropTrigger(Parse*, SrcList*, int);
- + void sqlite3DropTriggerPtr(Parse*, Trigger*);
- + Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
- + Trigger *sqlite3TriggerList(Parse *, Table *);
- + void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
- + int, int, int);
- + void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
- + void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
- + void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
- + TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
- + const char*,const char*);
- + TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
- + Select*,u8,Upsert*,
- + const char*,const char*);
- + TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,ExprList*, Expr*, u8,
- + const char*,const char*);
- + TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
- + const char*,const char*);
- + void sqlite3DeleteTrigger(sqlite3*, Trigger*);
- + void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
- + u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
- +# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
- +# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
- +#else
- +# define sqlite3TriggersExist(B,C,D,E,F) 0
- +# define sqlite3DeleteTrigger(A,B)
- +# define sqlite3DropTriggerPtr(A,B)
- +# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
- +# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
- +# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
- +# define sqlite3TriggerList(X, Y) 0
- +# define sqlite3ParseToplevel(p) p
- +# define sqlite3IsToplevel(p) 1
- +# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
- +#endif
- +
- +int sqlite3JoinType(Parse*, Token*, Token*, Token*);
- +void sqlite3SetJoinExpr(Expr*,int);
- +void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
- +void sqlite3DeferForeignKey(Parse*, int);
- +#ifndef SQLITE_OMIT_AUTHORIZATION
- + void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
- + int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
- + void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
- + void sqlite3AuthContextPop(AuthContext*);
- + int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
- +#else
- +# define sqlite3AuthRead(a,b,c,d)
- +# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
- +# define sqlite3AuthContextPush(a,b,c)
- +# define sqlite3AuthContextPop(a) ((void)(a))
- +#endif
- +int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
- +void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
- +void sqlite3Detach(Parse*, Expr*);
- +void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
- +int sqlite3FixSrcList(DbFixer*, SrcList*);
- +int sqlite3FixSelect(DbFixer*, Select*);
- +int sqlite3FixExpr(DbFixer*, Expr*);
- +int sqlite3FixExprList(DbFixer*, ExprList*);
- +int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
- +int sqlite3RealSameAsInt(double,sqlite3_int64);
- +int sqlite3AtoF(const char *z, double*, int, u8);
- +int sqlite3GetInt32(const char *, int*);
- +int sqlite3Atoi(const char*);
- +#ifndef SQLITE_OMIT_UTF16
- +int sqlite3Utf16ByteLen(const void *pData, int nChar);
- +#endif
- +int sqlite3Utf8CharLen(const char *pData, int nByte);
- +u32 sqlite3Utf8Read(const u8**);
- +LogEst sqlite3LogEst(u64);
- +LogEst sqlite3LogEstAdd(LogEst,LogEst);
- +#ifndef SQLITE_OMIT_VIRTUALTABLE
- +LogEst sqlite3LogEstFromDouble(double);
- +#endif
- +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
- + defined(SQLITE_ENABLE_STAT4) || \
- + defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
- +u64 sqlite3LogEstToInt(LogEst);
- +#endif
- +VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
- +const char *sqlite3VListNumToName(VList*,int);
- +int sqlite3VListNameToNum(VList*,const char*,int);
- +
- +/*
- +** Routines to read and write variable-length integers. These used to
- +** be defined locally, but now we use the varint routines in the util.c
- +** file.
- +*/
- +int sqlite3PutVarint(unsigned char*, u64);
- +u8 sqlite3GetVarint(const unsigned char *, u64 *);
- +u8 sqlite3GetVarint32(const unsigned char *, u32 *);
- +int sqlite3VarintLen(u64 v);
- +
- +/*
- +** The common case is for a varint to be a single byte. They following
- +** macros handle the common case without a procedure call, but then call
- +** the procedure for larger varints.
- +*/
- +#define getVarint32(A,B) \
- + (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
- +#define getVarint32NR(A,B) \
- + B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B))
- +#define putVarint32(A,B) \
- + (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
- + sqlite3PutVarint((A),(B)))
- +#define getVarint sqlite3GetVarint
- +#define putVarint sqlite3PutVarint
- +
- +
- +const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
- +void sqlite3TableAffinity(Vdbe*, Table*, int);
- +char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
- +int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
- +char sqlite3TableColumnAffinity(Table*,int);
- +char sqlite3ExprAffinity(const Expr *pExpr);
- +int sqlite3Atoi64(const char*, i64*, int, u8);
- +int sqlite3DecOrHexToI64(const char*, i64*);
- +void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
- +void sqlite3Error(sqlite3*,int);
- +void sqlite3SystemError(sqlite3*,int);
- +void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
- +u8 sqlite3HexToInt(int h);
- +int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
- +
- +#if defined(SQLITE_NEED_ERR_NAME)
- +const char *sqlite3ErrName(int);
- +#endif
- +
- +#ifdef SQLITE_ENABLE_DESERIALIZE
- +int sqlite3MemdbInit(void);
- +#endif
- +
- +const char *sqlite3ErrStr(int);
- +int sqlite3ReadSchema(Parse *pParse);
- +CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
- +int sqlite3IsBinary(const CollSeq*);
- +CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
- +void sqlite3SetTextEncoding(sqlite3 *db, u8);
- +CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
- +CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
- +int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
- +Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
- +Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
- +Expr *sqlite3ExprSkipCollate(Expr*);
- +Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
- +int sqlite3CheckCollSeq(Parse *, CollSeq *);
- +int sqlite3WritableSchema(sqlite3*);
- +int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
- +void sqlite3VdbeSetChanges(sqlite3 *, int);
- +int sqlite3AddInt64(i64*,i64);
- +int sqlite3SubInt64(i64*,i64);
- +int sqlite3MulInt64(i64*,i64);
- +int sqlite3AbsInt32(int);
- +#ifdef SQLITE_ENABLE_8_3_NAMES
- +void sqlite3FileSuffix3(const char*, char*);
- +#else
- +# define sqlite3FileSuffix3(X,Y)
- +#endif
- +u8 sqlite3GetBoolean(const char *z,u8);
- +
- +const void *sqlite3ValueText(sqlite3_value*, u8);
- +int sqlite3ValueBytes(sqlite3_value*, u8);
- +void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
- + void(*)(void*));
- +void sqlite3ValueSetNull(sqlite3_value*);
- +void sqlite3ValueFree(sqlite3_value*);
- +#ifndef SQLITE_UNTESTABLE
- +void sqlite3ResultIntReal(sqlite3_context*);
- +#endif
- +sqlite3_value *sqlite3ValueNew(sqlite3 *);
- +#ifndef SQLITE_OMIT_UTF16
- +char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
- +#endif
- +int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
- +void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
- +#ifndef SQLITE_AMALGAMATION
- +extern const unsigned char sqlite3OpcodeProperty[];
- +extern const char sqlite3StrBINARY[];
- +extern const unsigned char sqlite3UpperToLower[];
- +extern const unsigned char sqlite3CtypeMap[];
- +extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
- +extern FuncDefHash sqlite3BuiltinFunctions;
- +extern u32 sqlite3SelectTrace;
- +#ifndef SQLITE_OMIT_WSD
- +extern int sqlite3PendingByte;
- +#endif
- +#endif /* !defined(SQLITE_AMALGAMATION) */
- +#ifdef VDBE_PROFILE
- +extern sqlite3_uint64 sqlite3NProfileCnt;
- +#endif
- +void sqlite3RootPageMoved(sqlite3*, int, int, int);
- +void sqlite3Reindex(Parse*, Token*, Token*);
- +void sqlite3AlterFunctions(void);
- +void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
- +void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
- +int sqlite3GetToken(const unsigned char *, int *);
- +void sqlite3NestedParse(Parse*, const char*, ...);
- +void sqlite3ExpirePreparedStatements(sqlite3*, int);
- +void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
- +int sqlite3CodeSubselect(Parse*, Expr*);
- +void sqlite3SelectPrep(Parse*, Select*, NameContext*);
- +void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
- +int sqlite3MatchEName(
- + const struct ExprList_item*,
- + const char*,
- + const char*,
- + const char*
- +);
- +Bitmask sqlite3ExprColUsed(Expr*);
- +u8 sqlite3StrIHash(const char*);
- +int sqlite3ResolveExprNames(NameContext*, Expr*);
- +int sqlite3ResolveExprListNames(NameContext*, ExprList*);
- +void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
- +int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
- +int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
- +void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
- +void sqlite3AlterFinishAddColumn(Parse *, Token *);
- +void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
- +void *sqlite3RenameTokenMap(Parse*, void*, Token*);
- +void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom);
- +void sqlite3RenameExprUnmap(Parse*, Expr*);
- +void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
- +CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
- +char sqlite3AffinityType(const char*, Column*);
- +void sqlite3Analyze(Parse*, Token*, Token*);
- +int sqlite3InvokeBusyHandler(BusyHandler*);
- +int sqlite3FindDb(sqlite3*, Token*);
- +int sqlite3FindDbName(sqlite3 *, const char *);
- +int sqlite3AnalysisLoad(sqlite3*,int iDB);
- +void sqlite3DeleteIndexSamples(sqlite3*,Index*);
- +void sqlite3DefaultRowEst(Index*);
- +void sqlite3RegisterLikeFunctions(sqlite3*, int);
- +int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
- +void sqlite3SchemaClear(void *);
- +Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
- +int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
- +KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
- +void sqlite3KeyInfoUnref(KeyInfo*);
- +KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
- +KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
- +KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
- +int sqlite3HasExplicitNulls(Parse*, ExprList*);
- +
- +#ifdef SQLITE_DEBUG
- +int sqlite3KeyInfoIsWriteable(KeyInfo*);
- +#endif
- +int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
- + void (*)(sqlite3_context*,int,sqlite3_value **),
- + void (*)(sqlite3_context*,int,sqlite3_value **),
- + void (*)(sqlite3_context*),
- + void (*)(sqlite3_context*),
- + void (*)(sqlite3_context*,int,sqlite3_value **),
- + FuncDestructor *pDestructor
- +);
- +void sqlite3NoopDestructor(void*);
- +void sqlite3OomFault(sqlite3*);
- +void sqlite3OomClear(sqlite3*);
- +int sqlite3ApiExit(sqlite3 *db, int);
- +int sqlite3OpenTempDatabase(Parse *);
- +
- +void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
- +char *sqlite3StrAccumFinish(StrAccum*);
- +void sqlite3SelectDestInit(SelectDest*,int,int);
- +Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
- +
- +void sqlite3BackupRestart(sqlite3_backup *);
- +void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
- +
- +#ifndef SQLITE_OMIT_SUBQUERY
- +int sqlite3ExprCheckIN(Parse*, Expr*);
- +#else
- +# define sqlite3ExprCheckIN(x,y) SQLITE_OK
- +#endif
- +
- +#ifdef SQLITE_ENABLE_STAT4
- +int sqlite3Stat4ProbeSetValue(
- + Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
- +int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
- +void sqlite3Stat4ProbeFree(UnpackedRecord*);
- +int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
- +char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
- +#endif
- +
- +/*
- +** The interface to the LEMON-generated parser
- +*/
- +#ifndef SQLITE_AMALGAMATION
- + void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
- + void sqlite3ParserFree(void*, void(*)(void*));
- +#endif
- +void sqlite3Parser(void*, int, Token);
- +int sqlite3ParserFallback(int);
- +#ifdef YYTRACKMAXSTACKDEPTH
- + int sqlite3ParserStackPeak(void*);
- +#endif
- +
- +void sqlite3AutoLoadExtensions(sqlite3*);
- +#ifndef SQLITE_OMIT_LOAD_EXTENSION
- + void sqlite3CloseExtensions(sqlite3*);
- +#else
- +# define sqlite3CloseExtensions(X)
- +#endif
- +
- +#ifndef SQLITE_OMIT_SHARED_CACHE
- + void sqlite3TableLock(Parse *, int, int, u8, const char *);
- +#else
- + #define sqlite3TableLock(v,w,x,y,z)
- +#endif
- +
- +#ifdef SQLITE_TEST
- + int sqlite3Utf8To8(unsigned char*);
- +#endif
- +
- +#ifdef SQLITE_OMIT_VIRTUALTABLE
- +# define sqlite3VtabClear(Y)
- +# define sqlite3VtabSync(X,Y) SQLITE_OK
- +# define sqlite3VtabRollback(X)
- +# define sqlite3VtabCommit(X)
- +# define sqlite3VtabInSync(db) 0
- +# define sqlite3VtabLock(X)
- +# define sqlite3VtabUnlock(X)
- +# define sqlite3VtabModuleUnref(D,X)
- +# define sqlite3VtabUnlockList(X)
- +# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
- +# define sqlite3GetVTable(X,Y) ((VTable*)0)
- +#else
- + void sqlite3VtabClear(sqlite3 *db, Table*);
- + void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
- + int sqlite3VtabSync(sqlite3 *db, Vdbe*);
- + int sqlite3VtabRollback(sqlite3 *db);
- + int sqlite3VtabCommit(sqlite3 *db);
- + void sqlite3VtabLock(VTable *);
- + void sqlite3VtabUnlock(VTable *);
- + void sqlite3VtabModuleUnref(sqlite3*,Module*);
- + void sqlite3VtabUnlockList(sqlite3*);
- + int sqlite3VtabSavepoint(sqlite3 *, int, int);
- + void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
- + VTable *sqlite3GetVTable(sqlite3*, Table*);
- + Module *sqlite3VtabCreateModule(
- + sqlite3*,
- + const char*,
- + const sqlite3_module*,
- + void*,
- + void(*)(void*)
- + );
- +# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
- +#endif
- +int sqlite3ReadOnlyShadowTables(sqlite3 *db);
- +#ifndef SQLITE_OMIT_VIRTUALTABLE
- + int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
- + int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
- +#else
- +# define sqlite3ShadowTableName(A,B) 0
- +# define sqlite3IsShadowTableOf(A,B,C) 0
- +#endif
- +int sqlite3VtabEponymousTableInit(Parse*,Module*);
- +void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
- +void sqlite3VtabMakeWritable(Parse*,Table*);
- +void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
- +void sqlite3VtabFinishParse(Parse*, Token*);
- +void sqlite3VtabArgInit(Parse*);
- +void sqlite3VtabArgExtend(Parse*, Token*);
- +int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
- +int sqlite3VtabCallConnect(Parse*, Table*);
- +int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
- +int sqlite3VtabBegin(sqlite3 *, VTable *);
- +FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
- +sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
- +int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
- +int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
- +void sqlite3ParserReset(Parse*);
- +#ifdef SQLITE_ENABLE_NORMALIZE
- +char *sqlite3Normalize(Vdbe*, const char*);
- +#endif
- +int sqlite3Reprepare(Vdbe*);
- +void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
- +CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
- +CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
- +int sqlite3TempInMemory(const sqlite3*);
- +const char *sqlite3JournalModename(int);
- +#ifndef SQLITE_OMIT_WAL
- + int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
- + int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
- +#endif
- +#ifndef SQLITE_OMIT_CTE
- + With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
- + void sqlite3WithDelete(sqlite3*,With*);
- + void sqlite3WithPush(Parse*, With*, u8);
- +#else
- +#define sqlite3WithPush(x,y,z)
- +#define sqlite3WithDelete(x,y)
- +#endif
- +#ifndef SQLITE_OMIT_UPSERT
- + Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
- + void sqlite3UpsertDelete(sqlite3*,Upsert*);
- + Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
- + int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
- + void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
- +#else
- +#define sqlite3UpsertNew(v,w,x,y,z) ((Upsert*)0)
- +#define sqlite3UpsertDelete(x,y)
- +#define sqlite3UpsertDup(x,y) ((Upsert*)0)
- +#endif
- +
- +
- +/* Declarations for functions in fkey.c. All of these are replaced by
- +** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
- +** key functionality is available. If OMIT_TRIGGER is defined but
- +** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
- +** this case foreign keys are parsed, but no other functionality is
- +** provided (enforcement of FK constraints requires the triggers sub-system).
- +*/
- +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
- + void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
- + void sqlite3FkDropTable(Parse*, SrcList *, Table*);
- + void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
- + int sqlite3FkRequired(Parse*, Table*, int*, int);
- + u32 sqlite3FkOldmask(Parse*, Table*);
- + FKey *sqlite3FkReferences(Table *);
- +#else
- + #define sqlite3FkActions(a,b,c,d,e,f)
- + #define sqlite3FkCheck(a,b,c,d,e,f)
- + #define sqlite3FkDropTable(a,b,c)
- + #define sqlite3FkOldmask(a,b) 0
- + #define sqlite3FkRequired(a,b,c,d) 0
- + #define sqlite3FkReferences(a) 0
- +#endif
- +#ifndef SQLITE_OMIT_FOREIGN_KEY
- + void sqlite3FkDelete(sqlite3 *, Table*);
- + int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
- +#else
- + #define sqlite3FkDelete(a,b)
- + #define sqlite3FkLocateIndex(a,b,c,d,e)
- +#endif
- +
- +
- +/*
- +** Available fault injectors. Should be numbered beginning with 0.
- +*/
- +#define SQLITE_FAULTINJECTOR_MALLOC 0
- +#define SQLITE_FAULTINJECTOR_COUNT 1
- +
- +/*
- +** The interface to the code in fault.c used for identifying "benign"
- +** malloc failures. This is only present if SQLITE_UNTESTABLE
- +** is not defined.
- +*/
- +#ifndef SQLITE_UNTESTABLE
- + void sqlite3BeginBenignMalloc(void);
- + void sqlite3EndBenignMalloc(void);
- +#else
- + #define sqlite3BeginBenignMalloc()
- + #define sqlite3EndBenignMalloc()
- +#endif
- +
- +/*
- +** Allowed return values from sqlite3FindInIndex()
- +*/
- +#define IN_INDEX_ROWID 1 /* Search the rowid of the table */
- +#define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
- +#define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */
- +#define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */
- +#define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
- +/*
- +** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
- +*/
- +#define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */
- +#define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */
- +#define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
- +int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
- +
- +int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
- +int sqlite3JournalSize(sqlite3_vfs *);
- +#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
- + || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
- + int sqlite3JournalCreate(sqlite3_file *);
- +#endif
- +
- +int sqlite3JournalIsInMemory(sqlite3_file *p);
- +void sqlite3MemJournalOpen(sqlite3_file *);
- +
- +void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
- +#if SQLITE_MAX_EXPR_DEPTH>0
- + int sqlite3SelectExprHeight(Select *);
- + int sqlite3ExprCheckHeight(Parse*, int);
- +#else
- + #define sqlite3SelectExprHeight(x) 0
- + #define sqlite3ExprCheckHeight(x,y)
- +#endif
- +
- +u32 sqlite3Get4byte(const u8*);
- +void sqlite3Put4byte(u8*, u32);
- +
- +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
- + void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
- + void sqlite3ConnectionUnlocked(sqlite3 *db);
- + void sqlite3ConnectionClosed(sqlite3 *db);
- +#else
- + #define sqlite3ConnectionBlocked(x,y)
- + #define sqlite3ConnectionUnlocked(x)
- + #define sqlite3ConnectionClosed(x)
- +#endif
- +
- +#ifdef SQLITE_DEBUG
- + void sqlite3ParserTrace(FILE*, char *);
- +#endif
- +#if defined(YYCOVERAGE)
- + int sqlite3ParserCoverage(FILE*);
- +#endif
- +
- +/*
- +** If the SQLITE_ENABLE IOTRACE exists then the global variable
- +** sqlite3IoTrace is a pointer to a printf-like routine used to
- +** print I/O tracing messages.
- +*/
- +#ifdef SQLITE_ENABLE_IOTRACE
- +# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
- + void sqlite3VdbeIOTraceSql(Vdbe*);
- +SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
- +#else
- +# define IOTRACE(A)
- +# define sqlite3VdbeIOTraceSql(X)
- +#endif
- +
- +/*
- +** These routines are available for the mem2.c debugging memory allocator
- +** only. They are used to verify that different "types" of memory
- +** allocations are properly tracked by the system.
- +**
- +** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
- +** the MEMTYPE_* macros defined below. The type must be a bitmask with
- +** a single bit set.
- +**
- +** sqlite3MemdebugHasType() returns true if any of the bits in its second
- +** argument match the type set by the previous sqlite3MemdebugSetType().
- +** sqlite3MemdebugHasType() is intended for use inside assert() statements.
- +**
- +** sqlite3MemdebugNoType() returns true if none of the bits in its second
- +** argument match the type set by the previous sqlite3MemdebugSetType().
- +**
- +** Perhaps the most important point is the difference between MEMTYPE_HEAP
- +** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
- +** it might have been allocated by lookaside, except the allocation was
- +** too large or lookaside was already full. It is important to verify
- +** that allocations that might have been satisfied by lookaside are not
- +** passed back to non-lookaside free() routines. Asserts such as the
- +** example above are placed on the non-lookaside free() routines to verify
- +** this constraint.
- +**
- +** All of this is no-op for a production build. It only comes into
- +** play when the SQLITE_MEMDEBUG compile-time option is used.
- +*/
- +#ifdef SQLITE_MEMDEBUG
- + void sqlite3MemdebugSetType(void*,u8);
- + int sqlite3MemdebugHasType(void*,u8);
- + int sqlite3MemdebugNoType(void*,u8);
- +#else
- +# define sqlite3MemdebugSetType(X,Y) /* no-op */
- +# define sqlite3MemdebugHasType(X,Y) 1
- +# define sqlite3MemdebugNoType(X,Y) 1
- +#endif
- +#define MEMTYPE_HEAP 0x01 /* General heap allocations */
- +#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */
- +#define MEMTYPE_PCACHE 0x04 /* Page cache allocations */
- +
- +/*
- +** Threading interface
- +*/
- +#if SQLITE_MAX_WORKER_THREADS>0
- +int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
- +int sqlite3ThreadJoin(SQLiteThread*, void**);
- +#endif
- +
- +#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
- +int sqlite3DbpageRegister(sqlite3*);
- +#endif
- +#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
- +int sqlite3DbstatRegister(sqlite3*);
- +#endif
- +
- +int sqlite3ExprVectorSize(Expr *pExpr);
- +int sqlite3ExprIsVector(Expr *pExpr);
- +Expr *sqlite3VectorFieldSubexpr(Expr*, int);
- +Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
- +void sqlite3VectorErrorMsg(Parse*, Expr*);
- +
- +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
- +const char **sqlite3CompileOptions(int *pnOpt);
- +#endif
- +
- +#endif /* SQLITEINT_H */
- diff -Npur sqlite-version-3.32.2/src/test1.c sqlite-version-3.32.2-patched/src/test1.c
- --- sqlite-version-3.32.2/src/test1.c 2020-06-04 20:58:43.000000000 +0800
- +++ sqlite-version-3.32.2-patched/src/test1.c 2020-07-08 10:00:47.371088720 +0800
- @@ -8164,7 +8164,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp)
- #endif
- #endif
- #if defined(SQLITE_ENABLE_SELECTTRACE)
- - extern int sqlite3SelectTrace;
- + extern u32 sqlite3SelectTrace;
- #endif
-
- for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
- diff -Npur sqlite-version-3.32.2/src/window.c sqlite-version-3.32.2-patched/src/window.c
- --- sqlite-version-3.32.2/src/window.c 2020-06-04 20:58:43.000000000 +0800
- +++ sqlite-version-3.32.2-patched/src/window.c 2020-07-08 10:00:47.371088720 +0800
- @@ -942,7 +942,7 @@ static int sqlite3WindowExtraAggFuncDept
- */
- int sqlite3WindowRewrite(Parse *pParse, Select *p){
- int rc = SQLITE_OK;
- - if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){
- + if( ALWAYS(p->pWin && (p->selFlags & SF_WinRewrite)==0) ){
- Vdbe *v = sqlite3GetVdbe(pParse);
- sqlite3 *db = pParse->db;
- Select *pSub = 0; /* The subquery */
- diff -Npur sqlite-version-3.32.2/test/selectA.test sqlite-version-3.32.2-patched/test/selectA.test
- --- sqlite-version-3.32.2/test/selectA.test 2020-06-04 20:58:43.000000000 +0800
- +++ sqlite-version-3.32.2-patched/test/selectA.test 2020-07-08 10:00:50.899152517 +0800
- @@ -1446,5 +1446,26 @@ do_execsql_test 6.1 {
- SELECT * FROM (SELECT a FROM t1 UNION SELECT b FROM t2) WHERE a=a;
- } {12345}
-
- +# 2020-06-15 ticket 8f157e8010b22af0
- +#
- +reset_db
- +do_execsql_test 7.1 {
- + CREATE TABLE t1(c1); INSERT INTO t1 VALUES(12),(123),(1234),(NULL),('abc');
- + CREATE TABLE t2(c2); INSERT INTO t2 VALUES(44),(55),(123);
- + CREATE TABLE t3(c3,c4); INSERT INTO t3 VALUES(66,1),(123,2),(77,3);
- + CREATE VIEW t4 AS SELECT c3 FROM t3;
- + CREATE VIEW t5 AS SELECT c3 FROM t3 ORDER BY c4;
- +}
- +do_execsql_test 7.2 {
- + SELECT * FROM t1, t2 WHERE c1=(SELECT 123 INTERSECT SELECT c2 FROM t4) AND c1=123;
- +} {123 123}
- +do_execsql_test 7.3 {
- + SELECT * FROM t1, t2 WHERE c1=(SELECT 123 INTERSECT SELECT c2 FROM t5) AND c1=123;
- +} {123 123}
- +do_execsql_test 7.4 {
- + CREATE TABLE a(b);
- + CREATE VIEW c(d) AS SELECT b FROM a ORDER BY b;
- + SELECT sum(d) OVER( PARTITION BY(SELECT 0 FROM c JOIN a WHERE b =(SELECT b INTERSECT SELECT d FROM c) AND b = 123)) FROM c;
- +} {}
-
- finish_test
- diff -Npur sqlite-version-3.32.2/test/window1.test sqlite-version-3.32.2-patched/test/window1.test
- --- sqlite-version-3.32.2/test/window1.test 2020-06-04 20:58:43.000000000 +0800
- +++ sqlite-version-3.32.2-patched/test/window1.test 2020-07-08 10:00:47.371088720 +0800
- @@ -1743,5 +1743,47 @@ do_execsql_test 53.0 {
- WHERE a.c);
- } {4 4 4 4}
-
- +#-------------------------------------------------------------------------
- +reset_db
- +do_execsql_test 54.1 {
- + CREATE TABLE t1(a VARCHAR(20), b FLOAT);
- + INSERT INTO t1 VALUES('1',10.0);
- +}
- +
- +do_execsql_test 54.2 {
- + SELECT * FROM (
- + SELECT sum(b) OVER() AS c FROM t1
- + UNION
- + SELECT b AS c FROM t1
- + ) WHERE c>10;
- +}
- +
- +do_execsql_test 54.3 {
- + INSERT INTO t1 VALUES('2',5.0);
- + INSERT INTO t1 VALUES('3',15.0);
- +}
- +
- +do_execsql_test 54.4 {
- + SELECT * FROM (
- + SELECT sum(b) OVER() AS c FROM t1
- + UNION
- + SELECT b AS c FROM t1
- + ) WHERE c>10;
- +} {15.0 30.0}
- +
- +# 2020-06-05 ticket c8d3b9f0a750a529
- +reset_db
- +do_execsql_test 55.1 {
- + CREATE TABLE a(b);
- + SELECT
- + (SELECT b FROM a
- + GROUP BY b
- + HAVING (SELECT COUNT()OVER() + lead(b)OVER(ORDER BY SUM(DISTINCT b) + b))
- + )
- + FROM a
- + UNION
- + SELECT 99
- + ORDER BY 1;
- +} {99}
-
- finish_test
|