| | @@ -283,11 +283,11 @@ |
| 283 | 283 | #endif |
| 284 | 284 | #ifdef SQLITE_ENABLE_LOCKING_STYLE |
| 285 | 285 | "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE), |
| 286 | 286 | #endif |
| 287 | 287 | #if SQLITE_ENABLE_MATH_FUNCTIONS |
| 288 | | - "ENABLE_MATH_FUNCTIONS" |
| 288 | + "ENABLE_MATH_FUNCTIONS", |
| 289 | 289 | #endif |
| 290 | 290 | #if SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 291 | 291 | "ENABLE_MEMORY_MANAGEMENT", |
| 292 | 292 | #endif |
| 293 | 293 | #if SQLITE_ENABLE_MEMSYS3 |
| | @@ -1186,11 +1186,11 @@ |
| 1186 | 1186 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1187 | 1187 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1188 | 1188 | */ |
| 1189 | 1189 | #define SQLITE_VERSION "3.35.0" |
| 1190 | 1190 | #define SQLITE_VERSION_NUMBER 3035000 |
| 1191 | | -#define SQLITE_SOURCE_ID "2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66f660" |
| 1191 | +#define SQLITE_SOURCE_ID "2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124d8d7f" |
| 1192 | 1192 | |
| 1193 | 1193 | /* |
| 1194 | 1194 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1195 | 1195 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1196 | 1196 | ** |
| | @@ -4560,10 +4560,11 @@ |
| 4560 | 4560 | ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td> |
| 4561 | 4561 | ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile" |
| 4562 | 4562 | ** that uses dot-files in place of posix advisory locking. |
| 4563 | 4563 | ** <tr><td> file:data.db?mode=readonly <td> |
| 4564 | 4564 | ** An error. "readonly" is not a valid option for the "mode" parameter. |
| 4565 | +** Use "ro" instead: "file:data.db?mode=ro". |
| 4565 | 4566 | ** </table> |
| 4566 | 4567 | ** |
| 4567 | 4568 | ** ^URI hexadecimal escape sequences (%HH) are supported within the path and |
| 4568 | 4569 | ** query components of a URI. A hexadecimal escape sequence consists of a |
| 4569 | 4570 | ** percent sign - "%" - followed by exactly two hexadecimal digits |
| | @@ -11500,10 +11501,18 @@ |
| 11500 | 11501 | ** guaranteed that a call to sqlite3session_changeset() will return a |
| 11501 | 11502 | ** changeset containing zero changes. |
| 11502 | 11503 | */ |
| 11503 | 11504 | SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession); |
| 11504 | 11505 | |
| 11506 | +/* |
| 11507 | +** CAPI3REF: Query for the amount of heap memory used by a session object. |
| 11508 | +** |
| 11509 | +** This API returns the total amount of heap memory in bytes currently |
| 11510 | +** used by the session object passed as the only argument. |
| 11511 | +*/ |
| 11512 | +SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession); |
| 11513 | + |
| 11505 | 11514 | /* |
| 11506 | 11515 | ** CAPI3REF: Create An Iterator To Traverse A Changeset |
| 11507 | 11516 | ** CONSTRUCTOR: sqlite3_changeset_iter |
| 11508 | 11517 | ** |
| 11509 | 11518 | ** Create an iterator used to iterate through the contents of a changeset. |
| | @@ -14626,13 +14635,13 @@ |
| 14626 | 14635 | #endif |
| 14627 | 14636 | |
| 14628 | 14637 | /* |
| 14629 | 14638 | ** Macros for "wheretrace" |
| 14630 | 14639 | */ |
| 14640 | +SQLITE_PRIVATE u32 sqlite3WhereTrace; |
| 14631 | 14641 | #if defined(SQLITE_DEBUG) \ |
| 14632 | 14642 | && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
| 14633 | | -SQLITE_PRIVATE u32 sqlite3WhereTrace; |
| 14634 | 14643 | # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X |
| 14635 | 14644 | # define WHERETRACE_ENABLED 1 |
| 14636 | 14645 | #else |
| 14637 | 14646 | # define WHERETRACE(K,X) |
| 14638 | 14647 | #endif |
| | @@ -18158,11 +18167,11 @@ |
| 18158 | 18167 | ** TK_SELECT: 1st register of result vector */ |
| 18159 | 18168 | ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid. |
| 18160 | 18169 | ** TK_VARIABLE: variable number (always >= 1). |
| 18161 | 18170 | ** TK_SELECT_COLUMN: column of the result vector */ |
| 18162 | 18171 | i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */ |
| 18163 | | - i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */ |
| 18172 | + int iRightJoinTable; /* If EP_FromJoin, the right table of the join */ |
| 18164 | 18173 | AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */ |
| 18165 | 18174 | union { |
| 18166 | 18175 | Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL |
| 18167 | 18176 | ** for a column of an index on an expression */ |
| 18168 | 18177 | Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */ |
| | @@ -19361,11 +19370,10 @@ |
| 19361 | 19370 | SQLITE_PRIVATE void sqlite3WindowLink(Select *pSel, Window *pWin); |
| 19362 | 19371 | SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*, int); |
| 19363 | 19372 | SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Select*); |
| 19364 | 19373 | SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int); |
| 19365 | 19374 | SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*); |
| 19366 | | -SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*); |
| 19367 | 19375 | SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*); |
| 19368 | 19376 | SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p); |
| 19369 | 19377 | SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p); |
| 19370 | 19378 | SQLITE_PRIVATE void sqlite3WindowFunctions(void); |
| 19371 | 19379 | SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*); |
| | @@ -20098,10 +20106,11 @@ |
| 20098 | 20106 | SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...); |
| 20099 | 20107 | SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int); |
| 20100 | 20108 | SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int); |
| 20101 | 20109 | SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*); |
| 20102 | 20110 | SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*); |
| 20111 | +SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*); |
| 20103 | 20112 | SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p); |
| 20104 | 20113 | SQLITE_PRIVATE int sqlite3MatchEName( |
| 20105 | 20114 | const struct ExprList_item*, |
| 20106 | 20115 | const char*, |
| 20107 | 20116 | const char*, |
| | @@ -73754,10 +73763,13 @@ |
| 73754 | 73763 | getCellInfo(pSrc); |
| 73755 | 73764 | aOut += putVarint32(aOut, pSrc->info.nPayload); |
| 73756 | 73765 | if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey); |
| 73757 | 73766 | nIn = pSrc->info.nLocal; |
| 73758 | 73767 | aIn = pSrc->info.pPayload; |
| 73768 | + if( aIn+nIn>pSrc->pPage->aDataEnd ){ |
| 73769 | + return SQLITE_CORRUPT_BKPT; |
| 73770 | + } |
| 73759 | 73771 | nRem = pSrc->info.nPayload; |
| 73760 | 73772 | if( nIn==nRem && nIn<pDest->pPage->maxLocal ){ |
| 73761 | 73773 | memcpy(aOut, aIn, nIn); |
| 73762 | 73774 | pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace); |
| 73763 | 73775 | }else{ |
| | @@ -73774,10 +73786,13 @@ |
| 73774 | 73786 | pPgnoOut = &aOut[nOut]; |
| 73775 | 73787 | pBt->nPreformatSize += 4; |
| 73776 | 73788 | } |
| 73777 | 73789 | |
| 73778 | 73790 | if( nRem>nIn ){ |
| 73791 | + if( aIn+nIn+4>pSrc->pPage->aDataEnd ){ |
| 73792 | + return SQLITE_CORRUPT_BKPT; |
| 73793 | + } |
| 73779 | 73794 | ovflIn = get4byte(&pSrc->info.pPayload[nIn]); |
| 73780 | 73795 | } |
| 73781 | 73796 | |
| 73782 | 73797 | do { |
| 73783 | 73798 | nRem -= nOut; |
| | @@ -85613,11 +85628,11 @@ |
| 85613 | 85628 | testcase( zRawSql[0]=='#' ); |
| 85614 | 85629 | idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken); |
| 85615 | 85630 | assert( idx>0 ); |
| 85616 | 85631 | } |
| 85617 | 85632 | zRawSql += nToken; |
| 85618 | | - nextIndex = idx + 1; |
| 85633 | + nextIndex = MAX(idx + 1, nextIndex); |
| 85619 | 85634 | assert( idx>0 && idx<=p->nVar ); |
| 85620 | 85635 | pVar = &p->aVar[idx-1]; |
| 85621 | 85636 | if( pVar->flags & MEM_Null ){ |
| 85622 | 85637 | sqlite3_str_append(&out, "NULL", 4); |
| 85623 | 85638 | }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){ |
| | @@ -90934,10 +90949,12 @@ |
| 90934 | 90949 | case OP_RowCell: { |
| 90935 | 90950 | VdbeCursor *pDest; /* Cursor to write to */ |
| 90936 | 90951 | VdbeCursor *pSrc; /* Cursor to read from */ |
| 90937 | 90952 | i64 iKey; /* Rowid value to insert with */ |
| 90938 | 90953 | assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert ); |
| 90954 | + assert( pOp[1].opcode==OP_Insert || pOp->p3==0 ); |
| 90955 | + assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 ); |
| 90939 | 90956 | assert( pOp[1].p5 & OPFLAG_PREFORMAT ); |
| 90940 | 90957 | pDest = p->apCsr[pOp->p1]; |
| 90941 | 90958 | pSrc = p->apCsr[pOp->p2]; |
| 90942 | 90959 | iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0; |
| 90943 | 90960 | rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey); |
| | @@ -98200,11 +98217,11 @@ |
| 98200 | 98217 | #if !defined(SQLITE_OMIT_WINDOWFUNC) |
| 98201 | 98218 | /* |
| 98202 | 98219 | ** Walk all expressions linked into the list of Window objects passed |
| 98203 | 98220 | ** as the second argument. |
| 98204 | 98221 | */ |
| 98205 | | -static int walkWindowList(Walker *pWalker, Window *pList){ |
| 98222 | +static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){ |
| 98206 | 98223 | Window *pWin; |
| 98207 | 98224 | for(pWin=pList; pWin; pWin=pWin->pNextWin){ |
| 98208 | 98225 | int rc; |
| 98209 | 98226 | rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy); |
| 98210 | 98227 | if( rc ) return WRC_Abort; |
| | @@ -98219,10 +98236,11 @@ |
| 98219 | 98236 | ** not matter anyhow. */ |
| 98220 | 98237 | rc = sqlite3WalkExpr(pWalker, pWin->pStart); |
| 98221 | 98238 | if( NEVER(rc) ) return WRC_Abort; |
| 98222 | 98239 | rc = sqlite3WalkExpr(pWalker, pWin->pEnd); |
| 98223 | 98240 | if( NEVER(rc) ) return WRC_Abort; |
| 98241 | + if( bOneOnly ) break; |
| 98224 | 98242 | } |
| 98225 | 98243 | return WRC_Continue; |
| 98226 | 98244 | } |
| 98227 | 98245 | #endif |
| 98228 | 98246 | |
| | @@ -98266,11 +98284,11 @@ |
| 98266 | 98284 | if( pExpr->x.pList ){ |
| 98267 | 98285 | if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; |
| 98268 | 98286 | } |
| 98269 | 98287 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 98270 | 98288 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 98271 | | - if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort; |
| 98289 | + if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort; |
| 98272 | 98290 | } |
| 98273 | 98291 | #endif |
| 98274 | 98292 | } |
| 98275 | 98293 | } |
| 98276 | 98294 | break; |
| | @@ -98313,11 +98331,11 @@ |
| 98313 | 98331 | { |
| 98314 | 98332 | Parse *pParse = pWalker->pParse; |
| 98315 | 98333 | if( pParse && IN_RENAME_OBJECT ){ |
| 98316 | 98334 | /* The following may return WRC_Abort if there are unresolvable |
| 98317 | 98335 | ** symbols (e.g. a table that does not exist) in a window definition. */ |
| 98318 | | - int rc = walkWindowList(pWalker, p->pWinDefn); |
| 98336 | + int rc = walkWindowList(pWalker, p->pWinDefn, 0); |
| 98319 | 98337 | return rc; |
| 98320 | 98338 | } |
| 98321 | 98339 | } |
| 98322 | 98340 | #endif |
| 98323 | 98341 | return WRC_Continue; |
| | @@ -123431,11 +123449,11 @@ |
| 123431 | 123449 | ** to some other UNIQUE constraint is FAIL or IGNORE, then we need |
| 123432 | 123450 | ** to defer the running of the rowid conflict checking until after |
| 123433 | 123451 | ** the UNIQUE constraints have run. |
| 123434 | 123452 | */ |
| 123435 | 123453 | if( onError==OE_Replace /* IPK rule is REPLACE */ |
| 123436 | | - && onError!=overrideError /* Rules for other contraints are different */ |
| 123454 | + && onError!=overrideError /* Rules for other constraints are different */ |
| 123437 | 123455 | && pTab->pIndex /* There exist other constraints */ |
| 123438 | 123456 | ){ |
| 123439 | 123457 | ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1; |
| 123440 | 123458 | VdbeComment((v, "defer IPK REPLACE until last")); |
| 123441 | 123459 | } |
| | @@ -124484,11 +124502,11 @@ |
| 124484 | 124502 | if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break; |
| 124485 | 124503 | } |
| 124486 | 124504 | if( i==pSrcIdx->nColumn ){ |
| 124487 | 124505 | idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT; |
| 124488 | 124506 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest); |
| 124489 | | - sqlite3VdbeAddOp3(v, OP_RowCell, iDest, iSrc, regData); |
| 124507 | + sqlite3VdbeAddOp2(v, OP_RowCell, iDest, iSrc); |
| 124490 | 124508 | } |
| 124491 | 124509 | }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){ |
| 124492 | 124510 | idxInsFlags |= OPFLAG_NCHANGE; |
| 124493 | 124511 | } |
| 124494 | 124512 | if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){ |
| | @@ -130742,11 +130760,11 @@ |
| 130742 | 130760 | pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2); |
| 130743 | 130761 | if( pEq && isOuterJoin ){ |
| 130744 | 130762 | ExprSetProperty(pEq, EP_FromJoin); |
| 130745 | 130763 | assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) ); |
| 130746 | 130764 | ExprSetVVAProperty(pEq, EP_NoReduce); |
| 130747 | | - pEq->iRightJoinTable = (i16)pE2->iTable; |
| 130765 | + pEq->iRightJoinTable = pE2->iTable; |
| 130748 | 130766 | } |
| 130749 | 130767 | *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq); |
| 130750 | 130768 | } |
| 130751 | 130769 | |
| 130752 | 130770 | /* |
| | @@ -130778,11 +130796,11 @@ |
| 130778 | 130796 | SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr *p, int iTable){ |
| 130779 | 130797 | while( p ){ |
| 130780 | 130798 | ExprSetProperty(p, EP_FromJoin); |
| 130781 | 130799 | assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); |
| 130782 | 130800 | ExprSetVVAProperty(p, EP_NoReduce); |
| 130783 | | - p->iRightJoinTable = (i16)iTable; |
| 130801 | + p->iRightJoinTable = iTable; |
| 130784 | 130802 | if( p->op==TK_FUNCTION && p->x.pList ){ |
| 130785 | 130803 | int i; |
| 130786 | 130804 | for(i=0; i<p->x.pList->nExpr; i++){ |
| 130787 | 130805 | sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable); |
| 130788 | 130806 | } |
| | @@ -134044,10 +134062,92 @@ |
| 134044 | 134062 | pSrcItem->colUsed = 0; |
| 134045 | 134063 | sqlite3WalkSelect(&w, pSelect); |
| 134046 | 134064 | } |
| 134047 | 134065 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 134048 | 134066 | |
| 134067 | +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 134068 | +/* |
| 134069 | +** Assign new cursor numbers to each of the items in pSrc. For each |
| 134070 | +** new cursor number assigned, set an entry in the aCsrMap[] array |
| 134071 | +** to map the old cursor number to the new: |
| 134072 | +** |
| 134073 | +** aCsrMap[iOld] = iNew; |
| 134074 | +** |
| 134075 | +** The array is guaranteed by the caller to be large enough for all |
| 134076 | +** existing cursor numbers in pSrc. |
| 134077 | +** |
| 134078 | +** If pSrc contains any sub-selects, call this routine recursively |
| 134079 | +** on the FROM clause of each such sub-select, with iExcept set to -1. |
| 134080 | +*/ |
| 134081 | +static void srclistRenumberCursors( |
| 134082 | + Parse *pParse, /* Parse context */ |
| 134083 | + int *aCsrMap, /* Array to store cursor mappings in */ |
| 134084 | + SrcList *pSrc, /* FROM clause to renumber */ |
| 134085 | + int iExcept /* FROM clause item to skip */ |
| 134086 | +){ |
| 134087 | + int i; |
| 134088 | + struct SrcList_item *pItem; |
| 134089 | + for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){ |
| 134090 | + if( i!=iExcept ){ |
| 134091 | + Select *p; |
| 134092 | + pItem->iCursor = aCsrMap[pItem->iCursor] = pParse->nTab++; |
| 134093 | + for(p=pItem->pSelect; p; p=p->pPrior){ |
| 134094 | + srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1); |
| 134095 | + } |
| 134096 | + } |
| 134097 | + } |
| 134098 | +} |
| 134099 | + |
| 134100 | +/* |
| 134101 | +** Expression walker callback used by renumberCursors() to update |
| 134102 | +** Expr objects to match newly assigned cursor numbers. |
| 134103 | +*/ |
| 134104 | +static int renumberCursorsCb(Walker *pWalker, Expr *pExpr){ |
| 134105 | + int *aCsrMap = pWalker->u.aiCol; |
| 134106 | + if( pExpr->op==TK_COLUMN && aCsrMap[pExpr->iTable] ){ |
| 134107 | + pExpr->iTable = aCsrMap[pExpr->iTable]; |
| 134108 | + } |
| 134109 | + if( ExprHasProperty(pExpr, EP_FromJoin) && aCsrMap[pExpr->iRightJoinTable] ){ |
| 134110 | + pExpr->iRightJoinTable = aCsrMap[pExpr->iRightJoinTable]; |
| 134111 | + } |
| 134112 | + return WRC_Continue; |
| 134113 | +} |
| 134114 | + |
| 134115 | +/* |
| 134116 | +** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc) |
| 134117 | +** of the SELECT statement passed as the second argument, and to each |
| 134118 | +** cursor in the FROM clause of any FROM clause sub-selects, recursively. |
| 134119 | +** Except, do not assign a new cursor number to the iExcept'th element in |
| 134120 | +** the FROM clause of (*p). Update all expressions and other references |
| 134121 | +** to refer to the new cursor numbers. |
| 134122 | +** |
| 134123 | +** Argument aCsrMap is an array that may be used for temporary working |
| 134124 | +** space. Two guarantees are made by the caller: |
| 134125 | +** |
| 134126 | +** * the array is larger than the largest cursor number used within the |
| 134127 | +** select statement passed as an argument, and |
| 134128 | +** |
| 134129 | +** * the array entries for all cursor numbers that do *not* appear in |
| 134130 | +** FROM clauses of the select statement as described above are |
| 134131 | +** initialized to zero. |
| 134132 | +*/ |
| 134133 | +static void renumberCursors( |
| 134134 | + Parse *pParse, /* Parse context */ |
| 134135 | + Select *p, /* Select to renumber cursors within */ |
| 134136 | + int iExcept, /* FROM clause item to skip */ |
| 134137 | + int *aCsrMap /* Working space */ |
| 134138 | +){ |
| 134139 | + Walker w; |
| 134140 | + srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept); |
| 134141 | + memset(&w, 0, sizeof(w)); |
| 134142 | + w.u.aiCol = aCsrMap; |
| 134143 | + w.xExprCallback = renumberCursorsCb; |
| 134144 | + w.xSelectCallback = sqlite3SelectWalkNoop; |
| 134145 | + sqlite3WalkSelect(&w, p); |
| 134146 | +} |
| 134147 | +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 134148 | + |
| 134049 | 134149 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 134050 | 134150 | /* |
| 134051 | 134151 | ** This routine attempts to flatten subqueries as a performance optimization. |
| 134052 | 134152 | ** This routine returns 1 if it makes changes and 0 if no flattening occurs. |
| 134053 | 134153 | ** |
| | @@ -134138,13 +134238,13 @@ |
| 134138 | 134238 | ** (17b) no terms within the subquery compound may be aggregate |
| 134139 | 134239 | ** or DISTINCT, and |
| 134140 | 134240 | ** (17c) every term within the subquery compound must have a FROM clause |
| 134141 | 134241 | ** (17d) the outer query may not be |
| 134142 | 134242 | ** (17d1) aggregate, or |
| 134143 | | -** (17d2) DISTINCT, or |
| 134144 | | -** (17d3) a join. |
| 134145 | | -** (17e) the subquery may not contain window functions |
| 134243 | +** (17d2) DISTINCT |
| 134244 | +** (17e) the subquery may not contain window functions, and |
| 134245 | +** (17f) the subquery must not be the RHS of a LEFT JOIN. |
| 134146 | 134246 | ** |
| 134147 | 134247 | ** The parent and sub-query may contain WHERE clauses. Subject to |
| 134148 | 134248 | ** rules (11), (13) and (14), they may also contain ORDER BY, |
| 134149 | 134249 | ** LIMIT and OFFSET clauses. The subquery cannot use any compound |
| 134150 | 134250 | ** operator other than UNION ALL because all the other compound |
| | @@ -134156,12 +134256,12 @@ |
| 134156 | 134256 | ** SELECT statement, but all the code here does is make sure that no |
| 134157 | 134257 | ** such (illegal) sub-query is flattened. The caller will detect the |
| 134158 | 134258 | ** syntax error and return a detailed message. |
| 134159 | 134259 | ** |
| 134160 | 134260 | ** (18) If the sub-query is a compound select, then all terms of the |
| 134161 | | -** ORDER BY clause of the parent must be simple references to |
| 134162 | | -** columns of the sub-query. |
| 134261 | +** ORDER BY clause of the parent must be copies of a term returned |
| 134262 | +** by the parent query. |
| 134163 | 134263 | ** |
| 134164 | 134264 | ** (19) If the subquery uses LIMIT then the outer query may not |
| 134165 | 134265 | ** have a WHERE clause. |
| 134166 | 134266 | ** |
| 134167 | 134267 | ** (20) If the sub-query is a compound select, then it must not use |
| | @@ -134173,13 +134273,12 @@ |
| 134173 | 134273 | ** (21) If the subquery uses LIMIT then the outer query may not be |
| 134174 | 134274 | ** DISTINCT. (See ticket [752e1646fc]). |
| 134175 | 134275 | ** |
| 134176 | 134276 | ** (22) The subquery may not be a recursive CTE. |
| 134177 | 134277 | ** |
| 134178 | | -** (**) Subsumed into restriction (17d3). Was: If the outer query is |
| 134179 | | -** a recursive CTE, then the sub-query may not be a compound query. |
| 134180 | | -** This restriction is because transforming the |
| 134278 | +** (23) If the outer query is a recursive CTE, then the sub-query may not be |
| 134279 | +** a compound query. This restriction is because transforming the |
| 134181 | 134280 | ** parent to a compound query confuses the code that handles |
| 134182 | 134281 | ** recursive queries in multiSelect(). |
| 134183 | 134282 | ** |
| 134184 | 134283 | ** (**) We no longer attempt to flatten aggregate subqueries. Was: |
| 134185 | 134284 | ** The subquery may not be an aggregate that uses the built-in min() or |
| | @@ -134220,10 +134319,11 @@ |
| 134220 | 134319 | int i; /* Loop counter */ |
| 134221 | 134320 | Expr *pWhere; /* The WHERE clause */ |
| 134222 | 134321 | struct SrcList_item *pSubitem; /* The subquery */ |
| 134223 | 134322 | sqlite3 *db = pParse->db; |
| 134224 | 134323 | Walker w; /* Walker to persist agginfo data */ |
| 134324 | + int *aCsrMap = 0; |
| 134225 | 134325 | |
| 134226 | 134326 | /* Check to see if flattening is permitted. Return 0 if not. |
| 134227 | 134327 | */ |
| 134228 | 134328 | assert( p!=0 ); |
| 134229 | 134329 | assert( p->pPrior==0 ); |
| | @@ -134315,17 +134415,18 @@ |
| 134315 | 134415 | */ |
| 134316 | 134416 | if( pSub->pPrior ){ |
| 134317 | 134417 | if( pSub->pOrderBy ){ |
| 134318 | 134418 | return 0; /* Restriction (20) */ |
| 134319 | 134419 | } |
| 134320 | | - if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){ |
| 134321 | | - return 0; /* (17d1), (17d2), or (17d3) */ |
| 134420 | + if( isAgg || (p->selFlags & SF_Distinct)!=0 || isLeftJoin>0 ){ |
| 134421 | + return 0; /* (17d1), (17d2), or (17f) */ |
| 134322 | 134422 | } |
| 134323 | 134423 | for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){ |
| 134324 | 134424 | testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); |
| 134325 | 134425 | testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); |
| 134326 | 134426 | assert( pSub->pSrc!=0 ); |
| 134427 | + assert( (pSub->selFlags & SF_Recursive)==0 ); |
| 134327 | 134428 | assert( pSub->pEList->nExpr==pSub1->pEList->nExpr ); |
| 134328 | 134429 | if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */ |
| 134329 | 134430 | || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */ |
| 134330 | 134431 | || pSub1->pSrc->nSrc<1 /* (17c) */ |
| 134331 | 134432 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| | @@ -134342,19 +134443,18 @@ |
| 134342 | 134443 | int ii; |
| 134343 | 134444 | for(ii=0; ii<p->pOrderBy->nExpr; ii++){ |
| 134344 | 134445 | if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0; |
| 134345 | 134446 | } |
| 134346 | 134447 | } |
| 134347 | | - } |
| 134348 | | - |
| 134349 | | - /* Ex-restriction (23): |
| 134350 | | - ** The only way that the recursive part of a CTE can contain a compound |
| 134351 | | - ** subquery is for the subquery to be one term of a join. But if the |
| 134352 | | - ** subquery is a join, then the flattening has already been stopped by |
| 134353 | | - ** restriction (17d3) |
| 134354 | | - */ |
| 134355 | | - assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 ); |
| 134448 | + |
| 134449 | + /* Restriction (23) */ |
| 134450 | + if( (p->selFlags & SF_Recursive) ) return 0; |
| 134451 | + |
| 134452 | + if( pSrc->nSrc>1 ){ |
| 134453 | + aCsrMap = sqlite3DbMallocZero(db, pParse->nTab*sizeof(int)); |
| 134454 | + } |
| 134455 | + } |
| 134356 | 134456 | |
| 134357 | 134457 | /***** If we reach this point, flattening is permitted. *****/ |
| 134358 | 134458 | SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n", |
| 134359 | 134459 | pSub->selId, pSub, iFrom)); |
| 134360 | 134460 | |
| | @@ -134361,10 +134461,21 @@ |
| 134361 | 134461 | /* Authorize the subquery */ |
| 134362 | 134462 | pParse->zAuthContext = pSubitem->zName; |
| 134363 | 134463 | TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0); |
| 134364 | 134464 | testcase( i==SQLITE_DENY ); |
| 134365 | 134465 | pParse->zAuthContext = zSavedAuthContext; |
| 134466 | + |
| 134467 | + /* Delete the transient structures associated with thesubquery */ |
| 134468 | + pSub1 = pSubitem->pSelect; |
| 134469 | + sqlite3DbFree(db, pSubitem->zDatabase); |
| 134470 | + sqlite3DbFree(db, pSubitem->zName); |
| 134471 | + sqlite3DbFree(db, pSubitem->zAlias); |
| 134472 | + pSubitem->zDatabase = 0; |
| 134473 | + pSubitem->zName = 0; |
| 134474 | + pSubitem->zAlias = 0; |
| 134475 | + pSubitem->pSelect = 0; |
| 134476 | + assert( pSubitem->pOn==0 ); |
| 134366 | 134477 | |
| 134367 | 134478 | /* If the sub-query is a compound SELECT statement, then (by restrictions |
| 134368 | 134479 | ** 17 and 18 above) it must be a UNION ALL and the parent query must |
| 134369 | 134480 | ** be of the form: |
| 134370 | 134481 | ** |
| | @@ -134400,47 +134511,40 @@ |
| 134400 | 134511 | for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){ |
| 134401 | 134512 | Select *pNew; |
| 134402 | 134513 | ExprList *pOrderBy = p->pOrderBy; |
| 134403 | 134514 | Expr *pLimit = p->pLimit; |
| 134404 | 134515 | Select *pPrior = p->pPrior; |
| 134516 | + Table *pItemTab = pSubitem->pTab; |
| 134517 | + pSubitem->pTab = 0; |
| 134405 | 134518 | p->pOrderBy = 0; |
| 134406 | | - p->pSrc = 0; |
| 134407 | 134519 | p->pPrior = 0; |
| 134408 | 134520 | p->pLimit = 0; |
| 134409 | 134521 | pNew = sqlite3SelectDup(db, p, 0); |
| 134410 | 134522 | p->pLimit = pLimit; |
| 134411 | 134523 | p->pOrderBy = pOrderBy; |
| 134412 | | - p->pSrc = pSrc; |
| 134413 | 134524 | p->op = TK_ALL; |
| 134525 | + pSubitem->pTab = pItemTab; |
| 134414 | 134526 | if( pNew==0 ){ |
| 134415 | 134527 | p->pPrior = pPrior; |
| 134416 | 134528 | }else{ |
| 134529 | + if( aCsrMap && db->mallocFailed==0 ){ |
| 134530 | + renumberCursors(pParse, pNew, iFrom, aCsrMap); |
| 134531 | + } |
| 134417 | 134532 | pNew->pPrior = pPrior; |
| 134418 | 134533 | if( pPrior ) pPrior->pNext = pNew; |
| 134419 | 134534 | pNew->pNext = p; |
| 134420 | 134535 | p->pPrior = pNew; |
| 134421 | 134536 | SELECTTRACE(2,pParse,p,("compound-subquery flattener" |
| 134422 | 134537 | " creates %u as peer\n",pNew->selId)); |
| 134423 | 134538 | } |
| 134424 | | - if( db->mallocFailed ) return 1; |
| 134425 | | - } |
| 134426 | | - |
| 134427 | | - /* Begin flattening the iFrom-th entry of the FROM clause |
| 134428 | | - ** in the outer query. |
| 134429 | | - */ |
| 134430 | | - pSub = pSub1 = pSubitem->pSelect; |
| 134431 | | - |
| 134432 | | - /* Delete the transient table structure associated with the |
| 134433 | | - ** subquery |
| 134434 | | - */ |
| 134435 | | - sqlite3DbFree(db, pSubitem->zDatabase); |
| 134436 | | - sqlite3DbFree(db, pSubitem->zName); |
| 134437 | | - sqlite3DbFree(db, pSubitem->zAlias); |
| 134438 | | - pSubitem->zDatabase = 0; |
| 134439 | | - pSubitem->zName = 0; |
| 134440 | | - pSubitem->zAlias = 0; |
| 134441 | | - pSubitem->pSelect = 0; |
| 134539 | + assert( pSubitem->pSelect==0 ); |
| 134540 | + } |
| 134541 | + sqlite3DbFree(db, aCsrMap); |
| 134542 | + if( db->mallocFailed ){ |
| 134543 | + pSubitem->pSelect = pSub1; |
| 134544 | + return 1; |
| 134545 | + } |
| 134442 | 134546 | |
| 134443 | 134547 | /* Defer deleting the Table object associated with the |
| 134444 | 134548 | ** subquery until code generation is |
| 134445 | 134549 | ** complete, since there may still exist Expr.pTab entries that |
| 134446 | 134550 | ** refer to the subquery even after flattening. Ticket #3346. |
| | @@ -134470,26 +134574,21 @@ |
| 134470 | 134574 | ** iParent. The iParent cursor will never be used. Subsequent code |
| 134471 | 134575 | ** will scan expressions looking for iParent references and replace |
| 134472 | 134576 | ** those references with expressions that resolve to the subquery FROM |
| 134473 | 134577 | ** elements we are now copying in. |
| 134474 | 134578 | */ |
| 134579 | + pSub = pSub1; |
| 134475 | 134580 | for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){ |
| 134476 | 134581 | int nSubSrc; |
| 134477 | 134582 | u8 jointype = 0; |
| 134478 | 134583 | assert( pSub!=0 ); |
| 134479 | 134584 | pSubSrc = pSub->pSrc; /* FROM clause of subquery */ |
| 134480 | 134585 | nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */ |
| 134481 | 134586 | pSrc = pParent->pSrc; /* FROM clause of the outer query */ |
| 134482 | 134587 | |
| 134483 | | - if( pSrc ){ |
| 134484 | | - assert( pParent==p ); /* First time through the loop */ |
| 134485 | | - jointype = pSubitem->fg.jointype; |
| 134486 | | - }else{ |
| 134487 | | - assert( pParent!=p ); /* 2nd and subsequent times through the loop */ |
| 134488 | | - pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0); |
| 134489 | | - if( pSrc==0 ) break; |
| 134490 | | - pParent->pSrc = pSrc; |
| 134588 | + if( pParent==p ){ |
| 134589 | + jointype = pSubitem->fg.jointype; /* First time through the loop */ |
| 134491 | 134590 | } |
| 134492 | 134591 | |
| 134493 | 134592 | /* The subquery uses a single slot of the FROM clause of the outer |
| 134494 | 134593 | ** query. If the subquery has more than one element in its FROM clause, |
| 134495 | 134594 | ** then expand the outer query to make space for it to hold all elements |
| | @@ -136010,11 +136109,13 @@ |
| 136010 | 136109 | ** within the HAVING expression with a constant "1". |
| 136011 | 136110 | */ |
| 136012 | 136111 | static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){ |
| 136013 | 136112 | if( pExpr->op!=TK_AND ){ |
| 136014 | 136113 | Select *pS = pWalker->u.pSelect; |
| 136015 | | - if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){ |
| 136114 | + if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) |
| 136115 | + && ExprAlwaysFalse(pExpr)==0 |
| 136116 | + ){ |
| 136016 | 136117 | sqlite3 *db = pWalker->pParse->db; |
| 136017 | 136118 | Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1"); |
| 136018 | 136119 | if( pNew ){ |
| 136019 | 136120 | Expr *pWhere = pS->pWhere; |
| 136020 | 136121 | SWAP(Expr, *pNew, *pExpr); |
| | @@ -137284,19 +137385,17 @@ |
| 137284 | 137385 | sqlite3ExprListDelete(db, pMinMaxOrderBy); |
| 137285 | 137386 | #ifdef SQLITE_DEBUG |
| 137286 | 137387 | if( pAggInfo && !db->mallocFailed ){ |
| 137287 | 137388 | for(i=0; i<pAggInfo->nColumn; i++){ |
| 137288 | 137389 | Expr *pExpr = pAggInfo->aCol[i].pCExpr; |
| 137289 | | - assert( pExpr!=0 || db->mallocFailed ); |
| 137290 | | - if( pExpr==0 ) continue; |
| 137390 | + assert( pExpr!=0 ); |
| 137291 | 137391 | assert( pExpr->pAggInfo==pAggInfo ); |
| 137292 | 137392 | assert( pExpr->iAgg==i ); |
| 137293 | 137393 | } |
| 137294 | 137394 | for(i=0; i<pAggInfo->nFunc; i++){ |
| 137295 | 137395 | Expr *pExpr = pAggInfo->aFunc[i].pFExpr; |
| 137296 | | - assert( pExpr!=0 || db->mallocFailed ); |
| 137297 | | - if( pExpr==0 ) continue; |
| 137396 | + assert( pExpr!=0 ); |
| 137298 | 137397 | assert( pExpr->pAggInfo==pAggInfo ); |
| 137299 | 137398 | assert( pExpr->iAgg==i ); |
| 137300 | 137399 | } |
| 137301 | 137400 | } |
| 137302 | 137401 | #endif |
| | @@ -149427,11 +149526,11 @@ |
| 149427 | 149526 | pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm |
| 149428 | 149527 | ); |
| 149429 | 149528 | pBtm = pTerm; |
| 149430 | 149529 | pTop = 0; |
| 149431 | 149530 | if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 149432 | | - /* Range contraints that come from the LIKE optimization are |
| 149531 | + /* Range constraints that come from the LIKE optimization are |
| 149433 | 149532 | ** always used in pairs. */ |
| 149434 | 149533 | pTop = &pTerm[1]; |
| 149435 | 149534 | assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); |
| 149436 | 149535 | assert( pTop->wtFlags & TERM_LIKEOPT ); |
| 149437 | 149536 | assert( pTop->eOperator==WO_LT ); |
| | @@ -203997,10 +204096,11 @@ |
| 203997 | 204096 | int bIndirect; /* True if all changes are indirect */ |
| 203998 | 204097 | int bAutoAttach; /* True to auto-attach tables */ |
| 203999 | 204098 | int rc; /* Non-zero if an error has occurred */ |
| 204000 | 204099 | void *pFilterCtx; /* First argument to pass to xTableFilter */ |
| 204001 | 204100 | int (*xTableFilter)(void *pCtx, const char *zTab); |
| 204101 | + i64 nMalloc; /* Number of bytes of data allocated */ |
| 204002 | 204102 | sqlite3_value *pZeroBlob; /* Value containing X'' */ |
| 204003 | 204103 | sqlite3_session *pNext; /* Next session object on same db. */ |
| 204004 | 204104 | SessionTable *pTable; /* List of attached tables */ |
| 204005 | 204105 | SessionHook hook; /* APIs to grab new and old data with */ |
| 204006 | 204106 | }; |
| | @@ -204380,10 +204480,30 @@ |
| 204380 | 204480 | |
| 204381 | 204481 | if( pnWrite ) *pnWrite += nByte; |
| 204382 | 204482 | return SQLITE_OK; |
| 204383 | 204483 | } |
| 204384 | 204484 | |
| 204485 | +/* |
| 204486 | +** Allocate and return a pointer to a buffer nByte bytes in size. If |
| 204487 | +** pSession is not NULL, increase the sqlite3_session.nMalloc variable |
| 204488 | +** by the number of bytes allocated. |
| 204489 | +*/ |
| 204490 | +static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){ |
| 204491 | + void *pRet = sqlite3_malloc64(nByte); |
| 204492 | + if( pSession ) pSession->nMalloc += sqlite3_msize(pRet); |
| 204493 | + return pRet; |
| 204494 | +} |
| 204495 | + |
| 204496 | +/* |
| 204497 | +** Free buffer pFree, which must have been allocated by an earlier |
| 204498 | +** call to sessionMalloc64(). If pSession is not NULL, decrease the |
| 204499 | +** sqlite3_session.nMalloc counter by the number of bytes freed. |
| 204500 | +*/ |
| 204501 | +static void sessionFree(sqlite3_session *pSession, void *pFree){ |
| 204502 | + if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree); |
| 204503 | + sqlite3_free(pFree); |
| 204504 | +} |
| 204385 | 204505 | |
| 204386 | 204506 | /* |
| 204387 | 204507 | ** This macro is used to calculate hash key values for data structures. In |
| 204388 | 204508 | ** order to use this macro, the entire data structure must be represented |
| 204389 | 204509 | ** as a series of unsigned integers. In order to calculate a hash-key value |
| | @@ -204847,17 +204967,23 @@ |
| 204847 | 204967 | ** It is possible that a non-fatal OOM error occurs in this function. In |
| 204848 | 204968 | ** that case the hash-table does not grow, but SQLITE_OK is returned anyway. |
| 204849 | 204969 | ** Growing the hash table in this case is a performance optimization only, |
| 204850 | 204970 | ** it is not required for correct operation. |
| 204851 | 204971 | */ |
| 204852 | | -static int sessionGrowHash(int bPatchset, SessionTable *pTab){ |
| 204972 | +static int sessionGrowHash( |
| 204973 | + sqlite3_session *pSession, /* For memory accounting. May be NULL */ |
| 204974 | + int bPatchset, |
| 204975 | + SessionTable *pTab |
| 204976 | +){ |
| 204853 | 204977 | if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){ |
| 204854 | 204978 | int i; |
| 204855 | 204979 | SessionChange **apNew; |
| 204856 | 204980 | sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128); |
| 204857 | 204981 | |
| 204858 | | - apNew = (SessionChange **)sqlite3_malloc64(sizeof(SessionChange *) * nNew); |
| 204982 | + apNew = (SessionChange**)sessionMalloc64( |
| 204983 | + pSession, sizeof(SessionChange*) * nNew |
| 204984 | + ); |
| 204859 | 204985 | if( apNew==0 ){ |
| 204860 | 204986 | if( pTab->nChange==0 ){ |
| 204861 | 204987 | return SQLITE_ERROR; |
| 204862 | 204988 | } |
| 204863 | 204989 | return SQLITE_OK; |
| | @@ -204874,11 +205000,11 @@ |
| 204874 | 205000 | p->pNext = apNew[iHash]; |
| 204875 | 205001 | apNew[iHash] = p; |
| 204876 | 205002 | } |
| 204877 | 205003 | } |
| 204878 | 205004 | |
| 204879 | | - sqlite3_free(pTab->apChange); |
| 205005 | + sessionFree(pSession, pTab->apChange); |
| 204880 | 205006 | pTab->nChange = nNew; |
| 204881 | 205007 | pTab->apChange = apNew; |
| 204882 | 205008 | } |
| 204883 | 205009 | |
| 204884 | 205010 | return SQLITE_OK; |
| | @@ -204908,10 +205034,11 @@ |
| 204908 | 205034 | ** |
| 204909 | 205035 | ** All returned buffers are part of the same single allocation, which must |
| 204910 | 205036 | ** be freed using sqlite3_free() by the caller |
| 204911 | 205037 | */ |
| 204912 | 205038 | static int sessionTableInfo( |
| 205039 | + sqlite3_session *pSession, /* For memory accounting. May be NULL */ |
| 204913 | 205040 | sqlite3 *db, /* Database connection */ |
| 204914 | 205041 | const char *zDb, /* Name of attached database (e.g. "main") */ |
| 204915 | 205042 | const char *zThis, /* Table name */ |
| 204916 | 205043 | int *pnCol, /* OUT: number of columns */ |
| 204917 | 205044 | const char **pzTab, /* OUT: Copy of zThis */ |
| | @@ -204962,11 +205089,11 @@ |
| 204962 | 205089 | } |
| 204963 | 205090 | rc = sqlite3_reset(pStmt); |
| 204964 | 205091 | |
| 204965 | 205092 | if( rc==SQLITE_OK ){ |
| 204966 | 205093 | nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1); |
| 204967 | | - pAlloc = sqlite3_malloc64(nByte); |
| 205094 | + pAlloc = sessionMalloc64(pSession, nByte); |
| 204968 | 205095 | if( pAlloc==0 ){ |
| 204969 | 205096 | rc = SQLITE_NOMEM; |
| 204970 | 205097 | } |
| 204971 | 205098 | } |
| 204972 | 205099 | if( rc==SQLITE_OK ){ |
| | @@ -205005,11 +205132,11 @@ |
| 205005 | 205132 | }else{ |
| 205006 | 205133 | *pazCol = 0; |
| 205007 | 205134 | *pabPK = 0; |
| 205008 | 205135 | *pnCol = 0; |
| 205009 | 205136 | if( pzTab ) *pzTab = 0; |
| 205010 | | - sqlite3_free(azCol); |
| 205137 | + sessionFree(pSession, azCol); |
| 205011 | 205138 | } |
| 205012 | 205139 | sqlite3_finalize(pStmt); |
| 205013 | 205140 | return rc; |
| 205014 | 205141 | } |
| 205015 | 205142 | |
| | @@ -205027,11 +205154,11 @@ |
| 205027 | 205154 | */ |
| 205028 | 205155 | static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){ |
| 205029 | 205156 | if( pTab->nCol==0 ){ |
| 205030 | 205157 | u8 *abPK; |
| 205031 | 205158 | assert( pTab->azCol==0 || pTab->abPK==0 ); |
| 205032 | | - pSession->rc = sessionTableInfo(pSession->db, pSession->zDb, |
| 205159 | + pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb, |
| 205033 | 205160 | pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK |
| 205034 | 205161 | ); |
| 205035 | 205162 | if( pSession->rc==SQLITE_OK ){ |
| 205036 | 205163 | int i; |
| 205037 | 205164 | for(i=0; i<pTab->nCol; i++){ |
| | @@ -205118,11 +205245,11 @@ |
| 205118 | 205245 | pSession->rc = SQLITE_SCHEMA; |
| 205119 | 205246 | return; |
| 205120 | 205247 | } |
| 205121 | 205248 | |
| 205122 | 205249 | /* Grow the hash table if required */ |
| 205123 | | - if( sessionGrowHash(0, pTab) ){ |
| 205250 | + if( sessionGrowHash(pSession, 0, pTab) ){ |
| 205124 | 205251 | pSession->rc = SQLITE_NOMEM; |
| 205125 | 205252 | return; |
| 205126 | 205253 | } |
| 205127 | 205254 | |
| 205128 | 205255 | if( pTab->bStat1 ){ |
| | @@ -205185,11 +205312,11 @@ |
| 205185 | 205312 | rc = sessionSerializeValue(0, p, &nByte); |
| 205186 | 205313 | if( rc!=SQLITE_OK ) goto error_out; |
| 205187 | 205314 | } |
| 205188 | 205315 | |
| 205189 | 205316 | /* Allocate the change object */ |
| 205190 | | - pChange = (SessionChange *)sqlite3_malloc64(nByte); |
| 205317 | + pChange = (SessionChange *)sessionMalloc64(pSession, nByte); |
| 205191 | 205318 | if( !pChange ){ |
| 205192 | 205319 | rc = SQLITE_NOMEM; |
| 205193 | 205320 | goto error_out; |
| 205194 | 205321 | }else{ |
| 205195 | 205322 | memset(pChange, 0, sizeof(SessionChange)); |
| | @@ -205558,11 +205685,11 @@ |
| 205558 | 205685 | int bHasPk = 0; |
| 205559 | 205686 | int bMismatch = 0; |
| 205560 | 205687 | int nCol; /* Columns in zFrom.zTbl */ |
| 205561 | 205688 | u8 *abPK; |
| 205562 | 205689 | const char **azCol = 0; |
| 205563 | | - rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK); |
| 205690 | + rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, &azCol, &abPK); |
| 205564 | 205691 | if( rc==SQLITE_OK ){ |
| 205565 | 205692 | if( pTo->nCol!=nCol ){ |
| 205566 | 205693 | bMismatch = 1; |
| 205567 | 205694 | }else{ |
| 205568 | 205695 | int i; |
| | @@ -205656,11 +205783,11 @@ |
| 205656 | 205783 | |
| 205657 | 205784 | /* |
| 205658 | 205785 | ** Free the list of table objects passed as the first argument. The contents |
| 205659 | 205786 | ** of the changed-rows hash tables are also deleted. |
| 205660 | 205787 | */ |
| 205661 | | -static void sessionDeleteTable(SessionTable *pList){ |
| 205788 | +static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){ |
| 205662 | 205789 | SessionTable *pNext; |
| 205663 | 205790 | SessionTable *pTab; |
| 205664 | 205791 | |
| 205665 | 205792 | for(pTab=pList; pTab; pTab=pNext){ |
| 205666 | 205793 | int i; |
| | @@ -205668,16 +205795,16 @@ |
| 205668 | 205795 | for(i=0; i<pTab->nChange; i++){ |
| 205669 | 205796 | SessionChange *p; |
| 205670 | 205797 | SessionChange *pNextChange; |
| 205671 | 205798 | for(p=pTab->apChange[i]; p; p=pNextChange){ |
| 205672 | 205799 | pNextChange = p->pNext; |
| 205673 | | - sqlite3_free(p); |
| 205800 | + sessionFree(pSession, p); |
| 205674 | 205801 | } |
| 205675 | 205802 | } |
| 205676 | | - sqlite3_free((char*)pTab->azCol); /* cast works around VC++ bug */ |
| 205677 | | - sqlite3_free(pTab->apChange); |
| 205678 | | - sqlite3_free(pTab); |
| 205803 | + sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */ |
| 205804 | + sessionFree(pSession, pTab->apChange); |
| 205805 | + sessionFree(pSession, pTab); |
| 205679 | 205806 | } |
| 205680 | 205807 | } |
| 205681 | 205808 | |
| 205682 | 205809 | /* |
| 205683 | 205810 | ** Delete a session object previously allocated using sqlite3session_create(). |
| | @@ -205701,13 +205828,15 @@ |
| 205701 | 205828 | sqlite3_mutex_leave(sqlite3_db_mutex(db)); |
| 205702 | 205829 | sqlite3ValueFree(pSession->pZeroBlob); |
| 205703 | 205830 | |
| 205704 | 205831 | /* Delete all attached table objects. And the contents of their |
| 205705 | 205832 | ** associated hash-tables. */ |
| 205706 | | - sessionDeleteTable(pSession->pTable); |
| 205833 | + sessionDeleteTable(pSession, pSession->pTable); |
| 205707 | 205834 | |
| 205708 | | - /* Free the session object itself. */ |
| 205835 | + /* Assert that all allocations have been freed and then free the |
| 205836 | + ** session object itself. */ |
| 205837 | + assert( pSession->nMalloc==0 ); |
| 205709 | 205838 | sqlite3_free(pSession); |
| 205710 | 205839 | } |
| 205711 | 205840 | |
| 205712 | 205841 | /* |
| 205713 | 205842 | ** Set a table filter on a Session Object. |
| | @@ -205750,11 +205879,12 @@ |
| 205750 | 205879 | if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break; |
| 205751 | 205880 | } |
| 205752 | 205881 | |
| 205753 | 205882 | if( !pTab ){ |
| 205754 | 205883 | /* Allocate new SessionTable object. */ |
| 205755 | | - pTab = (SessionTable *)sqlite3_malloc64(sizeof(SessionTable) + nName + 1); |
| 205884 | + int nByte = sizeof(SessionTable) + nName + 1; |
| 205885 | + pTab = (SessionTable*)sessionMalloc64(pSession, nByte); |
| 205756 | 205886 | if( !pTab ){ |
| 205757 | 205887 | rc = SQLITE_NOMEM; |
| 205758 | 205888 | }else{ |
| 205759 | 205889 | /* Populate the new SessionTable object and link it into the list. |
| 205760 | 205890 | ** The new object must be linked onto the end of the list, not |
| | @@ -206347,11 +206477,11 @@ |
| 206347 | 206477 | sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */ |
| 206348 | 206478 | int nRewind = buf.nBuf; /* Initial size of write buffer */ |
| 206349 | 206479 | int nNoop; /* Size of buffer after writing tbl header */ |
| 206350 | 206480 | |
| 206351 | 206481 | /* Check the table schema is still Ok. */ |
| 206352 | | - rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK); |
| 206482 | + rc = sessionTableInfo(0, db, pSession->zDb, zName, &nCol, 0,&azCol,&abPK); |
| 206353 | 206483 | if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){ |
| 206354 | 206484 | rc = SQLITE_SCHEMA; |
| 206355 | 206485 | } |
| 206356 | 206486 | |
| 206357 | 206487 | /* Write a table header */ |
| | @@ -206521,10 +206651,17 @@ |
| 206521 | 206651 | } |
| 206522 | 206652 | sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db)); |
| 206523 | 206653 | |
| 206524 | 206654 | return (ret==0); |
| 206525 | 206655 | } |
| 206656 | + |
| 206657 | +/* |
| 206658 | +** Return the amount of heap memory in use. |
| 206659 | +*/ |
| 206660 | +SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){ |
| 206661 | + return pSession->nMalloc; |
| 206662 | +} |
| 206526 | 206663 | |
| 206527 | 206664 | /* |
| 206528 | 206665 | ** Do the work for either sqlite3changeset_start() or start_strm(). |
| 206529 | 206666 | */ |
| 206530 | 206667 | static int sessionChangesetStart( |
| | @@ -208333,11 +208470,11 @@ |
| 208333 | 208470 | }else{ |
| 208334 | 208471 | int nMinCol = 0; |
| 208335 | 208472 | int i; |
| 208336 | 208473 | |
| 208337 | 208474 | sqlite3changeset_pk(pIter, &abPK, 0); |
| 208338 | | - rc = sessionTableInfo( |
| 208475 | + rc = sessionTableInfo(0, |
| 208339 | 208476 | db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK |
| 208340 | 208477 | ); |
| 208341 | 208478 | if( rc!=SQLITE_OK ) break; |
| 208342 | 208479 | for(i=0; i<sApply.nCol; i++){ |
| 208343 | 208480 | if( sApply.abPK[i] ) nMinCol = i+1; |
| | @@ -208812,11 +208949,11 @@ |
| 208812 | 208949 | rc = SQLITE_SCHEMA; |
| 208813 | 208950 | break; |
| 208814 | 208951 | } |
| 208815 | 208952 | } |
| 208816 | 208953 | |
| 208817 | | - if( sessionGrowHash(pIter->bPatchset, pTab) ){ |
| 208954 | + if( sessionGrowHash(0, pIter->bPatchset, pTab) ){ |
| 208818 | 208955 | rc = SQLITE_NOMEM; |
| 208819 | 208956 | break; |
| 208820 | 208957 | } |
| 208821 | 208958 | iHash = sessionChangeHash( |
| 208822 | 208959 | pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange |
| | @@ -208998,11 +209135,11 @@ |
| 208998 | 209135 | /* |
| 208999 | 209136 | ** Delete a changegroup object. |
| 209000 | 209137 | */ |
| 209001 | 209138 | SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){ |
| 209002 | 209139 | if( pGrp ){ |
| 209003 | | - sessionDeleteTable(pGrp->pList); |
| 209140 | + sessionDeleteTable(0, pGrp->pList); |
| 209004 | 209141 | sqlite3_free(pGrp); |
| 209005 | 209142 | } |
| 209006 | 209143 | } |
| 209007 | 209144 | |
| 209008 | 209145 | /* |
| | @@ -209399,11 +209536,11 @@ |
| 209399 | 209536 | /* |
| 209400 | 209537 | ** Destroy a rebaser object |
| 209401 | 209538 | */ |
| 209402 | 209539 | SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){ |
| 209403 | 209540 | if( p ){ |
| 209404 | | - sessionDeleteTable(p->grp.pList); |
| 209541 | + sessionDeleteTable(0, p->grp.pList); |
| 209405 | 209542 | sqlite3_free(p); |
| 209406 | 209543 | } |
| 209407 | 209544 | } |
| 209408 | 209545 | |
| 209409 | 209546 | /* |
| | @@ -220128,18 +220265,14 @@ |
| 220128 | 220265 | if( pbNewTerm ) *pbNewTerm = 1; |
| 220129 | 220266 | } |
| 220130 | 220267 | }else{ |
| 220131 | 220268 | /* The following could be done by calling fts5SegIterLoadNPos(). But |
| 220132 | 220269 | ** this block is particularly performance critical, so equivalent |
| 220133 | | - ** code is inlined. |
| 220134 | | - ** |
| 220135 | | - ** Later: Switched back to fts5SegIterLoadNPos() because it supports |
| 220136 | | - ** detail=none mode. Not ideal. |
| 220137 | | - */ |
| 220270 | + ** code is inlined. */ |
| 220138 | 220271 | int nSz; |
| 220139 | 220272 | assert( p->rc==SQLITE_OK ); |
| 220140 | | - assert( pIter->iLeafOffset<=pIter->pLeaf->nn ); |
| 220273 | + assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn ); |
| 220141 | 220274 | fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz); |
| 220142 | 220275 | pIter->bDel = (nSz & 0x0001); |
| 220143 | 220276 | pIter->nPos = nSz>>1; |
| 220144 | 220277 | assert_nc( pIter->nPos>=0 ); |
| 220145 | 220278 | } |
| | @@ -227551,11 +227684,11 @@ |
| 227551 | 227684 | int nArg, /* Number of args */ |
| 227552 | 227685 | sqlite3_value **apUnused /* Function arguments */ |
| 227553 | 227686 | ){ |
| 227554 | 227687 | assert( nArg==0 ); |
| 227555 | 227688 | UNUSED_PARAM2(nArg, apUnused); |
| 227556 | | - sqlite3_result_text(pCtx, "fts5: 2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66f660", -1, SQLITE_TRANSIENT); |
| 227689 | + sqlite3_result_text(pCtx, "fts5: 2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124d8d7f", -1, SQLITE_TRANSIENT); |
| 227557 | 227690 | } |
| 227558 | 227691 | |
| 227559 | 227692 | /* |
| 227560 | 227693 | ** Return true if zName is the extension on one of the shadow tables used |
| 227561 | 227694 | ** by this module. |
| | @@ -232477,12 +232610,12 @@ |
| 232477 | 232610 | } |
| 232478 | 232611 | #endif /* SQLITE_CORE */ |
| 232479 | 232612 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 232480 | 232613 | |
| 232481 | 232614 | /************** End of stmt.c ************************************************/ |
| 232482 | | -#if __LINE__!=232482 |
| 232615 | +#if __LINE__!=232615 |
| 232483 | 232616 | #undef SQLITE_SOURCE_ID |
| 232484 | | -#define SQLITE_SOURCE_ID "2020-12-16 14:20:45 31cd1bbfa5b06723288d99d1cb423f88353bdef770b82e9103f71a796d66alt2" |
| 232617 | +#define SQLITE_SOURCE_ID "2020-12-30 13:20:27 45f46317ab8bd92dcd346bf00ba3a33b0cfd030b790c04e19ef33cff124dalt2" |
| 232485 | 232618 | #endif |
| 232486 | 232619 | /* Return the source-id for this library */ |
| 232487 | 232620 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 232488 | 232621 | /************************** End of sqlite3.c ******************************/ |
| 232489 | 232622 | |