| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.32.0. By combining all the individual C code files into this |
| 3 | +** version 3.32.1. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -1160,13 +1160,13 @@ |
| 1160 | 1160 | ** |
| 1161 | 1161 | ** See also: [sqlite3_libversion()], |
| 1162 | 1162 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1163 | 1163 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1164 | 1164 | */ |
| 1165 | | -#define SQLITE_VERSION "3.32.0" |
| 1166 | | -#define SQLITE_VERSION_NUMBER 3032000 |
| 1167 | | -#define SQLITE_SOURCE_ID "2020-05-17 13:47:28 69e149f76853d196c8855fedfc98848b60fb116ac36bc08824b1a122469f8ece" |
| 1165 | +#define SQLITE_VERSION "3.32.1" |
| 1166 | +#define SQLITE_VERSION_NUMBER 3032001 |
| 1167 | +#define SQLITE_SOURCE_ID "2020-05-25 16:19:56 0c1fcf4711a2e66c813aed38cf41cd3e2123ee8eb6db98118086764c4ba83350" |
| 1168 | 1168 | |
| 1169 | 1169 | /* |
| 1170 | 1170 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1171 | 1171 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1172 | 1172 | ** |
| | @@ -17841,11 +17841,11 @@ |
| 17841 | 17841 | /* |
| 17842 | 17842 | ** An instance of this structure contains information needed to generate |
| 17843 | 17843 | ** code for a SELECT that contains aggregate functions. |
| 17844 | 17844 | ** |
| 17845 | 17845 | ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a |
| 17846 | | -** pointer to this structure. The Expr.iColumn field is the index in |
| 17846 | +** pointer to this structure. The Expr.iAgg field is the index in |
| 17847 | 17847 | ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate |
| 17848 | 17848 | ** code for that node. |
| 17849 | 17849 | ** |
| 17850 | 17850 | ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the |
| 17851 | 17851 | ** original Select structure that describes the SELECT statement. These |
| | @@ -19080,10 +19080,13 @@ |
| 19080 | 19080 | SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*); |
| 19081 | 19081 | SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*); |
| 19082 | 19082 | SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*); |
| 19083 | 19083 | SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*); |
| 19084 | 19084 | SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*); |
| 19085 | +SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker*,Select*); |
| 19086 | +SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker*,Select*); |
| 19087 | + |
| 19085 | 19088 | #ifdef SQLITE_DEBUG |
| 19086 | 19089 | SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*); |
| 19087 | 19090 | #endif |
| 19088 | 19091 | |
| 19089 | 19092 | /* |
| | @@ -28274,10 +28277,17 @@ |
| 28274 | 28277 | #ifndef SQLITE_PRINT_BUF_SIZE |
| 28275 | 28278 | # define SQLITE_PRINT_BUF_SIZE 70 |
| 28276 | 28279 | #endif |
| 28277 | 28280 | #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ |
| 28278 | 28281 | |
| 28282 | +/* |
| 28283 | +** Hard limit on the precision of floating-point conversions. |
| 28284 | +*/ |
| 28285 | +#ifndef SQLITE_PRINTF_PRECISION_LIMIT |
| 28286 | +# define SQLITE_FP_PRECISION_LIMIT 100000000 |
| 28287 | +#endif |
| 28288 | + |
| 28279 | 28289 | /* |
| 28280 | 28290 | ** Render a string given by "fmt" into the StrAccum object. |
| 28281 | 28291 | */ |
| 28282 | 28292 | SQLITE_API void sqlite3_str_vappendf( |
| 28283 | 28293 | sqlite3_str *pAccum, /* Accumulate results here */ |
| | @@ -28474,10 +28484,12 @@ |
| 28474 | 28484 | ** precision The specified precision. The default |
| 28475 | 28485 | ** is -1. |
| 28476 | 28486 | ** xtype The class of the conversion. |
| 28477 | 28487 | ** infop Pointer to the appropriate info struct. |
| 28478 | 28488 | */ |
| 28489 | + assert( width>=0 ); |
| 28490 | + assert( precision>=(-1) ); |
| 28479 | 28491 | switch( xtype ){ |
| 28480 | 28492 | case etPOINTER: |
| 28481 | 28493 | flag_long = sizeof(char*)==sizeof(i64) ? 2 : |
| 28482 | 28494 | sizeof(char*)==sizeof(long int) ? 1 : 0; |
| 28483 | 28495 | /* Fall through into the next case */ |
| | @@ -28595,10 +28607,15 @@ |
| 28595 | 28607 | } |
| 28596 | 28608 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 28597 | 28609 | length = 0; |
| 28598 | 28610 | #else |
| 28599 | 28611 | if( precision<0 ) precision = 6; /* Set default precision */ |
| 28612 | +#ifdef SQLITE_FP_PRECISION_LIMIT |
| 28613 | + if( precision>SQLITE_FP_PRECISION_LIMIT ){ |
| 28614 | + precision = SQLITE_FP_PRECISION_LIMIT; |
| 28615 | + } |
| 28616 | +#endif |
| 28600 | 28617 | if( realvalue<0.0 ){ |
| 28601 | 28618 | realvalue = -realvalue; |
| 28602 | 28619 | prefix = '-'; |
| 28603 | 28620 | }else{ |
| 28604 | 28621 | prefix = flag_prefix; |
| | @@ -28877,11 +28894,11 @@ |
| 28877 | 28894 | }else{ |
| 28878 | 28895 | escarg = va_arg(ap,char*); |
| 28879 | 28896 | } |
| 28880 | 28897 | isnull = escarg==0; |
| 28881 | 28898 | if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); |
| 28882 | | - /* For %q, %Q, and %w, the precision is the number of byte (or |
| 28899 | + /* For %q, %Q, and %w, the precision is the number of bytes (or |
| 28883 | 28900 | ** characters if the ! flags is present) to use from the input. |
| 28884 | 28901 | ** Because of the extra quoting characters inserted, the number |
| 28885 | 28902 | ** of output characters may be larger than the precision. |
| 28886 | 28903 | */ |
| 28887 | 28904 | k = precision; |
| | @@ -29962,12 +29979,13 @@ |
| 29962 | 29979 | #else |
| 29963 | 29980 | pWin = 0; |
| 29964 | 29981 | #endif |
| 29965 | 29982 | } |
| 29966 | 29983 | if( pExpr->op==TK_AGG_FUNCTION ){ |
| 29967 | | - sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s", |
| 29968 | | - pExpr->op2, pExpr->u.zToken, zFlgs); |
| 29984 | + sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s iAgg=%d agg=%p", |
| 29985 | + pExpr->op2, pExpr->u.zToken, zFlgs, |
| 29986 | + pExpr->iAgg, pExpr->pAggInfo); |
| 29969 | 29987 | }else if( pExpr->op2!=0 ){ |
| 29970 | 29988 | const char *zOp2; |
| 29971 | 29989 | char zBuf[8]; |
| 29972 | 29990 | sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2); |
| 29973 | 29991 | zOp2 = zBuf; |
| | @@ -30856,10 +30874,11 @@ |
| 30856 | 30874 | /* UTF-16 Little-endian -> UTF-8 */ |
| 30857 | 30875 | while( zIn<zTerm ){ |
| 30858 | 30876 | c = *(zIn++); |
| 30859 | 30877 | c += (*(zIn++))<<8; |
| 30860 | 30878 | if( c>=0xd800 && c<0xe000 ){ |
| 30879 | +#ifdef SQLITE_REPLACE_INVALID_UTF |
| 30861 | 30880 | if( c>=0xdc00 || zIn>=zTerm ){ |
| 30862 | 30881 | c = 0xfffd; |
| 30863 | 30882 | }else{ |
| 30864 | 30883 | int c2 = *(zIn++); |
| 30865 | 30884 | c2 += (*(zIn++))<<8; |
| | @@ -30868,19 +30887,27 @@ |
| 30868 | 30887 | c = 0xfffd; |
| 30869 | 30888 | }else{ |
| 30870 | 30889 | c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000; |
| 30871 | 30890 | } |
| 30872 | 30891 | } |
| 30892 | +#else |
| 30893 | + if( zIn<zTerm ){ |
| 30894 | + int c2 = (*zIn++); |
| 30895 | + c2 += ((*zIn++)<<8); |
| 30896 | + c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); |
| 30897 | + } |
| 30898 | +#endif |
| 30873 | 30899 | } |
| 30874 | 30900 | WRITE_UTF8(z, c); |
| 30875 | 30901 | } |
| 30876 | 30902 | }else{ |
| 30877 | 30903 | /* UTF-16 Big-endian -> UTF-8 */ |
| 30878 | 30904 | while( zIn<zTerm ){ |
| 30879 | 30905 | c = (*(zIn++))<<8; |
| 30880 | 30906 | c += *(zIn++); |
| 30881 | 30907 | if( c>=0xd800 && c<0xe000 ){ |
| 30908 | +#ifdef SQLITE_REPLACE_INVALID_UTF |
| 30882 | 30909 | if( c>=0xdc00 || zIn>=zTerm ){ |
| 30883 | 30910 | c = 0xfffd; |
| 30884 | 30911 | }else{ |
| 30885 | 30912 | int c2 = (*(zIn++))<<8; |
| 30886 | 30913 | c2 += *(zIn++); |
| | @@ -30889,10 +30916,17 @@ |
| 30889 | 30916 | c = 0xfffd; |
| 30890 | 30917 | }else{ |
| 30891 | 30918 | c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000; |
| 30892 | 30919 | } |
| 30893 | 30920 | } |
| 30921 | +#else |
| 30922 | + if( zIn<zTerm ){ |
| 30923 | + int c2 = ((*zIn++)<<8); |
| 30924 | + c2 += (*zIn++); |
| 30925 | + c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); |
| 30926 | + } |
| 30927 | +#endif |
| 30894 | 30928 | } |
| 30895 | 30929 | WRITE_UTF8(z, c); |
| 30896 | 30930 | } |
| 30897 | 30931 | } |
| 30898 | 30932 | pMem->n = (int)(z - zOut); |
| | @@ -46821,11 +46855,13 @@ |
| 46821 | 46855 | pFile->pVfs = pVfs; |
| 46822 | 46856 | pFile->h = h; |
| 46823 | 46857 | if( isReadonly ){ |
| 46824 | 46858 | pFile->ctrlFlags |= WINFILE_RDONLY; |
| 46825 | 46859 | } |
| 46826 | | - if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 46860 | + if( (flags & SQLITE_OPEN_MAIN_DB) |
| 46861 | + && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) |
| 46862 | + ){ |
| 46827 | 46863 | pFile->ctrlFlags |= WINFILE_PSOW; |
| 46828 | 46864 | } |
| 46829 | 46865 | pFile->lastErrno = NO_ERROR; |
| 46830 | 46866 | pFile->zPath = zName; |
| 46831 | 46867 | #if SQLITE_MAX_MMAP_SIZE>0 |
| | @@ -48313,14 +48349,15 @@ |
| 48313 | 48349 | */ |
| 48314 | 48350 | SQLITE_PRIVATE int sqlite3MemdbInit(void){ |
| 48315 | 48351 | sqlite3_vfs *pLower = sqlite3_vfs_find(0); |
| 48316 | 48352 | int sz = pLower->szOsFile; |
| 48317 | 48353 | memdb_vfs.pAppData = pLower; |
| 48318 | | - /* In all known configurations of SQLite, the size of a default |
| 48319 | | - ** sqlite3_file is greater than the size of a memdb sqlite3_file. |
| 48320 | | - ** Should that ever change, remove the following NEVER() */ |
| 48321 | | - if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile); |
| 48354 | + /* The following conditional can only be true when compiled for |
| 48355 | + ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave |
| 48356 | + ** it in, to be safe, but it is marked as NO_TEST since there |
| 48357 | + ** is no way to reach it under most builds. */ |
| 48358 | + if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/ |
| 48322 | 48359 | memdb_vfs.szOsFile = sz; |
| 48323 | 48360 | return sqlite3_vfs_register(&memdb_vfs, 0); |
| 48324 | 48361 | } |
| 48325 | 48362 | #endif /* SQLITE_ENABLE_DESERIALIZE */ |
| 48326 | 48363 | |
| | @@ -59957,29 +59994,47 @@ |
| 59957 | 59994 | |
| 59958 | 59995 | aOut[0] = s1; |
| 59959 | 59996 | aOut[1] = s2; |
| 59960 | 59997 | } |
| 59961 | 59998 | |
| 59999 | +/* |
| 60000 | +** If there is the possibility of concurrent access to the SHM file |
| 60001 | +** from multiple threads and/or processes, then do a memory barrier. |
| 60002 | +*/ |
| 59962 | 60003 | static void walShmBarrier(Wal *pWal){ |
| 59963 | 60004 | if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){ |
| 59964 | 60005 | sqlite3OsShmBarrier(pWal->pDbFd); |
| 59965 | 60006 | } |
| 59966 | 60007 | } |
| 59967 | 60008 | |
| 60009 | +/* |
| 60010 | +** Add the SQLITE_NO_TSAN as part of the return-type of a function |
| 60011 | +** definition as a hint that the function contains constructs that |
| 60012 | +** might give false-positive TSAN warnings. |
| 60013 | +** |
| 60014 | +** See tag-20200519-1. |
| 60015 | +*/ |
| 60016 | +#if defined(__clang__) && !defined(SQLITE_NO_TSAN) |
| 60017 | +# define SQLITE_NO_TSAN __attribute__((no_sanitize_thread)) |
| 60018 | +#else |
| 60019 | +# define SQLITE_NO_TSAN |
| 60020 | +#endif |
| 60021 | + |
| 59968 | 60022 | /* |
| 59969 | 60023 | ** Write the header information in pWal->hdr into the wal-index. |
| 59970 | 60024 | ** |
| 59971 | 60025 | ** The checksum on pWal->hdr is updated before it is written. |
| 59972 | 60026 | */ |
| 59973 | | -static void walIndexWriteHdr(Wal *pWal){ |
| 60027 | +static SQLITE_NO_TSAN void walIndexWriteHdr(Wal *pWal){ |
| 59974 | 60028 | volatile WalIndexHdr *aHdr = walIndexHdr(pWal); |
| 59975 | 60029 | const int nCksum = offsetof(WalIndexHdr, aCksum); |
| 59976 | 60030 | |
| 59977 | 60031 | assert( pWal->writeLock ); |
| 59978 | 60032 | pWal->hdr.isInit = 1; |
| 59979 | 60033 | pWal->hdr.iVersion = WALINDEX_MAX_VERSION; |
| 59980 | 60034 | walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum); |
| 60035 | + /* Possible TSAN false-positive. See tag-20200519-1 */ |
| 59981 | 60036 | memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); |
| 59982 | 60037 | walShmBarrier(pWal); |
| 59983 | 60038 | memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); |
| 59984 | 60039 | } |
| 59985 | 60040 | |
| | @@ -61165,36 +61220,17 @@ |
| 61165 | 61220 | ** cannot be backfilled from the WAL. |
| 61166 | 61221 | */ |
| 61167 | 61222 | mxSafeFrame = pWal->hdr.mxFrame; |
| 61168 | 61223 | mxPage = pWal->hdr.nPage; |
| 61169 | 61224 | for(i=1; i<WAL_NREADER; i++){ |
| 61170 | | - /* Thread-sanitizer reports that the following is an unsafe read, |
| 61171 | | - ** as some other thread may be in the process of updating the value |
| 61172 | | - ** of the aReadMark[] slot. The assumption here is that if that is |
| 61173 | | - ** happening, the other client may only be increasing the value, |
| 61174 | | - ** not decreasing it. So assuming either that either the "old" or |
| 61175 | | - ** "new" version of the value is read, and not some arbitrary value |
| 61176 | | - ** that would never be written by a real client, things are still |
| 61177 | | - ** safe. |
| 61178 | | - ** |
| 61179 | | - ** Astute readers have pointed out that the assumption stated in the |
| 61180 | | - ** last sentence of the previous paragraph is not guaranteed to be |
| 61181 | | - ** true for all conforming systems. However, the assumption is true |
| 61182 | | - ** for all compilers and architectures in common use today (circa |
| 61183 | | - ** 2019-11-27) and the alternatives are both slow and complex, and |
| 61184 | | - ** so we will continue to go with the current design for now. If this |
| 61185 | | - ** bothers you, or if you really are running on a system where aligned |
| 61186 | | - ** 32-bit reads and writes are not atomic, then you can simply avoid |
| 61187 | | - ** the use of WAL mode, or only use WAL mode together with |
| 61188 | | - ** PRAGMA locking_mode=EXCLUSIVE and all will be well. |
| 61189 | | - */ |
| 61190 | | - u32 y = pInfo->aReadMark[i]; |
| 61225 | + u32 y = AtomicLoad(pInfo->aReadMark+i); |
| 61191 | 61226 | if( mxSafeFrame>y ){ |
| 61192 | 61227 | assert( y<=pWal->hdr.mxFrame ); |
| 61193 | 61228 | rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1); |
| 61194 | 61229 | if( rc==SQLITE_OK ){ |
| 61195 | | - pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED); |
| 61230 | + u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED); |
| 61231 | + AtomicStore(pInfo->aReadMark+i, iMark); |
| 61196 | 61232 | walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 61197 | 61233 | }else if( rc==SQLITE_BUSY ){ |
| 61198 | 61234 | mxSafeFrame = y; |
| 61199 | 61235 | xBusy = 0; |
| 61200 | 61236 | }else{ |
| | @@ -61208,11 +61244,11 @@ |
| 61208 | 61244 | rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter); |
| 61209 | 61245 | assert( rc==SQLITE_OK || pIter==0 ); |
| 61210 | 61246 | } |
| 61211 | 61247 | |
| 61212 | 61248 | if( pIter |
| 61213 | | - && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK |
| 61249 | + && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0),1))==SQLITE_OK |
| 61214 | 61250 | ){ |
| 61215 | 61251 | u32 nBackfill = pInfo->nBackfill; |
| 61216 | 61252 | |
| 61217 | 61253 | pInfo->nBackfillAttempted = mxSafeFrame; |
| 61218 | 61254 | |
| | @@ -61423,11 +61459,11 @@ |
| 61423 | 61459 | ** and *pChanged is set to 1. |
| 61424 | 61460 | ** |
| 61425 | 61461 | ** If the checksum cannot be verified return non-zero. If the header |
| 61426 | 61462 | ** is read successfully and the checksum verified, return zero. |
| 61427 | 61463 | */ |
| 61428 | | -static int walIndexTryHdr(Wal *pWal, int *pChanged){ |
| 61464 | +static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){ |
| 61429 | 61465 | u32 aCksum[2]; /* Checksum on the header content */ |
| 61430 | 61466 | WalIndexHdr h1, h2; /* Two copies of the header content */ |
| 61431 | 61467 | WalIndexHdr volatile *aHdr; /* Header in shared memory */ |
| 61432 | 61468 | |
| 61433 | 61469 | /* The first page of the wal-index must be mapped at this point. */ |
| | @@ -61436,17 +61472,23 @@ |
| 61436 | 61472 | /* Read the header. This might happen concurrently with a write to the |
| 61437 | 61473 | ** same area of shared memory on a different CPU in a SMP, |
| 61438 | 61474 | ** meaning it is possible that an inconsistent snapshot is read |
| 61439 | 61475 | ** from the file. If this happens, return non-zero. |
| 61440 | 61476 | ** |
| 61477 | + ** tag-20200519-1: |
| 61441 | 61478 | ** There are two copies of the header at the beginning of the wal-index. |
| 61442 | 61479 | ** When reading, read [0] first then [1]. Writes are in the reverse order. |
| 61443 | 61480 | ** Memory barriers are used to prevent the compiler or the hardware from |
| 61444 | | - ** reordering the reads and writes. |
| 61481 | + ** reordering the reads and writes. TSAN and similar tools can sometimes |
| 61482 | + ** give false-positive warnings about these accesses because the tools do not |
| 61483 | + ** account for the double-read and the memory barrier. The use of mutexes |
| 61484 | + ** here would be problematic as the memory being accessed is potentially |
| 61485 | + ** shared among multiple processes and not all mutex implementions work |
| 61486 | + ** reliably in that environment. |
| 61445 | 61487 | */ |
| 61446 | 61488 | aHdr = walIndexHdr(pWal); |
| 61447 | | - memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); |
| 61489 | + memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */ |
| 61448 | 61490 | walShmBarrier(pWal); |
| 61449 | 61491 | memcpy(&h2, (void *)&aHdr[1], sizeof(h2)); |
| 61450 | 61492 | |
| 61451 | 61493 | if( memcmp(&h1, &h2, sizeof(h1))!=0 ){ |
| 61452 | 61494 | return 1; /* Dirty read */ |
| | @@ -62109,16 +62151,18 @@ |
| 62109 | 62151 | ** needs to be flushed. |
| 62110 | 62152 | */ |
| 62111 | 62153 | SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ |
| 62112 | 62154 | int rc; /* Return code */ |
| 62113 | 62155 | int cnt = 0; /* Number of TryBeginRead attempts */ |
| 62156 | +#ifdef SQLITE_ENABLE_SNAPSHOT |
| 62157 | + int bChanged = 0; |
| 62158 | + WalIndexHdr *pSnapshot = pWal->pSnapshot; |
| 62159 | +#endif |
| 62114 | 62160 | |
| 62115 | 62161 | assert( pWal->ckptLock==0 ); |
| 62116 | 62162 | |
| 62117 | 62163 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 62118 | | - int bChanged = 0; |
| 62119 | | - WalIndexHdr *pSnapshot = pWal->pSnapshot; |
| 62120 | 62164 | if( pSnapshot ){ |
| 62121 | 62165 | if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 62122 | 62166 | bChanged = 1; |
| 62123 | 62167 | } |
| 62124 | 62168 | |
| | @@ -62283,26 +62327,28 @@ |
| 62283 | 62327 | for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){ |
| 62284 | 62328 | WalHashLoc sLoc; /* Hash table location */ |
| 62285 | 62329 | int iKey; /* Hash slot index */ |
| 62286 | 62330 | int nCollide; /* Number of hash collisions remaining */ |
| 62287 | 62331 | int rc; /* Error code */ |
| 62332 | + u32 iH; |
| 62288 | 62333 | |
| 62289 | 62334 | rc = walHashGet(pWal, iHash, &sLoc); |
| 62290 | 62335 | if( rc!=SQLITE_OK ){ |
| 62291 | 62336 | return rc; |
| 62292 | 62337 | } |
| 62293 | 62338 | nCollide = HASHTABLE_NSLOT; |
| 62294 | | - for(iKey=walHash(pgno); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){ |
| 62295 | | - u32 iH = sLoc.aHash[iKey]; |
| 62339 | + iKey = walHash(pgno); |
| 62340 | + while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){ |
| 62296 | 62341 | u32 iFrame = iH + sLoc.iZero; |
| 62297 | 62342 | if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){ |
| 62298 | 62343 | assert( iFrame>iRead || CORRUPT_DB ); |
| 62299 | 62344 | iRead = iFrame; |
| 62300 | 62345 | } |
| 62301 | 62346 | if( (nCollide--)==0 ){ |
| 62302 | 62347 | return SQLITE_CORRUPT_BKPT; |
| 62303 | 62348 | } |
| 62349 | + iKey = walNextHash(iKey); |
| 62304 | 62350 | } |
| 62305 | 62351 | if( iRead ) break; |
| 62306 | 62352 | } |
| 62307 | 62353 | |
| 62308 | 62354 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| | @@ -97436,10 +97482,47 @@ |
| 97436 | 97482 | } |
| 97437 | 97483 | p = p->pPrior; |
| 97438 | 97484 | }while( p!=0 ); |
| 97439 | 97485 | return WRC_Continue; |
| 97440 | 97486 | } |
| 97487 | + |
| 97488 | +/* Increase the walkerDepth when entering a subquery, and |
| 97489 | +** descrease when leaving the subquery. |
| 97490 | +*/ |
| 97491 | +SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){ |
| 97492 | + UNUSED_PARAMETER(pSelect); |
| 97493 | + pWalker->walkerDepth++; |
| 97494 | + return WRC_Continue; |
| 97495 | +} |
| 97496 | +SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){ |
| 97497 | + UNUSED_PARAMETER(pSelect); |
| 97498 | + pWalker->walkerDepth--; |
| 97499 | +} |
| 97500 | + |
| 97501 | + |
| 97502 | +/* |
| 97503 | +** No-op routine for the parse-tree walker. |
| 97504 | +** |
| 97505 | +** When this routine is the Walker.xExprCallback then expression trees |
| 97506 | +** are walked without any actions being taken at each node. Presumably, |
| 97507 | +** when this routine is used for Walker.xExprCallback then |
| 97508 | +** Walker.xSelectCallback is set to do something useful for every |
| 97509 | +** subquery in the parser tree. |
| 97510 | +*/ |
| 97511 | +SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ |
| 97512 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 97513 | + return WRC_Continue; |
| 97514 | +} |
| 97515 | + |
| 97516 | +/* |
| 97517 | +** No-op routine for the parse-tree walker for SELECT statements. |
| 97518 | +** subquery in the parser tree. |
| 97519 | +*/ |
| 97520 | +SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){ |
| 97521 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 97522 | + return WRC_Continue; |
| 97523 | +} |
| 97441 | 97524 | |
| 97442 | 97525 | /************** End of walker.c **********************************************/ |
| 97443 | 97526 | /************** Begin file resolve.c *****************************************/ |
| 97444 | 97527 | /* |
| 97445 | 97528 | ** 2008 August 18 |
| | @@ -97465,10 +97548,12 @@ |
| 97465 | 97548 | ** This needs to occur when copying a TK_AGG_FUNCTION node from an |
| 97466 | 97549 | ** outer query into an inner subquery. |
| 97467 | 97550 | ** |
| 97468 | 97551 | ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) |
| 97469 | 97552 | ** is a helper function - a callback for the tree walker. |
| 97553 | +** |
| 97554 | +** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c |
| 97470 | 97555 | */ |
| 97471 | 97556 | static int incrAggDepth(Walker *pWalker, Expr *pExpr){ |
| 97472 | 97557 | if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; |
| 97473 | 97558 | return WRC_Continue; |
| 97474 | 97559 | } |
| | @@ -103206,11 +103291,14 @@ |
| 103206 | 103291 | op = pExpr->op; |
| 103207 | 103292 | } |
| 103208 | 103293 | switch( op ){ |
| 103209 | 103294 | case TK_AGG_COLUMN: { |
| 103210 | 103295 | AggInfo *pAggInfo = pExpr->pAggInfo; |
| 103211 | | - struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 103296 | + struct AggInfo_col *pCol; |
| 103297 | + assert( pAggInfo!=0 ); |
| 103298 | + assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn ); |
| 103299 | + pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 103212 | 103300 | if( !pAggInfo->directMode ){ |
| 103213 | 103301 | assert( pCol->iMem>0 ); |
| 103214 | 103302 | return pCol->iMem; |
| 103215 | 103303 | }else if( pAggInfo->useSortingIdx ){ |
| 103216 | 103304 | Table *pTab = pCol->pTab; |
| | @@ -103506,11 +103594,14 @@ |
| 103506 | 103594 | sqlite3VdbeJumpHere(v, addr); |
| 103507 | 103595 | break; |
| 103508 | 103596 | } |
| 103509 | 103597 | case TK_AGG_FUNCTION: { |
| 103510 | 103598 | AggInfo *pInfo = pExpr->pAggInfo; |
| 103511 | | - if( pInfo==0 ){ |
| 103599 | + if( pInfo==0 |
| 103600 | + || NEVER(pExpr->iAgg<0) |
| 103601 | + || NEVER(pExpr->iAgg>=pInfo->nFunc) |
| 103602 | + ){ |
| 103512 | 103603 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 103513 | 103604 | sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); |
| 103514 | 103605 | }else{ |
| 103515 | 103606 | return pInfo->aFunc[pExpr->iAgg].iMem; |
| 103516 | 103607 | } |
| | @@ -105262,19 +105353,10 @@ |
| 105262 | 105353 | } |
| 105263 | 105354 | } |
| 105264 | 105355 | } |
| 105265 | 105356 | return WRC_Continue; |
| 105266 | 105357 | } |
| 105267 | | -static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ |
| 105268 | | - UNUSED_PARAMETER(pSelect); |
| 105269 | | - pWalker->walkerDepth++; |
| 105270 | | - return WRC_Continue; |
| 105271 | | -} |
| 105272 | | -static void analyzeAggregatesInSelectEnd(Walker *pWalker, Select *pSelect){ |
| 105273 | | - UNUSED_PARAMETER(pSelect); |
| 105274 | | - pWalker->walkerDepth--; |
| 105275 | | -} |
| 105276 | 105358 | |
| 105277 | 105359 | /* |
| 105278 | 105360 | ** Analyze the pExpr expression looking for aggregate functions and |
| 105279 | 105361 | ** for variables that need to be added to AggInfo object that pNC->pAggInfo |
| 105280 | 105362 | ** points to. Additional entries are made on the AggInfo object as |
| | @@ -105284,12 +105366,12 @@ |
| 105284 | 105366 | ** analyzed by sqlite3ResolveExprNames(). |
| 105285 | 105367 | */ |
| 105286 | 105368 | SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ |
| 105287 | 105369 | Walker w; |
| 105288 | 105370 | w.xExprCallback = analyzeAggregate; |
| 105289 | | - w.xSelectCallback = analyzeAggregatesInSelect; |
| 105290 | | - w.xSelectCallback2 = analyzeAggregatesInSelectEnd; |
| 105371 | + w.xSelectCallback = sqlite3WalkerDepthIncrease; |
| 105372 | + w.xSelectCallback2 = sqlite3WalkerDepthDecrease; |
| 105291 | 105373 | w.walkerDepth = 0; |
| 105292 | 105374 | w.u.pNC = pNC; |
| 105293 | 105375 | w.pParse = 0; |
| 105294 | 105376 | assert( pNC->pSrcList!=0 ); |
| 105295 | 105377 | sqlite3WalkExpr(&w, pExpr); |
| | @@ -122007,11 +122089,11 @@ |
| 122007 | 122089 | } |
| 122008 | 122090 | if( bAffinityDone==0 && (pUpIdx==0 || pUpIdx==pIdx) ){ |
| 122009 | 122091 | sqlite3TableAffinity(v, pTab, regNewData+1); |
| 122010 | 122092 | bAffinityDone = 1; |
| 122011 | 122093 | } |
| 122012 | | - VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName)); |
| 122094 | + VdbeNoopComment((v, "prep index %s", pIdx->zName)); |
| 122013 | 122095 | iThisCur = iIdxCur+ix; |
| 122014 | 122096 | |
| 122015 | 122097 | |
| 122016 | 122098 | /* Skip partial indices for which the WHERE clause is not true */ |
| 122017 | 122099 | if( pIdx->pPartIdxWhere ){ |
| | @@ -133979,33 +134061,10 @@ |
| 133979 | 134061 | } |
| 133980 | 134062 | } |
| 133981 | 134063 | return WRC_Continue; |
| 133982 | 134064 | } |
| 133983 | 134065 | |
| 133984 | | -/* |
| 133985 | | -** No-op routine for the parse-tree walker. |
| 133986 | | -** |
| 133987 | | -** When this routine is the Walker.xExprCallback then expression trees |
| 133988 | | -** are walked without any actions being taken at each node. Presumably, |
| 133989 | | -** when this routine is used for Walker.xExprCallback then |
| 133990 | | -** Walker.xSelectCallback is set to do something useful for every |
| 133991 | | -** subquery in the parser tree. |
| 133992 | | -*/ |
| 133993 | | -SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ |
| 133994 | | - UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 133995 | | - return WRC_Continue; |
| 133996 | | -} |
| 133997 | | - |
| 133998 | | -/* |
| 133999 | | -** No-op routine for the parse-tree walker for SELECT statements. |
| 134000 | | -** subquery in the parser tree. |
| 134001 | | -*/ |
| 134002 | | -SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){ |
| 134003 | | - UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 134004 | | - return WRC_Continue; |
| 134005 | | -} |
| 134006 | | - |
| 134007 | 134066 | #if SQLITE_DEBUG |
| 134008 | 134067 | /* |
| 134009 | 134068 | ** Always assert. This xSelectCallback2 implementation proves that the |
| 134010 | 134069 | ** xSelectCallback2 is never invoked. |
| 134011 | 134070 | */ |
| | @@ -135172,11 +135231,11 @@ |
| 135172 | 135231 | sAggInfo.mxReg = pParse->nMem; |
| 135173 | 135232 | if( db->mallocFailed ) goto select_end; |
| 135174 | 135233 | #if SELECTTRACE_ENABLED |
| 135175 | 135234 | if( sqlite3SelectTrace & 0x400 ){ |
| 135176 | 135235 | int ii; |
| 135177 | | - SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n")); |
| 135236 | + SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", &sAggInfo)); |
| 135178 | 135237 | sqlite3TreeViewSelect(0, p, 0); |
| 135179 | 135238 | for(ii=0; ii<sAggInfo.nColumn; ii++){ |
| 135180 | 135239 | sqlite3DebugPrintf("agg-column[%d] iMem=%d\n", |
| 135181 | 135240 | ii, sAggInfo.aCol[ii].iMem); |
| 135182 | 135241 | sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0); |
| | @@ -151215,10 +151274,27 @@ |
| 151215 | 151274 | if( pList ) pList->a[nInit+i].sortFlags = pAppend->a[i].sortFlags; |
| 151216 | 151275 | } |
| 151217 | 151276 | } |
| 151218 | 151277 | return pList; |
| 151219 | 151278 | } |
| 151279 | + |
| 151280 | +/* |
| 151281 | +** When rewriting a query, if the new subquery in the FROM clause |
| 151282 | +** contains TK_AGG_FUNCTION nodes that refer to an outer query, |
| 151283 | +** then we have to increase the Expr->op2 values of those nodes |
| 151284 | +** due to the extra subquery layer that was added. |
| 151285 | +** |
| 151286 | +** See also the incrAggDepth() routine in resolve.c |
| 151287 | +*/ |
| 151288 | +static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){ |
| 151289 | + if( pExpr->op==TK_AGG_FUNCTION |
| 151290 | + && pExpr->op2>=pWalker->walkerDepth |
| 151291 | + ){ |
| 151292 | + pExpr->op2++; |
| 151293 | + } |
| 151294 | + return WRC_Continue; |
| 151295 | +} |
| 151220 | 151296 | |
| 151221 | 151297 | /* |
| 151222 | 151298 | ** If the SELECT statement passed as the second argument does not invoke |
| 151223 | 151299 | ** any SQL window functions, this function is a no-op. Otherwise, it |
| 151224 | 151300 | ** rewrites the SELECT statement so that window function xStep functions |
| | @@ -151325,10 +151401,11 @@ |
| 151325 | 151401 | pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0 |
| 151326 | 151402 | ); |
| 151327 | 151403 | p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0); |
| 151328 | 151404 | if( p->pSrc ){ |
| 151329 | 151405 | Table *pTab2; |
| 151406 | + Walker w; |
| 151330 | 151407 | p->pSrc->a[0].pSelect = pSub; |
| 151331 | 151408 | sqlite3SrcListAssignCursors(pParse, p->pSrc); |
| 151332 | 151409 | pSub->selFlags |= SF_Expanded; |
| 151333 | 151410 | pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE); |
| 151334 | 151411 | pSub->selFlags |= (selFlags & SF_Aggregate); |
| | @@ -151340,10 +151417,15 @@ |
| 151340 | 151417 | }else{ |
| 151341 | 151418 | memcpy(pTab, pTab2, sizeof(Table)); |
| 151342 | 151419 | pTab->tabFlags |= TF_Ephemeral; |
| 151343 | 151420 | p->pSrc->a[0].pTab = pTab; |
| 151344 | 151421 | pTab = pTab2; |
| 151422 | + memset(&w, 0, sizeof(w)); |
| 151423 | + w.xExprCallback = sqlite3WindowExtraAggFuncDepth; |
| 151424 | + w.xSelectCallback = sqlite3WalkerDepthIncrease; |
| 151425 | + w.xSelectCallback2 = sqlite3WalkerDepthDecrease; |
| 151426 | + sqlite3WalkSelect(&w, pSub); |
| 151345 | 151427 | } |
| 151346 | 151428 | }else{ |
| 151347 | 151429 | sqlite3SelectDelete(db, pSub); |
| 151348 | 151430 | } |
| 151349 | 151431 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
| | @@ -224738,11 +224820,11 @@ |
| 224738 | 224820 | int nArg, /* Number of args */ |
| 224739 | 224821 | sqlite3_value **apUnused /* Function arguments */ |
| 224740 | 224822 | ){ |
| 224741 | 224823 | assert( nArg==0 ); |
| 224742 | 224824 | UNUSED_PARAM2(nArg, apUnused); |
| 224743 | | - sqlite3_result_text(pCtx, "fts5: 2020-05-17 00:26:44 1313557b512297e7b75ed748894379b2022aecf696d5a58318e46a668321c1ff", -1, SQLITE_TRANSIENT); |
| 224825 | + sqlite3_result_text(pCtx, "fts5: 2020-05-25 16:19:56 0c1fcf4711a2e66c813aed38cf41cd3e2123ee8eb6db98118086764c4ba83350", -1, SQLITE_TRANSIENT); |
| 224744 | 224826 | } |
| 224745 | 224827 | |
| 224746 | 224828 | /* |
| 224747 | 224829 | ** Return true if zName is the extension on one of the shadow tables used |
| 224748 | 224830 | ** by this module. |
| | @@ -229521,12 +229603,12 @@ |
| 229521 | 229603 | } |
| 229522 | 229604 | #endif /* SQLITE_CORE */ |
| 229523 | 229605 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 229524 | 229606 | |
| 229525 | 229607 | /************** End of stmt.c ************************************************/ |
| 229526 | | -#if __LINE__!=229526 |
| 229608 | +#if __LINE__!=229608 |
| 229527 | 229609 | #undef SQLITE_SOURCE_ID |
| 229528 | | -#define SQLITE_SOURCE_ID "2020-05-17 13:47:28 69e149f76853d196c8855fedfc98848b60fb116ac36bc08824b1a122469falt2" |
| 229610 | +#define SQLITE_SOURCE_ID "2020-05-25 16:19:56 0c1fcf4711a2e66c813aed38cf41cd3e2123ee8eb6db98118086764c4ba8alt2" |
| 229529 | 229611 | #endif |
| 229530 | 229612 | /* Return the source-id for this library */ |
| 229531 | 229613 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 229532 | 229614 | /************************** End of sqlite3.c ******************************/ |
| 229533 | 229615 | |