| | @@ -231,11 +231,11 @@ |
| 231 | 231 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 232 | 232 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 233 | 233 | */ |
| 234 | 234 | #define SQLITE_VERSION "3.8.7" |
| 235 | 235 | #define SQLITE_VERSION_NUMBER 3008007 |
| 236 | | -#define SQLITE_SOURCE_ID "2014-09-20 00:35:05 59e2c9df02d7e988c5ad44c560ead1e5288b12e7" |
| 236 | +#define SQLITE_SOURCE_ID "2014-10-17 11:24:17 e4ab094f8afce0817f4074e823fabe59fc29ebb4" |
| 237 | 237 | |
| 238 | 238 | /* |
| 239 | 239 | ** CAPI3REF: Run-Time Library Version Numbers |
| 240 | 240 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 241 | 241 | ** |
| | @@ -2791,13 +2791,13 @@ |
| 2791 | 2791 | ** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The |
| 2792 | 2792 | ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain |
| 2793 | 2793 | ** an English language description of the error following a failure of any |
| 2794 | 2794 | ** of the sqlite3_open() routines. |
| 2795 | 2795 | ** |
| 2796 | | -** ^The default encoding for the database will be UTF-8 if |
| 2797 | | -** sqlite3_open() or sqlite3_open_v2() is called and |
| 2798 | | -** UTF-16 in the native byte order if sqlite3_open16() is used. |
| 2796 | +** ^The default encoding will be UTF-8 for databases created using |
| 2797 | +** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases |
| 2798 | +** created using sqlite3_open16() will be UTF-16 in the native byte order. |
| 2799 | 2799 | ** |
| 2800 | 2800 | ** Whether or not an error occurs when it is opened, resources |
| 2801 | 2801 | ** associated with the [database connection] handle should be released by |
| 2802 | 2802 | ** passing it to [sqlite3_close()] when it is no longer required. |
| 2803 | 2803 | ** |
| | @@ -2881,17 +2881,18 @@ |
| 2881 | 2881 | ** ^SQLite uses the path component of the URI as the name of the disk file |
| 2882 | 2882 | ** which contains the database. ^If the path begins with a '/' character, |
| 2883 | 2883 | ** then it is interpreted as an absolute path. ^If the path does not begin |
| 2884 | 2884 | ** with a '/' (meaning that the authority section is omitted from the URI) |
| 2885 | 2885 | ** then the path is interpreted as a relative path. |
| 2886 | | -** ^On windows, the first component of an absolute path |
| 2887 | | -** is a drive specification (e.g. "C:"). |
| 2886 | +** ^(On windows, the first component of an absolute path |
| 2887 | +** is a drive specification (e.g. "C:").)^ |
| 2888 | 2888 | ** |
| 2889 | 2889 | ** [[core URI query parameters]] |
| 2890 | 2890 | ** The query component of a URI may contain parameters that are interpreted |
| 2891 | 2891 | ** either by SQLite itself, or by a [VFS | custom VFS implementation]. |
| 2892 | | -** SQLite interprets the following three query parameters: |
| 2892 | +** SQLite and its built-in [VFSes] interpret the |
| 2893 | +** following query parameters: |
| 2893 | 2894 | ** |
| 2894 | 2895 | ** <ul> |
| 2895 | 2896 | ** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of |
| 2896 | 2897 | ** a VFS object that provides the operating system interface that should |
| 2897 | 2898 | ** be used to access the database file on disk. ^If this option is set to |
| | @@ -2922,15 +2923,13 @@ |
| 2922 | 2923 | ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit. |
| 2923 | 2924 | ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in |
| 2924 | 2925 | ** a URI filename, its value overrides any behavior requested by setting |
| 2925 | 2926 | ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag. |
| 2926 | 2927 | ** |
| 2927 | | -** <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or |
| 2928 | | -** "1") or "false" (or "off" or "no" or "0") to indicate that the |
| 2928 | +** <li> <b>psow</b>: ^The psow parameter indicates whether or not the |
| 2929 | 2929 | ** [powersafe overwrite] property does or does not apply to the |
| 2930 | | -** storage media on which the database file resides. ^The psow query |
| 2931 | | -** parameter only works for the built-in unix and Windows VFSes. |
| 2930 | +** storage media on which the database file resides. |
| 2932 | 2931 | ** |
| 2933 | 2932 | ** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter |
| 2934 | 2933 | ** which if set disables file locking in rollback journal modes. This |
| 2935 | 2934 | ** is useful for accessing a database on a filesystem that does not |
| 2936 | 2935 | ** support locking. Caution: Database corruption might result if two |
| | @@ -3521,15 +3520,14 @@ |
| 3521 | 3520 | ** terminated. If any NUL characters occur at byte offsets less than |
| 3522 | 3521 | ** the value of the fourth parameter then the resulting string value will |
| 3523 | 3522 | ** contain embedded NULs. The result of expressions involving strings |
| 3524 | 3523 | ** with embedded NULs is undefined. |
| 3525 | 3524 | ** |
| 3526 | | -** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and |
| 3527 | | -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or |
| 3525 | +** ^The fifth argument to the BLOB and string binding interfaces |
| 3526 | +** is a destructor used to dispose of the BLOB or |
| 3528 | 3527 | ** string after SQLite has finished with it. ^The destructor is called |
| 3529 | | -** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(), |
| 3530 | | -** sqlite3_bind_text(), or sqlite3_bind_text16() fails. |
| 3528 | +** to dispose of the BLOB or string even if the call to bind API fails. |
| 3531 | 3529 | ** ^If the fifth argument is |
| 3532 | 3530 | ** the special value [SQLITE_STATIC], then SQLite assumes that the |
| 3533 | 3531 | ** information is in static, unmanaged space and does not need to be freed. |
| 3534 | 3532 | ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then |
| 3535 | 3533 | ** SQLite makes its own private copy of the data immediately, before |
| | @@ -3536,11 +3534,11 @@ |
| 3536 | 3534 | ** the sqlite3_bind_*() routine returns. |
| 3537 | 3535 | ** |
| 3538 | 3536 | ** ^The sixth argument to sqlite3_bind_text64() must be one of |
| 3539 | 3537 | ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE] |
| 3540 | 3538 | ** to specify the encoding of the text in the third parameter. If |
| 3541 | | -** the sixth argument to sqlite3_bind_text64() is not how of the |
| 3539 | +** the sixth argument to sqlite3_bind_text64() is not one of the |
| 3542 | 3540 | ** allowed values shown above, or if the text encoding is different |
| 3543 | 3541 | ** from the encoding specified by the sixth parameter, then the behavior |
| 3544 | 3542 | ** is undefined. |
| 3545 | 3543 | ** |
| 3546 | 3544 | ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that |
| | @@ -4572,11 +4570,11 @@ |
| 4572 | 4570 | ** |
| 4573 | 4571 | ** ^The sqlite3_result_null() interface sets the return value |
| 4574 | 4572 | ** of the application-defined function to be NULL. |
| 4575 | 4573 | ** |
| 4576 | 4574 | ** ^The sqlite3_result_text(), sqlite3_result_text16(), |
| 4577 | | -** sqlite3_result_text16le(), and sqlite3_result_text16be() |
| 4575 | +** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces |
| 4578 | 4576 | ** set the return value of the application-defined function to be |
| 4579 | 4577 | ** a text string which is represented as UTF-8, UTF-16 native byte order, |
| 4580 | 4578 | ** UTF-16 little endian, or UTF-16 big endian, respectively. |
| 4581 | 4579 | ** ^The sqlite3_result_text64() interface sets the return value of an |
| 4582 | 4580 | ** application-defined function to be a text string in an encoding |
| | @@ -6332,11 +6330,11 @@ |
| 6332 | 6330 | #define SQLITE_TESTCTRL_RESERVE 14 |
| 6333 | 6331 | #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 |
| 6334 | 6332 | #define SQLITE_TESTCTRL_ISKEYWORD 16 |
| 6335 | 6333 | #define SQLITE_TESTCTRL_SCRATCHMALLOC 17 |
| 6336 | 6334 | #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18 |
| 6337 | | -#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 |
| 6335 | +#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */ |
| 6338 | 6336 | #define SQLITE_TESTCTRL_NEVER_CORRUPT 20 |
| 6339 | 6337 | #define SQLITE_TESTCTRL_VDBE_COVERAGE 21 |
| 6340 | 6338 | #define SQLITE_TESTCTRL_BYTEORDER 22 |
| 6341 | 6339 | #define SQLITE_TESTCTRL_ISINIT 23 |
| 6342 | 6340 | #define SQLITE_TESTCTRL_SORTER_MMAP 24 |
| | @@ -7946,11 +7944,11 @@ |
| 7946 | 7944 | ** A macro to hint to the compiler that a function should not be |
| 7947 | 7945 | ** inlined. |
| 7948 | 7946 | */ |
| 7949 | 7947 | #if defined(__GNUC__) |
| 7950 | 7948 | # define SQLITE_NOINLINE __attribute__((noinline)) |
| 7951 | | -#elif defined(_MSC_VER) |
| 7949 | +#elif defined(_MSC_VER) && _MSC_VER>=1310 |
| 7952 | 7950 | # define SQLITE_NOINLINE __declspec(noinline) |
| 7953 | 7951 | #else |
| 7954 | 7952 | # define SQLITE_NOINLINE |
| 7955 | 7953 | #endif |
| 7956 | 7954 | |
| | @@ -8519,10 +8517,15 @@ |
| 8519 | 8517 | ** Macros to compute minimum and maximum of two numbers. |
| 8520 | 8518 | */ |
| 8521 | 8519 | #define MIN(A,B) ((A)<(B)?(A):(B)) |
| 8522 | 8520 | #define MAX(A,B) ((A)>(B)?(A):(B)) |
| 8523 | 8521 | |
| 8522 | +/* |
| 8523 | +** Swap two objects of type TYPE. |
| 8524 | +*/ |
| 8525 | +#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 8526 | + |
| 8524 | 8527 | /* |
| 8525 | 8528 | ** Check to see if this machine uses EBCDIC. (Yes, believe it or |
| 8526 | 8529 | ** not, there are still machines out there that use EBCDIC.) |
| 8527 | 8530 | */ |
| 8528 | 8531 | #if 'A' == '\301' |
| | @@ -8756,10 +8759,20 @@ |
| 8756 | 8759 | # define SQLITE_ENABLE_STAT3_OR_STAT4 1 |
| 8757 | 8760 | #elif SQLITE_ENABLE_STAT3_OR_STAT4 |
| 8758 | 8761 | # undef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 8759 | 8762 | #endif |
| 8760 | 8763 | |
| 8764 | +/* |
| 8765 | +** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not |
| 8766 | +** the Select query generator tracing logic is turned on. |
| 8767 | +*/ |
| 8768 | +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE) |
| 8769 | +# define SELECTTRACE_ENABLED 1 |
| 8770 | +#else |
| 8771 | +# define SELECTTRACE_ENABLED 0 |
| 8772 | +#endif |
| 8773 | + |
| 8761 | 8774 | /* |
| 8762 | 8775 | ** An instance of the following structure is used to store the busy-handler |
| 8763 | 8776 | ** callback for a given sqlite handle. |
| 8764 | 8777 | ** |
| 8765 | 8778 | ** The sqlite.busyHandler member of the sqlite struct contains the busy |
| | @@ -8895,10 +8908,11 @@ |
| 8895 | 8908 | typedef struct SrcList SrcList; |
| 8896 | 8909 | typedef struct StrAccum StrAccum; |
| 8897 | 8910 | typedef struct Table Table; |
| 8898 | 8911 | typedef struct TableLock TableLock; |
| 8899 | 8912 | typedef struct Token Token; |
| 8913 | +typedef struct TreeView TreeView; |
| 8900 | 8914 | typedef struct Trigger Trigger; |
| 8901 | 8915 | typedef struct TriggerPrg TriggerPrg; |
| 8902 | 8916 | typedef struct TriggerStep TriggerStep; |
| 8903 | 8917 | typedef struct UnpackedRecord UnpackedRecord; |
| 8904 | 8918 | typedef struct VTable VTable; |
| | @@ -11308,10 +11322,11 @@ |
| 11308 | 11322 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 11309 | 11323 | int nSample; /* Number of elements in aSample[] */ |
| 11310 | 11324 | int nSampleCol; /* Size of IndexSample.anEq[] and so on */ |
| 11311 | 11325 | tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */ |
| 11312 | 11326 | IndexSample *aSample; /* Samples of the left-most key */ |
| 11327 | + tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this table */ |
| 11313 | 11328 | #endif |
| 11314 | 11329 | }; |
| 11315 | 11330 | |
| 11316 | 11331 | /* |
| 11317 | 11332 | ** Allowed values for Index.idxType |
| | @@ -11738,11 +11753,11 @@ |
| 11738 | 11753 | #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */ |
| 11739 | 11754 | #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ |
| 11740 | 11755 | #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */ |
| 11741 | 11756 | #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */ |
| 11742 | 11757 | #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */ |
| 11743 | | -#define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */ |
| 11758 | + /* 0x0080 // not currently used */ |
| 11744 | 11759 | #define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */ |
| 11745 | 11760 | #define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */ |
| 11746 | 11761 | #define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */ |
| 11747 | 11762 | #define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */ |
| 11748 | 11763 | #define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */ |
| | @@ -11823,10 +11838,13 @@ |
| 11823 | 11838 | struct Select { |
| 11824 | 11839 | ExprList *pEList; /* The fields of the result */ |
| 11825 | 11840 | u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ |
| 11826 | 11841 | u16 selFlags; /* Various SF_* values */ |
| 11827 | 11842 | int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ |
| 11843 | +#if SELECTTRACE_ENABLED |
| 11844 | + char zSelName[12]; /* Symbolic name of this SELECT use for debugging */ |
| 11845 | +#endif |
| 11828 | 11846 | int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */ |
| 11829 | 11847 | u64 nSelectRow; /* Estimated number of result rows */ |
| 11830 | 11848 | SrcList *pSrc; /* The FROM clause */ |
| 11831 | 11849 | Expr *pWhere; /* The WHERE clause */ |
| 11832 | 11850 | ExprList *pGroupBy; /* The GROUP BY clause */ |
| | @@ -12081,10 +12099,14 @@ |
| 12081 | 12099 | yDbMask cookieMask; /* Bitmask of schema verified databases */ |
| 12082 | 12100 | int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */ |
| 12083 | 12101 | int regRowid; /* Register holding rowid of CREATE TABLE entry */ |
| 12084 | 12102 | int regRoot; /* Register holding root page number for new objects */ |
| 12085 | 12103 | int nMaxArg; /* Max args passed to user function by sub-program */ |
| 12104 | +#if SELECTTRACE_ENABLED |
| 12105 | + int nSelect; /* Number of SELECT statements seen */ |
| 12106 | + int nSelectIndent; /* How far to indent SELECTTRACE() output */ |
| 12107 | +#endif |
| 12086 | 12108 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 12087 | 12109 | int nTableLock; /* Number of locks in aTableLock */ |
| 12088 | 12110 | TableLock *aTableLock; /* Required table locks for shared-cache mode */ |
| 12089 | 12111 | #endif |
| 12090 | 12112 | AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */ |
| | @@ -12160,15 +12182,15 @@ |
| 12160 | 12182 | |
| 12161 | 12183 | /* |
| 12162 | 12184 | ** Bitfield flags for P5 value in various opcodes. |
| 12163 | 12185 | */ |
| 12164 | 12186 | #define OPFLAG_NCHANGE 0x01 /* Set to update db->nChange */ |
| 12187 | +#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */ |
| 12165 | 12188 | #define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */ |
| 12166 | 12189 | #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */ |
| 12167 | 12190 | #define OPFLAG_APPEND 0x08 /* This is likely to be an append */ |
| 12168 | 12191 | #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */ |
| 12169 | | -#define OPFLAG_CLEARCACHE 0x20 /* Clear pseudo-table cache in OP_Column */ |
| 12170 | 12192 | #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */ |
| 12171 | 12193 | #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */ |
| 12172 | 12194 | #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */ |
| 12173 | 12195 | #define OPFLAG_P2ISREG 0x02 /* P2 to OP_Open** is a register number */ |
| 12174 | 12196 | #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */ |
| | @@ -12428,10 +12450,21 @@ |
| 12428 | 12450 | Select *pSelect; /* The definition of this CTE */ |
| 12429 | 12451 | const char *zErr; /* Error message for circular references */ |
| 12430 | 12452 | } a[1]; |
| 12431 | 12453 | }; |
| 12432 | 12454 | |
| 12455 | +#ifdef SQLITE_DEBUG |
| 12456 | +/* |
| 12457 | +** An instance of the TreeView object is used for printing the content of |
| 12458 | +** data structures on sqlite3DebugPrintf() using a tree-like view. |
| 12459 | +*/ |
| 12460 | +struct TreeView { |
| 12461 | + int iLevel; /* Which level of the tree we are on */ |
| 12462 | + u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */ |
| 12463 | +}; |
| 12464 | +#endif /* SQLITE_DEBUG */ |
| 12465 | + |
| 12433 | 12466 | /* |
| 12434 | 12467 | ** Assuming zIn points to the first byte of a UTF-8 character, |
| 12435 | 12468 | ** advance zIn to point to the first byte of the next UTF-8 character. |
| 12436 | 12469 | */ |
| 12437 | 12470 | #define SQLITE_SKIP_UTF8(zIn) { \ |
| | @@ -12493,10 +12526,11 @@ |
| 12493 | 12526 | # define sqlite3Isalpha(x) isalpha((unsigned char)(x)) |
| 12494 | 12527 | # define sqlite3Isdigit(x) isdigit((unsigned char)(x)) |
| 12495 | 12528 | # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x)) |
| 12496 | 12529 | # define sqlite3Tolower(x) tolower((unsigned char)(x)) |
| 12497 | 12530 | #endif |
| 12531 | +SQLITE_PRIVATE int sqlite3IsIdChar(u8); |
| 12498 | 12532 | |
| 12499 | 12533 | /* |
| 12500 | 12534 | ** Internal function prototypes |
| 12501 | 12535 | */ |
| 12502 | 12536 | #define sqlite3StrICmp sqlite3_stricmp |
| | @@ -12591,29 +12625,18 @@ |
| 12591 | 12625 | #endif |
| 12592 | 12626 | #if defined(SQLITE_TEST) |
| 12593 | 12627 | SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*); |
| 12594 | 12628 | #endif |
| 12595 | 12629 | |
| 12596 | | -/* Output formatting for SQLITE_TESTCTRL_EXPLAIN */ |
| 12597 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 12598 | | -SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe*); |
| 12599 | | -SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe*, const char*, ...); |
| 12600 | | -SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe*); |
| 12601 | | -SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe*); |
| 12602 | | -SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe*); |
| 12603 | | -SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe*); |
| 12604 | | -SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe*, Select*); |
| 12605 | | -SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe*, Expr*); |
| 12606 | | -SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe*, ExprList*); |
| 12607 | | -SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe*); |
| 12608 | | -#else |
| 12609 | | -# define sqlite3ExplainBegin(X) |
| 12610 | | -# define sqlite3ExplainSelect(A,B) |
| 12611 | | -# define sqlite3ExplainExpr(A,B) |
| 12612 | | -# define sqlite3ExplainExprList(A,B) |
| 12613 | | -# define sqlite3ExplainFinish(X) |
| 12614 | | -# define sqlite3VdbeExplanation(X) 0 |
| 12630 | +#if defined(SQLITE_DEBUG) |
| 12631 | +SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView*,u8); |
| 12632 | +SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView*); |
| 12633 | +SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView*, const char*, ...); |
| 12634 | +SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView*, const char*, u8); |
| 12635 | +SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8); |
| 12636 | +SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*); |
| 12637 | +SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8); |
| 12615 | 12638 | #endif |
| 12616 | 12639 | |
| 12617 | 12640 | |
| 12618 | 12641 | SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...); |
| 12619 | 12642 | SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...); |
| | @@ -12791,11 +12814,11 @@ |
| 12791 | 12814 | SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*); |
| 12792 | 12815 | SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *); |
| 12793 | 12816 | SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*); |
| 12794 | 12817 | SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*); |
| 12795 | 12818 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*); |
| 12796 | | -SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*); |
| 12819 | +SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8); |
| 12797 | 12820 | SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); |
| 12798 | 12821 | SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); |
| 12799 | 12822 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); |
| 12800 | 12823 | SQLITE_PRIVATE int sqlite3IsRowid(const char*); |
| 12801 | 12824 | SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8); |
| | @@ -12815,10 +12838,15 @@ |
| 12815 | 12838 | SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int); |
| 12816 | 12839 | SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int); |
| 12817 | 12840 | SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int); |
| 12818 | 12841 | SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*); |
| 12819 | 12842 | SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int); |
| 12843 | +#if SELECTTRACE_ENABLED |
| 12844 | +SQLITE_PRIVATE void sqlite3SelectSetName(Select*,const char*); |
| 12845 | +#else |
| 12846 | +# define sqlite3SelectSetName(A,B) |
| 12847 | +#endif |
| 12820 | 12848 | SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*); |
| 12821 | 12849 | SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8); |
| 12822 | 12850 | SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*); |
| 12823 | 12851 | SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void); |
| 12824 | 12852 | SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void); |
| | @@ -13292,14 +13320,13 @@ |
| 13292 | 13320 | # define sqlite3MemdebugSetType(X,Y) /* no-op */ |
| 13293 | 13321 | # define sqlite3MemdebugHasType(X,Y) 1 |
| 13294 | 13322 | # define sqlite3MemdebugNoType(X,Y) 1 |
| 13295 | 13323 | #endif |
| 13296 | 13324 | #define MEMTYPE_HEAP 0x01 /* General heap allocations */ |
| 13297 | | -#define MEMTYPE_LOOKASIDE 0x02 /* Might have been lookaside memory */ |
| 13325 | +#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */ |
| 13298 | 13326 | #define MEMTYPE_SCRATCH 0x04 /* Scratch allocations */ |
| 13299 | 13327 | #define MEMTYPE_PCACHE 0x08 /* Page cache allocations */ |
| 13300 | | -#define MEMTYPE_DB 0x10 /* Uses sqlite3DbMalloc, not sqlite_malloc */ |
| 13301 | 13328 | |
| 13302 | 13329 | /* |
| 13303 | 13330 | ** Threading interface |
| 13304 | 13331 | */ |
| 13305 | 13332 | #if SQLITE_MAX_WORKER_THREADS>0 |
| | @@ -13439,10 +13466,17 @@ |
| 13439 | 13466 | 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */ |
| 13440 | 13467 | 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */ |
| 13441 | 13468 | }; |
| 13442 | 13469 | #endif |
| 13443 | 13470 | |
| 13471 | +/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards |
| 13472 | +** compatibility for legacy applications, the URI filename capability is |
| 13473 | +** disabled by default. |
| 13474 | +** |
| 13475 | +** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled |
| 13476 | +** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options. |
| 13477 | +*/ |
| 13444 | 13478 | #ifndef SQLITE_USE_URI |
| 13445 | 13479 | # define SQLITE_USE_URI 0 |
| 13446 | 13480 | #endif |
| 13447 | 13481 | |
| 13448 | 13482 | #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN |
| | @@ -13945,11 +13979,11 @@ |
| 13945 | 13979 | |
| 13946 | 13980 | /* Since ArraySize(azCompileOpt) is normally in single digits, a |
| 13947 | 13981 | ** linear search is adequate. No need for a binary search. */ |
| 13948 | 13982 | for(i=0; i<ArraySize(azCompileOpt); i++){ |
| 13949 | 13983 | if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0 |
| 13950 | | - && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0 |
| 13984 | + && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0 |
| 13951 | 13985 | ){ |
| 13952 | 13986 | return 1; |
| 13953 | 13987 | } |
| 13954 | 13988 | } |
| 13955 | 13989 | return 0; |
| | @@ -14060,21 +14094,19 @@ |
| 14060 | 14094 | #ifdef SQLITE_DEBUG |
| 14061 | 14095 | u8 seekOp; /* Most recent seek operation on this cursor */ |
| 14062 | 14096 | #endif |
| 14063 | 14097 | i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */ |
| 14064 | 14098 | u8 nullRow; /* True if pointing to a row with no data */ |
| 14065 | | - u8 rowidIsValid; /* True if lastRowid is valid */ |
| 14066 | 14099 | u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ |
| 14067 | 14100 | Bool isEphemeral:1; /* True for an ephemeral table */ |
| 14068 | 14101 | Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */ |
| 14069 | 14102 | Bool isTable:1; /* True if a table requiring integer keys */ |
| 14070 | 14103 | Bool isOrdered:1; /* True if the underlying table is BTREE_UNORDERED */ |
| 14071 | 14104 | Pgno pgnoRoot; /* Root page of the open btree cursor */ |
| 14072 | 14105 | sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */ |
| 14073 | 14106 | i64 seqCount; /* Sequence counter */ |
| 14074 | 14107 | i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 14075 | | - i64 lastRowid; /* Rowid being deleted by OP_Delete */ |
| 14076 | 14108 | VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */ |
| 14077 | 14109 | |
| 14078 | 14110 | /* Cached information about the header for the data record that the |
| 14079 | 14111 | ** cursor is currently pointing to. Only valid if cacheStatus matches |
| 14080 | 14112 | ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of |
| | @@ -14087,10 +14119,11 @@ |
| 14087 | 14119 | u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */ |
| 14088 | 14120 | u32 payloadSize; /* Total number of bytes in the record */ |
| 14089 | 14121 | u32 szRow; /* Byte available in aRow */ |
| 14090 | 14122 | u32 iHdrOffset; /* Offset to next unparsed byte of the header */ |
| 14091 | 14123 | const u8 *aRow; /* Data for the current row, if all on one page */ |
| 14124 | + u32 *aOffset; /* Pointer to aType[nField] */ |
| 14092 | 14125 | u32 aType[1]; /* Type values for all entries in the record */ |
| 14093 | 14126 | /* 2*nField extra array elements allocated for aType[], beyond the one |
| 14094 | 14127 | ** static element declared in the structure. nField total array slots for |
| 14095 | 14128 | ** aType[] and nField+1 array slots for aOffset[] */ |
| 14096 | 14129 | }; |
| | @@ -14163,11 +14196,11 @@ |
| 14163 | 14196 | int n; /* Number of characters in string value, excluding '\0' */ |
| 14164 | 14197 | char *z; /* String or BLOB value */ |
| 14165 | 14198 | /* ShallowCopy only needs to copy the information above */ |
| 14166 | 14199 | char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */ |
| 14167 | 14200 | int szMalloc; /* Size of the zMalloc allocation */ |
| 14168 | | - int iPadding1; /* Padding for 8-byte alignment */ |
| 14201 | + u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */ |
| 14169 | 14202 | sqlite3 *db; /* The associated database connection */ |
| 14170 | 14203 | void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */ |
| 14171 | 14204 | #ifdef SQLITE_DEBUG |
| 14172 | 14205 | Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ |
| 14173 | 14206 | void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */ |
| | @@ -14259,11 +14292,10 @@ |
| 14259 | 14292 | */ |
| 14260 | 14293 | struct sqlite3_context { |
| 14261 | 14294 | Mem *pOut; /* The return value is stored here */ |
| 14262 | 14295 | FuncDef *pFunc; /* Pointer to function information */ |
| 14263 | 14296 | Mem *pMem; /* Memory cell used to store aggregate context */ |
| 14264 | | - CollSeq *pColl; /* Collating sequence */ |
| 14265 | 14297 | Vdbe *pVdbe; /* The VM that owns this context */ |
| 14266 | 14298 | int iOp; /* Instruction number of OP_Function */ |
| 14267 | 14299 | int isError; /* Error code returned by the function. */ |
| 14268 | 14300 | u8 skipFlag; /* Skip accumulator loading if true */ |
| 14269 | 14301 | u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */ |
| | @@ -14348,14 +14380,10 @@ |
| 14348 | 14380 | i64 nFkConstraint; /* Number of imm. FK constraints this VM */ |
| 14349 | 14381 | i64 nStmtDefCons; /* Number of def. constraints when stmt started */ |
| 14350 | 14382 | i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ |
| 14351 | 14383 | char *zSql; /* Text of the SQL statement that generated this */ |
| 14352 | 14384 | void *pFree; /* Free this when deleting the vdbe */ |
| 14353 | | -#ifdef SQLITE_ENABLE_TREE_EXPLAIN |
| 14354 | | - Explain *pExplain; /* The explainer */ |
| 14355 | | - char *zExplain; /* Explanation of data structures */ |
| 14356 | | -#endif |
| 14357 | 14385 | VdbeFrame *pFrame; /* Parent frame */ |
| 14358 | 14386 | VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */ |
| 14359 | 14387 | int nFrame; /* Number of frames in pFrame list */ |
| 14360 | 14388 | u32 expmask; /* Binding to these vars invalidates VM */ |
| 14361 | 14389 | SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ |
| | @@ -14376,10 +14404,11 @@ |
| 14376 | 14404 | ** Function prototypes |
| 14377 | 14405 | */ |
| 14378 | 14406 | SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*); |
| 14379 | 14407 | void sqliteVdbePopStack(Vdbe*,int); |
| 14380 | 14408 | SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*); |
| 14409 | +SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*); |
| 14381 | 14410 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 14382 | 14411 | SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*); |
| 14383 | 14412 | #endif |
| 14384 | 14413 | SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32); |
| 14385 | 14414 | SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int); |
| | @@ -14675,11 +14704,11 @@ |
| 14675 | 14704 | sqlite3VdbeClearObject(db, pVdbe); |
| 14676 | 14705 | sqlite3DbFree(db, pVdbe); |
| 14677 | 14706 | } |
| 14678 | 14707 | db->pnBytesFreed = 0; |
| 14679 | 14708 | |
| 14680 | | - *pHighwater = 0; |
| 14709 | + *pHighwater = 0; /* IMP: R-64479-57858 */ |
| 14681 | 14710 | *pCurrent = nByte; |
| 14682 | 14711 | |
| 14683 | 14712 | break; |
| 14684 | 14713 | } |
| 14685 | 14714 | |
| | @@ -14700,21 +14729,23 @@ |
| 14700 | 14729 | if( db->aDb[i].pBt ){ |
| 14701 | 14730 | Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); |
| 14702 | 14731 | sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet); |
| 14703 | 14732 | } |
| 14704 | 14733 | } |
| 14705 | | - *pHighwater = 0; |
| 14734 | + *pHighwater = 0; /* IMP: R-42420-56072 */ |
| 14735 | + /* IMP: R-54100-20147 */ |
| 14736 | + /* IMP: R-29431-39229 */ |
| 14706 | 14737 | *pCurrent = nRet; |
| 14707 | 14738 | break; |
| 14708 | 14739 | } |
| 14709 | 14740 | |
| 14710 | 14741 | /* Set *pCurrent to non-zero if there are unresolved deferred foreign |
| 14711 | 14742 | ** key constraints. Set *pCurrent to zero if all foreign key constraints |
| 14712 | 14743 | ** have been satisfied. The *pHighwater is always set to zero. |
| 14713 | 14744 | */ |
| 14714 | 14745 | case SQLITE_DBSTATUS_DEFERRED_FKS: { |
| 14715 | | - *pHighwater = 0; |
| 14746 | + *pHighwater = 0; /* IMP: R-11967-56545 */ |
| 14716 | 14747 | *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; |
| 14717 | 14748 | break; |
| 14718 | 14749 | } |
| 14719 | 14750 | |
| 14720 | 14751 | default: { |
| | @@ -17098,11 +17129,11 @@ |
| 17098 | 17129 | ** allocation p. Also return true if p==NULL. |
| 17099 | 17130 | ** |
| 17100 | 17131 | ** This routine is designed for use within an assert() statement, to |
| 17101 | 17132 | ** verify the type of an allocation. For example: |
| 17102 | 17133 | ** |
| 17103 | | -** assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 17134 | +** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 17104 | 17135 | */ |
| 17105 | 17136 | SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){ |
| 17106 | 17137 | int rc = 1; |
| 17107 | 17138 | if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ |
| 17108 | 17139 | struct MemBlockHdr *pHdr; |
| | @@ -17120,11 +17151,11 @@ |
| 17120 | 17151 | ** allocation p. Also return true if p==NULL. |
| 17121 | 17152 | ** |
| 17122 | 17153 | ** This routine is designed for use within an assert() statement, to |
| 17123 | 17154 | ** verify the type of an allocation. For example: |
| 17124 | 17155 | ** |
| 17125 | | -** assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 17156 | +** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 17126 | 17157 | */ |
| 17127 | 17158 | SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){ |
| 17128 | 17159 | int rc = 1; |
| 17129 | 17160 | if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ |
| 17130 | 17161 | struct MemBlockHdr *pHdr; |
| | @@ -20195,11 +20226,11 @@ |
| 20195 | 20226 | mallocWithAlarm((int)n, &p); |
| 20196 | 20227 | sqlite3_mutex_leave(mem0.mutex); |
| 20197 | 20228 | }else{ |
| 20198 | 20229 | p = sqlite3GlobalConfig.m.xMalloc((int)n); |
| 20199 | 20230 | } |
| 20200 | | - assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */ |
| 20231 | + assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ |
| 20201 | 20232 | return p; |
| 20202 | 20233 | } |
| 20203 | 20234 | |
| 20204 | 20235 | /* |
| 20205 | 20236 | ** This version of the memory allocation is for use by the application. |
| | @@ -20332,39 +20363,41 @@ |
| 20332 | 20363 | ** Return the size of a memory allocation previously obtained from |
| 20333 | 20364 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 20334 | 20365 | */ |
| 20335 | 20366 | SQLITE_PRIVATE int sqlite3MallocSize(void *p){ |
| 20336 | 20367 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20337 | | - assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 20338 | 20368 | return sqlite3GlobalConfig.m.xSize(p); |
| 20339 | 20369 | } |
| 20340 | 20370 | SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
| 20341 | 20371 | if( db==0 ){ |
| 20372 | + assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20373 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20342 | 20374 | return sqlite3MallocSize(p); |
| 20343 | 20375 | }else{ |
| 20344 | 20376 | assert( sqlite3_mutex_held(db->mutex) ); |
| 20345 | 20377 | if( isLookaside(db, p) ){ |
| 20346 | 20378 | return db->lookaside.sz; |
| 20347 | 20379 | }else{ |
| 20348 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 20349 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); |
| 20350 | | - assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 20380 | + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20381 | + assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20351 | 20382 | return sqlite3GlobalConfig.m.xSize(p); |
| 20352 | 20383 | } |
| 20353 | 20384 | } |
| 20354 | 20385 | } |
| 20355 | 20386 | SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){ |
| 20387 | + assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20388 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20356 | 20389 | return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p); |
| 20357 | 20390 | } |
| 20358 | 20391 | |
| 20359 | 20392 | /* |
| 20360 | 20393 | ** Free memory previously obtained from sqlite3Malloc(). |
| 20361 | 20394 | */ |
| 20362 | 20395 | SQLITE_API void sqlite3_free(void *p){ |
| 20363 | 20396 | if( p==0 ) return; /* IMP: R-49053-54554 */ |
| 20364 | | - assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 20365 | 20397 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20398 | + assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20366 | 20399 | if( sqlite3GlobalConfig.bMemstat ){ |
| 20367 | 20400 | sqlite3_mutex_enter(mem0.mutex); |
| 20368 | 20401 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
| 20369 | 20402 | sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); |
| 20370 | 20403 | sqlite3GlobalConfig.m.xFree(p); |
| | @@ -20404,12 +20437,12 @@ |
| 20404 | 20437 | db->lookaside.pFree = pBuf; |
| 20405 | 20438 | db->lookaside.nOut--; |
| 20406 | 20439 | return; |
| 20407 | 20440 | } |
| 20408 | 20441 | } |
| 20409 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 20410 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); |
| 20442 | + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20443 | + assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20411 | 20444 | assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 20412 | 20445 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 20413 | 20446 | sqlite3_free(p); |
| 20414 | 20447 | } |
| 20415 | 20448 | |
| | @@ -20417,15 +20450,17 @@ |
| 20417 | 20450 | ** Change the size of an existing memory allocation |
| 20418 | 20451 | */ |
| 20419 | 20452 | SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){ |
| 20420 | 20453 | int nOld, nNew, nDiff; |
| 20421 | 20454 | void *pNew; |
| 20455 | + assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 20456 | + assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); |
| 20422 | 20457 | if( pOld==0 ){ |
| 20423 | | - return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */ |
| 20458 | + return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ |
| 20424 | 20459 | } |
| 20425 | 20460 | if( nBytes==0 ){ |
| 20426 | | - sqlite3_free(pOld); /* IMP: R-31593-10574 */ |
| 20461 | + sqlite3_free(pOld); /* IMP: R-26507-47431 */ |
| 20427 | 20462 | return 0; |
| 20428 | 20463 | } |
| 20429 | 20464 | if( nBytes>=0x7fffff00 ){ |
| 20430 | 20465 | /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ |
| 20431 | 20466 | return 0; |
| | @@ -20443,12 +20478,10 @@ |
| 20443 | 20478 | nDiff = nNew - nOld; |
| 20444 | 20479 | if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= |
| 20445 | 20480 | mem0.alarmThreshold-nDiff ){ |
| 20446 | 20481 | sqlite3MallocAlarm(nDiff); |
| 20447 | 20482 | } |
| 20448 | | - assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 20449 | | - assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); |
| 20450 | 20483 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 20451 | 20484 | if( pNew==0 && mem0.alarmCallback ){ |
| 20452 | 20485 | sqlite3MallocAlarm((int)nBytes); |
| 20453 | 20486 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 20454 | 20487 | } |
| | @@ -20458,11 +20491,11 @@ |
| 20458 | 20491 | } |
| 20459 | 20492 | sqlite3_mutex_leave(mem0.mutex); |
| 20460 | 20493 | }else{ |
| 20461 | 20494 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 20462 | 20495 | } |
| 20463 | | - assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */ |
| 20496 | + assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ |
| 20464 | 20497 | return pNew; |
| 20465 | 20498 | } |
| 20466 | 20499 | |
| 20467 | 20500 | /* |
| 20468 | 20501 | ** The public interface to sqlite3Realloc. Make sure that the memory |
| | @@ -20470,11 +20503,11 @@ |
| 20470 | 20503 | */ |
| 20471 | 20504 | SQLITE_API void *sqlite3_realloc(void *pOld, int n){ |
| 20472 | 20505 | #ifndef SQLITE_OMIT_AUTOINIT |
| 20473 | 20506 | if( sqlite3_initialize() ) return 0; |
| 20474 | 20507 | #endif |
| 20475 | | - if( n<0 ) n = 0; |
| 20508 | + if( n<0 ) n = 0; /* IMP: R-26507-47431 */ |
| 20476 | 20509 | return sqlite3Realloc(pOld, n); |
| 20477 | 20510 | } |
| 20478 | 20511 | SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ |
| 20479 | 20512 | #ifndef SQLITE_OMIT_AUTOINIT |
| 20480 | 20513 | if( sqlite3_initialize() ) return 0; |
| | @@ -20557,12 +20590,12 @@ |
| 20557 | 20590 | #endif |
| 20558 | 20591 | p = sqlite3Malloc(n); |
| 20559 | 20592 | if( !p && db ){ |
| 20560 | 20593 | db->mallocFailed = 1; |
| 20561 | 20594 | } |
| 20562 | | - sqlite3MemdebugSetType(p, MEMTYPE_DB | |
| 20563 | | - ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
| 20595 | + sqlite3MemdebugSetType(p, |
| 20596 | + (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); |
| 20564 | 20597 | return p; |
| 20565 | 20598 | } |
| 20566 | 20599 | |
| 20567 | 20600 | /* |
| 20568 | 20601 | ** Resize the block of memory pointed to by p to n bytes. If the |
| | @@ -20584,19 +20617,18 @@ |
| 20584 | 20617 | if( pNew ){ |
| 20585 | 20618 | memcpy(pNew, p, db->lookaside.sz); |
| 20586 | 20619 | sqlite3DbFree(db, p); |
| 20587 | 20620 | } |
| 20588 | 20621 | }else{ |
| 20589 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 20590 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); |
| 20622 | + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20623 | + assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20591 | 20624 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 20592 | 20625 | pNew = sqlite3_realloc64(p, n); |
| 20593 | 20626 | if( !pNew ){ |
| 20594 | | - sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP); |
| 20595 | 20627 | db->mallocFailed = 1; |
| 20596 | 20628 | } |
| 20597 | | - sqlite3MemdebugSetType(pNew, MEMTYPE_DB | |
| 20629 | + sqlite3MemdebugSetType(pNew, |
| 20598 | 20630 | (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
| 20599 | 20631 | } |
| 20600 | 20632 | } |
| 20601 | 20633 | return pNew; |
| 20602 | 20634 | } |
| | @@ -21757,10 +21789,73 @@ |
| 21757 | 21789 | fprintf(stdout,"%s", zBuf); |
| 21758 | 21790 | fflush(stdout); |
| 21759 | 21791 | } |
| 21760 | 21792 | #endif |
| 21761 | 21793 | |
| 21794 | +#ifdef SQLITE_DEBUG |
| 21795 | +/************************************************************************* |
| 21796 | +** Routines for implementing the "TreeView" display of hierarchical |
| 21797 | +** data structures for debugging. |
| 21798 | +** |
| 21799 | +** The main entry points (coded elsewhere) are: |
| 21800 | +** sqlite3TreeViewExpr(0, pExpr, 0); |
| 21801 | +** sqlite3TreeViewExprList(0, pList, 0, 0); |
| 21802 | +** sqlite3TreeViewSelect(0, pSelect, 0); |
| 21803 | +** Insert calls to those routines while debugging in order to display |
| 21804 | +** a diagram of Expr, ExprList, and Select objects. |
| 21805 | +** |
| 21806 | +*/ |
| 21807 | +/* Add a new subitem to the tree. The moreToFollow flag indicates that this |
| 21808 | +** is not the last item in the tree. */ |
| 21809 | +SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ |
| 21810 | + if( p==0 ){ |
| 21811 | + p = sqlite3_malloc( sizeof(*p) ); |
| 21812 | + if( p==0 ) return 0; |
| 21813 | + memset(p, 0, sizeof(*p)); |
| 21814 | + }else{ |
| 21815 | + p->iLevel++; |
| 21816 | + } |
| 21817 | + assert( moreToFollow==0 || moreToFollow==1 ); |
| 21818 | + if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; |
| 21819 | + return p; |
| 21820 | +} |
| 21821 | +/* Finished with one layer of the tree */ |
| 21822 | +SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView *p){ |
| 21823 | + if( p==0 ) return; |
| 21824 | + p->iLevel--; |
| 21825 | + if( p->iLevel<0 ) sqlite3_free(p); |
| 21826 | +} |
| 21827 | +/* Generate a single line of output for the tree, with a prefix that contains |
| 21828 | +** all the appropriate tree lines */ |
| 21829 | +SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ |
| 21830 | + va_list ap; |
| 21831 | + int i; |
| 21832 | + StrAccum acc; |
| 21833 | + char zBuf[500]; |
| 21834 | + sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); |
| 21835 | + acc.useMalloc = 0; |
| 21836 | + if( p ){ |
| 21837 | + for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){ |
| 21838 | + sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4); |
| 21839 | + } |
| 21840 | + sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); |
| 21841 | + } |
| 21842 | + va_start(ap, zFormat); |
| 21843 | + sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 21844 | + va_end(ap); |
| 21845 | + if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1); |
| 21846 | + sqlite3StrAccumFinish(&acc); |
| 21847 | + fprintf(stdout,"%s", zBuf); |
| 21848 | + fflush(stdout); |
| 21849 | +} |
| 21850 | +/* Shorthand for starting a new tree item that consists of a single label */ |
| 21851 | +SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){ |
| 21852 | + p = sqlite3TreeViewPush(p, moreToFollow); |
| 21853 | + sqlite3TreeViewLine(p, "%s", zLabel); |
| 21854 | +} |
| 21855 | +#endif /* SQLITE_DEBUG */ |
| 21856 | + |
| 21762 | 21857 | /* |
| 21763 | 21858 | ** variable-argument wrapper around sqlite3VXPrintf(). |
| 21764 | 21859 | */ |
| 21765 | 21860 | SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){ |
| 21766 | 21861 | va_list ap; |
| | @@ -21996,18 +22091,18 @@ |
| 21996 | 22091 | #endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */ |
| 21997 | 22092 | /******************************** End Unix Pthreads *************************/ |
| 21998 | 22093 | |
| 21999 | 22094 | |
| 22000 | 22095 | /********************************* Win32 Threads ****************************/ |
| 22001 | | -#if SQLITE_OS_WIN && !SQLITE_OS_WINRT && SQLITE_THREADSAFE>0 |
| 22096 | +#if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_THREADSAFE>0 |
| 22002 | 22097 | |
| 22003 | 22098 | #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */ |
| 22004 | 22099 | #include <process.h> |
| 22005 | 22100 | |
| 22006 | 22101 | /* A running thread */ |
| 22007 | 22102 | struct SQLiteThread { |
| 22008 | | - uintptr_t tid; /* The thread handle */ |
| 22103 | + void *tid; /* The thread handle */ |
| 22009 | 22104 | unsigned id; /* The thread identifier */ |
| 22010 | 22105 | void *(*xTask)(void*); /* The routine to run as a thread */ |
| 22011 | 22106 | void *pIn; /* Argument to xTask */ |
| 22012 | 22107 | void *pResult; /* Result of xTask */ |
| 22013 | 22108 | }; |
| | @@ -22051,11 +22146,11 @@ |
| 22051 | 22146 | if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
| 22052 | 22147 | memset(p, 0, sizeof(*p)); |
| 22053 | 22148 | }else{ |
| 22054 | 22149 | p->xTask = xTask; |
| 22055 | 22150 | p->pIn = pIn; |
| 22056 | | - p->tid = _beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); |
| 22151 | + p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); |
| 22057 | 22152 | if( p->tid==0 ){ |
| 22058 | 22153 | memset(p, 0, sizeof(*p)); |
| 22059 | 22154 | } |
| 22060 | 22155 | } |
| 22061 | 22156 | if( p->xTask==0 ){ |
| | @@ -22089,11 +22184,11 @@ |
| 22089 | 22184 | if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult; |
| 22090 | 22185 | sqlite3_free(p); |
| 22091 | 22186 | return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR; |
| 22092 | 22187 | } |
| 22093 | 22188 | |
| 22094 | | -#endif /* SQLITE_OS_WIN && !SQLITE_OS_WINRT */ |
| 22189 | +#endif /* SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT */ |
| 22095 | 22190 | /******************************** End Win32 Threads *************************/ |
| 22096 | 22191 | |
| 22097 | 22192 | |
| 22098 | 22193 | /********************************* Single-Threaded **************************/ |
| 22099 | 22194 | #ifndef SQLITE_THREADS_IMPLEMENTED |
| | @@ -29659,11 +29754,11 @@ |
| 29659 | 29754 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 29660 | 29755 | ** |
| 29661 | 29756 | ** * An I/O method finder function called FINDER that returns a pointer |
| 29662 | 29757 | ** to the METHOD object in the previous bullet. |
| 29663 | 29758 | */ |
| 29664 | | -#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
| 29759 | +#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK, SHMMAP) \ |
| 29665 | 29760 | static const sqlite3_io_methods METHOD = { \ |
| 29666 | 29761 | VERSION, /* iVersion */ \ |
| 29667 | 29762 | CLOSE, /* xClose */ \ |
| 29668 | 29763 | unixRead, /* xRead */ \ |
| 29669 | 29764 | unixWrite, /* xWrite */ \ |
| | @@ -29674,11 +29769,11 @@ |
| 29674 | 29769 | UNLOCK, /* xUnlock */ \ |
| 29675 | 29770 | CKLOCK, /* xCheckReservedLock */ \ |
| 29676 | 29771 | unixFileControl, /* xFileControl */ \ |
| 29677 | 29772 | unixSectorSize, /* xSectorSize */ \ |
| 29678 | 29773 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
| 29679 | | - unixShmMap, /* xShmMap */ \ |
| 29774 | + SHMMAP, /* xShmMap */ \ |
| 29680 | 29775 | unixShmLock, /* xShmLock */ \ |
| 29681 | 29776 | unixShmBarrier, /* xShmBarrier */ \ |
| 29682 | 29777 | unixShmUnmap, /* xShmUnmap */ \ |
| 29683 | 29778 | unixFetch, /* xFetch */ \ |
| 29684 | 29779 | unixUnfetch, /* xUnfetch */ \ |
| | @@ -29700,29 +29795,32 @@ |
| 29700 | 29795 | posixIoMethods, /* sqlite3_io_methods object name */ |
| 29701 | 29796 | 3, /* shared memory and mmap are enabled */ |
| 29702 | 29797 | unixClose, /* xClose method */ |
| 29703 | 29798 | unixLock, /* xLock method */ |
| 29704 | 29799 | unixUnlock, /* xUnlock method */ |
| 29705 | | - unixCheckReservedLock /* xCheckReservedLock method */ |
| 29800 | + unixCheckReservedLock, /* xCheckReservedLock method */ |
| 29801 | + unixShmMap /* xShmMap method */ |
| 29706 | 29802 | ) |
| 29707 | 29803 | IOMETHODS( |
| 29708 | 29804 | nolockIoFinder, /* Finder function name */ |
| 29709 | 29805 | nolockIoMethods, /* sqlite3_io_methods object name */ |
| 29710 | 29806 | 3, /* shared memory is disabled */ |
| 29711 | 29807 | nolockClose, /* xClose method */ |
| 29712 | 29808 | nolockLock, /* xLock method */ |
| 29713 | 29809 | nolockUnlock, /* xUnlock method */ |
| 29714 | | - nolockCheckReservedLock /* xCheckReservedLock method */ |
| 29810 | + nolockCheckReservedLock, /* xCheckReservedLock method */ |
| 29811 | + 0 /* xShmMap method */ |
| 29715 | 29812 | ) |
| 29716 | 29813 | IOMETHODS( |
| 29717 | 29814 | dotlockIoFinder, /* Finder function name */ |
| 29718 | 29815 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
| 29719 | 29816 | 1, /* shared memory is disabled */ |
| 29720 | 29817 | dotlockClose, /* xClose method */ |
| 29721 | 29818 | dotlockLock, /* xLock method */ |
| 29722 | 29819 | dotlockUnlock, /* xUnlock method */ |
| 29723 | | - dotlockCheckReservedLock /* xCheckReservedLock method */ |
| 29820 | + dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 29821 | + 0 /* xShmMap method */ |
| 29724 | 29822 | ) |
| 29725 | 29823 | |
| 29726 | 29824 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
| 29727 | 29825 | IOMETHODS( |
| 29728 | 29826 | flockIoFinder, /* Finder function name */ |
| | @@ -29729,11 +29827,12 @@ |
| 29729 | 29827 | flockIoMethods, /* sqlite3_io_methods object name */ |
| 29730 | 29828 | 1, /* shared memory is disabled */ |
| 29731 | 29829 | flockClose, /* xClose method */ |
| 29732 | 29830 | flockLock, /* xLock method */ |
| 29733 | 29831 | flockUnlock, /* xUnlock method */ |
| 29734 | | - flockCheckReservedLock /* xCheckReservedLock method */ |
| 29832 | + flockCheckReservedLock, /* xCheckReservedLock method */ |
| 29833 | + 0 /* xShmMap method */ |
| 29735 | 29834 | ) |
| 29736 | 29835 | #endif |
| 29737 | 29836 | |
| 29738 | 29837 | #if OS_VXWORKS |
| 29739 | 29838 | IOMETHODS( |
| | @@ -29741,11 +29840,12 @@ |
| 29741 | 29840 | semIoMethods, /* sqlite3_io_methods object name */ |
| 29742 | 29841 | 1, /* shared memory is disabled */ |
| 29743 | 29842 | semClose, /* xClose method */ |
| 29744 | 29843 | semLock, /* xLock method */ |
| 29745 | 29844 | semUnlock, /* xUnlock method */ |
| 29746 | | - semCheckReservedLock /* xCheckReservedLock method */ |
| 29845 | + semCheckReservedLock, /* xCheckReservedLock method */ |
| 29846 | + 0 /* xShmMap method */ |
| 29747 | 29847 | ) |
| 29748 | 29848 | #endif |
| 29749 | 29849 | |
| 29750 | 29850 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 29751 | 29851 | IOMETHODS( |
| | @@ -29753,11 +29853,12 @@ |
| 29753 | 29853 | afpIoMethods, /* sqlite3_io_methods object name */ |
| 29754 | 29854 | 1, /* shared memory is disabled */ |
| 29755 | 29855 | afpClose, /* xClose method */ |
| 29756 | 29856 | afpLock, /* xLock method */ |
| 29757 | 29857 | afpUnlock, /* xUnlock method */ |
| 29758 | | - afpCheckReservedLock /* xCheckReservedLock method */ |
| 29858 | + afpCheckReservedLock, /* xCheckReservedLock method */ |
| 29859 | + 0 /* xShmMap method */ |
| 29759 | 29860 | ) |
| 29760 | 29861 | #endif |
| 29761 | 29862 | |
| 29762 | 29863 | /* |
| 29763 | 29864 | ** The proxy locking method is a "super-method" in the sense that it |
| | @@ -29778,11 +29879,12 @@ |
| 29778 | 29879 | proxyIoMethods, /* sqlite3_io_methods object name */ |
| 29779 | 29880 | 1, /* shared memory is disabled */ |
| 29780 | 29881 | proxyClose, /* xClose method */ |
| 29781 | 29882 | proxyLock, /* xLock method */ |
| 29782 | 29883 | proxyUnlock, /* xUnlock method */ |
| 29783 | | - proxyCheckReservedLock /* xCheckReservedLock method */ |
| 29884 | + proxyCheckReservedLock, /* xCheckReservedLock method */ |
| 29885 | + 0 /* xShmMap method */ |
| 29784 | 29886 | ) |
| 29785 | 29887 | #endif |
| 29786 | 29888 | |
| 29787 | 29889 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 29788 | 29890 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| | @@ -29791,11 +29893,12 @@ |
| 29791 | 29893 | nfsIoMethods, /* sqlite3_io_methods object name */ |
| 29792 | 29894 | 1, /* shared memory is disabled */ |
| 29793 | 29895 | unixClose, /* xClose method */ |
| 29794 | 29896 | unixLock, /* xLock method */ |
| 29795 | 29897 | nfsUnlock, /* xUnlock method */ |
| 29796 | | - unixCheckReservedLock /* xCheckReservedLock method */ |
| 29898 | + unixCheckReservedLock, /* xCheckReservedLock method */ |
| 29899 | + 0 /* xShmMap method */ |
| 29797 | 29900 | ) |
| 29798 | 29901 | #endif |
| 29799 | 29902 | |
| 29800 | 29903 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 29801 | 29904 | /* |
| | @@ -33384,11 +33487,15 @@ |
| 33384 | 33487 | #endif |
| 33385 | 33488 | |
| 33386 | 33489 | #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \ |
| 33387 | 33490 | DWORD))aSyscall[63].pCurrent) |
| 33388 | 33491 | |
| 33492 | +#if !SQLITE_OS_WINCE |
| 33389 | 33493 | { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 }, |
| 33494 | +#else |
| 33495 | + { "WaitForSingleObjectEx", (SYSCALL)0, 0 }, |
| 33496 | +#endif |
| 33390 | 33497 | |
| 33391 | 33498 | #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \ |
| 33392 | 33499 | BOOL))aSyscall[64].pCurrent) |
| 33393 | 33500 | |
| 33394 | 33501 | #if SQLITE_OS_WINRT |
| | @@ -33727,11 +33834,12 @@ |
| 33727 | 33834 | #else |
| 33728 | 33835 | osSleep(milliseconds); |
| 33729 | 33836 | #endif |
| 33730 | 33837 | } |
| 33731 | 33838 | |
| 33732 | | -#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINRT && SQLITE_THREADSAFE>0 |
| 33839 | +#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \ |
| 33840 | + SQLITE_THREADSAFE>0 |
| 33733 | 33841 | SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){ |
| 33734 | 33842 | DWORD rc; |
| 33735 | 33843 | while( (rc = osWaitForSingleObjectEx(hObject, INFINITE, |
| 33736 | 33844 | TRUE))==WAIT_IO_COMPLETION ){} |
| 33737 | 33845 | return rc; |
| | @@ -39752,11 +39860,11 @@ |
| 39752 | 39860 | assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage ); |
| 39753 | 39861 | assert( pCache->n90pct == pCache->nMax*9/10 ); |
| 39754 | 39862 | if( createFlag==1 && ( |
| 39755 | 39863 | nPinned>=pGroup->mxPinned |
| 39756 | 39864 | || nPinned>=pCache->n90pct |
| 39757 | | - || pcache1UnderMemoryPressure(pCache) |
| 39865 | + || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned) |
| 39758 | 39866 | )){ |
| 39759 | 39867 | return 0; |
| 39760 | 39868 | } |
| 39761 | 39869 | |
| 39762 | 39870 | if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache); |
| | @@ -44373,17 +44481,19 @@ |
| 44373 | 44481 | if( !pNew ) rc = SQLITE_NOMEM; |
| 44374 | 44482 | } |
| 44375 | 44483 | |
| 44376 | 44484 | if( rc==SQLITE_OK ){ |
| 44377 | 44485 | pager_reset(pPager); |
| 44378 | | - sqlite3PageFree(pPager->pTmpSpace); |
| 44379 | | - pPager->pTmpSpace = pNew; |
| 44380 | 44486 | rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize); |
| 44381 | 44487 | } |
| 44382 | 44488 | if( rc==SQLITE_OK ){ |
| 44489 | + sqlite3PageFree(pPager->pTmpSpace); |
| 44490 | + pPager->pTmpSpace = pNew; |
| 44383 | 44491 | pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize); |
| 44384 | 44492 | pPager->pageSize = pageSize; |
| 44493 | + }else{ |
| 44494 | + sqlite3PageFree(pNew); |
| 44385 | 44495 | } |
| 44386 | 44496 | } |
| 44387 | 44497 | |
| 44388 | 44498 | *pPageSize = pPager->pageSize; |
| 44389 | 44499 | if( rc==SQLITE_OK ){ |
| | @@ -51383,13 +51493,14 @@ |
| 51383 | 51493 | ** stored in MemPage.pBt->mutex. |
| 51384 | 51494 | */ |
| 51385 | 51495 | struct MemPage { |
| 51386 | 51496 | u8 isInit; /* True if previously initialized. MUST BE FIRST! */ |
| 51387 | 51497 | u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ |
| 51388 | | - u8 intKey; /* True if intkey flag is set */ |
| 51389 | | - u8 leaf; /* True if leaf flag is set */ |
| 51390 | | - u8 hasData; /* True if this page stores data */ |
| 51498 | + u8 intKey; /* True if table b-trees. False for index b-trees */ |
| 51499 | + u8 intKeyLeaf; /* True if the leaf of an intKey table */ |
| 51500 | + u8 noPayload; /* True if internal intKey page (thus w/o data) */ |
| 51501 | + u8 leaf; /* True if a leaf page */ |
| 51391 | 51502 | u8 hdrOffset; /* 100 for page 1. 0 otherwise */ |
| 51392 | 51503 | u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ |
| 51393 | 51504 | u8 max1bytePayload; /* min(maxLocal,127) */ |
| 51394 | 51505 | u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ |
| 51395 | 51506 | u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ |
| | @@ -51545,11 +51656,11 @@ |
| 51545 | 51656 | int nRef; /* Number of references to this structure */ |
| 51546 | 51657 | BtShared *pNext; /* Next on a list of sharable BtShared structs */ |
| 51547 | 51658 | BtLock *pLock; /* List of locks held on this shared-btree struct */ |
| 51548 | 51659 | Btree *pWriter; /* Btree with currently open write transaction */ |
| 51549 | 51660 | #endif |
| 51550 | | - u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */ |
| 51661 | + u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */ |
| 51551 | 51662 | }; |
| 51552 | 51663 | |
| 51553 | 51664 | /* |
| 51554 | 51665 | ** Allowed values for BtShared.btsFlags |
| 51555 | 51666 | */ |
| | @@ -51566,16 +51677,14 @@ |
| 51566 | 51677 | ** about a cell. The parseCellPtr() function fills in this structure |
| 51567 | 51678 | ** based on information extract from the raw disk page. |
| 51568 | 51679 | */ |
| 51569 | 51680 | typedef struct CellInfo CellInfo; |
| 51570 | 51681 | struct CellInfo { |
| 51571 | | - i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ |
| 51572 | | - u8 *pCell; /* Pointer to the start of cell content */ |
| 51573 | | - u32 nData; /* Number of bytes of data */ |
| 51574 | | - u32 nPayload; /* Total amount of payload */ |
| 51575 | | - u16 nHeader; /* Size of the cell content header in bytes */ |
| 51576 | | - u16 nLocal; /* Amount of payload held locally */ |
| 51682 | + i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */ |
| 51683 | + u8 *pPayload; /* Pointer to the start of payload */ |
| 51684 | + u32 nPayload; /* Bytes of payload */ |
| 51685 | + u16 nLocal; /* Amount of payload held locally, not on overflow */ |
| 51577 | 51686 | u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ |
| 51578 | 51687 | u16 nSize; /* Size of the cell content on the main b-tree page */ |
| 51579 | 51688 | }; |
| 51580 | 51689 | |
| 51581 | 51690 | /* |
| | @@ -51768,10 +51877,12 @@ |
| 51768 | 51877 | u8 *aPgRef; /* 1 bit per page in the db (see above) */ |
| 51769 | 51878 | Pgno nPage; /* Number of pages in the database */ |
| 51770 | 51879 | int mxErr; /* Stop accumulating errors when this reaches zero */ |
| 51771 | 51880 | int nErr; /* Number of messages written to zErrMsg so far */ |
| 51772 | 51881 | int mallocFailed; /* A memory allocation error has occurred */ |
| 51882 | + const char *zPfx; /* Error message prefix */ |
| 51883 | + int v1, v2; /* Values for up to two %d fields in zPfx */ |
| 51773 | 51884 | StrAccum errMsg; /* Accumulate the error message text here */ |
| 51774 | 51885 | }; |
| 51775 | 51886 | |
| 51776 | 51887 | /* |
| 51777 | 51888 | ** Routines to read or write a two- and four-byte big-endian integer values. |
| | @@ -52554,11 +52665,13 @@ |
| 52554 | 52665 | ){ |
| 52555 | 52666 | BtCursor *p; |
| 52556 | 52667 | BtShared *pBt = pBtree->pBt; |
| 52557 | 52668 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
| 52558 | 52669 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 52559 | | - if( (p->curFlags & BTCF_Incrblob)!=0 && (isClearTable || p->info.nKey==iRow) ){ |
| 52670 | + if( (p->curFlags & BTCF_Incrblob)!=0 |
| 52671 | + && (isClearTable || p->info.nKey==iRow) |
| 52672 | + ){ |
| 52560 | 52673 | p->eState = CURSOR_INVALID; |
| 52561 | 52674 | } |
| 52562 | 52675 | } |
| 52563 | 52676 | } |
| 52564 | 52677 | |
| | @@ -52727,13 +52840,13 @@ |
| 52727 | 52840 | ** the cursors if and when a cursor is found that actually requires saving. |
| 52728 | 52841 | ** The common case is that no cursors need to be saved, so this routine is |
| 52729 | 52842 | ** broken out from its caller to avoid unnecessary stack pointer movement. |
| 52730 | 52843 | */ |
| 52731 | 52844 | static int SQLITE_NOINLINE saveCursorsOnList( |
| 52732 | | - BtCursor *p, /* The first cursor that needs saving */ |
| 52733 | | - Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */ |
| 52734 | | - BtCursor *pExcept /* Do not save this cursor */ |
| 52845 | + BtCursor *p, /* The first cursor that needs saving */ |
| 52846 | + Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */ |
| 52847 | + BtCursor *pExcept /* Do not save this cursor */ |
| 52735 | 52848 | ){ |
| 52736 | 52849 | do{ |
| 52737 | 52850 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){ |
| 52738 | 52851 | if( p->eState==CURSOR_VALID ){ |
| 52739 | 52852 | int rc = saveCursorPosition(p); |
| | @@ -52841,11 +52954,11 @@ |
| 52841 | 52954 | ** |
| 52842 | 52955 | ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor |
| 52843 | 52956 | ** back to where it ought to be if this routine returns true. |
| 52844 | 52957 | */ |
| 52845 | 52958 | SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){ |
| 52846 | | - return pCur && pCur->eState!=CURSOR_VALID; |
| 52959 | + return pCur->eState!=CURSOR_VALID; |
| 52847 | 52960 | } |
| 52848 | 52961 | |
| 52849 | 52962 | /* |
| 52850 | 52963 | ** This routine restores a cursor back to its original position after it |
| 52851 | 52964 | ** has been moved by some outside activity (such as a btree rebalance or |
| | @@ -53035,51 +53148,48 @@ |
| 53035 | 53148 | /* |
| 53036 | 53149 | ** Parse a cell content block and fill in the CellInfo structure. There |
| 53037 | 53150 | ** are two versions of this function. btreeParseCell() takes a |
| 53038 | 53151 | ** cell index as the second argument and btreeParseCellPtr() |
| 53039 | 53152 | ** takes a pointer to the body of the cell as its second argument. |
| 53040 | | -** |
| 53041 | | -** Within this file, the parseCell() macro can be called instead of |
| 53042 | | -** btreeParseCellPtr(). Using some compilers, this will be faster. |
| 53043 | 53153 | */ |
| 53044 | 53154 | static void btreeParseCellPtr( |
| 53045 | 53155 | MemPage *pPage, /* Page containing the cell */ |
| 53046 | 53156 | u8 *pCell, /* Pointer to the cell text. */ |
| 53047 | 53157 | CellInfo *pInfo /* Fill in this structure */ |
| 53048 | 53158 | ){ |
| 53049 | | - u16 n; /* Number bytes in cell content header */ |
| 53159 | + u8 *pIter; /* For scanning through pCell */ |
| 53050 | 53160 | u32 nPayload; /* Number of bytes of cell payload */ |
| 53051 | 53161 | |
| 53052 | 53162 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 53053 | | - |
| 53054 | | - pInfo->pCell = pCell; |
| 53055 | 53163 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
| 53056 | | - n = pPage->childPtrSize; |
| 53057 | | - assert( n==4-4*pPage->leaf ); |
| 53058 | | - if( pPage->intKey ){ |
| 53059 | | - if( pPage->hasData ){ |
| 53060 | | - assert( n==0 ); |
| 53061 | | - n = getVarint32(pCell, nPayload); |
| 53062 | | - }else{ |
| 53063 | | - nPayload = 0; |
| 53064 | | - } |
| 53065 | | - n += getVarint(&pCell[n], (u64*)&pInfo->nKey); |
| 53066 | | - pInfo->nData = nPayload; |
| 53067 | | - }else{ |
| 53068 | | - pInfo->nData = 0; |
| 53069 | | - n += getVarint32(&pCell[n], nPayload); |
| 53164 | + if( pPage->intKeyLeaf ){ |
| 53165 | + assert( pPage->childPtrSize==0 ); |
| 53166 | + pIter = pCell + getVarint32(pCell, nPayload); |
| 53167 | + pIter += getVarint(pIter, (u64*)&pInfo->nKey); |
| 53168 | + }else if( pPage->noPayload ){ |
| 53169 | + assert( pPage->childPtrSize==4 ); |
| 53170 | + pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); |
| 53171 | + pInfo->nPayload = 0; |
| 53172 | + pInfo->nLocal = 0; |
| 53173 | + pInfo->iOverflow = 0; |
| 53174 | + pInfo->pPayload = 0; |
| 53175 | + return; |
| 53176 | + }else{ |
| 53177 | + pIter = pCell + pPage->childPtrSize; |
| 53178 | + pIter += getVarint32(pIter, nPayload); |
| 53070 | 53179 | pInfo->nKey = nPayload; |
| 53071 | 53180 | } |
| 53072 | 53181 | pInfo->nPayload = nPayload; |
| 53073 | | - pInfo->nHeader = n; |
| 53182 | + pInfo->pPayload = pIter; |
| 53074 | 53183 | testcase( nPayload==pPage->maxLocal ); |
| 53075 | 53184 | testcase( nPayload==pPage->maxLocal+1 ); |
| 53076 | | - if( likely(nPayload<=pPage->maxLocal) ){ |
| 53185 | + if( nPayload<=pPage->maxLocal ){ |
| 53077 | 53186 | /* This is the (easy) common case where the entire payload fits |
| 53078 | 53187 | ** on the local page. No overflow is required. |
| 53079 | 53188 | */ |
| 53080 | | - if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4; |
| 53189 | + pInfo->nSize = nPayload + (u16)(pIter - pCell); |
| 53190 | + if( pInfo->nSize<4 ) pInfo->nSize = 4; |
| 53081 | 53191 | pInfo->nLocal = (u16)nPayload; |
| 53082 | 53192 | pInfo->iOverflow = 0; |
| 53083 | 53193 | }else{ |
| 53084 | 53194 | /* If the payload will not fit completely on the local page, we have |
| 53085 | 53195 | ** to decide how much to store locally and how much to spill onto |
| | @@ -53102,33 +53212,32 @@ |
| 53102 | 53212 | if( surplus <= maxLocal ){ |
| 53103 | 53213 | pInfo->nLocal = (u16)surplus; |
| 53104 | 53214 | }else{ |
| 53105 | 53215 | pInfo->nLocal = (u16)minLocal; |
| 53106 | 53216 | } |
| 53107 | | - pInfo->iOverflow = (u16)(pInfo->nLocal + n); |
| 53217 | + pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell); |
| 53108 | 53218 | pInfo->nSize = pInfo->iOverflow + 4; |
| 53109 | 53219 | } |
| 53110 | 53220 | } |
| 53111 | | -#define parseCell(pPage, iCell, pInfo) \ |
| 53112 | | - btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
| 53113 | 53221 | static void btreeParseCell( |
| 53114 | 53222 | MemPage *pPage, /* Page containing the cell */ |
| 53115 | 53223 | int iCell, /* The cell index. First cell is 0 */ |
| 53116 | 53224 | CellInfo *pInfo /* Fill in this structure */ |
| 53117 | 53225 | ){ |
| 53118 | | - parseCell(pPage, iCell, pInfo); |
| 53226 | + btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo); |
| 53119 | 53227 | } |
| 53120 | 53228 | |
| 53121 | 53229 | /* |
| 53122 | 53230 | ** Compute the total number of bytes that a Cell needs in the cell |
| 53123 | 53231 | ** data area of the btree-page. The return number includes the cell |
| 53124 | 53232 | ** data header and the local payload, but not any overflow page or |
| 53125 | 53233 | ** the space used by the cell pointer. |
| 53126 | 53234 | */ |
| 53127 | 53235 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 53128 | | - u8 *pIter = &pCell[pPage->childPtrSize]; |
| 53129 | | - u32 nSize; |
| 53236 | + u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ |
| 53237 | + u8 *pEnd; /* End mark for a varint */ |
| 53238 | + u32 nSize; /* Size value to return */ |
| 53130 | 53239 | |
| 53131 | 53240 | #ifdef SQLITE_DEBUG |
| 53132 | 53241 | /* The value returned by this function should always be the same as |
| 53133 | 53242 | ** the (CellInfo.nSize) value found by doing a full parse of the |
| 53134 | 53243 | ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
| | @@ -53135,47 +53244,48 @@ |
| 53135 | 53244 | ** this function verifies that this invariant is not violated. */ |
| 53136 | 53245 | CellInfo debuginfo; |
| 53137 | 53246 | btreeParseCellPtr(pPage, pCell, &debuginfo); |
| 53138 | 53247 | #endif |
| 53139 | 53248 | |
| 53249 | + if( pPage->noPayload ){ |
| 53250 | + pEnd = &pIter[9]; |
| 53251 | + while( (*pIter++)&0x80 && pIter<pEnd ); |
| 53252 | + assert( pPage->childPtrSize==4 ); |
| 53253 | + return (u16)(pIter - pCell); |
| 53254 | + } |
| 53255 | + nSize = *pIter; |
| 53256 | + if( nSize>=0x80 ){ |
| 53257 | + pEnd = &pIter[9]; |
| 53258 | + nSize &= 0x7f; |
| 53259 | + do{ |
| 53260 | + nSize = (nSize<<7) | (*++pIter & 0x7f); |
| 53261 | + }while( *(pIter)>=0x80 && pIter<pEnd ); |
| 53262 | + } |
| 53263 | + pIter++; |
| 53140 | 53264 | if( pPage->intKey ){ |
| 53141 | | - u8 *pEnd; |
| 53142 | | - if( pPage->hasData ){ |
| 53143 | | - pIter += getVarint32(pIter, nSize); |
| 53144 | | - }else{ |
| 53145 | | - nSize = 0; |
| 53146 | | - } |
| 53147 | | - |
| 53148 | 53265 | /* pIter now points at the 64-bit integer key value, a variable length |
| 53149 | 53266 | ** integer. The following block moves pIter to point at the first byte |
| 53150 | 53267 | ** past the end of the key value. */ |
| 53151 | 53268 | pEnd = &pIter[9]; |
| 53152 | 53269 | while( (*pIter++)&0x80 && pIter<pEnd ); |
| 53153 | | - }else{ |
| 53154 | | - pIter += getVarint32(pIter, nSize); |
| 53155 | 53270 | } |
| 53156 | | - |
| 53157 | 53271 | testcase( nSize==pPage->maxLocal ); |
| 53158 | 53272 | testcase( nSize==pPage->maxLocal+1 ); |
| 53159 | | - if( nSize>pPage->maxLocal ){ |
| 53273 | + if( nSize<=pPage->maxLocal ){ |
| 53274 | + nSize += (u32)(pIter - pCell); |
| 53275 | + if( nSize<4 ) nSize = 4; |
| 53276 | + }else{ |
| 53160 | 53277 | int minLocal = pPage->minLocal; |
| 53161 | 53278 | nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); |
| 53162 | 53279 | testcase( nSize==pPage->maxLocal ); |
| 53163 | 53280 | testcase( nSize==pPage->maxLocal+1 ); |
| 53164 | 53281 | if( nSize>pPage->maxLocal ){ |
| 53165 | 53282 | nSize = minLocal; |
| 53166 | 53283 | } |
| 53167 | | - nSize += 4; |
| 53168 | | - } |
| 53169 | | - nSize += (u32)(pIter - pCell); |
| 53170 | | - |
| 53171 | | - /* The minimum size of any cell is 4 bytes. */ |
| 53172 | | - if( nSize<4 ){ |
| 53173 | | - nSize = 4; |
| 53174 | | - } |
| 53175 | | - |
| 53176 | | - assert( nSize==debuginfo.nSize ); |
| 53284 | + nSize += 4 + (u16)(pIter - pCell); |
| 53285 | + } |
| 53286 | + assert( nSize==debuginfo.nSize || CORRUPT_DB ); |
| 53177 | 53287 | return (u16)nSize; |
| 53178 | 53288 | } |
| 53179 | 53289 | |
| 53180 | 53290 | #ifdef SQLITE_DEBUG |
| 53181 | 53291 | /* This variation on cellSizePtr() is used inside of assert() statements |
| | @@ -53194,11 +53304,10 @@ |
| 53194 | 53304 | static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ |
| 53195 | 53305 | CellInfo info; |
| 53196 | 53306 | if( *pRC ) return; |
| 53197 | 53307 | assert( pCell!=0 ); |
| 53198 | 53308 | btreeParseCellPtr(pPage, pCell, &info); |
| 53199 | | - assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
| 53200 | 53309 | if( info.iOverflow ){ |
| 53201 | 53310 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 53202 | 53311 | ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); |
| 53203 | 53312 | } |
| 53204 | 53313 | } |
| | @@ -53407,11 +53516,11 @@ |
| 53407 | 53516 | ** does it detect cells or freeblocks that encrouch into the reserved bytes |
| 53408 | 53517 | ** at the end of the page. So do additional corruption checks inside this |
| 53409 | 53518 | ** routine and return SQLITE_CORRUPT if any problems are found. |
| 53410 | 53519 | */ |
| 53411 | 53520 | static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ |
| 53412 | | - u16 iPtr; /* Address of pointer to next freeblock */ |
| 53521 | + u16 iPtr; /* Address of ptr to next freeblock */ |
| 53413 | 53522 | u16 iFreeBlk; /* Address of the next freeblock */ |
| 53414 | 53523 | u8 hdr; /* Page header size. 0 or 100 */ |
| 53415 | 53524 | u8 nFrag = 0; /* Reduction in fragmentation */ |
| 53416 | 53525 | u16 iOrigSize = iSize; /* Original value of iSize */ |
| 53417 | 53526 | u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */ |
| | @@ -53459,13 +53568,13 @@ |
| 53459 | 53568 | iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); |
| 53460 | 53569 | iSize = iEnd - iStart; |
| 53461 | 53570 | iFreeBlk = get2byte(&data[iFreeBlk]); |
| 53462 | 53571 | } |
| 53463 | 53572 | |
| 53464 | | - /* If iPtr is another freeblock (that is, if iPtr is not the freelist pointer |
| 53465 | | - ** in the page header) then check to see if iStart should be coalesced |
| 53466 | | - ** onto the end of iPtr. |
| 53573 | + /* If iPtr is another freeblock (that is, if iPtr is not the freelist |
| 53574 | + ** pointer in the page header) then check to see if iStart should be |
| 53575 | + ** coalesced onto the end of iPtr. |
| 53467 | 53576 | */ |
| 53468 | 53577 | if( iPtr>hdr+1 ){ |
| 53469 | 53578 | int iPtrEnd = iPtr + get2byte(&data[iPtr+2]); |
| 53470 | 53579 | if( iPtrEnd+3>=iStart ){ |
| 53471 | 53580 | if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT; |
| | @@ -53515,16 +53624,18 @@ |
| 53515 | 53624 | flagByte &= ~PTF_LEAF; |
| 53516 | 53625 | pPage->childPtrSize = 4-4*pPage->leaf; |
| 53517 | 53626 | pBt = pPage->pBt; |
| 53518 | 53627 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 53519 | 53628 | pPage->intKey = 1; |
| 53520 | | - pPage->hasData = pPage->leaf; |
| 53629 | + pPage->intKeyLeaf = pPage->leaf; |
| 53630 | + pPage->noPayload = !pPage->leaf; |
| 53521 | 53631 | pPage->maxLocal = pBt->maxLeaf; |
| 53522 | 53632 | pPage->minLocal = pBt->minLeaf; |
| 53523 | 53633 | }else if( flagByte==PTF_ZERODATA ){ |
| 53524 | 53634 | pPage->intKey = 0; |
| 53525 | | - pPage->hasData = 0; |
| 53635 | + pPage->intKeyLeaf = 0; |
| 53636 | + pPage->noPayload = 0; |
| 53526 | 53637 | pPage->maxLocal = pBt->maxLocal; |
| 53527 | 53638 | pPage->minLocal = pBt->minLocal; |
| 53528 | 53639 | }else{ |
| 53529 | 53640 | return SQLITE_CORRUPT_BKPT; |
| 53530 | 53641 | } |
| | @@ -54175,11 +54286,12 @@ |
| 54175 | 54286 | #endif |
| 54176 | 54287 | } |
| 54177 | 54288 | |
| 54178 | 54289 | /* |
| 54179 | 54290 | ** Make sure pBt->pTmpSpace points to an allocation of |
| 54180 | | -** MX_CELL_SIZE(pBt) bytes. |
| 54291 | +** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child |
| 54292 | +** pointer. |
| 54181 | 54293 | */ |
| 54182 | 54294 | static void allocateTempSpace(BtShared *pBt){ |
| 54183 | 54295 | if( !pBt->pTmpSpace ){ |
| 54184 | 54296 | pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); |
| 54185 | 54297 | |
| | @@ -54190,21 +54302,32 @@ |
| 54190 | 54302 | ** can mean that fillInCell() only initializes the first 2 or 3 |
| 54191 | 54303 | ** bytes of pTmpSpace, but that the first 4 bytes are copied from |
| 54192 | 54304 | ** it into a database page. This is not actually a problem, but it |
| 54193 | 54305 | ** does cause a valgrind error when the 1 or 2 bytes of unitialized |
| 54194 | 54306 | ** data is passed to system call write(). So to avoid this error, |
| 54195 | | - ** zero the first 4 bytes of temp space here. */ |
| 54196 | | - if( pBt->pTmpSpace ) memset(pBt->pTmpSpace, 0, 4); |
| 54307 | + ** zero the first 4 bytes of temp space here. |
| 54308 | + ** |
| 54309 | + ** Also: Provide four bytes of initialized space before the |
| 54310 | + ** beginning of pTmpSpace as an area available to prepend the |
| 54311 | + ** left-child pointer to the beginning of a cell. |
| 54312 | + */ |
| 54313 | + if( pBt->pTmpSpace ){ |
| 54314 | + memset(pBt->pTmpSpace, 0, 8); |
| 54315 | + pBt->pTmpSpace += 4; |
| 54316 | + } |
| 54197 | 54317 | } |
| 54198 | 54318 | } |
| 54199 | 54319 | |
| 54200 | 54320 | /* |
| 54201 | 54321 | ** Free the pBt->pTmpSpace allocation |
| 54202 | 54322 | */ |
| 54203 | 54323 | static void freeTempSpace(BtShared *pBt){ |
| 54204 | | - sqlite3PageFree( pBt->pTmpSpace); |
| 54205 | | - pBt->pTmpSpace = 0; |
| 54324 | + if( pBt->pTmpSpace ){ |
| 54325 | + pBt->pTmpSpace -= 4; |
| 54326 | + sqlite3PageFree(pBt->pTmpSpace); |
| 54327 | + pBt->pTmpSpace = 0; |
| 54328 | + } |
| 54206 | 54329 | } |
| 54207 | 54330 | |
| 54208 | 54331 | /* |
| 54209 | 54332 | ** Close an open database and invalidate all cursors. |
| 54210 | 54333 | */ |
| | @@ -54694,15 +54817,15 @@ |
| 54694 | 54817 | */ |
| 54695 | 54818 | static void unlockBtreeIfUnused(BtShared *pBt){ |
| 54696 | 54819 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 54697 | 54820 | assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE ); |
| 54698 | 54821 | if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){ |
| 54699 | | - assert( pBt->pPage1->aData ); |
| 54822 | + MemPage *pPage1 = pBt->pPage1; |
| 54823 | + assert( pPage1->aData ); |
| 54700 | 54824 | assert( sqlite3PagerRefcount(pBt->pPager)==1 ); |
| 54701 | | - assert( pBt->pPage1->aData ); |
| 54702 | | - releasePage(pBt->pPage1); |
| 54703 | 54825 | pBt->pPage1 = 0; |
| 54826 | + releasePage(pPage1); |
| 54704 | 54827 | } |
| 54705 | 54828 | } |
| 54706 | 54829 | |
| 54707 | 54830 | /* |
| 54708 | 54831 | ** If pBt points to an empty file then convert that empty file |
| | @@ -55739,10 +55862,14 @@ |
| 55739 | 55862 | assert( pBt->pPage1 && pBt->pPage1->aData ); |
| 55740 | 55863 | |
| 55741 | 55864 | if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){ |
| 55742 | 55865 | return SQLITE_READONLY; |
| 55743 | 55866 | } |
| 55867 | + if( wrFlag ){ |
| 55868 | + allocateTempSpace(pBt); |
| 55869 | + if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM; |
| 55870 | + } |
| 55744 | 55871 | if( iTable==1 && btreePagecount(pBt)==0 ){ |
| 55745 | 55872 | assert( wrFlag==0 ); |
| 55746 | 55873 | iTable = 0; |
| 55747 | 55874 | } |
| 55748 | 55875 | |
| | @@ -55928,12 +56055,13 @@ |
| 55928 | 56055 | ** to return an integer result code for historical reasons. |
| 55929 | 56056 | */ |
| 55930 | 56057 | SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
| 55931 | 56058 | assert( cursorHoldsMutex(pCur) ); |
| 55932 | 56059 | assert( pCur->eState==CURSOR_VALID ); |
| 56060 | + assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 ); |
| 55933 | 56061 | getCellInfo(pCur); |
| 55934 | | - *pSize = pCur->info.nData; |
| 56062 | + *pSize = pCur->info.nPayload; |
| 55935 | 56063 | return SQLITE_OK; |
| 55936 | 56064 | } |
| 55937 | 56065 | |
| 55938 | 56066 | /* |
| 55939 | 56067 | ** Given the page number of an overflow page in the database (parameter |
| | @@ -56080,34 +56208,32 @@ |
| 56080 | 56208 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
| 56081 | 56209 | int eOp /* zero to read. non-zero to write. */ |
| 56082 | 56210 | ){ |
| 56083 | 56211 | unsigned char *aPayload; |
| 56084 | 56212 | int rc = SQLITE_OK; |
| 56085 | | - u32 nKey; |
| 56086 | 56213 | int iIdx = 0; |
| 56087 | 56214 | MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ |
| 56088 | 56215 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
| 56089 | 56216 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 56090 | | - int bEnd; /* True if reading to end of data */ |
| 56217 | + unsigned char * const pBufStart = pBuf; |
| 56218 | + int bEnd; /* True if reading to end of data */ |
| 56091 | 56219 | #endif |
| 56092 | 56220 | |
| 56093 | 56221 | assert( pPage ); |
| 56094 | 56222 | assert( pCur->eState==CURSOR_VALID ); |
| 56095 | 56223 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 56096 | 56224 | assert( cursorHoldsMutex(pCur) ); |
| 56097 | | - assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */ |
| 56225 | + assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */ |
| 56098 | 56226 | |
| 56099 | 56227 | getCellInfo(pCur); |
| 56100 | | - aPayload = pCur->info.pCell + pCur->info.nHeader; |
| 56101 | | - nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey); |
| 56228 | + aPayload = pCur->info.pPayload; |
| 56102 | 56229 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 56103 | | - bEnd = (offset+amt==nKey+pCur->info.nData); |
| 56230 | + bEnd = offset+amt==pCur->info.nPayload; |
| 56104 | 56231 | #endif |
| 56232 | + assert( offset+amt <= pCur->info.nPayload ); |
| 56105 | 56233 | |
| 56106 | | - if( NEVER(offset+amt > nKey+pCur->info.nData) |
| 56107 | | - || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] |
| 56108 | | - ){ |
| 56234 | + if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){ |
| 56109 | 56235 | /* Trying to read or write past the end of the data is an error */ |
| 56110 | 56236 | return SQLITE_CORRUPT_BKPT; |
| 56111 | 56237 | } |
| 56112 | 56238 | |
| 56113 | 56239 | /* Check if data must be read/written to/from the btree page itself. */ |
| | @@ -56159,11 +56285,13 @@ |
| 56159 | 56285 | |
| 56160 | 56286 | /* If the overflow page-list cache has been allocated and the |
| 56161 | 56287 | ** entry for the first required overflow page is valid, skip |
| 56162 | 56288 | ** directly to it. |
| 56163 | 56289 | */ |
| 56164 | | - if( (pCur->curFlags & BTCF_ValidOvfl)!=0 && pCur->aOverflow[offset/ovflSize] ){ |
| 56290 | + if( (pCur->curFlags & BTCF_ValidOvfl)!=0 |
| 56291 | + && pCur->aOverflow[offset/ovflSize] |
| 56292 | + ){ |
| 56165 | 56293 | iIdx = (offset/ovflSize); |
| 56166 | 56294 | nextPage = pCur->aOverflow[iIdx]; |
| 56167 | 56295 | offset = (offset%ovflSize); |
| 56168 | 56296 | } |
| 56169 | 56297 | |
| | @@ -56212,10 +56340,11 @@ |
| 56212 | 56340 | ** 2) data is required from the start of this overflow page, and |
| 56213 | 56341 | ** 3) the database is file-backed, and |
| 56214 | 56342 | ** 4) there is no open write-transaction, and |
| 56215 | 56343 | ** 5) the database is not a WAL database, |
| 56216 | 56344 | ** 6) all data from the page is being read. |
| 56345 | + ** 7) at least 4 bytes have already been read into the output buffer |
| 56217 | 56346 | ** |
| 56218 | 56347 | ** then data can be read directly from the database file into the |
| 56219 | 56348 | ** output buffer, bypassing the page-cache altogether. This speeds |
| 56220 | 56349 | ** up loading large records that span many overflow pages. |
| 56221 | 56350 | */ |
| | @@ -56223,13 +56352,15 @@ |
| 56223 | 56352 | && offset==0 /* (2) */ |
| 56224 | 56353 | && (bEnd || a==ovflSize) /* (6) */ |
| 56225 | 56354 | && pBt->inTransaction==TRANS_READ /* (4) */ |
| 56226 | 56355 | && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */ |
| 56227 | 56356 | && pBt->pPage1->aData[19]==0x01 /* (5) */ |
| 56357 | + && &pBuf[-4]>=pBufStart /* (7) */ |
| 56228 | 56358 | ){ |
| 56229 | 56359 | u8 aSave[4]; |
| 56230 | 56360 | u8 *aWrite = &pBuf[-4]; |
| 56361 | + assert( aWrite>=pBufStart ); /* hence (7) */ |
| 56231 | 56362 | memcpy(aSave, aWrite, 4); |
| 56232 | 56363 | rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); |
| 56233 | 56364 | nextPage = get4byte(aWrite); |
| 56234 | 56365 | memcpy(aWrite, aSave, 4); |
| 56235 | 56366 | }else |
| | @@ -56337,11 +56468,11 @@ |
| 56337 | 56468 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 56338 | 56469 | assert( cursorHoldsMutex(pCur) ); |
| 56339 | 56470 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
| 56340 | 56471 | assert( pCur->info.nSize>0 ); |
| 56341 | 56472 | *pAmt = pCur->info.nLocal; |
| 56342 | | - return (void*)(pCur->info.pCell + pCur->info.nHeader); |
| 56473 | + return (void*)pCur->info.pPayload; |
| 56343 | 56474 | } |
| 56344 | 56475 | |
| 56345 | 56476 | |
| 56346 | 56477 | /* |
| 56347 | 56478 | ** For the entry that cursor pCur is point to, return as |
| | @@ -56765,11 +56896,11 @@ |
| 56765 | 56896 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
| 56766 | 56897 | if( xRecordCompare==0 ){ |
| 56767 | 56898 | for(;;){ |
| 56768 | 56899 | i64 nCellKey; |
| 56769 | 56900 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
| 56770 | | - if( pPage->hasData ){ |
| 56901 | + if( pPage->intKeyLeaf ){ |
| 56771 | 56902 | while( 0x80 <= *(pCell++) ){ |
| 56772 | 56903 | if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT; |
| 56773 | 56904 | } |
| 56774 | 56905 | } |
| 56775 | 56906 | getVarint(pCell, (u64*)&nCellKey); |
| | @@ -57024,13 +57155,13 @@ |
| 57024 | 57155 | ** was already pointing to the first entry in the database before |
| 57025 | 57156 | ** this routine was called, then set *pRes=1. |
| 57026 | 57157 | ** |
| 57027 | 57158 | ** The main entry point is sqlite3BtreePrevious(). That routine is optimized |
| 57028 | 57159 | ** for the common case of merely decrementing the cell counter BtCursor.aiIdx |
| 57029 | | -** to the previous cell on the current page. The (slower) btreePrevious() helper |
| 57030 | | -** routine is called when it is necessary to move to a different page or |
| 57031 | | -** to restore the cursor. |
| 57160 | +** to the previous cell on the current page. The (slower) btreePrevious() |
| 57161 | +** helper routine is called when it is necessary to move to a different page |
| 57162 | +** or to restore the cursor. |
| 57032 | 57163 | ** |
| 57033 | 57164 | ** The calling function will set *pRes to 0 or 1. The initial *pRes value |
| 57034 | 57165 | ** will be 1 if the cursor being stepped corresponds to an SQL index and |
| 57035 | 57166 | ** if this routine could have been skipped if that SQL index had been |
| 57036 | 57167 | ** a unique index. Otherwise the caller will have set *pRes to zero. |
| | @@ -57048,12 +57179,11 @@ |
| 57048 | 57179 | assert( *pRes==0 ); |
| 57049 | 57180 | assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
| 57050 | 57181 | assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 ); |
| 57051 | 57182 | assert( pCur->info.nSize==0 ); |
| 57052 | 57183 | if( pCur->eState!=CURSOR_VALID ){ |
| 57053 | | - assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 57054 | | - rc = btreeRestoreCursorPosition(pCur); |
| 57184 | + rc = restoreCursorPosition(pCur); |
| 57055 | 57185 | if( rc!=SQLITE_OK ){ |
| 57056 | 57186 | return rc; |
| 57057 | 57187 | } |
| 57058 | 57188 | if( CURSOR_INVALID==pCur->eState ){ |
| 57059 | 57189 | *pRes = 1; |
| | @@ -57354,11 +57484,11 @@ |
| 57354 | 57484 | if( rc ) goto end_allocate_page; |
| 57355 | 57485 | if( closest<k-1 ){ |
| 57356 | 57486 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 57357 | 57487 | } |
| 57358 | 57488 | put4byte(&aData[4], k-1); |
| 57359 | | - noContent = !btreeGetHasContent(pBt, *pPgno) ? PAGER_GET_NOCONTENT : 0; |
| 57489 | + noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0; |
| 57360 | 57490 | rc = btreeGetPage(pBt, *pPgno, ppPage, noContent); |
| 57361 | 57491 | if( rc==SQLITE_OK ){ |
| 57362 | 57492 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
| 57363 | 57493 | if( rc!=SQLITE_OK ){ |
| 57364 | 57494 | releasePage(*ppPage); |
| | @@ -57387,11 +57517,11 @@ |
| 57387 | 57517 | ** content for any page that really does lie past the end of the database |
| 57388 | 57518 | ** file on disk. So the effects of disabling the no-content optimization |
| 57389 | 57519 | ** here are confined to those pages that lie between the end of the |
| 57390 | 57520 | ** database image and the end of the database file. |
| 57391 | 57521 | */ |
| 57392 | | - int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)) ? PAGER_GET_NOCONTENT : 0; |
| 57522 | + int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0; |
| 57393 | 57523 | |
| 57394 | 57524 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 57395 | 57525 | if( rc ) return rc; |
| 57396 | 57526 | pBt->nPage++; |
| 57397 | 57527 | if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; |
| | @@ -57586,22 +57716,29 @@ |
| 57586 | 57716 | *pRC = freePage2(pPage->pBt, pPage, pPage->pgno); |
| 57587 | 57717 | } |
| 57588 | 57718 | } |
| 57589 | 57719 | |
| 57590 | 57720 | /* |
| 57591 | | -** Free any overflow pages associated with the given Cell. |
| 57721 | +** Free any overflow pages associated with the given Cell. Write the |
| 57722 | +** local Cell size (the number of bytes on the original page, omitting |
| 57723 | +** overflow) into *pnSize. |
| 57592 | 57724 | */ |
| 57593 | | -static int clearCell(MemPage *pPage, unsigned char *pCell){ |
| 57725 | +static int clearCell( |
| 57726 | + MemPage *pPage, /* The page that contains the Cell */ |
| 57727 | + unsigned char *pCell, /* First byte of the Cell */ |
| 57728 | + u16 *pnSize /* Write the size of the Cell here */ |
| 57729 | +){ |
| 57594 | 57730 | BtShared *pBt = pPage->pBt; |
| 57595 | 57731 | CellInfo info; |
| 57596 | 57732 | Pgno ovflPgno; |
| 57597 | 57733 | int rc; |
| 57598 | 57734 | int nOvfl; |
| 57599 | 57735 | u32 ovflPageSize; |
| 57600 | 57736 | |
| 57601 | 57737 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 57602 | 57738 | btreeParseCellPtr(pPage, pCell, &info); |
| 57739 | + *pnSize = info.nSize; |
| 57603 | 57740 | if( info.iOverflow==0 ){ |
| 57604 | 57741 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
| 57605 | 57742 | } |
| 57606 | 57743 | if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){ |
| 57607 | 57744 | return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */ |
| | @@ -57681,54 +57818,87 @@ |
| 57681 | 57818 | unsigned char *pPrior; |
| 57682 | 57819 | unsigned char *pPayload; |
| 57683 | 57820 | BtShared *pBt = pPage->pBt; |
| 57684 | 57821 | Pgno pgnoOvfl = 0; |
| 57685 | 57822 | int nHeader; |
| 57686 | | - CellInfo info; |
| 57687 | 57823 | |
| 57688 | 57824 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 57689 | 57825 | |
| 57690 | 57826 | /* pPage is not necessarily writeable since pCell might be auxiliary |
| 57691 | 57827 | ** buffer space that is separate from the pPage buffer area */ |
| 57692 | 57828 | assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize] |
| 57693 | 57829 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 57694 | 57830 | |
| 57695 | 57831 | /* Fill in the header. */ |
| 57696 | | - nHeader = 0; |
| 57697 | | - if( !pPage->leaf ){ |
| 57698 | | - nHeader += 4; |
| 57699 | | - } |
| 57700 | | - if( pPage->hasData ){ |
| 57701 | | - nHeader += putVarint32(&pCell[nHeader], nData+nZero); |
| 57832 | + nHeader = pPage->childPtrSize; |
| 57833 | + nPayload = nData + nZero; |
| 57834 | + if( pPage->intKeyLeaf ){ |
| 57835 | + nHeader += putVarint32(&pCell[nHeader], nPayload); |
| 57702 | 57836 | }else{ |
| 57703 | | - nData = nZero = 0; |
| 57837 | + assert( nData==0 ); |
| 57838 | + assert( nZero==0 ); |
| 57704 | 57839 | } |
| 57705 | 57840 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
| 57706 | | - btreeParseCellPtr(pPage, pCell, &info); |
| 57707 | | - assert( info.nHeader==nHeader ); |
| 57708 | | - assert( info.nKey==nKey ); |
| 57709 | | - assert( info.nData==(u32)(nData+nZero) ); |
| 57710 | 57841 | |
| 57711 | | - /* Fill in the payload */ |
| 57712 | | - nPayload = nData + nZero; |
| 57842 | + /* Fill in the payload size */ |
| 57713 | 57843 | if( pPage->intKey ){ |
| 57714 | 57844 | pSrc = pData; |
| 57715 | 57845 | nSrc = nData; |
| 57716 | 57846 | nData = 0; |
| 57717 | 57847 | }else{ |
| 57718 | 57848 | if( NEVER(nKey>0x7fffffff || pKey==0) ){ |
| 57719 | 57849 | return SQLITE_CORRUPT_BKPT; |
| 57720 | 57850 | } |
| 57721 | | - nPayload += (int)nKey; |
| 57851 | + nPayload = (int)nKey; |
| 57722 | 57852 | pSrc = pKey; |
| 57723 | 57853 | nSrc = (int)nKey; |
| 57724 | 57854 | } |
| 57725 | | - *pnSize = info.nSize; |
| 57726 | | - spaceLeft = info.nLocal; |
| 57855 | + if( nPayload<=pPage->maxLocal ){ |
| 57856 | + n = nHeader + nPayload; |
| 57857 | + testcase( n==3 ); |
| 57858 | + testcase( n==4 ); |
| 57859 | + if( n<4 ) n = 4; |
| 57860 | + *pnSize = n; |
| 57861 | + spaceLeft = nPayload; |
| 57862 | + pPrior = pCell; |
| 57863 | + }else{ |
| 57864 | + int mn = pPage->minLocal; |
| 57865 | + n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); |
| 57866 | + testcase( n==pPage->maxLocal ); |
| 57867 | + testcase( n==pPage->maxLocal+1 ); |
| 57868 | + if( n > pPage->maxLocal ) n = mn; |
| 57869 | + spaceLeft = n; |
| 57870 | + *pnSize = n + nHeader + 4; |
| 57871 | + pPrior = &pCell[nHeader+n]; |
| 57872 | + } |
| 57727 | 57873 | pPayload = &pCell[nHeader]; |
| 57728 | | - pPrior = &pCell[info.iOverflow]; |
| 57729 | 57874 | |
| 57875 | + /* At this point variables should be set as follows: |
| 57876 | + ** |
| 57877 | + ** nPayload Total payload size in bytes |
| 57878 | + ** pPayload Begin writing payload here |
| 57879 | + ** spaceLeft Space available at pPayload. If nPayload>spaceLeft, |
| 57880 | + ** that means content must spill into overflow pages. |
| 57881 | + ** *pnSize Size of the local cell (not counting overflow pages) |
| 57882 | + ** pPrior Where to write the pgno of the first overflow page |
| 57883 | + ** |
| 57884 | + ** Use a call to btreeParseCellPtr() to verify that the values above |
| 57885 | + ** were computed correctly. |
| 57886 | + */ |
| 57887 | +#if SQLITE_DEBUG |
| 57888 | + { |
| 57889 | + CellInfo info; |
| 57890 | + btreeParseCellPtr(pPage, pCell, &info); |
| 57891 | + assert( nHeader=(int)(info.pPayload - pCell) ); |
| 57892 | + assert( info.nKey==nKey ); |
| 57893 | + assert( *pnSize == info.nSize ); |
| 57894 | + assert( spaceLeft == info.nLocal ); |
| 57895 | + assert( pPrior == &pCell[info.iOverflow] ); |
| 57896 | + } |
| 57897 | +#endif |
| 57898 | + |
| 57899 | + /* Write the payload into the local Cell and any extra into overflow pages */ |
| 57730 | 57900 | while( nPayload>0 ){ |
| 57731 | 57901 | if( spaceLeft==0 ){ |
| 57732 | 57902 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 57733 | 57903 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
| 57734 | 57904 | if( pBt->autoVacuum ){ |
| | @@ -57865,15 +58035,10 @@ |
| 57865 | 58035 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 57866 | 58036 | ** in pPage->apOvfl[] and make it point to the cell content (either |
| 57867 | 58037 | ** in pTemp or the original pCell) and also record its index. |
| 57868 | 58038 | ** Allocating a new entry in pPage->aCell[] implies that |
| 57869 | 58039 | ** pPage->nOverflow is incremented. |
| 57870 | | -** |
| 57871 | | -** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 57872 | | -** cell. The caller will overwrite them after this function returns. If |
| 57873 | | -** nSkip is non-zero, then pCell may not point to an invalid memory location |
| 57874 | | -** (but pCell+nSkip is always valid). |
| 57875 | 58040 | */ |
| 57876 | 58041 | static void insertCell( |
| 57877 | 58042 | MemPage *pPage, /* Page into which we are copying */ |
| 57878 | 58043 | int i, /* New cell becomes the i-th cell of the page */ |
| 57879 | 58044 | u8 *pCell, /* Content of the new cell */ |
| | @@ -57886,11 +58051,10 @@ |
| 57886 | 58051 | int j; /* Loop counter */ |
| 57887 | 58052 | int end; /* First byte past the last cell pointer in data[] */ |
| 57888 | 58053 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 57889 | 58054 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 57890 | 58055 | u8 *data; /* The content of the whole page */ |
| 57891 | | - int nSkip = (iChild ? 4 : 0); |
| 57892 | 58056 | |
| 57893 | 58057 | if( *pRC ) return; |
| 57894 | 58058 | |
| 57895 | 58059 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 57896 | 58060 | assert( MX_CELL(pPage->pBt)<=10921 ); |
| | @@ -57904,11 +58068,11 @@ |
| 57904 | 58068 | ** might be less than 8 (leaf-size + pointer) on the interior node. Hence |
| 57905 | 58069 | ** the term after the || in the following assert(). */ |
| 57906 | 58070 | assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) ); |
| 57907 | 58071 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
| 57908 | 58072 | if( pTemp ){ |
| 57909 | | - memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
| 58073 | + memcpy(pTemp, pCell, sz); |
| 57910 | 58074 | pCell = pTemp; |
| 57911 | 58075 | } |
| 57912 | 58076 | if( iChild ){ |
| 57913 | 58077 | put4byte(pCell, iChild); |
| 57914 | 58078 | } |
| | @@ -57933,11 +58097,11 @@ |
| 57933 | 58097 | ** if it returns success */ |
| 57934 | 58098 | assert( idx >= end+2 ); |
| 57935 | 58099 | assert( idx+sz <= (int)pPage->pBt->usableSize ); |
| 57936 | 58100 | pPage->nCell++; |
| 57937 | 58101 | pPage->nFree -= (u16)(2 + sz); |
| 57938 | | - memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
| 58102 | + memcpy(&data[idx], pCell, sz); |
| 57939 | 58103 | if( iChild ){ |
| 57940 | 58104 | put4byte(&data[idx], iChild); |
| 57941 | 58105 | } |
| 57942 | 58106 | memmove(&data[ins+2], &data[ins], end-ins); |
| 57943 | 58107 | put2byte(&data[ins], idx); |
| | @@ -58432,11 +58596,11 @@ |
| 58432 | 58596 | ** |
| 58433 | 58597 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 58434 | 58598 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
| 58435 | 58599 | */ |
| 58436 | 58600 | leafCorrection = apOld[0]->leaf*4; |
| 58437 | | - leafData = apOld[0]->hasData; |
| 58601 | + leafData = apOld[0]->intKeyLeaf; |
| 58438 | 58602 | for(i=0; i<nOld; i++){ |
| 58439 | 58603 | int limit; |
| 58440 | 58604 | |
| 58441 | 58605 | /* Before doing anything else, take a copy of the i'th original sibling |
| 58442 | 58606 | ** The rest of this function will use data from the copies rather |
| | @@ -59008,11 +59172,11 @@ |
| 59008 | 59172 | int const iIdx = pCur->aiIdx[iPage-1]; |
| 59009 | 59173 | |
| 59010 | 59174 | rc = sqlite3PagerWrite(pParent->pDbPage); |
| 59011 | 59175 | if( rc==SQLITE_OK ){ |
| 59012 | 59176 | #ifndef SQLITE_OMIT_QUICKBALANCE |
| 59013 | | - if( pPage->hasData |
| 59177 | + if( pPage->intKeyLeaf |
| 59014 | 59178 | && pPage->nOverflow==1 |
| 59015 | 59179 | && pPage->aiOvfl[0]==pPage->nCell |
| 59016 | 59180 | && pParent->pgno!=1 |
| 59017 | 59181 | && pParent->nCell==iIdx |
| 59018 | 59182 | ){ |
| | @@ -59127,11 +59291,12 @@ |
| 59127 | 59291 | assert( pCur->skipNext!=SQLITE_OK ); |
| 59128 | 59292 | return pCur->skipNext; |
| 59129 | 59293 | } |
| 59130 | 59294 | |
| 59131 | 59295 | assert( cursorHoldsMutex(pCur) ); |
| 59132 | | - assert( (pCur->curFlags & BTCF_WriteFlag)!=0 && pBt->inTransaction==TRANS_WRITE |
| 59296 | + assert( (pCur->curFlags & BTCF_WriteFlag)!=0 |
| 59297 | + && pBt->inTransaction==TRANS_WRITE |
| 59133 | 59298 | && (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
| 59134 | 59299 | assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 59135 | 59300 | |
| 59136 | 59301 | /* Assert that the caller has been consistent. If this cursor was opened |
| 59137 | 59302 | ** expecting an index b-tree, then the caller should be inserting blob |
| | @@ -59160,11 +59325,12 @@ |
| 59160 | 59325 | invalidateIncrblobCursors(p, nKey, 0); |
| 59161 | 59326 | |
| 59162 | 59327 | /* If the cursor is currently on the last row and we are appending a |
| 59163 | 59328 | ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto() |
| 59164 | 59329 | ** call */ |
| 59165 | | - if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 && pCur->info.nKey==nKey-1 ){ |
| 59330 | + if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 |
| 59331 | + && pCur->info.nKey==nKey-1 ){ |
| 59166 | 59332 | loc = -1; |
| 59167 | 59333 | } |
| 59168 | 59334 | } |
| 59169 | 59335 | |
| 59170 | 59336 | if( !loc ){ |
| | @@ -59179,13 +59345,12 @@ |
| 59179 | 59345 | |
| 59180 | 59346 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 59181 | 59347 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 59182 | 59348 | loc==0 ? "overwrite" : "new entry")); |
| 59183 | 59349 | assert( pPage->isInit ); |
| 59184 | | - allocateTempSpace(pBt); |
| 59185 | 59350 | newCell = pBt->pTmpSpace; |
| 59186 | | - if( newCell==0 ) return SQLITE_NOMEM; |
| 59351 | + assert( newCell!=0 ); |
| 59187 | 59352 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
| 59188 | 59353 | if( rc ) goto end_insert; |
| 59189 | 59354 | assert( szNew==cellSizePtr(pPage, newCell) ); |
| 59190 | 59355 | assert( szNew <= MX_CELL_SIZE(pBt) ); |
| 59191 | 59356 | idx = pCur->aiIdx[pCur->iPage]; |
| | @@ -59198,12 +59363,11 @@ |
| 59198 | 59363 | } |
| 59199 | 59364 | oldCell = findCell(pPage, idx); |
| 59200 | 59365 | if( !pPage->leaf ){ |
| 59201 | 59366 | memcpy(newCell, oldCell, 4); |
| 59202 | 59367 | } |
| 59203 | | - szOld = cellSizePtr(pPage, oldCell); |
| 59204 | | - rc = clearCell(pPage, oldCell); |
| 59368 | + rc = clearCell(pPage, oldCell, &szOld); |
| 59205 | 59369 | dropCell(pPage, idx, szOld, &rc); |
| 59206 | 59370 | if( rc ) goto end_insert; |
| 59207 | 59371 | }else if( loc<0 && pPage->nCell>0 ){ |
| 59208 | 59372 | assert( pPage->leaf ); |
| 59209 | 59373 | idx = ++pCur->aiIdx[pCur->iPage]; |
| | @@ -59261,10 +59425,11 @@ |
| 59261 | 59425 | int rc; /* Return code */ |
| 59262 | 59426 | MemPage *pPage; /* Page to delete cell from */ |
| 59263 | 59427 | unsigned char *pCell; /* Pointer to cell to delete */ |
| 59264 | 59428 | int iCellIdx; /* Index of cell to delete */ |
| 59265 | 59429 | int iCellDepth; /* Depth of node containing pCell */ |
| 59430 | + u16 szCell; /* Size of the cell being deleted */ |
| 59266 | 59431 | |
| 59267 | 59432 | assert( cursorHoldsMutex(pCur) ); |
| 59268 | 59433 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 59269 | 59434 | assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
| 59270 | 59435 | assert( pCur->curFlags & BTCF_WriteFlag ); |
| | @@ -59309,12 +59474,12 @@ |
| 59309 | 59474 | invalidateIncrblobCursors(p, pCur->info.nKey, 0); |
| 59310 | 59475 | } |
| 59311 | 59476 | |
| 59312 | 59477 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 59313 | 59478 | if( rc ) return rc; |
| 59314 | | - rc = clearCell(pPage, pCell); |
| 59315 | | - dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc); |
| 59479 | + rc = clearCell(pPage, pCell, &szCell); |
| 59480 | + dropCell(pPage, iCellIdx, szCell, &rc); |
| 59316 | 59481 | if( rc ) return rc; |
| 59317 | 59482 | |
| 59318 | 59483 | /* If the cell deleted was not located on a leaf page, then the cursor |
| 59319 | 59484 | ** is currently pointing to the largest entry in the sub-tree headed |
| 59320 | 59485 | ** by the child-page of the cell that was just deleted from an internal |
| | @@ -59327,14 +59492,12 @@ |
| 59327 | 59492 | unsigned char *pTmp; |
| 59328 | 59493 | |
| 59329 | 59494 | pCell = findCell(pLeaf, pLeaf->nCell-1); |
| 59330 | 59495 | nCell = cellSizePtr(pLeaf, pCell); |
| 59331 | 59496 | assert( MX_CELL_SIZE(pBt) >= nCell ); |
| 59332 | | - |
| 59333 | | - allocateTempSpace(pBt); |
| 59334 | 59497 | pTmp = pBt->pTmpSpace; |
| 59335 | | - |
| 59498 | + assert( pTmp!=0 ); |
| 59336 | 59499 | rc = sqlite3PagerWrite(pLeaf->pDbPage); |
| 59337 | 59500 | insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); |
| 59338 | 59501 | dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc); |
| 59339 | 59502 | if( rc ) return rc; |
| 59340 | 59503 | } |
| | @@ -59542,10 +59705,11 @@ |
| 59542 | 59705 | MemPage *pPage; |
| 59543 | 59706 | int rc; |
| 59544 | 59707 | unsigned char *pCell; |
| 59545 | 59708 | int i; |
| 59546 | 59709 | int hdr; |
| 59710 | + u16 szCell; |
| 59547 | 59711 | |
| 59548 | 59712 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 59549 | 59713 | if( pgno>btreePagecount(pBt) ){ |
| 59550 | 59714 | return SQLITE_CORRUPT_BKPT; |
| 59551 | 59715 | } |
| | @@ -59557,11 +59721,11 @@ |
| 59557 | 59721 | pCell = findCell(pPage, i); |
| 59558 | 59722 | if( !pPage->leaf ){ |
| 59559 | 59723 | rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); |
| 59560 | 59724 | if( rc ) goto cleardatabasepage_out; |
| 59561 | 59725 | } |
| 59562 | | - rc = clearCell(pPage, pCell); |
| 59726 | + rc = clearCell(pPage, pCell, &szCell); |
| 59563 | 59727 | if( rc ) goto cleardatabasepage_out; |
| 59564 | 59728 | } |
| 59565 | 59729 | if( !pPage->leaf ){ |
| 59566 | 59730 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange); |
| 59567 | 59731 | if( rc ) goto cleardatabasepage_out; |
| | @@ -59903,24 +60067,25 @@ |
| 59903 | 60067 | /* |
| 59904 | 60068 | ** Append a message to the error message string. |
| 59905 | 60069 | */ |
| 59906 | 60070 | static void checkAppendMsg( |
| 59907 | 60071 | IntegrityCk *pCheck, |
| 59908 | | - char *zMsg1, |
| 59909 | 60072 | const char *zFormat, |
| 59910 | 60073 | ... |
| 59911 | 60074 | ){ |
| 59912 | 60075 | va_list ap; |
| 60076 | + char zBuf[200]; |
| 59913 | 60077 | if( !pCheck->mxErr ) return; |
| 59914 | 60078 | pCheck->mxErr--; |
| 59915 | 60079 | pCheck->nErr++; |
| 59916 | 60080 | va_start(ap, zFormat); |
| 59917 | 60081 | if( pCheck->errMsg.nChar ){ |
| 59918 | 60082 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
| 59919 | 60083 | } |
| 59920 | | - if( zMsg1 ){ |
| 59921 | | - sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1); |
| 60084 | + if( pCheck->zPfx ){ |
| 60085 | + sqlite3_snprintf(sizeof(zBuf), zBuf, pCheck->zPfx, pCheck->v1, pCheck->v2); |
| 60086 | + sqlite3StrAccumAppendAll(&pCheck->errMsg, zBuf); |
| 59922 | 60087 | } |
| 59923 | 60088 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 59924 | 60089 | va_end(ap); |
| 59925 | 60090 | if( pCheck->errMsg.accError==STRACCUM_NOMEM ){ |
| 59926 | 60091 | pCheck->mallocFailed = 1; |
| | @@ -59954,18 +60119,18 @@ |
| 59954 | 60119 | ** Return 1 if there are 2 or more references to the page and 0 if |
| 59955 | 60120 | ** if this is the first reference to the page. |
| 59956 | 60121 | ** |
| 59957 | 60122 | ** Also check that the page number is in bounds. |
| 59958 | 60123 | */ |
| 59959 | | -static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){ |
| 60124 | +static int checkRef(IntegrityCk *pCheck, Pgno iPage){ |
| 59960 | 60125 | if( iPage==0 ) return 1; |
| 59961 | 60126 | if( iPage>pCheck->nPage ){ |
| 59962 | | - checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
| 60127 | + checkAppendMsg(pCheck, "invalid page number %d", iPage); |
| 59963 | 60128 | return 1; |
| 59964 | 60129 | } |
| 59965 | 60130 | if( getPageReferenced(pCheck, iPage) ){ |
| 59966 | | - checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
| 60131 | + checkAppendMsg(pCheck, "2nd reference to page %d", iPage); |
| 59967 | 60132 | return 1; |
| 59968 | 60133 | } |
| 59969 | 60134 | setPageReferenced(pCheck, iPage); |
| 59970 | 60135 | return 0; |
| 59971 | 60136 | } |
| | @@ -59978,26 +60143,25 @@ |
| 59978 | 60143 | */ |
| 59979 | 60144 | static void checkPtrmap( |
| 59980 | 60145 | IntegrityCk *pCheck, /* Integrity check context */ |
| 59981 | 60146 | Pgno iChild, /* Child page number */ |
| 59982 | 60147 | u8 eType, /* Expected pointer map type */ |
| 59983 | | - Pgno iParent, /* Expected pointer map parent page number */ |
| 59984 | | - char *zContext /* Context description (used for error msg) */ |
| 60148 | + Pgno iParent /* Expected pointer map parent page number */ |
| 59985 | 60149 | ){ |
| 59986 | 60150 | int rc; |
| 59987 | 60151 | u8 ePtrmapType; |
| 59988 | 60152 | Pgno iPtrmapParent; |
| 59989 | 60153 | |
| 59990 | 60154 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 59991 | 60155 | if( rc!=SQLITE_OK ){ |
| 59992 | 60156 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1; |
| 59993 | | - checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 60157 | + checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild); |
| 59994 | 60158 | return; |
| 59995 | 60159 | } |
| 59996 | 60160 | |
| 59997 | 60161 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 59998 | | - checkAppendMsg(pCheck, zContext, |
| 60162 | + checkAppendMsg(pCheck, |
| 59999 | 60163 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 60000 | 60164 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 60001 | 60165 | } |
| 60002 | 60166 | } |
| 60003 | 60167 | #endif |
| | @@ -60008,51 +60172,50 @@ |
| 60008 | 60172 | */ |
| 60009 | 60173 | static void checkList( |
| 60010 | 60174 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 60011 | 60175 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 60012 | 60176 | int iPage, /* Page number for first page in the list */ |
| 60013 | | - int N, /* Expected number of pages in the list */ |
| 60014 | | - char *zContext /* Context for error messages */ |
| 60177 | + int N /* Expected number of pages in the list */ |
| 60015 | 60178 | ){ |
| 60016 | 60179 | int i; |
| 60017 | 60180 | int expected = N; |
| 60018 | 60181 | int iFirst = iPage; |
| 60019 | 60182 | while( N-- > 0 && pCheck->mxErr ){ |
| 60020 | 60183 | DbPage *pOvflPage; |
| 60021 | 60184 | unsigned char *pOvflData; |
| 60022 | 60185 | if( iPage<1 ){ |
| 60023 | | - checkAppendMsg(pCheck, zContext, |
| 60186 | + checkAppendMsg(pCheck, |
| 60024 | 60187 | "%d of %d pages missing from overflow list starting at %d", |
| 60025 | 60188 | N+1, expected, iFirst); |
| 60026 | 60189 | break; |
| 60027 | 60190 | } |
| 60028 | | - if( checkRef(pCheck, iPage, zContext) ) break; |
| 60191 | + if( checkRef(pCheck, iPage) ) break; |
| 60029 | 60192 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
| 60030 | | - checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
| 60193 | + checkAppendMsg(pCheck, "failed to get page %d", iPage); |
| 60031 | 60194 | break; |
| 60032 | 60195 | } |
| 60033 | 60196 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
| 60034 | 60197 | if( isFreeList ){ |
| 60035 | 60198 | int n = get4byte(&pOvflData[4]); |
| 60036 | 60199 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60037 | 60200 | if( pCheck->pBt->autoVacuum ){ |
| 60038 | | - checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 60201 | + checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0); |
| 60039 | 60202 | } |
| 60040 | 60203 | #endif |
| 60041 | 60204 | if( n>(int)pCheck->pBt->usableSize/4-2 ){ |
| 60042 | | - checkAppendMsg(pCheck, zContext, |
| 60205 | + checkAppendMsg(pCheck, |
| 60043 | 60206 | "freelist leaf count too big on page %d", iPage); |
| 60044 | 60207 | N--; |
| 60045 | 60208 | }else{ |
| 60046 | 60209 | for(i=0; i<n; i++){ |
| 60047 | 60210 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
| 60048 | 60211 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60049 | 60212 | if( pCheck->pBt->autoVacuum ){ |
| 60050 | | - checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 60213 | + checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0); |
| 60051 | 60214 | } |
| 60052 | 60215 | #endif |
| 60053 | | - checkRef(pCheck, iFreePage, zContext); |
| 60216 | + checkRef(pCheck, iFreePage); |
| 60054 | 60217 | } |
| 60055 | 60218 | N -= n; |
| 60056 | 60219 | } |
| 60057 | 60220 | } |
| 60058 | 60221 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| | @@ -60061,11 +60224,11 @@ |
| 60061 | 60224 | ** page in this overflow list, check that the pointer-map entry for |
| 60062 | 60225 | ** the following page matches iPage. |
| 60063 | 60226 | */ |
| 60064 | 60227 | if( pCheck->pBt->autoVacuum && N>0 ){ |
| 60065 | 60228 | i = get4byte(pOvflData); |
| 60066 | | - checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 60229 | + checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage); |
| 60067 | 60230 | } |
| 60068 | 60231 | } |
| 60069 | 60232 | #endif |
| 60070 | 60233 | iPage = get4byte(pOvflData); |
| 60071 | 60234 | sqlite3PagerUnref(pOvflPage); |
| | @@ -60093,11 +60256,10 @@ |
| 60093 | 60256 | ** the root of the tree. |
| 60094 | 60257 | */ |
| 60095 | 60258 | static int checkTreePage( |
| 60096 | 60259 | IntegrityCk *pCheck, /* Context for the sanity check */ |
| 60097 | 60260 | int iPage, /* Page number of the page to check */ |
| 60098 | | - char *zParentContext, /* Parent context */ |
| 60099 | 60261 | i64 *pnParentMinKey, |
| 60100 | 60262 | i64 *pnParentMaxKey |
| 60101 | 60263 | ){ |
| 60102 | 60264 | MemPage *pPage; |
| 60103 | 60265 | int i, rc, depth, d2, pgno, cnt; |
| | @@ -60104,38 +60266,42 @@ |
| 60104 | 60266 | int hdr, cellStart; |
| 60105 | 60267 | int nCell; |
| 60106 | 60268 | u8 *data; |
| 60107 | 60269 | BtShared *pBt; |
| 60108 | 60270 | int usableSize; |
| 60109 | | - char zContext[100]; |
| 60110 | 60271 | char *hit = 0; |
| 60111 | 60272 | i64 nMinKey = 0; |
| 60112 | 60273 | i64 nMaxKey = 0; |
| 60113 | | - |
| 60114 | | - sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
| 60274 | + const char *saved_zPfx = pCheck->zPfx; |
| 60275 | + int saved_v1 = pCheck->v1; |
| 60276 | + int saved_v2 = pCheck->v2; |
| 60115 | 60277 | |
| 60116 | 60278 | /* Check that the page exists |
| 60117 | 60279 | */ |
| 60118 | 60280 | pBt = pCheck->pBt; |
| 60119 | 60281 | usableSize = pBt->usableSize; |
| 60120 | 60282 | if( iPage==0 ) return 0; |
| 60121 | | - if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
| 60283 | + if( checkRef(pCheck, iPage) ) return 0; |
| 60284 | + pCheck->zPfx = "Page %d: "; |
| 60285 | + pCheck->v1 = iPage; |
| 60122 | 60286 | if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
| 60123 | | - checkAppendMsg(pCheck, zContext, |
| 60287 | + checkAppendMsg(pCheck, |
| 60124 | 60288 | "unable to get the page. error code=%d", rc); |
| 60125 | | - return 0; |
| 60289 | + depth = -1; |
| 60290 | + goto end_of_check; |
| 60126 | 60291 | } |
| 60127 | 60292 | |
| 60128 | 60293 | /* Clear MemPage.isInit to make sure the corruption detection code in |
| 60129 | 60294 | ** btreeInitPage() is executed. */ |
| 60130 | 60295 | pPage->isInit = 0; |
| 60131 | 60296 | if( (rc = btreeInitPage(pPage))!=0 ){ |
| 60132 | 60297 | assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ |
| 60133 | | - checkAppendMsg(pCheck, zContext, |
| 60298 | + checkAppendMsg(pCheck, |
| 60134 | 60299 | "btreeInitPage() returns error code %d", rc); |
| 60135 | 60300 | releasePage(pPage); |
| 60136 | | - return 0; |
| 60301 | + depth = -1; |
| 60302 | + goto end_of_check; |
| 60137 | 60303 | } |
| 60138 | 60304 | |
| 60139 | 60305 | /* Check out all the cells. |
| 60140 | 60306 | */ |
| 60141 | 60307 | depth = 0; |
| | @@ -60144,99 +60310,101 @@ |
| 60144 | 60310 | u32 sz; |
| 60145 | 60311 | CellInfo info; |
| 60146 | 60312 | |
| 60147 | 60313 | /* Check payload overflow pages |
| 60148 | 60314 | */ |
| 60149 | | - sqlite3_snprintf(sizeof(zContext), zContext, |
| 60150 | | - "On tree page %d cell %d: ", iPage, i); |
| 60315 | + pCheck->zPfx = "On tree page %d cell %d: "; |
| 60316 | + pCheck->v1 = iPage; |
| 60317 | + pCheck->v2 = i; |
| 60151 | 60318 | pCell = findCell(pPage,i); |
| 60152 | 60319 | btreeParseCellPtr(pPage, pCell, &info); |
| 60153 | | - sz = info.nData; |
| 60154 | | - if( !pPage->intKey ) sz += (int)info.nKey; |
| 60320 | + sz = info.nPayload; |
| 60155 | 60321 | /* For intKey pages, check that the keys are in order. |
| 60156 | 60322 | */ |
| 60157 | | - else if( i==0 ) nMinKey = nMaxKey = info.nKey; |
| 60158 | | - else{ |
| 60159 | | - if( info.nKey <= nMaxKey ){ |
| 60160 | | - checkAppendMsg(pCheck, zContext, |
| 60161 | | - "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey); |
| 60323 | + if( pPage->intKey ){ |
| 60324 | + if( i==0 ){ |
| 60325 | + nMinKey = nMaxKey = info.nKey; |
| 60326 | + }else if( info.nKey <= nMaxKey ){ |
| 60327 | + checkAppendMsg(pCheck, |
| 60328 | + "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey); |
| 60162 | 60329 | } |
| 60163 | 60330 | nMaxKey = info.nKey; |
| 60164 | 60331 | } |
| 60165 | | - assert( sz==info.nPayload ); |
| 60166 | 60332 | if( (sz>info.nLocal) |
| 60167 | 60333 | && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize]) |
| 60168 | 60334 | ){ |
| 60169 | 60335 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
| 60170 | 60336 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 60171 | 60337 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60172 | 60338 | if( pBt->autoVacuum ){ |
| 60173 | | - checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
| 60339 | + checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage); |
| 60174 | 60340 | } |
| 60175 | 60341 | #endif |
| 60176 | | - checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
| 60342 | + checkList(pCheck, 0, pgnoOvfl, nPage); |
| 60177 | 60343 | } |
| 60178 | 60344 | |
| 60179 | 60345 | /* Check sanity of left child page. |
| 60180 | 60346 | */ |
| 60181 | 60347 | if( !pPage->leaf ){ |
| 60182 | 60348 | pgno = get4byte(pCell); |
| 60183 | 60349 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60184 | 60350 | if( pBt->autoVacuum ){ |
| 60185 | | - checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 60351 | + checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
| 60186 | 60352 | } |
| 60187 | 60353 | #endif |
| 60188 | | - d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey); |
| 60354 | + d2 = checkTreePage(pCheck, pgno, &nMinKey, i==0?NULL:&nMaxKey); |
| 60189 | 60355 | if( i>0 && d2!=depth ){ |
| 60190 | | - checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 60356 | + checkAppendMsg(pCheck, "Child page depth differs"); |
| 60191 | 60357 | } |
| 60192 | 60358 | depth = d2; |
| 60193 | 60359 | } |
| 60194 | 60360 | } |
| 60195 | 60361 | |
| 60196 | 60362 | if( !pPage->leaf ){ |
| 60197 | 60363 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 60198 | | - sqlite3_snprintf(sizeof(zContext), zContext, |
| 60199 | | - "On page %d at right child: ", iPage); |
| 60364 | + pCheck->zPfx = "On page %d at right child: "; |
| 60365 | + pCheck->v1 = iPage; |
| 60200 | 60366 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60201 | 60367 | if( pBt->autoVacuum ){ |
| 60202 | | - checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 60368 | + checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
| 60203 | 60369 | } |
| 60204 | 60370 | #endif |
| 60205 | | - checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey); |
| 60371 | + checkTreePage(pCheck, pgno, NULL, !pPage->nCell?NULL:&nMaxKey); |
| 60206 | 60372 | } |
| 60207 | 60373 | |
| 60208 | 60374 | /* For intKey leaf pages, check that the min/max keys are in order |
| 60209 | 60375 | ** with any left/parent/right pages. |
| 60210 | 60376 | */ |
| 60377 | + pCheck->zPfx = "Page %d: "; |
| 60378 | + pCheck->v1 = iPage; |
| 60211 | 60379 | if( pPage->leaf && pPage->intKey ){ |
| 60212 | 60380 | /* if we are a left child page */ |
| 60213 | 60381 | if( pnParentMinKey ){ |
| 60214 | 60382 | /* if we are the left most child page */ |
| 60215 | 60383 | if( !pnParentMaxKey ){ |
| 60216 | 60384 | if( nMaxKey > *pnParentMinKey ){ |
| 60217 | | - checkAppendMsg(pCheck, zContext, |
| 60385 | + checkAppendMsg(pCheck, |
| 60218 | 60386 | "Rowid %lld out of order (max larger than parent min of %lld)", |
| 60219 | 60387 | nMaxKey, *pnParentMinKey); |
| 60220 | 60388 | } |
| 60221 | 60389 | }else{ |
| 60222 | 60390 | if( nMinKey <= *pnParentMinKey ){ |
| 60223 | | - checkAppendMsg(pCheck, zContext, |
| 60391 | + checkAppendMsg(pCheck, |
| 60224 | 60392 | "Rowid %lld out of order (min less than parent min of %lld)", |
| 60225 | 60393 | nMinKey, *pnParentMinKey); |
| 60226 | 60394 | } |
| 60227 | 60395 | if( nMaxKey > *pnParentMaxKey ){ |
| 60228 | | - checkAppendMsg(pCheck, zContext, |
| 60396 | + checkAppendMsg(pCheck, |
| 60229 | 60397 | "Rowid %lld out of order (max larger than parent max of %lld)", |
| 60230 | 60398 | nMaxKey, *pnParentMaxKey); |
| 60231 | 60399 | } |
| 60232 | 60400 | *pnParentMinKey = nMaxKey; |
| 60233 | 60401 | } |
| 60234 | 60402 | /* else if we're a right child page */ |
| 60235 | 60403 | } else if( pnParentMaxKey ){ |
| 60236 | 60404 | if( nMinKey <= *pnParentMaxKey ){ |
| 60237 | | - checkAppendMsg(pCheck, zContext, |
| 60405 | + checkAppendMsg(pCheck, |
| 60238 | 60406 | "Rowid %lld out of order (min less than parent max of %lld)", |
| 60239 | 60407 | nMinKey, *pnParentMaxKey); |
| 60240 | 60408 | } |
| 60241 | 60409 | } |
| 60242 | 60410 | } |
| | @@ -60244,10 +60412,11 @@ |
| 60244 | 60412 | /* Check for complete coverage of the page |
| 60245 | 60413 | */ |
| 60246 | 60414 | data = pPage->aData; |
| 60247 | 60415 | hdr = pPage->hdrOffset; |
| 60248 | 60416 | hit = sqlite3PageMalloc( pBt->pageSize ); |
| 60417 | + pCheck->zPfx = 0; |
| 60249 | 60418 | if( hit==0 ){ |
| 60250 | 60419 | pCheck->mallocFailed = 1; |
| 60251 | 60420 | }else{ |
| 60252 | 60421 | int contentOffset = get2byteNotZero(&data[hdr+5]); |
| 60253 | 60422 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| | @@ -60261,11 +60430,12 @@ |
| 60261 | 60430 | int j; |
| 60262 | 60431 | if( pc<=usableSize-4 ){ |
| 60263 | 60432 | size = cellSizePtr(pPage, &data[pc]); |
| 60264 | 60433 | } |
| 60265 | 60434 | if( (int)(pc+size-1)>=usableSize ){ |
| 60266 | | - checkAppendMsg(pCheck, 0, |
| 60435 | + pCheck->zPfx = 0; |
| 60436 | + checkAppendMsg(pCheck, |
| 60267 | 60437 | "Corruption detected in cell %d on page %d",i,iPage); |
| 60268 | 60438 | }else{ |
| 60269 | 60439 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 60270 | 60440 | } |
| 60271 | 60441 | } |
| | @@ -60283,23 +60453,28 @@ |
| 60283 | 60453 | } |
| 60284 | 60454 | for(i=cnt=0; i<usableSize; i++){ |
| 60285 | 60455 | if( hit[i]==0 ){ |
| 60286 | 60456 | cnt++; |
| 60287 | 60457 | }else if( hit[i]>1 ){ |
| 60288 | | - checkAppendMsg(pCheck, 0, |
| 60458 | + checkAppendMsg(pCheck, |
| 60289 | 60459 | "Multiple uses for byte %d of page %d", i, iPage); |
| 60290 | 60460 | break; |
| 60291 | 60461 | } |
| 60292 | 60462 | } |
| 60293 | 60463 | if( cnt!=data[hdr+7] ){ |
| 60294 | | - checkAppendMsg(pCheck, 0, |
| 60464 | + checkAppendMsg(pCheck, |
| 60295 | 60465 | "Fragmentation of %d bytes reported as %d on page %d", |
| 60296 | 60466 | cnt, data[hdr+7], iPage); |
| 60297 | 60467 | } |
| 60298 | 60468 | } |
| 60299 | 60469 | sqlite3PageFree(hit); |
| 60300 | 60470 | releasePage(pPage); |
| 60471 | + |
| 60472 | +end_of_check: |
| 60473 | + pCheck->zPfx = saved_zPfx; |
| 60474 | + pCheck->v1 = saved_v1; |
| 60475 | + pCheck->v2 = saved_v2; |
| 60301 | 60476 | return depth+1; |
| 60302 | 60477 | } |
| 60303 | 60478 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
| 60304 | 60479 | |
| 60305 | 60480 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
| | @@ -60336,10 +60511,13 @@ |
| 60336 | 60511 | sCheck.pPager = pBt->pPager; |
| 60337 | 60512 | sCheck.nPage = btreePagecount(sCheck.pBt); |
| 60338 | 60513 | sCheck.mxErr = mxErr; |
| 60339 | 60514 | sCheck.nErr = 0; |
| 60340 | 60515 | sCheck.mallocFailed = 0; |
| 60516 | + sCheck.zPfx = 0; |
| 60517 | + sCheck.v1 = 0; |
| 60518 | + sCheck.v2 = 0; |
| 60341 | 60519 | *pnErr = 0; |
| 60342 | 60520 | if( sCheck.nPage==0 ){ |
| 60343 | 60521 | sqlite3BtreeLeave(p); |
| 60344 | 60522 | return 0; |
| 60345 | 60523 | } |
| | @@ -60355,53 +60533,57 @@ |
| 60355 | 60533 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); |
| 60356 | 60534 | sCheck.errMsg.useMalloc = 2; |
| 60357 | 60535 | |
| 60358 | 60536 | /* Check the integrity of the freelist |
| 60359 | 60537 | */ |
| 60538 | + sCheck.zPfx = "Main freelist: "; |
| 60360 | 60539 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 60361 | | - get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
| 60540 | + get4byte(&pBt->pPage1->aData[36])); |
| 60541 | + sCheck.zPfx = 0; |
| 60362 | 60542 | |
| 60363 | 60543 | /* Check all the tables. |
| 60364 | 60544 | */ |
| 60365 | 60545 | for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ |
| 60366 | 60546 | if( aRoot[i]==0 ) continue; |
| 60367 | 60547 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60368 | 60548 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 60369 | | - checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 60549 | + checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0); |
| 60370 | 60550 | } |
| 60371 | 60551 | #endif |
| 60372 | | - checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL); |
| 60552 | + sCheck.zPfx = "List of tree roots: "; |
| 60553 | + checkTreePage(&sCheck, aRoot[i], NULL, NULL); |
| 60554 | + sCheck.zPfx = 0; |
| 60373 | 60555 | } |
| 60374 | 60556 | |
| 60375 | 60557 | /* Make sure every page in the file is referenced |
| 60376 | 60558 | */ |
| 60377 | 60559 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
| 60378 | 60560 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 60379 | 60561 | if( getPageReferenced(&sCheck, i)==0 ){ |
| 60380 | | - checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 60562 | + checkAppendMsg(&sCheck, "Page %d is never used", i); |
| 60381 | 60563 | } |
| 60382 | 60564 | #else |
| 60383 | 60565 | /* If the database supports auto-vacuum, make sure no tables contain |
| 60384 | 60566 | ** references to pointer-map pages. |
| 60385 | 60567 | */ |
| 60386 | 60568 | if( getPageReferenced(&sCheck, i)==0 && |
| 60387 | 60569 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
| 60388 | | - checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 60570 | + checkAppendMsg(&sCheck, "Page %d is never used", i); |
| 60389 | 60571 | } |
| 60390 | 60572 | if( getPageReferenced(&sCheck, i)!=0 && |
| 60391 | 60573 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
| 60392 | | - checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 60574 | + checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i); |
| 60393 | 60575 | } |
| 60394 | 60576 | #endif |
| 60395 | 60577 | } |
| 60396 | 60578 | |
| 60397 | 60579 | /* Make sure this analysis did not leave any unref() pages. |
| 60398 | 60580 | ** This is an internal consistency check; an integrity check |
| 60399 | 60581 | ** of the integrity check. |
| 60400 | 60582 | */ |
| 60401 | 60583 | if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){ |
| 60402 | | - checkAppendMsg(&sCheck, 0, |
| 60584 | + checkAppendMsg(&sCheck, |
| 60403 | 60585 | "Outstanding page count goes from %d to %d during this analysis", |
| 60404 | 60586 | nRef, sqlite3PagerRefcount(pBt->pPager) |
| 60405 | 60587 | ); |
| 60406 | 60588 | } |
| 60407 | 60589 | |
| | @@ -60593,11 +60775,11 @@ |
| 60593 | 60775 | |
| 60594 | 60776 | /* Save the positions of all other cursors open on this table. This is |
| 60595 | 60777 | ** required in case any of them are holding references to an xFetch |
| 60596 | 60778 | ** version of the b-tree page modified by the accessPayload call below. |
| 60597 | 60779 | ** |
| 60598 | | - ** Note that pCsr must be open on a BTREE_INTKEY table and saveCursorPosition() |
| 60780 | + ** Note that pCsr must be open on a INTKEY table and saveCursorPosition() |
| 60599 | 60781 | ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence |
| 60600 | 60782 | ** saveAllCursors can only return SQLITE_OK. |
| 60601 | 60783 | */ |
| 60602 | 60784 | VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr); |
| 60603 | 60785 | assert( rc==SQLITE_OK ); |
| | @@ -61461,11 +61643,14 @@ |
| 61461 | 61643 | /* If MEM_Dyn is set then Mem.xDel!=0. |
| 61462 | 61644 | ** Mem.xDel is might not be initialized if MEM_Dyn is clear. |
| 61463 | 61645 | */ |
| 61464 | 61646 | assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 ); |
| 61465 | 61647 | |
| 61466 | | - /* MEM_Dyn may only be set if Mem.szMalloc==0 */ |
| 61648 | + /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we |
| 61649 | + ** ensure that if Mem.szMalloc>0 then it is safe to do |
| 61650 | + ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn. |
| 61651 | + ** That saves a few cycles in inner loops. */ |
| 61467 | 61652 | assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 ); |
| 61468 | 61653 | |
| 61469 | 61654 | /* Cannot be both MEM_Int and MEM_Real at the same time */ |
| 61470 | 61655 | assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) ); |
| 61471 | 61656 | |
| | @@ -61570,11 +61755,11 @@ |
| 61570 | 61755 | }else{ |
| 61571 | 61756 | pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); |
| 61572 | 61757 | } |
| 61573 | 61758 | } |
| 61574 | 61759 | |
| 61575 | | - if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){ |
| 61760 | + if( bPreserve && pMem->z && pMem->z!=pMem->zMalloc ){ |
| 61576 | 61761 | memcpy(pMem->zMalloc, pMem->z, pMem->n); |
| 61577 | 61762 | } |
| 61578 | 61763 | if( (pMem->flags&MEM_Dyn)!=0 ){ |
| 61579 | 61764 | assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC ); |
| 61580 | 61765 | pMem->xDel((void *)(pMem->z)); |
| | @@ -61597,11 +61782,12 @@ |
| 61597 | 61782 | ** |
| 61598 | 61783 | ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM) |
| 61599 | 61784 | ** if unable to complete the resizing. |
| 61600 | 61785 | */ |
| 61601 | 61786 | SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){ |
| 61602 | | - assert( szNew>=0 ); |
| 61787 | + assert( szNew>0 ); |
| 61788 | + assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 ); |
| 61603 | 61789 | if( pMem->szMalloc<szNew ){ |
| 61604 | 61790 | return sqlite3VdbeMemGrow(pMem, szNew, 0); |
| 61605 | 61791 | } |
| 61606 | 61792 | assert( (pMem->flags & MEM_Dyn)==0 ); |
| 61607 | 61793 | pMem->z = pMem->zMalloc; |
| | @@ -62321,11 +62507,14 @@ |
| 62321 | 62507 | nAlloc += (enc==SQLITE_UTF8?1:2); |
| 62322 | 62508 | } |
| 62323 | 62509 | if( nByte>iLimit ){ |
| 62324 | 62510 | return SQLITE_TOOBIG; |
| 62325 | 62511 | } |
| 62326 | | - if( sqlite3VdbeMemClearAndResize(pMem, nAlloc) ){ |
| 62512 | + testcase( nAlloc==0 ); |
| 62513 | + testcase( nAlloc==31 ); |
| 62514 | + testcase( nAlloc==32 ); |
| 62515 | + if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){ |
| 62327 | 62516 | return SQLITE_NOMEM; |
| 62328 | 62517 | } |
| 62329 | 62518 | memcpy(pMem->z, z, nAlloc); |
| 62330 | 62519 | }else if( xDel==SQLITE_DYNAMIC ){ |
| 62331 | 62520 | sqlite3VdbeMemRelease(pMem); |
| | @@ -62424,11 +62613,11 @@ |
| 62424 | 62613 | /* |
| 62425 | 62614 | ** The pVal argument is known to be a value other than NULL. |
| 62426 | 62615 | ** Convert it into a string with encoding enc and return a pointer |
| 62427 | 62616 | ** to a zero-terminated version of that string. |
| 62428 | 62617 | */ |
| 62429 | | -SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){ |
| 62618 | +static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){ |
| 62430 | 62619 | assert( pVal!=0 ); |
| 62431 | 62620 | assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); |
| 62432 | 62621 | assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); |
| 62433 | 62622 | assert( (pVal->flags & MEM_RowSet)==0 ); |
| 62434 | 62623 | assert( (pVal->flags & (MEM_Null))==0 ); |
| | @@ -63751,11 +63940,12 @@ |
| 63751 | 63940 | if( addr==p->nOp-1 ) p->nOp--; |
| 63752 | 63941 | } |
| 63753 | 63942 | } |
| 63754 | 63943 | |
| 63755 | 63944 | /* |
| 63756 | | -** Remove the last opcode inserted |
| 63945 | +** If the last opcode is "op" and it is not a jump destination, |
| 63946 | +** then remove it. Return true if and only if an opcode was removed. |
| 63757 | 63947 | */ |
| 63758 | 63948 | SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ |
| 63759 | 63949 | if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){ |
| 63760 | 63950 | sqlite3VdbeChangeToNoop(p, p->nOp-1); |
| 63761 | 63951 | return 1; |
| | @@ -64743,11 +64933,11 @@ |
| 64743 | 64933 | ** the call above. */ |
| 64744 | 64934 | }else if( pCx->pCursor ){ |
| 64745 | 64935 | sqlite3BtreeCloseCursor(pCx->pCursor); |
| 64746 | 64936 | } |
| 64747 | 64937 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 64748 | | - if( pCx->pVtabCursor ){ |
| 64938 | + else if( pCx->pVtabCursor ){ |
| 64749 | 64939 | sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; |
| 64750 | 64940 | const sqlite3_module *pModule = pVtabCursor->pVtab->pModule; |
| 64751 | 64941 | p->inVtabMethod = 1; |
| 64752 | 64942 | pModule->xClose(pVtabCursor); |
| 64753 | 64943 | p->inVtabMethod = 0; |
| | @@ -64786,13 +64976,14 @@ |
| 64786 | 64976 | static void closeAllCursors(Vdbe *p){ |
| 64787 | 64977 | if( p->pFrame ){ |
| 64788 | 64978 | VdbeFrame *pFrame; |
| 64789 | 64979 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 64790 | 64980 | sqlite3VdbeFrameRestore(pFrame); |
| 64981 | + p->pFrame = 0; |
| 64982 | + p->nFrame = 0; |
| 64791 | 64983 | } |
| 64792 | | - p->pFrame = 0; |
| 64793 | | - p->nFrame = 0; |
| 64984 | + assert( p->nFrame==0 ); |
| 64794 | 64985 | |
| 64795 | 64986 | if( p->apCsr ){ |
| 64796 | 64987 | int i; |
| 64797 | 64988 | for(i=0; i<p->nCursor; i++){ |
| 64798 | 64989 | VdbeCursor *pC = p->apCsr[i]; |
| | @@ -64810,11 +65001,11 @@ |
| 64810 | 65001 | p->pDelFrame = pDel->pParent; |
| 64811 | 65002 | sqlite3VdbeFrameDelete(pDel); |
| 64812 | 65003 | } |
| 64813 | 65004 | |
| 64814 | 65005 | /* Delete any auxdata allocations made by the VM */ |
| 64815 | | - sqlite3VdbeDeleteAuxData(p, -1, 0); |
| 65006 | + if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0); |
| 64816 | 65007 | assert( p->pAuxData==0 ); |
| 64817 | 65008 | } |
| 64818 | 65009 | |
| 64819 | 65010 | /* |
| 64820 | 65011 | ** Clean up the VM after a single run. |
| | @@ -65676,14 +65867,10 @@ |
| 65676 | 65867 | for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
| 65677 | 65868 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 65678 | 65869 | sqlite3DbFree(db, p->aColName); |
| 65679 | 65870 | sqlite3DbFree(db, p->zSql); |
| 65680 | 65871 | sqlite3DbFree(db, p->pFree); |
| 65681 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 65682 | | - sqlite3DbFree(db, p->zExplain); |
| 65683 | | - sqlite3DbFree(db, p->pExplain); |
| 65684 | | -#endif |
| 65685 | 65872 | } |
| 65686 | 65873 | |
| 65687 | 65874 | /* |
| 65688 | 65875 | ** Delete an entire VDBE. |
| 65689 | 65876 | */ |
| | @@ -65720,13 +65907,11 @@ |
| 65720 | 65907 | #endif |
| 65721 | 65908 | assert( p->deferredMoveto ); |
| 65722 | 65909 | assert( p->isTable ); |
| 65723 | 65910 | rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); |
| 65724 | 65911 | if( rc ) return rc; |
| 65725 | | - p->lastRowid = p->movetoTarget; |
| 65726 | 65912 | if( res!=0 ) return SQLITE_CORRUPT_BKPT; |
| 65727 | | - p->rowidIsValid = 1; |
| 65728 | 65913 | #ifdef SQLITE_TEST |
| 65729 | 65914 | sqlite3_search_count++; |
| 65730 | 65915 | #endif |
| 65731 | 65916 | p->deferredMoveto = 0; |
| 65732 | 65917 | p->cacheStatus = CACHE_STALE; |
| | @@ -65747,10 +65932,21 @@ |
| 65747 | 65932 | rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow); |
| 65748 | 65933 | p->cacheStatus = CACHE_STALE; |
| 65749 | 65934 | if( isDifferentRow ) p->nullRow = 1; |
| 65750 | 65935 | return rc; |
| 65751 | 65936 | } |
| 65937 | + |
| 65938 | +/* |
| 65939 | +** Check to ensure that the cursor is valid. Restore the cursor |
| 65940 | +** if need be. Return any I/O error from the restore operation. |
| 65941 | +*/ |
| 65942 | +SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){ |
| 65943 | + if( sqlite3BtreeCursorHasMoved(p->pCursor) ){ |
| 65944 | + return handleMovedCursor(p); |
| 65945 | + } |
| 65946 | + return SQLITE_OK; |
| 65947 | +} |
| 65752 | 65948 | |
| 65753 | 65949 | /* |
| 65754 | 65950 | ** Make sure the cursor p is ready to read or write the row to which it |
| 65755 | 65951 | ** was last positioned. Return an error code if an OOM fault or I/O error |
| 65756 | 65952 | ** prevents us from positioning the cursor to its correct position. |
| | @@ -65765,11 +65961,11 @@ |
| 65765 | 65961 | */ |
| 65766 | 65962 | SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){ |
| 65767 | 65963 | if( p->deferredMoveto ){ |
| 65768 | 65964 | return handleDeferredMoveto(p); |
| 65769 | 65965 | } |
| 65770 | | - if( sqlite3BtreeCursorHasMoved(p->pCursor) ){ |
| 65966 | + if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){ |
| 65771 | 65967 | return handleMovedCursor(p); |
| 65772 | 65968 | } |
| 65773 | 65969 | return SQLITE_OK; |
| 65774 | 65970 | } |
| 65775 | 65971 | |
| | @@ -67390,10 +67586,11 @@ |
| 67390 | 67586 | void (*xDel)(void *), |
| 67391 | 67587 | unsigned char enc |
| 67392 | 67588 | ){ |
| 67393 | 67589 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 67394 | 67590 | assert( xDel!=SQLITE_DYNAMIC ); |
| 67591 | + if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; |
| 67395 | 67592 | if( n>0x7fffffff ){ |
| 67396 | 67593 | (void)invokeValueDestructor(z, xDel, pCtx); |
| 67397 | 67594 | }else{ |
| 67398 | 67595 | setResultStrOrError(pCtx, z, (int)n, enc, xDel); |
| 67399 | 67596 | } |
| | @@ -68712,125 +68909,10 @@ |
| 68712 | 68909 | return sqlite3StrAccumFinish(&out); |
| 68713 | 68910 | } |
| 68714 | 68911 | |
| 68715 | 68912 | #endif /* #ifndef SQLITE_OMIT_TRACE */ |
| 68716 | 68913 | |
| 68717 | | -/***************************************************************************** |
| 68718 | | -** The following code implements the data-structure explaining logic |
| 68719 | | -** for the Vdbe. |
| 68720 | | -*/ |
| 68721 | | - |
| 68722 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 68723 | | - |
| 68724 | | -/* |
| 68725 | | -** Allocate a new Explain object |
| 68726 | | -*/ |
| 68727 | | -SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){ |
| 68728 | | - if( pVdbe ){ |
| 68729 | | - Explain *p; |
| 68730 | | - sqlite3BeginBenignMalloc(); |
| 68731 | | - p = (Explain *)sqlite3MallocZero( sizeof(Explain) ); |
| 68732 | | - if( p ){ |
| 68733 | | - p->pVdbe = pVdbe; |
| 68734 | | - sqlite3_free(pVdbe->pExplain); |
| 68735 | | - pVdbe->pExplain = p; |
| 68736 | | - sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase), |
| 68737 | | - SQLITE_MAX_LENGTH); |
| 68738 | | - p->str.useMalloc = 2; |
| 68739 | | - }else{ |
| 68740 | | - sqlite3EndBenignMalloc(); |
| 68741 | | - } |
| 68742 | | - } |
| 68743 | | -} |
| 68744 | | - |
| 68745 | | -/* |
| 68746 | | -** Return true if the Explain ends with a new-line. |
| 68747 | | -*/ |
| 68748 | | -static int endsWithNL(Explain *p){ |
| 68749 | | - return p && p->str.zText && p->str.nChar |
| 68750 | | - && p->str.zText[p->str.nChar-1]=='\n'; |
| 68751 | | -} |
| 68752 | | - |
| 68753 | | -/* |
| 68754 | | -** Append text to the indentation |
| 68755 | | -*/ |
| 68756 | | -SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){ |
| 68757 | | - Explain *p; |
| 68758 | | - if( pVdbe && (p = pVdbe->pExplain)!=0 ){ |
| 68759 | | - va_list ap; |
| 68760 | | - if( p->nIndent && endsWithNL(p) ){ |
| 68761 | | - int n = p->nIndent; |
| 68762 | | - if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent); |
| 68763 | | - sqlite3AppendSpace(&p->str, p->aIndent[n-1]); |
| 68764 | | - } |
| 68765 | | - va_start(ap, zFormat); |
| 68766 | | - sqlite3VXPrintf(&p->str, SQLITE_PRINTF_INTERNAL, zFormat, ap); |
| 68767 | | - va_end(ap); |
| 68768 | | - } |
| 68769 | | -} |
| 68770 | | - |
| 68771 | | -/* |
| 68772 | | -** Append a '\n' if there is not already one. |
| 68773 | | -*/ |
| 68774 | | -SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){ |
| 68775 | | - Explain *p; |
| 68776 | | - if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){ |
| 68777 | | - sqlite3StrAccumAppend(&p->str, "\n", 1); |
| 68778 | | - } |
| 68779 | | -} |
| 68780 | | - |
| 68781 | | -/* |
| 68782 | | -** Push a new indentation level. Subsequent lines will be indented |
| 68783 | | -** so that they begin at the current cursor position. |
| 68784 | | -*/ |
| 68785 | | -SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){ |
| 68786 | | - Explain *p; |
| 68787 | | - if( pVdbe && (p = pVdbe->pExplain)!=0 ){ |
| 68788 | | - if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){ |
| 68789 | | - const char *z = p->str.zText; |
| 68790 | | - int i = p->str.nChar-1; |
| 68791 | | - int x; |
| 68792 | | - while( i>=0 && z[i]!='\n' ){ i--; } |
| 68793 | | - x = (p->str.nChar - 1) - i; |
| 68794 | | - if( p->nIndent && x<p->aIndent[p->nIndent-1] ){ |
| 68795 | | - x = p->aIndent[p->nIndent-1]; |
| 68796 | | - } |
| 68797 | | - p->aIndent[p->nIndent] = x; |
| 68798 | | - } |
| 68799 | | - p->nIndent++; |
| 68800 | | - } |
| 68801 | | -} |
| 68802 | | - |
| 68803 | | -/* |
| 68804 | | -** Pop the indentation stack by one level. |
| 68805 | | -*/ |
| 68806 | | -SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){ |
| 68807 | | - if( p && p->pExplain ) p->pExplain->nIndent--; |
| 68808 | | -} |
| 68809 | | - |
| 68810 | | -/* |
| 68811 | | -** Free the indentation structure |
| 68812 | | -*/ |
| 68813 | | -SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){ |
| 68814 | | - if( pVdbe && pVdbe->pExplain ){ |
| 68815 | | - sqlite3_free(pVdbe->zExplain); |
| 68816 | | - sqlite3ExplainNL(pVdbe); |
| 68817 | | - pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str); |
| 68818 | | - sqlite3_free(pVdbe->pExplain); |
| 68819 | | - pVdbe->pExplain = 0; |
| 68820 | | - sqlite3EndBenignMalloc(); |
| 68821 | | - } |
| 68822 | | -} |
| 68823 | | - |
| 68824 | | -/* |
| 68825 | | -** Return the explanation of a virtual machine. |
| 68826 | | -*/ |
| 68827 | | -SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){ |
| 68828 | | - return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0; |
| 68829 | | -} |
| 68830 | | -#endif /* defined(SQLITE_DEBUG) */ |
| 68831 | | - |
| 68832 | 68914 | /************** End of vdbetrace.c *******************************************/ |
| 68833 | 68915 | /************** Begin file vdbe.c ********************************************/ |
| 68834 | 68916 | /* |
| 68835 | 68917 | ** 2001 September 15 |
| 68836 | 68918 | ** |
| | @@ -69043,10 +69125,11 @@ |
| 69043 | 69125 | if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ |
| 69044 | 69126 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; |
| 69045 | 69127 | memset(pCx, 0, sizeof(VdbeCursor)); |
| 69046 | 69128 | pCx->iDb = iDb; |
| 69047 | 69129 | pCx->nField = nField; |
| 69130 | + pCx->aOffset = &pCx->aType[nField]; |
| 69048 | 69131 | if( isBtreeCursor ){ |
| 69049 | 69132 | pCx->pCursor = (BtCursor*) |
| 69050 | 69133 | &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; |
| 69051 | 69134 | sqlite3BtreeCursorZero(pCx->pCursor); |
| 69052 | 69135 | } |
| | @@ -70475,21 +70558,14 @@ |
| 70475 | 70558 | assert( pOp->p4type==P4_FUNCDEF ); |
| 70476 | 70559 | ctx.pFunc = pOp->p4.pFunc; |
| 70477 | 70560 | ctx.iOp = pc; |
| 70478 | 70561 | ctx.pVdbe = p; |
| 70479 | 70562 | MemSetTypeFlag(ctx.pOut, MEM_Null); |
| 70480 | | - |
| 70481 | 70563 | ctx.fErrorOrAux = 0; |
| 70482 | | - if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
| 70483 | | - assert( pOp>aOp ); |
| 70484 | | - assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 70485 | | - assert( pOp[-1].opcode==OP_CollSeq ); |
| 70486 | | - ctx.pColl = pOp[-1].p4.pColl; |
| 70487 | | - } |
| 70488 | 70564 | db->lastRowid = lastRowid; |
| 70489 | 70565 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
| 70490 | | - lastRowid = db->lastRowid; |
| 70566 | + lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */ |
| 70491 | 70567 | |
| 70492 | 70568 | /* If the function returned an error, throw an exception */ |
| 70493 | 70569 | if( ctx.fErrorOrAux ){ |
| 70494 | 70570 | if( ctx.isError ){ |
| 70495 | 70571 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut)); |
| | @@ -71201,11 +71277,11 @@ |
| 71201 | 71277 | memAboutToChange(p, pDest); |
| 71202 | 71278 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 71203 | 71279 | pC = p->apCsr[pOp->p1]; |
| 71204 | 71280 | assert( pC!=0 ); |
| 71205 | 71281 | assert( p2<pC->nField ); |
| 71206 | | - aOffset = pC->aType + pC->nField; |
| 71282 | + aOffset = pC->aOffset; |
| 71207 | 71283 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 71208 | 71284 | assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */ |
| 71209 | 71285 | #endif |
| 71210 | 71286 | pCrsr = pC->pCursor; |
| 71211 | 71287 | assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */ |
| | @@ -71212,11 +71288,11 @@ |
| 71212 | 71288 | assert( pCrsr!=0 || pC->nullRow ); /* pC->nullRow on PseudoTables */ |
| 71213 | 71289 | |
| 71214 | 71290 | /* If the cursor cache is stale, bring it up-to-date */ |
| 71215 | 71291 | rc = sqlite3VdbeCursorMoveto(pC); |
| 71216 | 71292 | if( rc ) goto abort_due_to_error; |
| 71217 | | - if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){ |
| 71293 | + if( pC->cacheStatus!=p->cacheCtr ){ |
| 71218 | 71294 | if( pC->nullRow ){ |
| 71219 | 71295 | if( pCrsr==0 ){ |
| 71220 | 71296 | assert( pC->pseudoTableReg>0 ); |
| 71221 | 71297 | pReg = &aMem[pC->pseudoTableReg]; |
| 71222 | 71298 | assert( pReg->flags & MEM_Blob ); |
| | @@ -71257,18 +71333,10 @@ |
| 71257 | 71333 | } |
| 71258 | 71334 | pC->cacheStatus = p->cacheCtr; |
| 71259 | 71335 | pC->iHdrOffset = getVarint32(pC->aRow, offset); |
| 71260 | 71336 | pC->nHdrParsed = 0; |
| 71261 | 71337 | aOffset[0] = offset; |
| 71262 | | - if( avail<offset ){ |
| 71263 | | - /* pC->aRow does not have to hold the entire row, but it does at least |
| 71264 | | - ** need to cover the header of the record. If pC->aRow does not contain |
| 71265 | | - ** the complete header, then set it to zero, forcing the header to be |
| 71266 | | - ** dynamically allocated. */ |
| 71267 | | - pC->aRow = 0; |
| 71268 | | - pC->szRow = 0; |
| 71269 | | - } |
| 71270 | 71338 | |
| 71271 | 71339 | /* Make sure a corrupt database has not given us an oversize header. |
| 71272 | 71340 | ** Do this now to avoid an oversize memory allocation. |
| 71273 | 71341 | ** |
| 71274 | 71342 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| | @@ -71279,19 +71347,36 @@ |
| 71279 | 71347 | */ |
| 71280 | 71348 | if( offset > 98307 || offset > pC->payloadSize ){ |
| 71281 | 71349 | rc = SQLITE_CORRUPT_BKPT; |
| 71282 | 71350 | goto op_column_error; |
| 71283 | 71351 | } |
| 71352 | + |
| 71353 | + if( avail<offset ){ |
| 71354 | + /* pC->aRow does not have to hold the entire row, but it does at least |
| 71355 | + ** need to cover the header of the record. If pC->aRow does not contain |
| 71356 | + ** the complete header, then set it to zero, forcing the header to be |
| 71357 | + ** dynamically allocated. */ |
| 71358 | + pC->aRow = 0; |
| 71359 | + pC->szRow = 0; |
| 71360 | + } |
| 71361 | + |
| 71362 | + /* The following goto is an optimization. It can be omitted and |
| 71363 | + ** everything will still work. But OP_Column is measurably faster |
| 71364 | + ** by skipping the subsequent conditional, which is always true. |
| 71365 | + */ |
| 71366 | + assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ |
| 71367 | + goto op_column_read_header; |
| 71284 | 71368 | } |
| 71285 | 71369 | |
| 71286 | 71370 | /* Make sure at least the first p2+1 entries of the header have been |
| 71287 | 71371 | ** parsed and valid information is in aOffset[] and pC->aType[]. |
| 71288 | 71372 | */ |
| 71289 | 71373 | if( pC->nHdrParsed<=p2 ){ |
| 71290 | 71374 | /* If there is more header available for parsing in the record, try |
| 71291 | 71375 | ** to extract additional fields up through the p2+1-th field |
| 71292 | 71376 | */ |
| 71377 | + op_column_read_header: |
| 71293 | 71378 | if( pC->iHdrOffset<aOffset[0] ){ |
| 71294 | 71379 | /* Make sure zData points to enough of the record to cover the header. */ |
| 71295 | 71380 | if( pC->aRow==0 ){ |
| 71296 | 71381 | memset(&sMem, 0, sizeof(sMem)); |
| 71297 | 71382 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], |
| | @@ -71332,19 +71417,20 @@ |
| 71332 | 71417 | if( pC->aRow==0 ){ |
| 71333 | 71418 | sqlite3VdbeMemRelease(&sMem); |
| 71334 | 71419 | sMem.flags = MEM_Null; |
| 71335 | 71420 | } |
| 71336 | 71421 | |
| 71337 | | - /* If we have read more header data than was contained in the header, |
| 71338 | | - ** or if the end of the last field appears to be past the end of the |
| 71339 | | - ** record, or if the end of the last field appears to be before the end |
| 71340 | | - ** of the record (when all fields present), then we must be dealing |
| 71341 | | - ** with a corrupt database. |
| 71422 | + /* The record is corrupt if any of the following are true: |
| 71423 | + ** (1) the bytes of the header extend past the declared header size |
| 71424 | + ** (zHdr>zEndHdr) |
| 71425 | + ** (2) the entire header was used but not all data was used |
| 71426 | + ** (zHdr==zEndHdr && offset!=pC->payloadSize) |
| 71427 | + ** (3) the end of the data extends beyond the end of the record. |
| 71428 | + ** (offset > pC->payloadSize) |
| 71342 | 71429 | */ |
| 71343 | | - if( (zHdr > zEndHdr) |
| 71430 | + if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset!=pC->payloadSize)) |
| 71344 | 71431 | || (offset > pC->payloadSize) |
| 71345 | | - || (zHdr==zEndHdr && offset!=pC->payloadSize) |
| 71346 | 71432 | ){ |
| 71347 | 71433 | rc = SQLITE_CORRUPT_BKPT; |
| 71348 | 71434 | goto op_column_error; |
| 71349 | 71435 | } |
| 71350 | 71436 | } |
| | @@ -71531,11 +71617,11 @@ |
| 71531 | 71617 | ** out how much space is required for the new record. |
| 71532 | 71618 | */ |
| 71533 | 71619 | pRec = pLast; |
| 71534 | 71620 | do{ |
| 71535 | 71621 | assert( memIsValid(pRec) ); |
| 71536 | | - serial_type = sqlite3VdbeSerialType(pRec, file_format); |
| 71622 | + pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format); |
| 71537 | 71623 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 71538 | 71624 | if( pRec->flags & MEM_Zero ){ |
| 71539 | 71625 | if( nData ){ |
| 71540 | 71626 | sqlite3VdbeMemExpandBlob(pRec); |
| 71541 | 71627 | }else{ |
| | @@ -71580,11 +71666,11 @@ |
| 71580 | 71666 | i = putVarint32(zNewRecord, nHdr); |
| 71581 | 71667 | j = nHdr; |
| 71582 | 71668 | assert( pData0<=pLast ); |
| 71583 | 71669 | pRec = pData0; |
| 71584 | 71670 | do{ |
| 71585 | | - serial_type = sqlite3VdbeSerialType(pRec, file_format); |
| 71671 | + serial_type = pRec->uTemp; |
| 71586 | 71672 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
| 71587 | 71673 | j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
| 71588 | 71674 | }while( (++pRec)<=pLast ); |
| 71589 | 71675 | assert( i==nHdr ); |
| 71590 | 71676 | assert( j==nByte ); |
| | @@ -72205,14 +72291,10 @@ |
| 72205 | 72291 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor); |
| 72206 | 72292 | pCur->pKeyInfo = pKeyInfo; |
| 72207 | 72293 | assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 72208 | 72294 | sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR)); |
| 72209 | 72295 | |
| 72210 | | - /* Since it performs no memory allocation or IO, the only value that |
| 72211 | | - ** sqlite3BtreeCursor() may return is SQLITE_OK. */ |
| 72212 | | - assert( rc==SQLITE_OK ); |
| 72213 | | - |
| 72214 | 72296 | /* Set the VdbeCursor.isTable variable. Previous versions of |
| 72215 | 72297 | ** SQLite used to check if the root-page flags were sane at this point |
| 72216 | 72298 | ** and report database corruption if they were not, but this check has |
| 72217 | 72299 | ** since moved into the btree layer. */ |
| 72218 | 72300 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
| | @@ -72483,11 +72565,10 @@ |
| 72483 | 72565 | pIn3 = &aMem[pOp->p3]; |
| 72484 | 72566 | if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
| 72485 | 72567 | applyNumericAffinity(pIn3, 0); |
| 72486 | 72568 | } |
| 72487 | 72569 | iKey = sqlite3VdbeIntValue(pIn3); |
| 72488 | | - pC->rowidIsValid = 0; |
| 72489 | 72570 | |
| 72490 | 72571 | /* If the P3 value could not be converted into an integer without |
| 72491 | 72572 | ** loss of information, then special processing is required... */ |
| 72492 | 72573 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 72493 | 72574 | if( (pIn3->flags & MEM_Real)==0 ){ |
| | @@ -72519,17 +72600,14 @@ |
| 72519 | 72600 | assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); |
| 72520 | 72601 | if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; |
| 72521 | 72602 | } |
| 72522 | 72603 | } |
| 72523 | 72604 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res); |
| 72605 | + pC->movetoTarget = iKey; /* Used by OP_Delete */ |
| 72524 | 72606 | if( rc!=SQLITE_OK ){ |
| 72525 | 72607 | goto abort_due_to_error; |
| 72526 | 72608 | } |
| 72527 | | - if( res==0 ){ |
| 72528 | | - pC->rowidIsValid = 1; |
| 72529 | | - pC->lastRowid = iKey; |
| 72530 | | - } |
| 72531 | 72609 | }else{ |
| 72532 | 72610 | nField = pOp->p4.i; |
| 72533 | 72611 | assert( pOp->p4type==P4_INT32 ); |
| 72534 | 72612 | assert( nField>0 ); |
| 72535 | 72613 | r.pKeyInfo = pC->pKeyInfo; |
| | @@ -72555,11 +72633,10 @@ |
| 72555 | 72633 | ExpandBlob(r.aMem); |
| 72556 | 72634 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res); |
| 72557 | 72635 | if( rc!=SQLITE_OK ){ |
| 72558 | 72636 | goto abort_due_to_error; |
| 72559 | 72637 | } |
| 72560 | | - pC->rowidIsValid = 0; |
| 72561 | 72638 | } |
| 72562 | 72639 | pC->deferredMoveto = 0; |
| 72563 | 72640 | pC->cacheStatus = CACHE_STALE; |
| 72564 | 72641 | #ifdef SQLITE_TEST |
| 72565 | 72642 | sqlite3_search_count++; |
| | @@ -72567,21 +72644,19 @@ |
| 72567 | 72644 | if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); |
| 72568 | 72645 | if( res<0 || (res==0 && oc==OP_SeekGT) ){ |
| 72569 | 72646 | res = 0; |
| 72570 | 72647 | rc = sqlite3BtreeNext(pC->pCursor, &res); |
| 72571 | 72648 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 72572 | | - pC->rowidIsValid = 0; |
| 72573 | 72649 | }else{ |
| 72574 | 72650 | res = 0; |
| 72575 | 72651 | } |
| 72576 | 72652 | }else{ |
| 72577 | 72653 | assert( oc==OP_SeekLT || oc==OP_SeekLE ); |
| 72578 | 72654 | if( res>0 || (res==0 && oc==OP_SeekLT) ){ |
| 72579 | 72655 | res = 0; |
| 72580 | 72656 | rc = sqlite3BtreePrevious(pC->pCursor, &res); |
| 72581 | 72657 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 72582 | | - pC->rowidIsValid = 0; |
| 72583 | 72658 | }else{ |
| 72584 | 72659 | /* res might be negative because the table is empty. Check to |
| 72585 | 72660 | ** see if this is the case. |
| 72586 | 72661 | */ |
| 72587 | 72662 | res = sqlite3BtreeEof(pC->pCursor); |
| | @@ -72614,11 +72689,10 @@ |
| 72614 | 72689 | assert( pC->pCursor!=0 ); |
| 72615 | 72690 | assert( pC->isTable ); |
| 72616 | 72691 | pC->nullRow = 0; |
| 72617 | 72692 | pIn2 = &aMem[pOp->p2]; |
| 72618 | 72693 | pC->movetoTarget = sqlite3VdbeIntValue(pIn2); |
| 72619 | | - pC->rowidIsValid = 0; |
| 72620 | 72694 | pC->deferredMoveto = 1; |
| 72621 | 72695 | break; |
| 72622 | 72696 | } |
| 72623 | 72697 | |
| 72624 | 72698 | |
| | @@ -72800,19 +72874,17 @@ |
| 72800 | 72874 | pCrsr = pC->pCursor; |
| 72801 | 72875 | assert( pCrsr!=0 ); |
| 72802 | 72876 | res = 0; |
| 72803 | 72877 | iKey = pIn3->u.i; |
| 72804 | 72878 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); |
| 72805 | | - pC->lastRowid = pIn3->u.i; |
| 72806 | | - pC->rowidIsValid = res==0 ?1:0; |
| 72879 | + pC->movetoTarget = iKey; /* Used by OP_Delete */ |
| 72807 | 72880 | pC->nullRow = 0; |
| 72808 | 72881 | pC->cacheStatus = CACHE_STALE; |
| 72809 | 72882 | pC->deferredMoveto = 0; |
| 72810 | 72883 | VdbeBranchTaken(res!=0,2); |
| 72811 | 72884 | if( res!=0 ){ |
| 72812 | 72885 | pc = pOp->p2 - 1; |
| 72813 | | - assert( pC->rowidIsValid==0 ); |
| 72814 | 72886 | } |
| 72815 | 72887 | pC->seekResult = res; |
| 72816 | 72888 | break; |
| 72817 | 72889 | } |
| 72818 | 72890 | |
| | @@ -72942,36 +73014,24 @@ |
| 72942 | 73014 | ** largest possible integer (9223372036854775807) then the database |
| 72943 | 73015 | ** engine starts picking positive candidate ROWIDs at random until |
| 72944 | 73016 | ** it finds one that is not previously used. */ |
| 72945 | 73017 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 72946 | 73018 | ** an AUTOINCREMENT table. */ |
| 72947 | | - /* on the first attempt, simply do one more than previous */ |
| 72948 | | - v = lastRowid; |
| 72949 | | - v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 72950 | | - v++; /* ensure non-zero */ |
| 72951 | 73019 | cnt = 0; |
| 72952 | | - while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v, |
| 73020 | + do{ |
| 73021 | + sqlite3_randomness(sizeof(v), &v); |
| 73022 | + v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ |
| 73023 | + }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v, |
| 72953 | 73024 | 0, &res))==SQLITE_OK) |
| 72954 | 73025 | && (res==0) |
| 72955 | | - && (++cnt<100)){ |
| 72956 | | - /* collision - try another random rowid */ |
| 72957 | | - sqlite3_randomness(sizeof(v), &v); |
| 72958 | | - if( cnt<5 ){ |
| 72959 | | - /* try "small" random rowids for the initial attempts */ |
| 72960 | | - v &= 0xffffff; |
| 72961 | | - }else{ |
| 72962 | | - v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 72963 | | - } |
| 72964 | | - v++; /* ensure non-zero */ |
| 72965 | | - } |
| 73026 | + && (++cnt<100)); |
| 72966 | 73027 | if( rc==SQLITE_OK && res==0 ){ |
| 72967 | 73028 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
| 72968 | 73029 | goto abort_due_to_error; |
| 72969 | 73030 | } |
| 72970 | 73031 | assert( v>0 ); /* EV: R-40812-03570 */ |
| 72971 | 73032 | } |
| 72972 | | - pC->rowidIsValid = 0; |
| 72973 | 73033 | pC->deferredMoveto = 0; |
| 72974 | 73034 | pC->cacheStatus = CACHE_STALE; |
| 72975 | 73035 | } |
| 72976 | 73036 | pOut->u.i = v; |
| 72977 | 73037 | break; |
| | @@ -73072,11 +73132,10 @@ |
| 73072 | 73132 | } |
| 73073 | 73133 | rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, |
| 73074 | 73134 | pData->z, pData->n, nZero, |
| 73075 | 73135 | (pOp->p5 & OPFLAG_APPEND)!=0, seekResult |
| 73076 | 73136 | ); |
| 73077 | | - pC->rowidIsValid = 0; |
| 73078 | 73137 | pC->deferredMoveto = 0; |
| 73079 | 73138 | pC->cacheStatus = CACHE_STALE; |
| 73080 | 73139 | |
| 73081 | 73140 | /* Invoke the update-hook if required. */ |
| 73082 | 73141 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| | @@ -73109,37 +73168,36 @@ |
| 73109 | 73168 | ** pointing to. The update hook will be invoked, if it exists. |
| 73110 | 73169 | ** If P4 is not NULL then the P1 cursor must have been positioned |
| 73111 | 73170 | ** using OP_NotFound prior to invoking this opcode. |
| 73112 | 73171 | */ |
| 73113 | 73172 | case OP_Delete: { |
| 73114 | | - i64 iKey; |
| 73115 | 73173 | VdbeCursor *pC; |
| 73116 | 73174 | |
| 73117 | 73175 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 73118 | 73176 | pC = p->apCsr[pOp->p1]; |
| 73119 | 73177 | assert( pC!=0 ); |
| 73120 | 73178 | assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ |
| 73121 | | - iKey = pC->lastRowid; /* Only used for the update hook */ |
| 73122 | | - |
| 73123 | | - /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or |
| 73124 | | - ** OP_Column on the same table without any intervening operations that |
| 73125 | | - ** might move or invalidate the cursor. Hence cursor pC is always pointing |
| 73126 | | - ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation |
| 73127 | | - ** below is always a no-op and cannot fail. We will run it anyhow, though, |
| 73128 | | - ** to guard against future changes to the code generator. |
| 73129 | | - **/ |
| 73130 | 73179 | assert( pC->deferredMoveto==0 ); |
| 73131 | | - rc = sqlite3VdbeCursorMoveto(pC); |
| 73132 | | - if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 73133 | 73180 | |
| 73181 | +#ifdef SQLITE_DEBUG |
| 73182 | + /* The seek operation that positioned the cursor prior to OP_Delete will |
| 73183 | + ** have also set the pC->movetoTarget field to the rowid of the row that |
| 73184 | + ** is being deleted */ |
| 73185 | + if( pOp->p4.z && pC->isTable ){ |
| 73186 | + i64 iKey = 0; |
| 73187 | + sqlite3BtreeKeySize(pC->pCursor, &iKey); |
| 73188 | + assert( pC->movetoTarget==iKey ); |
| 73189 | + } |
| 73190 | +#endif |
| 73191 | + |
| 73134 | 73192 | rc = sqlite3BtreeDelete(pC->pCursor); |
| 73135 | 73193 | pC->cacheStatus = CACHE_STALE; |
| 73136 | 73194 | |
| 73137 | 73195 | /* Invoke the update-hook if required. */ |
| 73138 | 73196 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){ |
| 73139 | 73197 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, |
| 73140 | | - db->aDb[pC->iDb].zName, pOp->p4.z, iKey); |
| 73198 | + db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget); |
| 73141 | 73199 | assert( pC->iDb>=0 ); |
| 73142 | 73200 | } |
| 73143 | 73201 | if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; |
| 73144 | 73202 | break; |
| 73145 | 73203 | } |
| | @@ -73188,23 +73246,32 @@ |
| 73188 | 73246 | pc = pOp->p2-1; |
| 73189 | 73247 | } |
| 73190 | 73248 | break; |
| 73191 | 73249 | }; |
| 73192 | 73250 | |
| 73193 | | -/* Opcode: SorterData P1 P2 * * * |
| 73251 | +/* Opcode: SorterData P1 P2 P3 * * |
| 73194 | 73252 | ** Synopsis: r[P2]=data |
| 73195 | 73253 | ** |
| 73196 | 73254 | ** Write into register P2 the current sorter data for sorter cursor P1. |
| 73255 | +** Then clear the column header cache on cursor P3. |
| 73256 | +** |
| 73257 | +** This opcode is normally use to move a record out of the sorter and into |
| 73258 | +** a register that is the source for a pseudo-table cursor created using |
| 73259 | +** OpenPseudo. That pseudo-table cursor is the one that is identified by |
| 73260 | +** parameter P3. Clearing the P3 column cache as part of this opcode saves |
| 73261 | +** us from having to issue a separate NullRow instruction to clear that cache. |
| 73197 | 73262 | */ |
| 73198 | 73263 | case OP_SorterData: { |
| 73199 | 73264 | VdbeCursor *pC; |
| 73200 | 73265 | |
| 73201 | 73266 | pOut = &aMem[pOp->p2]; |
| 73202 | 73267 | pC = p->apCsr[pOp->p1]; |
| 73203 | 73268 | assert( isSorter(pC) ); |
| 73204 | 73269 | rc = sqlite3VdbeSorterRowkey(pC, pOut); |
| 73205 | 73270 | assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); |
| 73271 | + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 73272 | + p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; |
| 73206 | 73273 | break; |
| 73207 | 73274 | } |
| 73208 | 73275 | |
| 73209 | 73276 | /* Opcode: RowData P1 P2 * * * |
| 73210 | 73277 | ** Synopsis: r[P2]=data |
| | @@ -73247,20 +73314,24 @@ |
| 73247 | 73314 | assert( pC!=0 ); |
| 73248 | 73315 | assert( pC->nullRow==0 ); |
| 73249 | 73316 | assert( pC->pseudoTableReg==0 ); |
| 73250 | 73317 | assert( pC->pCursor!=0 ); |
| 73251 | 73318 | pCrsr = pC->pCursor; |
| 73252 | | - assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 73253 | 73319 | |
| 73254 | 73320 | /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or |
| 73255 | 73321 | ** OP_Rewind/Op_Next with no intervening instructions that might invalidate |
| 73256 | | - ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always |
| 73257 | | - ** a no-op and can never fail. But we leave it in place as a safety. |
| 73322 | + ** the cursor. If this where not the case, on of the following assert()s |
| 73323 | + ** would fail. Should this ever change (because of changes in the code |
| 73324 | + ** generator) then the fix would be to insert a call to |
| 73325 | + ** sqlite3VdbeCursorMoveto(). |
| 73258 | 73326 | */ |
| 73259 | 73327 | assert( pC->deferredMoveto==0 ); |
| 73328 | + assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 73329 | +#if 0 /* Not required due to the previous to assert() statements */ |
| 73260 | 73330 | rc = sqlite3VdbeCursorMoveto(pC); |
| 73261 | | - if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 73331 | + if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 73332 | +#endif |
| 73262 | 73333 | |
| 73263 | 73334 | if( pC->isTable==0 ){ |
| 73264 | 73335 | assert( !pC->isTable ); |
| 73265 | 73336 | VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64); |
| 73266 | 73337 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
| | @@ -73273,11 +73344,12 @@ |
| 73273 | 73344 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 73274 | 73345 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 73275 | 73346 | goto too_big; |
| 73276 | 73347 | } |
| 73277 | 73348 | } |
| 73278 | | - if( sqlite3VdbeMemClearAndResize(pOut, n) ){ |
| 73349 | + testcase( n==0 ); |
| 73350 | + if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){ |
| 73279 | 73351 | goto no_mem; |
| 73280 | 73352 | } |
| 73281 | 73353 | pOut->n = n; |
| 73282 | 73354 | MemSetTypeFlag(pOut, MEM_Blob); |
| 73283 | 73355 | if( pC->isTable==0 ){ |
| | @@ -73324,18 +73396,14 @@ |
| 73324 | 73396 | rc = pModule->xRowid(pC->pVtabCursor, &v); |
| 73325 | 73397 | sqlite3VtabImportErrmsg(p, pVtab); |
| 73326 | 73398 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 73327 | 73399 | }else{ |
| 73328 | 73400 | assert( pC->pCursor!=0 ); |
| 73329 | | - rc = sqlite3VdbeCursorMoveto(pC); |
| 73401 | + rc = sqlite3VdbeCursorRestore(pC); |
| 73330 | 73402 | if( rc ) goto abort_due_to_error; |
| 73331 | | - if( pC->rowidIsValid ){ |
| 73332 | | - v = pC->lastRowid; |
| 73333 | | - }else{ |
| 73334 | | - rc = sqlite3BtreeKeySize(pC->pCursor, &v); |
| 73335 | | - assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */ |
| 73336 | | - } |
| 73403 | + rc = sqlite3BtreeKeySize(pC->pCursor, &v); |
| 73404 | + assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */ |
| 73337 | 73405 | } |
| 73338 | 73406 | pOut->u.i = v; |
| 73339 | 73407 | break; |
| 73340 | 73408 | } |
| 73341 | 73409 | |
| | @@ -73350,11 +73418,10 @@ |
| 73350 | 73418 | |
| 73351 | 73419 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 73352 | 73420 | pC = p->apCsr[pOp->p1]; |
| 73353 | 73421 | assert( pC!=0 ); |
| 73354 | 73422 | pC->nullRow = 1; |
| 73355 | | - pC->rowidIsValid = 0; |
| 73356 | 73423 | pC->cacheStatus = CACHE_STALE; |
| 73357 | 73424 | if( pC->pCursor ){ |
| 73358 | 73425 | sqlite3BtreeClearCursor(pC->pCursor); |
| 73359 | 73426 | } |
| 73360 | 73427 | break; |
| | @@ -73384,11 +73451,10 @@ |
| 73384 | 73451 | res = 0; |
| 73385 | 73452 | assert( pCrsr!=0 ); |
| 73386 | 73453 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 73387 | 73454 | pC->nullRow = (u8)res; |
| 73388 | 73455 | pC->deferredMoveto = 0; |
| 73389 | | - pC->rowidIsValid = 0; |
| 73390 | 73456 | pC->cacheStatus = CACHE_STALE; |
| 73391 | 73457 | #ifdef SQLITE_DEBUG |
| 73392 | 73458 | pC->seekOp = OP_Last; |
| 73393 | 73459 | #endif |
| 73394 | 73460 | if( pOp->p2>0 ){ |
| | @@ -73451,11 +73517,10 @@ |
| 73451 | 73517 | pCrsr = pC->pCursor; |
| 73452 | 73518 | assert( pCrsr ); |
| 73453 | 73519 | rc = sqlite3BtreeFirst(pCrsr, &res); |
| 73454 | 73520 | pC->deferredMoveto = 0; |
| 73455 | 73521 | pC->cacheStatus = CACHE_STALE; |
| 73456 | | - pC->rowidIsValid = 0; |
| 73457 | 73522 | } |
| 73458 | 73523 | pC->nullRow = (u8)res; |
| 73459 | 73524 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
| 73460 | 73525 | VdbeBranchTaken(res!=0,2); |
| 73461 | 73526 | if( res ){ |
| | @@ -73577,11 +73642,10 @@ |
| 73577 | 73642 | sqlite3_search_count++; |
| 73578 | 73643 | #endif |
| 73579 | 73644 | }else{ |
| 73580 | 73645 | pC->nullRow = 1; |
| 73581 | 73646 | } |
| 73582 | | - pC->rowidIsValid = 0; |
| 73583 | 73647 | goto check_for_interrupt; |
| 73584 | 73648 | } |
| 73585 | 73649 | |
| 73586 | 73650 | /* Opcode: IdxInsert P1 P2 P3 * P5 |
| 73587 | 73651 | ** Synopsis: key=r[P2] |
| | @@ -73693,14 +73757,20 @@ |
| 73693 | 73757 | pC = p->apCsr[pOp->p1]; |
| 73694 | 73758 | assert( pC!=0 ); |
| 73695 | 73759 | pCrsr = pC->pCursor; |
| 73696 | 73760 | assert( pCrsr!=0 ); |
| 73697 | 73761 | pOut->flags = MEM_Null; |
| 73698 | | - rc = sqlite3VdbeCursorMoveto(pC); |
| 73699 | | - if( NEVER(rc) ) goto abort_due_to_error; |
| 73762 | + assert( pC->isTable==0 ); |
| 73700 | 73763 | assert( pC->deferredMoveto==0 ); |
| 73701 | | - assert( pC->isTable==0 ); |
| 73764 | + |
| 73765 | + /* sqlite3VbeCursorRestore() can only fail if the record has been deleted |
| 73766 | + ** out from under the cursor. That will never happend for an IdxRowid |
| 73767 | + ** opcode, hence the NEVER() arround the check of the return value. |
| 73768 | + */ |
| 73769 | + rc = sqlite3VdbeCursorRestore(pC); |
| 73770 | + if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 73771 | + |
| 73702 | 73772 | if( !pC->nullRow ){ |
| 73703 | 73773 | rowid = 0; /* Not needed. Only used to silence a warning. */ |
| 73704 | 73774 | rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid); |
| 73705 | 73775 | if( rc!=SQLITE_OK ){ |
| 73706 | 73776 | goto abort_due_to_error; |
| | @@ -74556,18 +74626,13 @@ |
| 74556 | 74626 | ctx.pMem = pMem = &aMem[pOp->p3]; |
| 74557 | 74627 | pMem->n++; |
| 74558 | 74628 | sqlite3VdbeMemInit(&t, db, MEM_Null); |
| 74559 | 74629 | ctx.pOut = &t; |
| 74560 | 74630 | ctx.isError = 0; |
| 74561 | | - ctx.pColl = 0; |
| 74631 | + ctx.pVdbe = p; |
| 74632 | + ctx.iOp = pc; |
| 74562 | 74633 | ctx.skipFlag = 0; |
| 74563 | | - if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
| 74564 | | - assert( pOp>p->aOp ); |
| 74565 | | - assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 74566 | | - assert( pOp[-1].opcode==OP_CollSeq ); |
| 74567 | | - ctx.pColl = pOp[-1].p4.pColl; |
| 74568 | | - } |
| 74569 | 74634 | (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
| 74570 | 74635 | if( ctx.isError ){ |
| 74571 | 74636 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t)); |
| 74572 | 74637 | rc = ctx.isError; |
| 74573 | 74638 | } |
| | @@ -78162,11 +78227,11 @@ |
| 78162 | 78227 | if( rc==SQLITE_OK ){ |
| 78163 | 78228 | #if SQLITE_MAX_WORKER_THREADS |
| 78164 | 78229 | assert( pSorter->bUseThreads==0 || pSorter->nTask>1 ); |
| 78165 | 78230 | if( pSorter->bUseThreads ){ |
| 78166 | 78231 | int iTask; |
| 78167 | | - PmaReader *pReadr; |
| 78232 | + PmaReader *pReadr = 0; |
| 78168 | 78233 | SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1]; |
| 78169 | 78234 | rc = vdbeSortAllocUnpacked(pLast); |
| 78170 | 78235 | if( rc==SQLITE_OK ){ |
| 78171 | 78236 | pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader)); |
| 78172 | 78237 | pSorter->pReader = pReadr; |
| | @@ -81597,10 +81662,11 @@ |
| 81597 | 81662 | pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; |
| 81598 | 81663 | pNew->addrOpenEphm[0] = -1; |
| 81599 | 81664 | pNew->addrOpenEphm[1] = -1; |
| 81600 | 81665 | pNew->nSelectRow = p->nSelectRow; |
| 81601 | 81666 | pNew->pWith = withDup(db, p->pWith); |
| 81667 | + sqlite3SelectSetName(pNew, p->zSelName); |
| 81602 | 81668 | return pNew; |
| 81603 | 81669 | } |
| 81604 | 81670 | #else |
| 81605 | 81671 | SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ |
| 81606 | 81672 | assert( p==0 ); |
| | @@ -81739,36 +81805,44 @@ |
| 81739 | 81805 | } |
| 81740 | 81806 | |
| 81741 | 81807 | /* |
| 81742 | 81808 | ** These routines are Walker callbacks. Walker.u.pi is a pointer |
| 81743 | 81809 | ** to an integer. These routines are checking an expression to see |
| 81744 | | -** if it is a constant. Set *Walker.u.pi to 0 if the expression is |
| 81810 | +** if it is a constant. Set *Walker.u.i to 0 if the expression is |
| 81745 | 81811 | ** not constant. |
| 81746 | 81812 | ** |
| 81747 | 81813 | ** These callback routines are used to implement the following: |
| 81748 | 81814 | ** |
| 81749 | | -** sqlite3ExprIsConstant() |
| 81750 | | -** sqlite3ExprIsConstantNotJoin() |
| 81751 | | -** sqlite3ExprIsConstantOrFunction() |
| 81815 | +** sqlite3ExprIsConstant() pWalker->u.i==1 |
| 81816 | +** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2 |
| 81817 | +** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4 |
| 81752 | 81818 | ** |
| 81819 | +** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions |
| 81820 | +** in a CREATE TABLE statement. The Walker.u.i value is 4 when parsing |
| 81821 | +** an existing schema and 3 when processing a new statement. A bound |
| 81822 | +** parameter raises an error for new statements, but is silently converted |
| 81823 | +** to NULL for existing schemas. This allows sqlite_master tables that |
| 81824 | +** contain a bound parameter because they were generated by older versions |
| 81825 | +** of SQLite to be parsed by newer versions of SQLite without raising a |
| 81826 | +** malformed schema error. |
| 81753 | 81827 | */ |
| 81754 | 81828 | static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
| 81755 | 81829 | |
| 81756 | | - /* If pWalker->u.i is 3 then any term of the expression that comes from |
| 81830 | + /* If pWalker->u.i is 2 then any term of the expression that comes from |
| 81757 | 81831 | ** the ON or USING clauses of a join disqualifies the expression |
| 81758 | 81832 | ** from being considered constant. */ |
| 81759 | | - if( pWalker->u.i==3 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 81833 | + if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 81760 | 81834 | pWalker->u.i = 0; |
| 81761 | 81835 | return WRC_Abort; |
| 81762 | 81836 | } |
| 81763 | 81837 | |
| 81764 | 81838 | switch( pExpr->op ){ |
| 81765 | 81839 | /* Consider functions to be constant if all their arguments are constant |
| 81766 | | - ** and either pWalker->u.i==2 or the function as the SQLITE_FUNC_CONST |
| 81840 | + ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST |
| 81767 | 81841 | ** flag. */ |
| 81768 | 81842 | case TK_FUNCTION: |
| 81769 | | - if( pWalker->u.i==2 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 81843 | + if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 81770 | 81844 | return WRC_Continue; |
| 81771 | 81845 | } |
| 81772 | 81846 | /* Fall through */ |
| 81773 | 81847 | case TK_ID: |
| 81774 | 81848 | case TK_COLUMN: |
| | @@ -81778,10 +81852,23 @@ |
| 81778 | 81852 | testcase( pExpr->op==TK_COLUMN ); |
| 81779 | 81853 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 81780 | 81854 | testcase( pExpr->op==TK_AGG_COLUMN ); |
| 81781 | 81855 | pWalker->u.i = 0; |
| 81782 | 81856 | return WRC_Abort; |
| 81857 | + case TK_VARIABLE: |
| 81858 | + if( pWalker->u.i==4 ){ |
| 81859 | + /* Silently convert bound parameters that appear inside of CREATE |
| 81860 | + ** statements into a NULL when parsing the CREATE statement text out |
| 81861 | + ** of the sqlite_master table */ |
| 81862 | + pExpr->op = TK_NULL; |
| 81863 | + }else if( pWalker->u.i==3 ){ |
| 81864 | + /* A bound parameter in a CREATE statement that originates from |
| 81865 | + ** sqlite3_prepare() causes an error */ |
| 81866 | + pWalker->u.i = 0; |
| 81867 | + return WRC_Abort; |
| 81868 | + } |
| 81869 | + /* Fall through */ |
| 81783 | 81870 | default: |
| 81784 | 81871 | testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ |
| 81785 | 81872 | testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ |
| 81786 | 81873 | return WRC_Continue; |
| 81787 | 81874 | } |
| | @@ -81818,11 +81905,11 @@ |
| 81818 | 81905 | ** that does no originate from the ON or USING clauses of a join. |
| 81819 | 81906 | ** Return 0 if it involves variables or function calls or terms from |
| 81820 | 81907 | ** an ON or USING clause. |
| 81821 | 81908 | */ |
| 81822 | 81909 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){ |
| 81823 | | - return exprIsConst(p, 3); |
| 81910 | + return exprIsConst(p, 2); |
| 81824 | 81911 | } |
| 81825 | 81912 | |
| 81826 | 81913 | /* |
| 81827 | 81914 | ** Walk an expression tree. Return 1 if the expression is constant |
| 81828 | 81915 | ** or a function call with constant arguments. Return and 0 if there |
| | @@ -81830,12 +81917,13 @@ |
| 81830 | 81917 | ** |
| 81831 | 81918 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 81832 | 81919 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 81833 | 81920 | ** a constant. |
| 81834 | 81921 | */ |
| 81835 | | -SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){ |
| 81836 | | - return exprIsConst(p, 2); |
| 81922 | +SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ |
| 81923 | + assert( isInit==0 || isInit==1 ); |
| 81924 | + return exprIsConst(p, 3+isInit); |
| 81837 | 81925 | } |
| 81838 | 81926 | |
| 81839 | 81927 | /* |
| 81840 | 81928 | ** If the expression p codes a constant integer that is small enough |
| 81841 | 81929 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| | @@ -83741,94 +83829,90 @@ |
| 83741 | 83829 | iMem = ++pParse->nMem; |
| 83742 | 83830 | sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); |
| 83743 | 83831 | exprToRegister(pExpr, iMem); |
| 83744 | 83832 | } |
| 83745 | 83833 | |
| 83746 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 83834 | +#ifdef SQLITE_DEBUG |
| 83747 | 83835 | /* |
| 83748 | 83836 | ** Generate a human-readable explanation of an expression tree. |
| 83749 | 83837 | */ |
| 83750 | | -SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){ |
| 83751 | | - int op; /* The opcode being coded */ |
| 83838 | +SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ |
| 83752 | 83839 | const char *zBinOp = 0; /* Binary operator */ |
| 83753 | 83840 | const char *zUniOp = 0; /* Unary operator */ |
| 83841 | + pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 83754 | 83842 | if( pExpr==0 ){ |
| 83755 | | - op = TK_NULL; |
| 83756 | | - }else{ |
| 83757 | | - op = pExpr->op; |
| 83843 | + sqlite3TreeViewLine(pView, "nil"); |
| 83844 | + sqlite3TreeViewPop(pView); |
| 83845 | + return; |
| 83758 | 83846 | } |
| 83759 | | - switch( op ){ |
| 83847 | + switch( pExpr->op ){ |
| 83760 | 83848 | case TK_AGG_COLUMN: { |
| 83761 | | - sqlite3ExplainPrintf(pOut, "AGG{%d:%d}", |
| 83849 | + sqlite3TreeViewLine(pView, "AGG{%d:%d}", |
| 83762 | 83850 | pExpr->iTable, pExpr->iColumn); |
| 83763 | 83851 | break; |
| 83764 | 83852 | } |
| 83765 | 83853 | case TK_COLUMN: { |
| 83766 | 83854 | if( pExpr->iTable<0 ){ |
| 83767 | 83855 | /* This only happens when coding check constraints */ |
| 83768 | | - sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn); |
| 83856 | + sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn); |
| 83769 | 83857 | }else{ |
| 83770 | | - sqlite3ExplainPrintf(pOut, "{%d:%d}", |
| 83858 | + sqlite3TreeViewLine(pView, "{%d:%d}", |
| 83771 | 83859 | pExpr->iTable, pExpr->iColumn); |
| 83772 | 83860 | } |
| 83773 | 83861 | break; |
| 83774 | 83862 | } |
| 83775 | 83863 | case TK_INTEGER: { |
| 83776 | 83864 | if( pExpr->flags & EP_IntValue ){ |
| 83777 | | - sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue); |
| 83865 | + sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); |
| 83778 | 83866 | }else{ |
| 83779 | | - sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken); |
| 83867 | + sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); |
| 83780 | 83868 | } |
| 83781 | 83869 | break; |
| 83782 | 83870 | } |
| 83783 | 83871 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 83784 | 83872 | case TK_FLOAT: { |
| 83785 | | - sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken); |
| 83873 | + sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 83786 | 83874 | break; |
| 83787 | 83875 | } |
| 83788 | 83876 | #endif |
| 83789 | 83877 | case TK_STRING: { |
| 83790 | | - sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken); |
| 83878 | + sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); |
| 83791 | 83879 | break; |
| 83792 | 83880 | } |
| 83793 | 83881 | case TK_NULL: { |
| 83794 | | - sqlite3ExplainPrintf(pOut,"NULL"); |
| 83882 | + sqlite3TreeViewLine(pView,"NULL"); |
| 83795 | 83883 | break; |
| 83796 | 83884 | } |
| 83797 | 83885 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 83798 | 83886 | case TK_BLOB: { |
| 83799 | | - sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken); |
| 83887 | + sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 83800 | 83888 | break; |
| 83801 | 83889 | } |
| 83802 | 83890 | #endif |
| 83803 | 83891 | case TK_VARIABLE: { |
| 83804 | | - sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)", |
| 83805 | | - pExpr->u.zToken, pExpr->iColumn); |
| 83892 | + sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", |
| 83893 | + pExpr->u.zToken, pExpr->iColumn); |
| 83806 | 83894 | break; |
| 83807 | 83895 | } |
| 83808 | 83896 | case TK_REGISTER: { |
| 83809 | | - sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable); |
| 83897 | + sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); |
| 83810 | 83898 | break; |
| 83811 | 83899 | } |
| 83812 | 83900 | case TK_AS: { |
| 83813 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83901 | + sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken); |
| 83902 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 83903 | + break; |
| 83904 | + } |
| 83905 | + case TK_ID: { |
| 83906 | + sqlite3TreeViewLine(pView,"ID %Q", pExpr->u.zToken); |
| 83814 | 83907 | break; |
| 83815 | 83908 | } |
| 83816 | 83909 | #ifndef SQLITE_OMIT_CAST |
| 83817 | 83910 | case TK_CAST: { |
| 83818 | 83911 | /* Expressions of the form: CAST(pLeft AS token) */ |
| 83819 | | - const char *zAff = "unk"; |
| 83820 | | - switch( sqlite3AffinityType(pExpr->u.zToken, 0) ){ |
| 83821 | | - case SQLITE_AFF_TEXT: zAff = "TEXT"; break; |
| 83822 | | - case SQLITE_AFF_NONE: zAff = "NONE"; break; |
| 83823 | | - case SQLITE_AFF_NUMERIC: zAff = "NUMERIC"; break; |
| 83824 | | - case SQLITE_AFF_INTEGER: zAff = "INTEGER"; break; |
| 83825 | | - case SQLITE_AFF_REAL: zAff = "REAL"; break; |
| 83826 | | - } |
| 83827 | | - sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff); |
| 83828 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83829 | | - sqlite3ExplainPrintf(pOut, ")"); |
| 83912 | + sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); |
| 83913 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 83830 | 83914 | break; |
| 83831 | 83915 | } |
| 83832 | 83916 | #endif /* SQLITE_OMIT_CAST */ |
| 83833 | 83917 | case TK_LT: zBinOp = "LT"; break; |
| 83834 | 83918 | case TK_LE: zBinOp = "LE"; break; |
| | @@ -83848,21 +83932,22 @@ |
| 83848 | 83932 | case TK_BITOR: zBinOp = "BITOR"; break; |
| 83849 | 83933 | case TK_SLASH: zBinOp = "DIV"; break; |
| 83850 | 83934 | case TK_LSHIFT: zBinOp = "LSHIFT"; break; |
| 83851 | 83935 | case TK_RSHIFT: zBinOp = "RSHIFT"; break; |
| 83852 | 83936 | case TK_CONCAT: zBinOp = "CONCAT"; break; |
| 83937 | + case TK_DOT: zBinOp = "DOT"; break; |
| 83853 | 83938 | |
| 83854 | 83939 | case TK_UMINUS: zUniOp = "UMINUS"; break; |
| 83855 | 83940 | case TK_UPLUS: zUniOp = "UPLUS"; break; |
| 83856 | 83941 | case TK_BITNOT: zUniOp = "BITNOT"; break; |
| 83857 | 83942 | case TK_NOT: zUniOp = "NOT"; break; |
| 83858 | 83943 | case TK_ISNULL: zUniOp = "ISNULL"; break; |
| 83859 | 83944 | case TK_NOTNULL: zUniOp = "NOTNULL"; break; |
| 83860 | 83945 | |
| 83861 | 83946 | case TK_COLLATE: { |
| 83862 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83863 | | - sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken); |
| 83947 | + sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); |
| 83948 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 83864 | 83949 | break; |
| 83865 | 83950 | } |
| 83866 | 83951 | |
| 83867 | 83952 | case TK_AGG_FUNCTION: |
| 83868 | 83953 | case TK_FUNCTION: { |
| | @@ -83870,45 +83955,40 @@ |
| 83870 | 83955 | if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
| 83871 | 83956 | pFarg = 0; |
| 83872 | 83957 | }else{ |
| 83873 | 83958 | pFarg = pExpr->x.pList; |
| 83874 | 83959 | } |
| 83875 | | - if( op==TK_AGG_FUNCTION ){ |
| 83876 | | - sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(", |
| 83960 | + if( pExpr->op==TK_AGG_FUNCTION ){ |
| 83961 | + sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", |
| 83877 | 83962 | pExpr->op2, pExpr->u.zToken); |
| 83878 | 83963 | }else{ |
| 83879 | | - sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken); |
| 83964 | + sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); |
| 83880 | 83965 | } |
| 83881 | 83966 | if( pFarg ){ |
| 83882 | | - sqlite3ExplainExprList(pOut, pFarg); |
| 83967 | + sqlite3TreeViewExprList(pView, pFarg, 0, 0); |
| 83883 | 83968 | } |
| 83884 | | - sqlite3ExplainPrintf(pOut, ")"); |
| 83885 | 83969 | break; |
| 83886 | 83970 | } |
| 83887 | 83971 | #ifndef SQLITE_OMIT_SUBQUERY |
| 83888 | 83972 | case TK_EXISTS: { |
| 83889 | | - sqlite3ExplainPrintf(pOut, "EXISTS("); |
| 83890 | | - sqlite3ExplainSelect(pOut, pExpr->x.pSelect); |
| 83891 | | - sqlite3ExplainPrintf(pOut,")"); |
| 83973 | + sqlite3TreeViewLine(pView, "EXISTS-expr"); |
| 83974 | + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 83892 | 83975 | break; |
| 83893 | 83976 | } |
| 83894 | 83977 | case TK_SELECT: { |
| 83895 | | - sqlite3ExplainPrintf(pOut, "("); |
| 83896 | | - sqlite3ExplainSelect(pOut, pExpr->x.pSelect); |
| 83897 | | - sqlite3ExplainPrintf(pOut, ")"); |
| 83978 | + sqlite3TreeViewLine(pView, "SELECT-expr"); |
| 83979 | + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 83898 | 83980 | break; |
| 83899 | 83981 | } |
| 83900 | 83982 | case TK_IN: { |
| 83901 | | - sqlite3ExplainPrintf(pOut, "IN("); |
| 83902 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83903 | | - sqlite3ExplainPrintf(pOut, ","); |
| 83983 | + sqlite3TreeViewLine(pView, "IN"); |
| 83984 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 83904 | 83985 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 83905 | | - sqlite3ExplainSelect(pOut, pExpr->x.pSelect); |
| 83986 | + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 83906 | 83987 | }else{ |
| 83907 | | - sqlite3ExplainExprList(pOut, pExpr->x.pList); |
| 83988 | + sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 83908 | 83989 | } |
| 83909 | | - sqlite3ExplainPrintf(pOut, ")"); |
| 83910 | 83990 | break; |
| 83911 | 83991 | } |
| 83912 | 83992 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| 83913 | 83993 | |
| 83914 | 83994 | /* |
| | @@ -83924,17 +84004,14 @@ |
| 83924 | 84004 | */ |
| 83925 | 84005 | case TK_BETWEEN: { |
| 83926 | 84006 | Expr *pX = pExpr->pLeft; |
| 83927 | 84007 | Expr *pY = pExpr->x.pList->a[0].pExpr; |
| 83928 | 84008 | Expr *pZ = pExpr->x.pList->a[1].pExpr; |
| 83929 | | - sqlite3ExplainPrintf(pOut, "BETWEEN("); |
| 83930 | | - sqlite3ExplainExpr(pOut, pX); |
| 83931 | | - sqlite3ExplainPrintf(pOut, ","); |
| 83932 | | - sqlite3ExplainExpr(pOut, pY); |
| 83933 | | - sqlite3ExplainPrintf(pOut, ","); |
| 83934 | | - sqlite3ExplainExpr(pOut, pZ); |
| 83935 | | - sqlite3ExplainPrintf(pOut, ")"); |
| 84009 | + sqlite3TreeViewLine(pView, "BETWEEN"); |
| 84010 | + sqlite3TreeViewExpr(pView, pX, 1); |
| 84011 | + sqlite3TreeViewExpr(pView, pY, 1); |
| 84012 | + sqlite3TreeViewExpr(pView, pZ, 0); |
| 83936 | 84013 | break; |
| 83937 | 84014 | } |
| 83938 | 84015 | case TK_TRIGGER: { |
| 83939 | 84016 | /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 83940 | 84017 | ** to a column in the new.* or old.* pseudo-tables available to |
| | @@ -83941,19 +84018,18 @@ |
| 83941 | 84018 | ** trigger programs. In this case Expr.iTable is set to 1 for the |
| 83942 | 84019 | ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
| 83943 | 84020 | ** is set to the column of the pseudo-table to read, or to -1 to |
| 83944 | 84021 | ** read the rowid field. |
| 83945 | 84022 | */ |
| 83946 | | - sqlite3ExplainPrintf(pOut, "%s(%d)", |
| 84023 | + sqlite3TreeViewLine(pView, "%s(%d)", |
| 83947 | 84024 | pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); |
| 83948 | 84025 | break; |
| 83949 | 84026 | } |
| 83950 | 84027 | case TK_CASE: { |
| 83951 | | - sqlite3ExplainPrintf(pOut, "CASE("); |
| 83952 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83953 | | - sqlite3ExplainPrintf(pOut, ","); |
| 83954 | | - sqlite3ExplainExprList(pOut, pExpr->x.pList); |
| 84028 | + sqlite3TreeViewLine(pView, "CASE"); |
| 84029 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 84030 | + sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 83955 | 84031 | break; |
| 83956 | 84032 | } |
| 83957 | 84033 | #ifndef SQLITE_OMIT_TRIGGER |
| 83958 | 84034 | case TK_RAISE: { |
| 83959 | 84035 | const char *zType = "unk"; |
| | @@ -83961,59 +84037,61 @@ |
| 83961 | 84037 | case OE_Rollback: zType = "rollback"; break; |
| 83962 | 84038 | case OE_Abort: zType = "abort"; break; |
| 83963 | 84039 | case OE_Fail: zType = "fail"; break; |
| 83964 | 84040 | case OE_Ignore: zType = "ignore"; break; |
| 83965 | 84041 | } |
| 83966 | | - sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken); |
| 84042 | + sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); |
| 83967 | 84043 | break; |
| 83968 | 84044 | } |
| 83969 | 84045 | #endif |
| 84046 | + default: { |
| 84047 | + sqlite3TreeViewLine(pView, "op=%d", pExpr->op); |
| 84048 | + break; |
| 84049 | + } |
| 83970 | 84050 | } |
| 83971 | 84051 | if( zBinOp ){ |
| 83972 | | - sqlite3ExplainPrintf(pOut,"%s(", zBinOp); |
| 83973 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83974 | | - sqlite3ExplainPrintf(pOut,","); |
| 83975 | | - sqlite3ExplainExpr(pOut, pExpr->pRight); |
| 83976 | | - sqlite3ExplainPrintf(pOut,")"); |
| 84052 | + sqlite3TreeViewLine(pView, "%s", zBinOp); |
| 84053 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 84054 | + sqlite3TreeViewExpr(pView, pExpr->pRight, 0); |
| 83977 | 84055 | }else if( zUniOp ){ |
| 83978 | | - sqlite3ExplainPrintf(pOut,"%s(", zUniOp); |
| 83979 | | - sqlite3ExplainExpr(pOut, pExpr->pLeft); |
| 83980 | | - sqlite3ExplainPrintf(pOut,")"); |
| 83981 | | - } |
| 83982 | | -} |
| 83983 | | -#endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */ |
| 83984 | | - |
| 83985 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 84056 | + sqlite3TreeViewLine(pView, "%s", zUniOp); |
| 84057 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 84058 | + } |
| 84059 | + sqlite3TreeViewPop(pView); |
| 84060 | +} |
| 84061 | +#endif /* SQLITE_DEBUG */ |
| 84062 | + |
| 84063 | +#ifdef SQLITE_DEBUG |
| 83986 | 84064 | /* |
| 83987 | 84065 | ** Generate a human-readable explanation of an expression list. |
| 83988 | 84066 | */ |
| 83989 | | -SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){ |
| 84067 | +SQLITE_PRIVATE void sqlite3TreeViewExprList( |
| 84068 | + TreeView *pView, |
| 84069 | + const ExprList *pList, |
| 84070 | + u8 moreToFollow, |
| 84071 | + const char *zLabel |
| 84072 | +){ |
| 83990 | 84073 | int i; |
| 83991 | | - if( pList==0 || pList->nExpr==0 ){ |
| 83992 | | - sqlite3ExplainPrintf(pOut, "(empty-list)"); |
| 83993 | | - return; |
| 83994 | | - }else if( pList->nExpr==1 ){ |
| 83995 | | - sqlite3ExplainExpr(pOut, pList->a[0].pExpr); |
| 84074 | + pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 84075 | + if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; |
| 84076 | + if( pList==0 ){ |
| 84077 | + sqlite3TreeViewLine(pView, "%s (empty)", zLabel); |
| 83996 | 84078 | }else{ |
| 83997 | | - sqlite3ExplainPush(pOut); |
| 84079 | + sqlite3TreeViewLine(pView, "%s", zLabel); |
| 83998 | 84080 | for(i=0; i<pList->nExpr; i++){ |
| 83999 | | - sqlite3ExplainPrintf(pOut, "item[%d] = ", i); |
| 84000 | | - sqlite3ExplainPush(pOut); |
| 84001 | | - sqlite3ExplainExpr(pOut, pList->a[i].pExpr); |
| 84002 | | - sqlite3ExplainPop(pOut); |
| 84003 | | - if( pList->a[i].zName ){ |
| 84081 | + sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1); |
| 84082 | +#if 0 |
| 84083 | + if( pList->a[i].zName ){ |
| 84004 | 84084 | sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName); |
| 84005 | 84085 | } |
| 84006 | 84086 | if( pList->a[i].bSpanIsTab ){ |
| 84007 | 84087 | sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan); |
| 84008 | 84088 | } |
| 84009 | | - if( i<pList->nExpr-1 ){ |
| 84010 | | - sqlite3ExplainNL(pOut); |
| 84011 | | - } |
| 84089 | +#endif |
| 84012 | 84090 | } |
| 84013 | | - sqlite3ExplainPop(pOut); |
| 84014 | 84091 | } |
| 84092 | + sqlite3TreeViewPop(pView); |
| 84015 | 84093 | } |
| 84016 | 84094 | #endif /* SQLITE_DEBUG */ |
| 84017 | 84095 | |
| 84018 | 84096 | /* |
| 84019 | 84097 | ** Generate code that pushes the value of every element of the given |
| | @@ -87130,29 +87208,27 @@ |
| 87130 | 87208 | tRowcnt v; |
| 87131 | 87209 | |
| 87132 | 87210 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87133 | 87211 | if( z==0 ) z = ""; |
| 87134 | 87212 | #else |
| 87135 | | - if( NEVER(z==0) ) z = ""; |
| 87213 | + assert( z!=0 ); |
| 87136 | 87214 | #endif |
| 87137 | 87215 | for(i=0; *z && i<nOut; i++){ |
| 87138 | 87216 | v = 0; |
| 87139 | 87217 | while( (c=z[0])>='0' && c<='9' ){ |
| 87140 | 87218 | v = v*10 + c - '0'; |
| 87141 | 87219 | z++; |
| 87142 | 87220 | } |
| 87143 | 87221 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87144 | | - if( aOut ){ |
| 87145 | | - aOut[i] = v; |
| 87146 | | - }else |
| 87222 | + if( aOut ) aOut[i] = v; |
| 87223 | + if( aLog ) aLog[i] = sqlite3LogEst(v); |
| 87147 | 87224 | #else |
| 87148 | 87225 | assert( aOut==0 ); |
| 87149 | 87226 | UNUSED_PARAMETER(aOut); |
| 87227 | + assert( aLog!=0 ); |
| 87228 | + aLog[i] = sqlite3LogEst(v); |
| 87150 | 87229 | #endif |
| 87151 | | - { |
| 87152 | | - aLog[i] = sqlite3LogEst(v); |
| 87153 | | - } |
| 87154 | 87230 | if( *z==' ' ) z++; |
| 87155 | 87231 | } |
| 87156 | 87232 | #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87157 | 87233 | assert( pIndex!=0 ); |
| 87158 | 87234 | #else |
| | @@ -87209,12 +87285,21 @@ |
| 87209 | 87285 | pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); |
| 87210 | 87286 | } |
| 87211 | 87287 | z = argv[2]; |
| 87212 | 87288 | |
| 87213 | 87289 | if( pIndex ){ |
| 87290 | + int nCol = pIndex->nKeyCol+1; |
| 87291 | +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87292 | + tRowcnt * const aiRowEst = pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero( |
| 87293 | + sizeof(tRowcnt) * nCol |
| 87294 | + ); |
| 87295 | + if( aiRowEst==0 ) pInfo->db->mallocFailed = 1; |
| 87296 | +#else |
| 87297 | + tRowcnt * const aiRowEst = 0; |
| 87298 | +#endif |
| 87214 | 87299 | pIndex->bUnordered = 0; |
| 87215 | | - decodeIntArray((char*)z, pIndex->nKeyCol+1, 0, pIndex->aiRowLogEst, pIndex); |
| 87300 | + decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex); |
| 87216 | 87301 | if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0]; |
| 87217 | 87302 | }else{ |
| 87218 | 87303 | Index fakeIdx; |
| 87219 | 87304 | fakeIdx.szIdxRow = pTable->szTabRow; |
| 87220 | 87305 | #ifdef SQLITE_ENABLE_COSTMULT |
| | @@ -87269,29 +87354,42 @@ |
| 87269 | 87354 | ** unique. */ |
| 87270 | 87355 | nCol = pIdx->nSampleCol-1; |
| 87271 | 87356 | pIdx->aAvgEq[nCol] = 1; |
| 87272 | 87357 | } |
| 87273 | 87358 | for(iCol=0; iCol<nCol; iCol++){ |
| 87359 | + int nSample = pIdx->nSample; |
| 87274 | 87360 | int i; /* Used to iterate through samples */ |
| 87275 | 87361 | tRowcnt sumEq = 0; /* Sum of the nEq values */ |
| 87276 | | - tRowcnt nSum = 0; /* Number of terms contributing to sumEq */ |
| 87277 | 87362 | tRowcnt avgEq = 0; |
| 87278 | | - tRowcnt nDLt = pFinal->anDLt[iCol]; |
| 87363 | + tRowcnt nRow; /* Number of rows in index */ |
| 87364 | + i64 nSum100 = 0; /* Number of terms contributing to sumEq */ |
| 87365 | + i64 nDist100; /* Number of distinct values in index */ |
| 87366 | + |
| 87367 | + if( pIdx->aiRowEst==0 || pIdx->aiRowEst[iCol+1]==0 ){ |
| 87368 | + nRow = pFinal->anLt[iCol]; |
| 87369 | + nDist100 = (i64)100 * pFinal->anDLt[iCol]; |
| 87370 | + nSample--; |
| 87371 | + }else{ |
| 87372 | + nRow = pIdx->aiRowEst[0]; |
| 87373 | + nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1]; |
| 87374 | + } |
| 87279 | 87375 | |
| 87280 | 87376 | /* Set nSum to the number of distinct (iCol+1) field prefixes that |
| 87281 | | - ** occur in the stat4 table for this index before pFinal. Set |
| 87282 | | - ** sumEq to the sum of the nEq values for column iCol for the same |
| 87283 | | - ** set (adding the value only once where there exist duplicate |
| 87284 | | - ** prefixes). */ |
| 87285 | | - for(i=0; i<(pIdx->nSample-1); i++){ |
| 87286 | | - if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){ |
| 87377 | + ** occur in the stat4 table for this index. Set sumEq to the sum of |
| 87378 | + ** the nEq values for column iCol for the same set (adding the value |
| 87379 | + ** only once where there exist duplicate prefixes). */ |
| 87380 | + for(i=0; i<nSample; i++){ |
| 87381 | + if( i==(pIdx->nSample-1) |
| 87382 | + || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] |
| 87383 | + ){ |
| 87287 | 87384 | sumEq += aSample[i].anEq[iCol]; |
| 87288 | | - nSum++; |
| 87385 | + nSum100 += 100; |
| 87289 | 87386 | } |
| 87290 | 87387 | } |
| 87291 | | - if( nDLt>nSum ){ |
| 87292 | | - avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum); |
| 87388 | + |
| 87389 | + if( nDist100>nSum100 ){ |
| 87390 | + avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100); |
| 87293 | 87391 | } |
| 87294 | 87392 | if( avgEq==0 ) avgEq = 1; |
| 87295 | 87393 | pIdx->aAvgEq[iCol] = avgEq; |
| 87296 | 87394 | } |
| 87297 | 87395 | } |
| | @@ -87538,10 +87636,15 @@ |
| 87538 | 87636 | if( rc==SQLITE_OK ){ |
| 87539 | 87637 | int lookasideEnabled = db->lookaside.bEnabled; |
| 87540 | 87638 | db->lookaside.bEnabled = 0; |
| 87541 | 87639 | rc = loadStat4(db, sInfo.zDatabase); |
| 87542 | 87640 | db->lookaside.bEnabled = lookasideEnabled; |
| 87641 | + } |
| 87642 | + for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
| 87643 | + Index *pIdx = sqliteHashData(i); |
| 87644 | + sqlite3_free(pIdx->aiRowEst); |
| 87645 | + pIdx->aiRowEst = 0; |
| 87543 | 87646 | } |
| 87544 | 87647 | #endif |
| 87545 | 87648 | |
| 87546 | 87649 | if( rc==SQLITE_NOMEM ){ |
| 87547 | 87650 | db->mallocFailed = 1; |
| | @@ -88832,10 +88935,13 @@ |
| 88832 | 88935 | #endif |
| 88833 | 88936 | if( db==0 || db->pnBytesFreed==0 ) sqlite3KeyInfoUnref(p->pKeyInfo); |
| 88834 | 88937 | sqlite3ExprDelete(db, p->pPartIdxWhere); |
| 88835 | 88938 | sqlite3DbFree(db, p->zColAff); |
| 88836 | 88939 | if( p->isResized ) sqlite3DbFree(db, p->azColl); |
| 88940 | +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 88941 | + sqlite3_free(p->aiRowEst); |
| 88942 | +#endif |
| 88837 | 88943 | sqlite3DbFree(db, p); |
| 88838 | 88944 | } |
| 88839 | 88945 | |
| 88840 | 88946 | /* |
| 88841 | 88947 | ** For the index called zIdxName which is found in the database iDb, |
| | @@ -89633,11 +89739,11 @@ |
| 89633 | 89739 | Column *pCol; |
| 89634 | 89740 | sqlite3 *db = pParse->db; |
| 89635 | 89741 | p = pParse->pNewTable; |
| 89636 | 89742 | if( p!=0 ){ |
| 89637 | 89743 | pCol = &(p->aCol[p->nCol-1]); |
| 89638 | | - if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){ |
| 89744 | + if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){ |
| 89639 | 89745 | sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", |
| 89640 | 89746 | pCol->zName); |
| 89641 | 89747 | }else{ |
| 89642 | 89748 | /* A copy of pExpr is used instead of the original, as pExpr contains |
| 89643 | 89749 | ** tokens that point to volatile memory. The 'span' of the expression |
| | @@ -91141,11 +91247,11 @@ |
| 91141 | 91247 | pIndex->nKeyCol); VdbeCoverage(v); |
| 91142 | 91248 | sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
| 91143 | 91249 | }else{ |
| 91144 | 91250 | addr2 = sqlite3VdbeCurrentAddr(v); |
| 91145 | 91251 | } |
| 91146 | | - sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord); |
| 91252 | + sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
| 91147 | 91253 | sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1); |
| 91148 | 91254 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 91149 | 91255 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 91150 | 91256 | sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
| 91151 | 91257 | sqlite3VdbeJumpHere(v, addr1); |
| | @@ -94030,11 +94136,14 @@ |
| 94030 | 94136 | |
| 94031 | 94137 | /* |
| 94032 | 94138 | ** Return the collating function associated with a function. |
| 94033 | 94139 | */ |
| 94034 | 94140 | static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
| 94035 | | - return context->pColl; |
| 94141 | + VdbeOp *pOp = &context->pVdbe->aOp[context->iOp-1]; |
| 94142 | + assert( pOp->opcode==OP_CollSeq ); |
| 94143 | + assert( pOp->p4type==P4_COLLSEQ ); |
| 94144 | + return pOp->p4.pColl; |
| 94036 | 94145 | } |
| 94037 | 94146 | |
| 94038 | 94147 | /* |
| 94039 | 94148 | ** Indicate that the accumulator load should be skipped on this |
| 94040 | 94149 | ** iteration of the aggregate loop. |
| | @@ -94575,14 +94684,16 @@ |
| 94575 | 94684 | ** character is exactly one byte in size. Also, all characters are |
| 94576 | 94685 | ** able to participate in upper-case-to-lower-case mappings in EBCDIC |
| 94577 | 94686 | ** whereas only characters less than 0x80 do in ASCII. |
| 94578 | 94687 | */ |
| 94579 | 94688 | #if defined(SQLITE_EBCDIC) |
| 94580 | | -# define sqlite3Utf8Read(A) (*((*A)++)) |
| 94581 | | -# define GlobUpperToLower(A) A = sqlite3UpperToLower[A] |
| 94689 | +# define sqlite3Utf8Read(A) (*((*A)++)) |
| 94690 | +# define GlobUpperToLower(A) A = sqlite3UpperToLower[A] |
| 94691 | +# define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] |
| 94582 | 94692 | #else |
| 94583 | | -# define GlobUpperToLower(A) if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; } |
| 94693 | +# define GlobUpperToLower(A) if( A<=0x7f ){ A = sqlite3UpperToLower[A]; } |
| 94694 | +# define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] |
| 94584 | 94695 | #endif |
| 94585 | 94696 | |
| 94586 | 94697 | static const struct compareInfo globInfo = { '*', '?', '[', 0 }; |
| 94587 | 94698 | /* The correct SQL-92 behavior is for the LIKE operator to ignore |
| 94588 | 94699 | ** case. Thus 'a' LIKE 'A' would be true. */ |
| | @@ -94591,11 +94702,11 @@ |
| 94591 | 94702 | ** is case sensitive causing 'a' LIKE 'A' to be false */ |
| 94592 | 94703 | static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; |
| 94593 | 94704 | |
| 94594 | 94705 | /* |
| 94595 | 94706 | ** Compare two UTF-8 strings for equality where the first string can |
| 94596 | | -** potentially be a "glob" expression. Return true (1) if they |
| 94707 | +** potentially be a "glob" or "like" expression. Return true (1) if they |
| 94597 | 94708 | ** are the same and false (0) if they are different. |
| 94598 | 94709 | ** |
| 94599 | 94710 | ** Globbing rules: |
| 94600 | 94711 | ** |
| 94601 | 94712 | ** '*' Matches any sequence of zero or more characters. |
| | @@ -94611,120 +94722,147 @@ |
| 94611 | 94722 | ** in the list by making it the first character after '[' or '^'. A |
| 94612 | 94723 | ** range of characters can be specified using '-'. Example: |
| 94613 | 94724 | ** "[a-z]" matches any single lower-case letter. To match a '-', make |
| 94614 | 94725 | ** it the last character in the list. |
| 94615 | 94726 | ** |
| 94727 | +** Like matching rules: |
| 94728 | +** |
| 94729 | +** '%' Matches any sequence of zero or more characters |
| 94730 | +** |
| 94731 | +*** '_' Matches any one character |
| 94732 | +** |
| 94733 | +** Ec Where E is the "esc" character and c is any other |
| 94734 | +** character, including '%', '_', and esc, match exactly c. |
| 94735 | +** |
| 94736 | +** The comments through this routine usually assume glob matching. |
| 94737 | +** |
| 94616 | 94738 | ** This routine is usually quick, but can be N**2 in the worst case. |
| 94617 | | -** |
| 94618 | | -** Hints: to match '*' or '?', put them in "[]". Like this: |
| 94619 | | -** |
| 94620 | | -** abc[*]xyz Matches "abc*xyz" only |
| 94621 | 94739 | */ |
| 94622 | 94740 | static int patternCompare( |
| 94623 | 94741 | const u8 *zPattern, /* The glob pattern */ |
| 94624 | 94742 | const u8 *zString, /* The string to compare against the glob */ |
| 94625 | 94743 | const struct compareInfo *pInfo, /* Information about how to do the compare */ |
| 94626 | 94744 | u32 esc /* The escape character */ |
| 94627 | 94745 | ){ |
| 94628 | | - u32 c, c2; |
| 94629 | | - int invert; |
| 94630 | | - int seen; |
| 94631 | | - u8 matchOne = pInfo->matchOne; |
| 94632 | | - u8 matchAll = pInfo->matchAll; |
| 94633 | | - u8 matchSet = pInfo->matchSet; |
| 94634 | | - u8 noCase = pInfo->noCase; |
| 94635 | | - int prevEscape = 0; /* True if the previous character was 'escape' */ |
| 94746 | + u32 c, c2; /* Next pattern and input string chars */ |
| 94747 | + u32 matchOne = pInfo->matchOne; /* "?" or "_" */ |
| 94748 | + u32 matchAll = pInfo->matchAll; /* "*" or "%" */ |
| 94749 | + u32 matchOther; /* "[" or the escape character */ |
| 94750 | + u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ |
| 94751 | + const u8 *zEscaped = 0; /* One past the last escaped input char */ |
| 94752 | + |
| 94753 | + /* The GLOB operator does not have an ESCAPE clause. And LIKE does not |
| 94754 | + ** have the matchSet operator. So we either have to look for one or |
| 94755 | + ** the other, never both. Hence the single variable matchOther is used |
| 94756 | + ** to store the one we have to look for. |
| 94757 | + */ |
| 94758 | + matchOther = esc ? esc : pInfo->matchSet; |
| 94636 | 94759 | |
| 94637 | 94760 | while( (c = sqlite3Utf8Read(&zPattern))!=0 ){ |
| 94638 | | - if( c==matchAll && !prevEscape ){ |
| 94761 | + if( c==matchAll ){ /* Match "*" */ |
| 94762 | + /* Skip over multiple "*" characters in the pattern. If there |
| 94763 | + ** are also "?" characters, skip those as well, but consume a |
| 94764 | + ** single character of the input string for each "?" skipped */ |
| 94639 | 94765 | while( (c=sqlite3Utf8Read(&zPattern)) == matchAll |
| 94640 | 94766 | || c == matchOne ){ |
| 94641 | 94767 | if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ |
| 94642 | 94768 | return 0; |
| 94643 | 94769 | } |
| 94644 | 94770 | } |
| 94645 | 94771 | if( c==0 ){ |
| 94646 | | - return 1; |
| 94647 | | - }else if( c==esc ){ |
| 94648 | | - c = sqlite3Utf8Read(&zPattern); |
| 94649 | | - if( c==0 ){ |
| 94650 | | - return 0; |
| 94651 | | - } |
| 94652 | | - }else if( c==matchSet ){ |
| 94653 | | - assert( esc==0 ); /* This is GLOB, not LIKE */ |
| 94654 | | - assert( matchSet<0x80 ); /* '[' is a single-byte character */ |
| 94655 | | - while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ |
| 94656 | | - SQLITE_SKIP_UTF8(zString); |
| 94657 | | - } |
| 94658 | | - return *zString!=0; |
| 94659 | | - } |
| 94660 | | - while( (c2 = sqlite3Utf8Read(&zString))!=0 ){ |
| 94661 | | - if( noCase ){ |
| 94662 | | - GlobUpperToLower(c2); |
| 94663 | | - GlobUpperToLower(c); |
| 94664 | | - while( c2 != 0 && c2 != c ){ |
| 94665 | | - c2 = sqlite3Utf8Read(&zString); |
| 94666 | | - GlobUpperToLower(c2); |
| 94667 | | - } |
| 94668 | | - }else{ |
| 94669 | | - while( c2 != 0 && c2 != c ){ |
| 94670 | | - c2 = sqlite3Utf8Read(&zString); |
| 94671 | | - } |
| 94672 | | - } |
| 94673 | | - if( c2==0 ) return 0; |
| 94674 | | - if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 94675 | | - } |
| 94676 | | - return 0; |
| 94677 | | - }else if( c==matchOne && !prevEscape ){ |
| 94678 | | - if( sqlite3Utf8Read(&zString)==0 ){ |
| 94679 | | - return 0; |
| 94680 | | - } |
| 94681 | | - }else if( c==matchSet ){ |
| 94682 | | - u32 prior_c = 0; |
| 94683 | | - assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ |
| 94684 | | - seen = 0; |
| 94685 | | - invert = 0; |
| 94686 | | - c = sqlite3Utf8Read(&zString); |
| 94687 | | - if( c==0 ) return 0; |
| 94688 | | - c2 = sqlite3Utf8Read(&zPattern); |
| 94689 | | - if( c2=='^' ){ |
| 94690 | | - invert = 1; |
| 94691 | | - c2 = sqlite3Utf8Read(&zPattern); |
| 94692 | | - } |
| 94693 | | - if( c2==']' ){ |
| 94694 | | - if( c==']' ) seen = 1; |
| 94695 | | - c2 = sqlite3Utf8Read(&zPattern); |
| 94696 | | - } |
| 94697 | | - while( c2 && c2!=']' ){ |
| 94698 | | - if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ |
| 94699 | | - c2 = sqlite3Utf8Read(&zPattern); |
| 94700 | | - if( c>=prior_c && c<=c2 ) seen = 1; |
| 94701 | | - prior_c = 0; |
| 94702 | | - }else{ |
| 94703 | | - if( c==c2 ){ |
| 94704 | | - seen = 1; |
| 94705 | | - } |
| 94706 | | - prior_c = c2; |
| 94707 | | - } |
| 94708 | | - c2 = sqlite3Utf8Read(&zPattern); |
| 94709 | | - } |
| 94710 | | - if( c2==0 || (seen ^ invert)==0 ){ |
| 94711 | | - return 0; |
| 94712 | | - } |
| 94713 | | - }else if( esc==c && !prevEscape ){ |
| 94714 | | - prevEscape = 1; |
| 94715 | | - }else{ |
| 94716 | | - c2 = sqlite3Utf8Read(&zString); |
| 94717 | | - if( noCase ){ |
| 94718 | | - GlobUpperToLower(c); |
| 94719 | | - GlobUpperToLower(c2); |
| 94720 | | - } |
| 94721 | | - if( c!=c2 ){ |
| 94722 | | - return 0; |
| 94723 | | - } |
| 94724 | | - prevEscape = 0; |
| 94725 | | - } |
| 94772 | + return 1; /* "*" at the end of the pattern matches */ |
| 94773 | + }else if( c==matchOther ){ |
| 94774 | + if( esc ){ |
| 94775 | + c = sqlite3Utf8Read(&zPattern); |
| 94776 | + if( c==0 ) return 0; |
| 94777 | + }else{ |
| 94778 | + /* "[...]" immediately follows the "*". We have to do a slow |
| 94779 | + ** recursive search in this case, but it is an unusual case. */ |
| 94780 | + assert( matchOther<0x80 ); /* '[' is a single-byte character */ |
| 94781 | + while( *zString |
| 94782 | + && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ |
| 94783 | + SQLITE_SKIP_UTF8(zString); |
| 94784 | + } |
| 94785 | + return *zString!=0; |
| 94786 | + } |
| 94787 | + } |
| 94788 | + |
| 94789 | + /* At this point variable c contains the first character of the |
| 94790 | + ** pattern string past the "*". Search in the input string for the |
| 94791 | + ** first matching character and recursively contine the match from |
| 94792 | + ** that point. |
| 94793 | + ** |
| 94794 | + ** For a case-insensitive search, set variable cx to be the same as |
| 94795 | + ** c but in the other case and search the input string for either |
| 94796 | + ** c or cx. |
| 94797 | + */ |
| 94798 | + if( c<=0x80 ){ |
| 94799 | + u32 cx; |
| 94800 | + if( noCase ){ |
| 94801 | + cx = sqlite3Toupper(c); |
| 94802 | + c = sqlite3Tolower(c); |
| 94803 | + }else{ |
| 94804 | + cx = c; |
| 94805 | + } |
| 94806 | + while( (c2 = *(zString++))!=0 ){ |
| 94807 | + if( c2!=c && c2!=cx ) continue; |
| 94808 | + if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 94809 | + } |
| 94810 | + }else{ |
| 94811 | + while( (c2 = sqlite3Utf8Read(&zString))!=0 ){ |
| 94812 | + if( c2!=c ) continue; |
| 94813 | + if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 94814 | + } |
| 94815 | + } |
| 94816 | + return 0; |
| 94817 | + } |
| 94818 | + if( c==matchOther ){ |
| 94819 | + if( esc ){ |
| 94820 | + c = sqlite3Utf8Read(&zPattern); |
| 94821 | + if( c==0 ) return 0; |
| 94822 | + zEscaped = zPattern; |
| 94823 | + }else{ |
| 94824 | + u32 prior_c = 0; |
| 94825 | + int seen = 0; |
| 94826 | + int invert = 0; |
| 94827 | + c = sqlite3Utf8Read(&zString); |
| 94828 | + if( c==0 ) return 0; |
| 94829 | + c2 = sqlite3Utf8Read(&zPattern); |
| 94830 | + if( c2=='^' ){ |
| 94831 | + invert = 1; |
| 94832 | + c2 = sqlite3Utf8Read(&zPattern); |
| 94833 | + } |
| 94834 | + if( c2==']' ){ |
| 94835 | + if( c==']' ) seen = 1; |
| 94836 | + c2 = sqlite3Utf8Read(&zPattern); |
| 94837 | + } |
| 94838 | + while( c2 && c2!=']' ){ |
| 94839 | + if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ |
| 94840 | + c2 = sqlite3Utf8Read(&zPattern); |
| 94841 | + if( c>=prior_c && c<=c2 ) seen = 1; |
| 94842 | + prior_c = 0; |
| 94843 | + }else{ |
| 94844 | + if( c==c2 ){ |
| 94845 | + seen = 1; |
| 94846 | + } |
| 94847 | + prior_c = c2; |
| 94848 | + } |
| 94849 | + c2 = sqlite3Utf8Read(&zPattern); |
| 94850 | + } |
| 94851 | + if( c2==0 || (seen ^ invert)==0 ){ |
| 94852 | + return 0; |
| 94853 | + } |
| 94854 | + continue; |
| 94855 | + } |
| 94856 | + } |
| 94857 | + c2 = sqlite3Utf8Read(&zString); |
| 94858 | + if( c==c2 ) continue; |
| 94859 | + if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){ |
| 94860 | + continue; |
| 94861 | + } |
| 94862 | + if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; |
| 94863 | + return 0; |
| 94726 | 94864 | } |
| 94727 | 94865 | return *zString==0; |
| 94728 | 94866 | } |
| 94729 | 94867 | |
| 94730 | 94868 | /* |
| | @@ -103884,10 +104022,24 @@ |
| 103884 | 104022 | ** |
| 103885 | 104023 | ************************************************************************* |
| 103886 | 104024 | ** This file contains C code routines that are called by the parser |
| 103887 | 104025 | ** to handle SELECT statements in SQLite. |
| 103888 | 104026 | */ |
| 104027 | + |
| 104028 | +/* |
| 104029 | +** Trace output macros |
| 104030 | +*/ |
| 104031 | +#if SELECTTRACE_ENABLED |
| 104032 | +/***/ int sqlite3SelectTrace = 0; |
| 104033 | +# define SELECTTRACE(K,P,S,X) \ |
| 104034 | + if(sqlite3SelectTrace&(K)) \ |
| 104035 | + sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\ |
| 104036 | + sqlite3DebugPrintf X |
| 104037 | +#else |
| 104038 | +# define SELECTTRACE(K,P,S,X) |
| 104039 | +#endif |
| 104040 | + |
| 103889 | 104041 | |
| 103890 | 104042 | /* |
| 103891 | 104043 | ** An instance of the following object is used to record information about |
| 103892 | 104044 | ** how to process the DISTINCT keyword, to simplify passing that information |
| 103893 | 104045 | ** into the selectInnerLoop() routine. |
| | @@ -103996,10 +104148,22 @@ |
| 103996 | 104148 | assert( pNew->pSrc!=0 || pParse->nErr>0 ); |
| 103997 | 104149 | } |
| 103998 | 104150 | assert( pNew!=&standin ); |
| 103999 | 104151 | return pNew; |
| 104000 | 104152 | } |
| 104153 | + |
| 104154 | +#if SELECTTRACE_ENABLED |
| 104155 | +/* |
| 104156 | +** Set the name of a Select object |
| 104157 | +*/ |
| 104158 | +SQLITE_PRIVATE void sqlite3SelectSetName(Select *p, const char *zName){ |
| 104159 | + if( p && zName ){ |
| 104160 | + sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName); |
| 104161 | + } |
| 104162 | +} |
| 104163 | +#endif |
| 104164 | + |
| 104001 | 104165 | |
| 104002 | 104166 | /* |
| 104003 | 104167 | ** Delete the given Select structure and all of its substructures. |
| 104004 | 104168 | */ |
| 104005 | 104169 | SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){ |
| | @@ -105026,11 +105190,10 @@ |
| 105026 | 105190 | int regRow; |
| 105027 | 105191 | int regRowid; |
| 105028 | 105192 | int nKey; |
| 105029 | 105193 | int iSortTab; /* Sorter cursor to read from */ |
| 105030 | 105194 | int nSortData; /* Trailing values to read from sorter */ |
| 105031 | | - u8 p5; /* p5 parameter for 1st OP_Column */ |
| 105032 | 105195 | int i; |
| 105033 | 105196 | int bSeq; /* True if sorter record includes seq. no. */ |
| 105034 | 105197 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 105035 | 105198 | struct ExprList_item *aOutEx = p->pEList->a; |
| 105036 | 105199 | #endif |
| | @@ -105060,23 +105223,20 @@ |
| 105060 | 105223 | sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData); |
| 105061 | 105224 | if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce); |
| 105062 | 105225 | addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak); |
| 105063 | 105226 | VdbeCoverage(v); |
| 105064 | 105227 | codeOffset(v, p->iOffset, addrContinue); |
| 105065 | | - sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut); |
| 105066 | | - p5 = OPFLAG_CLEARCACHE; |
| 105228 | + sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab); |
| 105067 | 105229 | bSeq = 0; |
| 105068 | 105230 | }else{ |
| 105069 | 105231 | addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v); |
| 105070 | 105232 | codeOffset(v, p->iOffset, addrContinue); |
| 105071 | 105233 | iSortTab = iTab; |
| 105072 | | - p5 = 0; |
| 105073 | 105234 | bSeq = 1; |
| 105074 | 105235 | } |
| 105075 | 105236 | for(i=0; i<nSortData; i++){ |
| 105076 | 105237 | sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i); |
| 105077 | | - if( i==0 ) sqlite3VdbeChangeP5(v, p5); |
| 105078 | 105238 | VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan)); |
| 105079 | 105239 | } |
| 105080 | 105240 | switch( eDest ){ |
| 105081 | 105241 | case SRT_Table: |
| 105082 | 105242 | case SRT_EphemTab: { |
| | @@ -107226,10 +107386,12 @@ |
| 107226 | 107386 | } |
| 107227 | 107387 | } |
| 107228 | 107388 | } |
| 107229 | 107389 | |
| 107230 | 107390 | /***** If we reach this point, flattening is permitted. *****/ |
| 107391 | + SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n", |
| 107392 | + pSub->zSelName, pSub, iFrom)); |
| 107231 | 107393 | |
| 107232 | 107394 | /* Authorize the subquery */ |
| 107233 | 107395 | pParse->zAuthContext = pSubitem->zName; |
| 107234 | 107396 | TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0); |
| 107235 | 107397 | testcase( i==SQLITE_DENY ); |
| | @@ -107278,10 +107440,11 @@ |
| 107278 | 107440 | p->pSrc = 0; |
| 107279 | 107441 | p->pPrior = 0; |
| 107280 | 107442 | p->pLimit = 0; |
| 107281 | 107443 | p->pOffset = 0; |
| 107282 | 107444 | pNew = sqlite3SelectDup(db, p, 0); |
| 107445 | + sqlite3SelectSetName(pNew, pSub->zSelName); |
| 107283 | 107446 | p->pOffset = pOffset; |
| 107284 | 107447 | p->pLimit = pLimit; |
| 107285 | 107448 | p->pOrderBy = pOrderBy; |
| 107286 | 107449 | p->pSrc = pSrc; |
| 107287 | 107450 | p->op = TK_ALL; |
| | @@ -107290,10 +107453,13 @@ |
| 107290 | 107453 | }else{ |
| 107291 | 107454 | pNew->pPrior = pPrior; |
| 107292 | 107455 | if( pPrior ) pPrior->pNext = pNew; |
| 107293 | 107456 | pNew->pNext = p; |
| 107294 | 107457 | p->pPrior = pNew; |
| 107458 | + SELECTTRACE(2,pParse,p, |
| 107459 | + ("compound-subquery flattener creates %s.%p as peer\n", |
| 107460 | + pNew->zSelName, pNew)); |
| 107295 | 107461 | } |
| 107296 | 107462 | if( db->mallocFailed ) return 1; |
| 107297 | 107463 | } |
| 107298 | 107464 | |
| 107299 | 107465 | /* Begin flattening the iFrom-th entry of the FROM clause |
| | @@ -107419,12 +107585,27 @@ |
| 107419 | 107585 | if( isAgg ){ |
| 107420 | 107586 | substExprList(db, pParent->pGroupBy, iParent, pSub->pEList); |
| 107421 | 107587 | pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList); |
| 107422 | 107588 | } |
| 107423 | 107589 | if( pSub->pOrderBy ){ |
| 107590 | + /* At this point, any non-zero iOrderByCol values indicate that the |
| 107591 | + ** ORDER BY column expression is identical to the iOrderByCol'th |
| 107592 | + ** expression returned by SELECT statement pSub. Since these values |
| 107593 | + ** do not necessarily correspond to columns in SELECT statement pParent, |
| 107594 | + ** zero them before transfering the ORDER BY clause. |
| 107595 | + ** |
| 107596 | + ** Not doing this may cause an error if a subsequent call to this |
| 107597 | + ** function attempts to flatten a compound sub-query into pParent |
| 107598 | + ** (the only way this can happen is if the compound sub-query is |
| 107599 | + ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */ |
| 107600 | + ExprList *pOrderBy = pSub->pOrderBy; |
| 107601 | + for(i=0; i<pOrderBy->nExpr; i++){ |
| 107602 | + pOrderBy->a[i].u.x.iOrderByCol = 0; |
| 107603 | + } |
| 107424 | 107604 | assert( pParent->pOrderBy==0 ); |
| 107425 | | - pParent->pOrderBy = pSub->pOrderBy; |
| 107605 | + assert( pSub->pPrior==0 ); |
| 107606 | + pParent->pOrderBy = pOrderBy; |
| 107426 | 107607 | pSub->pOrderBy = 0; |
| 107427 | 107608 | }else if( pParent->pOrderBy ){ |
| 107428 | 107609 | substExprList(db, pParent->pOrderBy, iParent, pSub->pEList); |
| 107429 | 107610 | } |
| 107430 | 107611 | if( pSub->pWhere ){ |
| | @@ -107465,10 +107646,17 @@ |
| 107465 | 107646 | |
| 107466 | 107647 | /* Finially, delete what is left of the subquery and return |
| 107467 | 107648 | ** success. |
| 107468 | 107649 | */ |
| 107469 | 107650 | sqlite3SelectDelete(db, pSub1); |
| 107651 | + |
| 107652 | +#if SELECTTRACE_ENABLED |
| 107653 | + if( sqlite3SelectTrace & 0x100 ){ |
| 107654 | + sqlite3DebugPrintf("After flattening:\n"); |
| 107655 | + sqlite3TreeViewSelect(0, p, 0); |
| 107656 | + } |
| 107657 | +#endif |
| 107470 | 107658 | |
| 107471 | 107659 | return 1; |
| 107472 | 107660 | } |
| 107473 | 107661 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 107474 | 107662 | |
| | @@ -107936,10 +108124,11 @@ |
| 107936 | 108124 | if( pTab->pSelect || IsVirtual(pTab) ){ |
| 107937 | 108125 | /* We reach here if the named table is a really a view */ |
| 107938 | 108126 | if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort; |
| 107939 | 108127 | assert( pFrom->pSelect==0 ); |
| 107940 | 108128 | pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0); |
| 108129 | + sqlite3SelectSetName(pFrom->pSelect, pTab->zName); |
| 107941 | 108130 | sqlite3WalkSelect(pWalker, pFrom->pSelect); |
| 107942 | 108131 | } |
| 107943 | 108132 | #endif |
| 107944 | 108133 | } |
| 107945 | 108134 | |
| | @@ -108470,10 +108659,17 @@ |
| 108470 | 108659 | if( p==0 || db->mallocFailed || pParse->nErr ){ |
| 108471 | 108660 | return 1; |
| 108472 | 108661 | } |
| 108473 | 108662 | if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; |
| 108474 | 108663 | memset(&sAggInfo, 0, sizeof(sAggInfo)); |
| 108664 | +#if SELECTTRACE_ENABLED |
| 108665 | + pParse->nSelectIndent++; |
| 108666 | + SELECTTRACE(1,pParse,p, ("begin processing:\n")); |
| 108667 | + if( sqlite3SelectTrace & 0x100 ){ |
| 108668 | + sqlite3TreeViewSelect(0, p, 0); |
| 108669 | + } |
| 108670 | +#endif |
| 108475 | 108671 | |
| 108476 | 108672 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo ); |
| 108477 | 108673 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo ); |
| 108478 | 108674 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue ); |
| 108479 | 108675 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue ); |
| | @@ -108626,10 +108822,14 @@ |
| 108626 | 108822 | /* If there is are a sequence of queries, do the earlier ones first. |
| 108627 | 108823 | */ |
| 108628 | 108824 | if( p->pPrior ){ |
| 108629 | 108825 | rc = multiSelect(pParse, p, pDest); |
| 108630 | 108826 | explainSetInteger(pParse->iSelectId, iRestoreSelectId); |
| 108827 | +#if SELECTTRACE_ENABLED |
| 108828 | + SELECTTRACE(1,pParse,p,("end compound-select processing\n")); |
| 108829 | + pParse->nSelectIndent--; |
| 108830 | +#endif |
| 108631 | 108831 | return rc; |
| 108632 | 108832 | } |
| 108633 | 108833 | #endif |
| 108634 | 108834 | |
| 108635 | 108835 | /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and |
| | @@ -108961,16 +109161,15 @@ |
| 108961 | 109161 | ** from the previous row currently stored in a0, a1, a2... |
| 108962 | 109162 | */ |
| 108963 | 109163 | addrTopOfLoop = sqlite3VdbeCurrentAddr(v); |
| 108964 | 109164 | sqlite3ExprCacheClear(pParse); |
| 108965 | 109165 | if( groupBySort ){ |
| 108966 | | - sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut); |
| 109166 | + sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, sortOut,sortPTab); |
| 108967 | 109167 | } |
| 108968 | 109168 | for(j=0; j<pGroupBy->nExpr; j++){ |
| 108969 | 109169 | if( groupBySort ){ |
| 108970 | 109170 | sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j); |
| 108971 | | - if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE); |
| 108972 | 109171 | }else{ |
| 108973 | 109172 | sAggInfo.directMode = 1; |
| 108974 | 109173 | sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j); |
| 108975 | 109174 | } |
| 108976 | 109175 | } |
| | @@ -109225,107 +109424,110 @@ |
| 109225 | 109424 | generateColumnNames(pParse, pTabList, pEList); |
| 109226 | 109425 | } |
| 109227 | 109426 | |
| 109228 | 109427 | sqlite3DbFree(db, sAggInfo.aCol); |
| 109229 | 109428 | sqlite3DbFree(db, sAggInfo.aFunc); |
| 109429 | +#if SELECTTRACE_ENABLED |
| 109430 | + SELECTTRACE(1,pParse,p,("end processing\n")); |
| 109431 | + pParse->nSelectIndent--; |
| 109432 | +#endif |
| 109230 | 109433 | return rc; |
| 109231 | 109434 | } |
| 109232 | 109435 | |
| 109233 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 109436 | +#ifdef SQLITE_DEBUG |
| 109234 | 109437 | /* |
| 109235 | 109438 | ** Generate a human-readable description of a the Select object. |
| 109236 | 109439 | */ |
| 109237 | | -static void explainOneSelect(Vdbe *pVdbe, Select *p){ |
| 109238 | | - sqlite3ExplainPrintf(pVdbe, "SELECT "); |
| 109239 | | - if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ |
| 109240 | | - if( p->selFlags & SF_Distinct ){ |
| 109241 | | - sqlite3ExplainPrintf(pVdbe, "DISTINCT "); |
| 109242 | | - } |
| 109243 | | - if( p->selFlags & SF_Aggregate ){ |
| 109244 | | - sqlite3ExplainPrintf(pVdbe, "agg_flag "); |
| 109245 | | - } |
| 109246 | | - sqlite3ExplainNL(pVdbe); |
| 109247 | | - sqlite3ExplainPrintf(pVdbe, " "); |
| 109248 | | - } |
| 109249 | | - sqlite3ExplainExprList(pVdbe, p->pEList); |
| 109250 | | - sqlite3ExplainNL(pVdbe); |
| 109440 | +SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ |
| 109441 | + int n = 0; |
| 109442 | + pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 109443 | + sqlite3TreeViewLine(pView, "SELECT%s%s", |
| 109444 | + ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), |
| 109445 | + ((p->selFlags & SF_Aggregate) ? " agg_flag" : "") |
| 109446 | + ); |
| 109447 | + if( p->pSrc && p->pSrc->nSrc ) n++; |
| 109448 | + if( p->pWhere ) n++; |
| 109449 | + if( p->pGroupBy ) n++; |
| 109450 | + if( p->pHaving ) n++; |
| 109451 | + if( p->pOrderBy ) n++; |
| 109452 | + if( p->pLimit ) n++; |
| 109453 | + if( p->pOffset ) n++; |
| 109454 | + if( p->pPrior ) n++; |
| 109455 | + sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set"); |
| 109251 | 109456 | if( p->pSrc && p->pSrc->nSrc ){ |
| 109252 | 109457 | int i; |
| 109253 | | - sqlite3ExplainPrintf(pVdbe, "FROM "); |
| 109254 | | - sqlite3ExplainPush(pVdbe); |
| 109458 | + pView = sqlite3TreeViewPush(pView, (n--)>0); |
| 109459 | + sqlite3TreeViewLine(pView, "FROM"); |
| 109255 | 109460 | for(i=0; i<p->pSrc->nSrc; i++){ |
| 109256 | 109461 | struct SrcList_item *pItem = &p->pSrc->a[i]; |
| 109257 | | - sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor); |
| 109258 | | - if( pItem->pSelect ){ |
| 109259 | | - sqlite3ExplainSelect(pVdbe, pItem->pSelect); |
| 109260 | | - if( pItem->pTab ){ |
| 109261 | | - sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName); |
| 109262 | | - } |
| 109462 | + StrAccum x; |
| 109463 | + char zLine[100]; |
| 109464 | + sqlite3StrAccumInit(&x, zLine, sizeof(zLine), 0); |
| 109465 | + sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor); |
| 109466 | + if( pItem->zDatabase ){ |
| 109467 | + sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName); |
| 109263 | 109468 | }else if( pItem->zName ){ |
| 109264 | | - sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName); |
| 109469 | + sqlite3XPrintf(&x, 0, " %s", pItem->zName); |
| 109470 | + } |
| 109471 | + if( pItem->pTab ){ |
| 109472 | + sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName); |
| 109265 | 109473 | } |
| 109266 | 109474 | if( pItem->zAlias ){ |
| 109267 | | - sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias); |
| 109475 | + sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias); |
| 109268 | 109476 | } |
| 109269 | 109477 | if( pItem->jointype & JT_LEFT ){ |
| 109270 | | - sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN"); |
| 109478 | + sqlite3XPrintf(&x, 0, " LEFT-JOIN"); |
| 109271 | 109479 | } |
| 109272 | | - sqlite3ExplainNL(pVdbe); |
| 109480 | + sqlite3StrAccumFinish(&x); |
| 109481 | + sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); |
| 109482 | + if( pItem->pSelect ){ |
| 109483 | + sqlite3TreeViewSelect(pView, pItem->pSelect, 0); |
| 109484 | + } |
| 109485 | + sqlite3TreeViewPop(pView); |
| 109273 | 109486 | } |
| 109274 | | - sqlite3ExplainPop(pVdbe); |
| 109487 | + sqlite3TreeViewPop(pView); |
| 109275 | 109488 | } |
| 109276 | 109489 | if( p->pWhere ){ |
| 109277 | | - sqlite3ExplainPrintf(pVdbe, "WHERE "); |
| 109278 | | - sqlite3ExplainExpr(pVdbe, p->pWhere); |
| 109279 | | - sqlite3ExplainNL(pVdbe); |
| 109490 | + sqlite3TreeViewItem(pView, "WHERE", (n--)>0); |
| 109491 | + sqlite3TreeViewExpr(pView, p->pWhere, 0); |
| 109492 | + sqlite3TreeViewPop(pView); |
| 109280 | 109493 | } |
| 109281 | 109494 | if( p->pGroupBy ){ |
| 109282 | | - sqlite3ExplainPrintf(pVdbe, "GROUPBY "); |
| 109283 | | - sqlite3ExplainExprList(pVdbe, p->pGroupBy); |
| 109284 | | - sqlite3ExplainNL(pVdbe); |
| 109495 | + sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); |
| 109285 | 109496 | } |
| 109286 | 109497 | if( p->pHaving ){ |
| 109287 | | - sqlite3ExplainPrintf(pVdbe, "HAVING "); |
| 109288 | | - sqlite3ExplainExpr(pVdbe, p->pHaving); |
| 109289 | | - sqlite3ExplainNL(pVdbe); |
| 109498 | + sqlite3TreeViewItem(pView, "HAVING", (n--)>0); |
| 109499 | + sqlite3TreeViewExpr(pView, p->pHaving, 0); |
| 109500 | + sqlite3TreeViewPop(pView); |
| 109290 | 109501 | } |
| 109291 | 109502 | if( p->pOrderBy ){ |
| 109292 | | - sqlite3ExplainPrintf(pVdbe, "ORDERBY "); |
| 109293 | | - sqlite3ExplainExprList(pVdbe, p->pOrderBy); |
| 109294 | | - sqlite3ExplainNL(pVdbe); |
| 109503 | + sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); |
| 109295 | 109504 | } |
| 109296 | 109505 | if( p->pLimit ){ |
| 109297 | | - sqlite3ExplainPrintf(pVdbe, "LIMIT "); |
| 109298 | | - sqlite3ExplainExpr(pVdbe, p->pLimit); |
| 109299 | | - sqlite3ExplainNL(pVdbe); |
| 109506 | + sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); |
| 109507 | + sqlite3TreeViewExpr(pView, p->pLimit, 0); |
| 109508 | + sqlite3TreeViewPop(pView); |
| 109300 | 109509 | } |
| 109301 | 109510 | if( p->pOffset ){ |
| 109302 | | - sqlite3ExplainPrintf(pVdbe, "OFFSET "); |
| 109303 | | - sqlite3ExplainExpr(pVdbe, p->pOffset); |
| 109304 | | - sqlite3ExplainNL(pVdbe); |
| 109305 | | - } |
| 109306 | | -} |
| 109307 | | -SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){ |
| 109308 | | - if( p==0 ){ |
| 109309 | | - sqlite3ExplainPrintf(pVdbe, "(null-select)"); |
| 109310 | | - return; |
| 109311 | | - } |
| 109312 | | - sqlite3ExplainPush(pVdbe); |
| 109313 | | - while( p ){ |
| 109314 | | - explainOneSelect(pVdbe, p); |
| 109315 | | - p = p->pNext; |
| 109316 | | - if( p==0 ) break; |
| 109317 | | - sqlite3ExplainNL(pVdbe); |
| 109318 | | - sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op)); |
| 109319 | | - } |
| 109320 | | - sqlite3ExplainPrintf(pVdbe, "END"); |
| 109321 | | - sqlite3ExplainPop(pVdbe); |
| 109322 | | -} |
| 109323 | | - |
| 109324 | | -/* End of the structure debug printing code |
| 109325 | | -*****************************************************************************/ |
| 109326 | | -#endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */ |
| 109511 | + sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); |
| 109512 | + sqlite3TreeViewExpr(pView, p->pOffset, 0); |
| 109513 | + sqlite3TreeViewPop(pView); |
| 109514 | + } |
| 109515 | + if( p->pPrior ){ |
| 109516 | + const char *zOp = "UNION"; |
| 109517 | + switch( p->op ){ |
| 109518 | + case TK_ALL: zOp = "UNION ALL"; break; |
| 109519 | + case TK_INTERSECT: zOp = "INTERSECT"; break; |
| 109520 | + case TK_EXCEPT: zOp = "EXCEPT"; break; |
| 109521 | + } |
| 109522 | + sqlite3TreeViewItem(pView, zOp, (n--)>0); |
| 109523 | + sqlite3TreeViewSelect(pView, p->pPrior, 0); |
| 109524 | + sqlite3TreeViewPop(pView); |
| 109525 | + } |
| 109526 | + sqlite3TreeViewPop(pView); |
| 109527 | +} |
| 109528 | +#endif /* SQLITE_DEBUG */ |
| 109327 | 109529 | |
| 109328 | 109530 | /************** End of select.c **********************************************/ |
| 109329 | 109531 | /************** Begin file table.c *******************************************/ |
| 109330 | 109532 | /* |
| 109331 | 109533 | ** 2001 September 15 |
| | @@ -112311,10 +112513,11 @@ |
| 112311 | 112513 | } |
| 112312 | 112514 | sqlite3DbFree(db, pVTable); |
| 112313 | 112515 | }else if( ALWAYS(pVTable->pVtab) ){ |
| 112314 | 112516 | /* Justification of ALWAYS(): A correct vtab constructor must allocate |
| 112315 | 112517 | ** the sqlite3_vtab object if successful. */ |
| 112518 | + memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0])); |
| 112316 | 112519 | pVTable->pVtab->pModule = pMod->pModule; |
| 112317 | 112520 | pVTable->nRef = 1; |
| 112318 | 112521 | if( sCtx.pTab ){ |
| 112319 | 112522 | const char *zFormat = "vtable constructor did not declare schema: %s"; |
| 112320 | 112523 | *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); |
| | @@ -113719,15 +113922,10 @@ |
| 113719 | 113922 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 113720 | 113923 | assert( TK_GE==TK_EQ+4 ); |
| 113721 | 113924 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL; |
| 113722 | 113925 | } |
| 113723 | 113926 | |
| 113724 | | -/* |
| 113725 | | -** Swap two objects of type TYPE. |
| 113726 | | -*/ |
| 113727 | | -#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 113728 | | - |
| 113729 | 113927 | /* |
| 113730 | 113928 | ** Commute a comparison operator. Expressions of the form "X op Y" |
| 113731 | 113929 | ** are converted into "Y op X". |
| 113732 | 113930 | ** |
| 113733 | 113931 | ** If left/right precedence rules come into play when determining the |
| | @@ -115566,21 +115764,28 @@ |
| 115566 | 115764 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 115567 | 115765 | whereKeyStats(pParse, p, pRec, 0, a); |
| 115568 | 115766 | iLower = a[0]; |
| 115569 | 115767 | iUpper = a[0] + a[1]; |
| 115570 | 115768 | } |
| 115769 | + |
| 115770 | + assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
| 115771 | + assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
| 115772 | + assert( p->aSortOrder!=0 ); |
| 115773 | + if( p->aSortOrder[nEq] ){ |
| 115774 | + /* The roles of pLower and pUpper are swapped for a DESC index */ |
| 115775 | + SWAP(WhereTerm*, pLower, pUpper); |
| 115776 | + } |
| 115571 | 115777 | |
| 115572 | 115778 | /* If possible, improve on the iLower estimate using ($P:$L). */ |
| 115573 | 115779 | if( pLower ){ |
| 115574 | 115780 | int bOk; /* True if value is extracted from pExpr */ |
| 115575 | 115781 | Expr *pExpr = pLower->pExpr->pRight; |
| 115576 | | - assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
| 115577 | 115782 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115578 | 115783 | if( rc==SQLITE_OK && bOk ){ |
| 115579 | 115784 | tRowcnt iNew; |
| 115580 | 115785 | whereKeyStats(pParse, p, pRec, 0, a); |
| 115581 | | - iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0); |
| 115786 | + iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115582 | 115787 | if( iNew>iLower ) iLower = iNew; |
| 115583 | 115788 | nOut--; |
| 115584 | 115789 | pLower = 0; |
| 115585 | 115790 | } |
| 115586 | 115791 | } |
| | @@ -115587,16 +115792,15 @@ |
| 115587 | 115792 | |
| 115588 | 115793 | /* If possible, improve on the iUpper estimate using ($P:$U). */ |
| 115589 | 115794 | if( pUpper ){ |
| 115590 | 115795 | int bOk; /* True if value is extracted from pExpr */ |
| 115591 | 115796 | Expr *pExpr = pUpper->pExpr->pRight; |
| 115592 | | - assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
| 115593 | 115797 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115594 | 115798 | if( rc==SQLITE_OK && bOk ){ |
| 115595 | 115799 | tRowcnt iNew; |
| 115596 | 115800 | whereKeyStats(pParse, p, pRec, 1, a); |
| 115597 | | - iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0); |
| 115801 | + iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115598 | 115802 | if( iNew<iUpper ) iUpper = iNew; |
| 115599 | 115803 | nOut--; |
| 115600 | 115804 | pUpper = 0; |
| 115601 | 115805 | } |
| 115602 | 115806 | } |
| | @@ -116091,65 +116295,52 @@ |
| 116091 | 116295 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 116092 | 116296 | } |
| 116093 | 116297 | |
| 116094 | 116298 | /* |
| 116095 | 116299 | ** Argument pLevel describes a strategy for scanning table pTab. This |
| 116096 | | -** function returns a pointer to a string buffer containing a description |
| 116097 | | -** of the subset of table rows scanned by the strategy in the form of an |
| 116098 | | -** SQL expression. Or, if all rows are scanned, NULL is returned. |
| 116300 | +** function appends text to pStr that describes the subset of table |
| 116301 | +** rows scanned by the strategy in the form of an SQL expression. |
| 116099 | 116302 | ** |
| 116100 | 116303 | ** For example, if the query: |
| 116101 | 116304 | ** |
| 116102 | 116305 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 116103 | 116306 | ** |
| 116104 | 116307 | ** is run and there is an index on (a, b), then this function returns a |
| 116105 | 116308 | ** string similar to: |
| 116106 | 116309 | ** |
| 116107 | 116310 | ** "a=? AND b>?" |
| 116108 | | -** |
| 116109 | | -** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 116110 | | -** It is the responsibility of the caller to free the buffer when it is |
| 116111 | | -** no longer required. |
| 116112 | 116311 | */ |
| 116113 | | -static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 116312 | +static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
| 116114 | 116313 | Index *pIndex = pLoop->u.btree.pIndex; |
| 116115 | 116314 | u16 nEq = pLoop->u.btree.nEq; |
| 116116 | 116315 | u16 nSkip = pLoop->u.btree.nSkip; |
| 116117 | 116316 | int i, j; |
| 116118 | 116317 | Column *aCol = pTab->aCol; |
| 116119 | 116318 | i16 *aiColumn = pIndex->aiColumn; |
| 116120 | | - StrAccum txt; |
| 116121 | | - |
| 116122 | | - if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
| 116123 | | - return 0; |
| 116124 | | - } |
| 116125 | | - sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
| 116126 | | - txt.db = db; |
| 116127 | | - sqlite3StrAccumAppend(&txt, " (", 2); |
| 116319 | + |
| 116320 | + if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| 116321 | + sqlite3StrAccumAppend(pStr, " (", 2); |
| 116128 | 116322 | for(i=0; i<nEq; i++){ |
| 116129 | 116323 | char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName; |
| 116130 | 116324 | if( i>=nSkip ){ |
| 116131 | | - explainAppendTerm(&txt, i, z, "="); |
| 116325 | + explainAppendTerm(pStr, i, z, "="); |
| 116132 | 116326 | }else{ |
| 116133 | | - if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5); |
| 116134 | | - sqlite3StrAccumAppend(&txt, "ANY(", 4); |
| 116135 | | - sqlite3StrAccumAppendAll(&txt, z); |
| 116136 | | - sqlite3StrAccumAppend(&txt, ")", 1); |
| 116327 | + if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 116328 | + sqlite3XPrintf(pStr, 0, "ANY(%s)", z); |
| 116137 | 116329 | } |
| 116138 | 116330 | } |
| 116139 | 116331 | |
| 116140 | 116332 | j = i; |
| 116141 | 116333 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
| 116142 | 116334 | char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
| 116143 | | - explainAppendTerm(&txt, i++, z, ">"); |
| 116335 | + explainAppendTerm(pStr, i++, z, ">"); |
| 116144 | 116336 | } |
| 116145 | 116337 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
| 116146 | 116338 | char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
| 116147 | | - explainAppendTerm(&txt, i, z, "<"); |
| 116339 | + explainAppendTerm(pStr, i, z, "<"); |
| 116148 | 116340 | } |
| 116149 | | - sqlite3StrAccumAppend(&txt, ")", 1); |
| 116150 | | - return sqlite3StrAccumFinish(&txt); |
| 116341 | + sqlite3StrAccumAppend(pStr, ")", 1); |
| 116151 | 116342 | } |
| 116152 | 116343 | |
| 116153 | 116344 | /* |
| 116154 | 116345 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 116155 | 116346 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| | @@ -116169,72 +116360,90 @@ |
| 116169 | 116360 | #endif |
| 116170 | 116361 | { |
| 116171 | 116362 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 116172 | 116363 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 116173 | 116364 | sqlite3 *db = pParse->db; /* Database handle */ |
| 116174 | | - char *zMsg; /* Text to add to EQP output */ |
| 116175 | 116365 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| 116176 | 116366 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 116177 | 116367 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 116178 | 116368 | u32 flags; /* Flags that describe this loop */ |
| 116369 | + char *zMsg; /* Text to add to EQP output */ |
| 116370 | + StrAccum str; /* EQP output string */ |
| 116371 | + char zBuf[100]; /* Initial space for EQP output string */ |
| 116179 | 116372 | |
| 116180 | 116373 | pLoop = pLevel->pWLoop; |
| 116181 | 116374 | flags = pLoop->wsFlags; |
| 116182 | 116375 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
| 116183 | 116376 | |
| 116184 | 116377 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 116185 | 116378 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 116186 | 116379 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 116187 | 116380 | |
| 116188 | | - zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
| 116381 | + sqlite3StrAccumInit(&str, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
| 116382 | + str.db = db; |
| 116383 | + sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); |
| 116189 | 116384 | if( pItem->pSelect ){ |
| 116190 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
| 116385 | + sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId); |
| 116191 | 116386 | }else{ |
| 116192 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); |
| 116387 | + sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName); |
| 116193 | 116388 | } |
| 116194 | 116389 | |
| 116195 | 116390 | if( pItem->zAlias ){ |
| 116196 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 116197 | | - } |
| 116198 | | - if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
| 116199 | | - && ALWAYS(pLoop->u.btree.pIndex!=0) |
| 116200 | | - ){ |
| 116201 | | - const char *zFmt; |
| 116202 | | - Index *pIdx = pLoop->u.btree.pIndex; |
| 116203 | | - char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
| 116391 | + sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias); |
| 116392 | + } |
| 116393 | + if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ |
| 116394 | + const char *zFmt = 0; |
| 116395 | + Index *pIdx; |
| 116396 | + |
| 116397 | + assert( pLoop->u.btree.pIndex!=0 ); |
| 116398 | + pIdx = pLoop->u.btree.pIndex; |
| 116204 | 116399 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
| 116205 | 116400 | if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 116206 | | - zFmt = zWhere ? "%s USING PRIMARY KEY%.0s%s" : "%s%.0s%s"; |
| 116401 | + if( isSearch ){ |
| 116402 | + zFmt = "PRIMARY KEY"; |
| 116403 | + } |
| 116207 | 116404 | }else if( flags & WHERE_AUTO_INDEX ){ |
| 116208 | | - zFmt = "%s USING AUTOMATIC COVERING INDEX%.0s%s"; |
| 116405 | + zFmt = "AUTOMATIC COVERING INDEX"; |
| 116209 | 116406 | }else if( flags & WHERE_IDX_ONLY ){ |
| 116210 | | - zFmt = "%s USING COVERING INDEX %s%s"; |
| 116407 | + zFmt = "COVERING INDEX %s"; |
| 116211 | 116408 | }else{ |
| 116212 | | - zFmt = "%s USING INDEX %s%s"; |
| 116409 | + zFmt = "INDEX %s"; |
| 116213 | 116410 | } |
| 116214 | | - zMsg = sqlite3MAppendf(db, zMsg, zFmt, zMsg, pIdx->zName, zWhere); |
| 116215 | | - sqlite3DbFree(db, zWhere); |
| 116411 | + if( zFmt ){ |
| 116412 | + sqlite3StrAccumAppend(&str, " USING ", 7); |
| 116413 | + sqlite3XPrintf(&str, 0, zFmt, pIdx->zName); |
| 116414 | + explainIndexRange(&str, pLoop, pItem->pTab); |
| 116415 | + } |
| 116216 | 116416 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
| 116217 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
| 116218 | | - |
| 116417 | + const char *zRange; |
| 116219 | 116418 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
| 116220 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
| 116419 | + zRange = "(rowid=?)"; |
| 116221 | 116420 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
| 116222 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 116421 | + zRange = "(rowid>? AND rowid<?)"; |
| 116223 | 116422 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 116224 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
| 116225 | | - }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){ |
| 116226 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 116423 | + zRange = "(rowid>?)"; |
| 116424 | + }else{ |
| 116425 | + assert( flags&WHERE_TOP_LIMIT); |
| 116426 | + zRange = "(rowid<?)"; |
| 116227 | 116427 | } |
| 116428 | + sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY "); |
| 116429 | + sqlite3StrAccumAppendAll(&str, zRange); |
| 116228 | 116430 | } |
| 116229 | 116431 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 116230 | 116432 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
| 116231 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
| 116433 | + sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s", |
| 116232 | 116434 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
| 116233 | 116435 | } |
| 116234 | 116436 | #endif |
| 116235 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); |
| 116437 | +#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS |
| 116438 | + if( pLoop->nOut>=10 ){ |
| 116439 | + sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut)); |
| 116440 | + }else{ |
| 116441 | + sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 116442 | + } |
| 116443 | +#endif |
| 116444 | + zMsg = sqlite3StrAccumFinish(&str); |
| 116236 | 116445 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
| 116237 | 116446 | } |
| 116238 | 116447 | } |
| 116239 | 116448 | #else |
| 116240 | 116449 | # define explainOneScan(u,v,w,x,y,z) |
| | @@ -116884,12 +117093,13 @@ |
| 116884 | 117093 | |
| 116885 | 117094 | /* Run a separate WHERE clause for each term of the OR clause. After |
| 116886 | 117095 | ** eliminating duplicates from other WHERE clauses, the action for each |
| 116887 | 117096 | ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 116888 | 117097 | */ |
| 116889 | | - wctrlFlags = WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
| 116890 | | - WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY; |
| 117098 | + wctrlFlags = WHERE_OMIT_OPEN_CLOSE |
| 117099 | + | WHERE_FORCE_TABLE |
| 117100 | + | WHERE_ONETABLE_ONLY; |
| 116891 | 117101 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 116892 | 117102 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
| 116893 | 117103 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 116894 | 117104 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 116895 | 117105 | Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
| | @@ -116897,10 +117107,11 @@ |
| 116897 | 117107 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
| 116898 | 117108 | pAndExpr->pLeft = pOrExpr; |
| 116899 | 117109 | pOrExpr = pAndExpr; |
| 116900 | 117110 | } |
| 116901 | 117111 | /* Loop through table entries that match term pOrTerm. */ |
| 117112 | + WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); |
| 116902 | 117113 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 116903 | 117114 | wctrlFlags, iCovCur); |
| 116904 | 117115 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 116905 | 117116 | if( pSubWInfo ){ |
| 116906 | 117117 | WhereLoop *pSubLoop; |
| | @@ -117116,25 +117327,30 @@ |
| 117116 | 117327 | } |
| 117117 | 117328 | |
| 117118 | 117329 | return pLevel->notReady; |
| 117119 | 117330 | } |
| 117120 | 117331 | |
| 117121 | | -#if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 117332 | +#ifdef WHERETRACE_ENABLED |
| 117122 | 117333 | /* |
| 117123 | | -** Generate "Explanation" text for a WhereTerm. |
| 117334 | +** Print the content of a WhereTerm object |
| 117124 | 117335 | */ |
| 117125 | | -static void whereExplainTerm(Vdbe *v, WhereTerm *pTerm){ |
| 117126 | | - char zType[4]; |
| 117127 | | - memcpy(zType, "...", 4); |
| 117128 | | - if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 117129 | | - if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 117130 | | - if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
| 117131 | | - sqlite3ExplainPrintf(v, "%s ", zType); |
| 117132 | | - sqlite3ExplainExpr(v, pTerm->pExpr); |
| 117133 | | -} |
| 117134 | | -#endif /* WHERETRACE_ENABLED && SQLITE_ENABLE_TREE_EXPLAIN */ |
| 117135 | | - |
| 117336 | +static void whereTermPrint(WhereTerm *pTerm, int iTerm){ |
| 117337 | + if( pTerm==0 ){ |
| 117338 | + sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); |
| 117339 | + }else{ |
| 117340 | + char zType[4]; |
| 117341 | + memcpy(zType, "...", 4); |
| 117342 | + if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 117343 | + if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 117344 | + if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
| 117345 | + sqlite3DebugPrintf("TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x\n", |
| 117346 | + iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb, |
| 117347 | + pTerm->eOperator); |
| 117348 | + sqlite3TreeViewExpr(0, pTerm->pExpr, 0); |
| 117349 | + } |
| 117350 | +} |
| 117351 | +#endif |
| 117136 | 117352 | |
| 117137 | 117353 | #ifdef WHERETRACE_ENABLED |
| 117138 | 117354 | /* |
| 117139 | 117355 | ** Print a WhereLoop object for debugging purposes |
| 117140 | 117356 | */ |
| | @@ -117174,31 +117390,16 @@ |
| 117174 | 117390 | sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip); |
| 117175 | 117391 | }else{ |
| 117176 | 117392 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 117177 | 117393 | } |
| 117178 | 117394 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
| 117179 | | -#ifdef SQLITE_ENABLE_TREE_EXPLAIN |
| 117180 | | - /* If the 0x100 bit of wheretracing is set, then show all of the constraint |
| 117181 | | - ** expressions in the WhereLoop.aLTerm[] array. |
| 117182 | | - */ |
| 117183 | | - if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ /* WHERETRACE 0x100 */ |
| 117395 | + if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| 117184 | 117396 | int i; |
| 117185 | | - Vdbe *v = pWInfo->pParse->pVdbe; |
| 117186 | | - sqlite3ExplainBegin(v); |
| 117187 | 117397 | for(i=0; i<p->nLTerm; i++){ |
| 117188 | | - WhereTerm *pTerm = p->aLTerm[i]; |
| 117189 | | - if( pTerm==0 ) continue; |
| 117190 | | - sqlite3ExplainPrintf(v, " (%d) #%-2d ", i+1, (int)(pTerm-pWC->a)); |
| 117191 | | - sqlite3ExplainPush(v); |
| 117192 | | - whereExplainTerm(v, pTerm); |
| 117193 | | - sqlite3ExplainPop(v); |
| 117194 | | - sqlite3ExplainNL(v); |
| 117195 | | - } |
| 117196 | | - sqlite3ExplainFinish(v); |
| 117197 | | - sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v)); |
| 117198 | | - } |
| 117199 | | -#endif |
| 117398 | + whereTermPrint(p->aLTerm[i], i); |
| 117399 | + } |
| 117400 | + } |
| 117200 | 117401 | } |
| 117201 | 117402 | #endif |
| 117202 | 117403 | |
| 117203 | 117404 | /* |
| 117204 | 117405 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| | @@ -117507,11 +117708,11 @@ |
| 117507 | 117708 | if( ppPrev==0 ){ |
| 117508 | 117709 | /* There already exists a WhereLoop on the list that is better |
| 117509 | 117710 | ** than pTemplate, so just ignore pTemplate */ |
| 117510 | 117711 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 117511 | 117712 | if( sqlite3WhereTrace & 0x8 ){ |
| 117512 | | - sqlite3DebugPrintf("ins-noop: "); |
| 117713 | + sqlite3DebugPrintf(" skip: "); |
| 117513 | 117714 | whereLoopPrint(pTemplate, pBuilder->pWC); |
| 117514 | 117715 | } |
| 117515 | 117716 | #endif |
| 117516 | 117717 | return SQLITE_OK; |
| 117517 | 117718 | }else{ |
| | @@ -117523,14 +117724,14 @@ |
| 117523 | 117724 | ** WhereLoop and insert it. |
| 117524 | 117725 | */ |
| 117525 | 117726 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 117526 | 117727 | if( sqlite3WhereTrace & 0x8 ){ |
| 117527 | 117728 | if( p!=0 ){ |
| 117528 | | - sqlite3DebugPrintf("ins-del: "); |
| 117729 | + sqlite3DebugPrintf("replace: "); |
| 117529 | 117730 | whereLoopPrint(p, pBuilder->pWC); |
| 117530 | 117731 | } |
| 117531 | | - sqlite3DebugPrintf("ins-new: "); |
| 117732 | + sqlite3DebugPrintf(" add: "); |
| 117532 | 117733 | whereLoopPrint(pTemplate, pBuilder->pWC); |
| 117533 | 117734 | } |
| 117534 | 117735 | #endif |
| 117535 | 117736 | if( p==0 ){ |
| 117536 | 117737 | /* Allocate a new WhereLoop to add to the end of the list */ |
| | @@ -117550,11 +117751,11 @@ |
| 117550 | 117751 | pToDel = *ppTail; |
| 117551 | 117752 | if( pToDel==0 ) break; |
| 117552 | 117753 | *ppTail = pToDel->pNextLoop; |
| 117553 | 117754 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 117554 | 117755 | if( sqlite3WhereTrace & 0x8 ){ |
| 117555 | | - sqlite3DebugPrintf("ins-del: "); |
| 117756 | + sqlite3DebugPrintf(" delete: "); |
| 117556 | 117757 | whereLoopPrint(pToDel, pBuilder->pWC); |
| 117557 | 117758 | } |
| 117558 | 117759 | #endif |
| 117559 | 117760 | whereLoopDelete(db, pToDel); |
| 117560 | 117761 | } |
| | @@ -117714,15 +117915,18 @@ |
| 117714 | 117915 | pNew->aLTerm[pNew->nLTerm++] = 0; |
| 117715 | 117916 | pNew->wsFlags |= WHERE_SKIPSCAN; |
| 117716 | 117917 | nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
| 117717 | 117918 | if( pTerm ){ |
| 117718 | 117919 | /* TUNING: When estimating skip-scan for a term that is also indexable, |
| 117719 | | - ** increase the cost of the skip-scan by 2x, to make it a little less |
| 117920 | + ** multiply the cost of the skip-scan by 2.0, to make it a little less |
| 117720 | 117921 | ** desirable than the regular index lookup. */ |
| 117721 | 117922 | nIter += 10; assert( 10==sqlite3LogEst(2) ); |
| 117722 | 117923 | } |
| 117723 | 117924 | pNew->nOut -= nIter; |
| 117925 | + /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 117926 | + ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 117927 | + nIter += 5; |
| 117724 | 117928 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 117725 | 117929 | pNew->nOut = saved_nOut; |
| 117726 | 117930 | pNew->u.btree.nEq = saved_nEq; |
| 117727 | 117931 | pNew->u.btree.nSkip = saved_nSkip; |
| 117728 | 117932 | } |
| | @@ -118073,13 +118277,21 @@ |
| 118073 | 118277 | pNew->u.btree.nSkip = 0; |
| 118074 | 118278 | pNew->u.btree.pIndex = 0; |
| 118075 | 118279 | pNew->nLTerm = 1; |
| 118076 | 118280 | pNew->aLTerm[0] = pTerm; |
| 118077 | 118281 | /* TUNING: One-time cost for computing the automatic index is |
| 118078 | | - ** approximately 7*N*log2(N) where N is the number of rows in |
| 118079 | | - ** the table being indexed. */ |
| 118080 | | - pNew->rSetup = rLogSize + rSize + 28; assert( 28==sqlite3LogEst(7) ); |
| 118282 | + ** estimated to be X*N*log2(N) where N is the number of rows in |
| 118283 | + ** the table being indexed and where X is 7 (LogEst=28) for normal |
| 118284 | + ** tables or 1.375 (LogEst=4) for views and subqueries. The value |
| 118285 | + ** of X is smaller for views and subqueries so that the query planner |
| 118286 | + ** will be more aggressive about generating automatic indexes for |
| 118287 | + ** those objects, since there is no opportunity to add schema |
| 118288 | + ** indexes on subqueries and views. */ |
| 118289 | + pNew->rSetup = rLogSize + rSize + 4; |
| 118290 | + if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ |
| 118291 | + pNew->rSetup += 24; |
| 118292 | + } |
| 118081 | 118293 | ApplyCostMultiplier(pNew->rSetup, pTab->costMult); |
| 118082 | 118294 | /* TUNING: Each index lookup yields 20 rows in the table. This |
| 118083 | 118295 | ** is more than the usual guess of 10 rows, since we have no way |
| 118084 | 118296 | ** of knowing how selective the index will ultimately be. It would |
| 118085 | 118297 | ** not be unreasonable to make this value much larger. */ |
| | @@ -118363,11 +118575,10 @@ |
| 118363 | 118575 | WhereLoopBuilder sSubBuild; |
| 118364 | 118576 | WhereOrSet sSum, sCur; |
| 118365 | 118577 | struct SrcList_item *pItem; |
| 118366 | 118578 | |
| 118367 | 118579 | pWC = pBuilder->pWC; |
| 118368 | | - if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
| 118369 | 118580 | pWCEnd = pWC->a + pWC->nTerm; |
| 118370 | 118581 | pNew = pBuilder->pNew; |
| 118371 | 118582 | memset(&sSum, 0, sizeof(sSum)); |
| 118372 | 118583 | pItem = pWInfo->pTabList->a + pNew->iTab; |
| 118373 | 118584 | iCur = pItem->iCursor; |
| | @@ -118384,10 +118595,11 @@ |
| 118384 | 118595 | |
| 118385 | 118596 | sSubBuild = *pBuilder; |
| 118386 | 118597 | sSubBuild.pOrderBy = 0; |
| 118387 | 118598 | sSubBuild.pOrSet = &sCur; |
| 118388 | 118599 | |
| 118600 | + WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm)); |
| 118389 | 118601 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
| 118390 | 118602 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 118391 | 118603 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 118392 | 118604 | }else if( pOrTerm->leftCursor==iCur ){ |
| 118393 | 118605 | tempWC.pWInfo = pWC->pWInfo; |
| | @@ -118398,18 +118610,30 @@ |
| 118398 | 118610 | sSubBuild.pWC = &tempWC; |
| 118399 | 118611 | }else{ |
| 118400 | 118612 | continue; |
| 118401 | 118613 | } |
| 118402 | 118614 | sCur.n = 0; |
| 118615 | +#ifdef WHERETRACE_ENABLED |
| 118616 | + WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", |
| 118617 | + (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); |
| 118618 | + if( sqlite3WhereTrace & 0x400 ){ |
| 118619 | + for(i=0; i<sSubBuild.pWC->nTerm; i++){ |
| 118620 | + whereTermPrint(&sSubBuild.pWC->a[i], i); |
| 118621 | + } |
| 118622 | + } |
| 118623 | +#endif |
| 118403 | 118624 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 118404 | 118625 | if( IsVirtual(pItem->pTab) ){ |
| 118405 | 118626 | rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
| 118406 | 118627 | }else |
| 118407 | 118628 | #endif |
| 118408 | 118629 | { |
| 118409 | 118630 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 118410 | 118631 | } |
| 118632 | + if( rc==SQLITE_OK ){ |
| 118633 | + rc = whereLoopAddOr(&sSubBuild, mExtra); |
| 118634 | + } |
| 118411 | 118635 | assert( rc==SQLITE_OK || sCur.n==0 ); |
| 118412 | 118636 | if( sCur.n==0 ){ |
| 118413 | 118637 | sSum.n = 0; |
| 118414 | 118638 | break; |
| 118415 | 118639 | }else if( once ){ |
| | @@ -118450,10 +118674,11 @@ |
| 118450 | 118674 | pNew->rRun = sSum.a[i].rRun + 1; |
| 118451 | 118675 | pNew->nOut = sSum.a[i].nOut; |
| 118452 | 118676 | pNew->prereq = sSum.a[i].prereq; |
| 118453 | 118677 | rc = whereLoopInsert(pBuilder, pNew); |
| 118454 | 118678 | } |
| 118679 | + WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm)); |
| 118455 | 118680 | } |
| 118456 | 118681 | } |
| 118457 | 118682 | return rc; |
| 118458 | 118683 | } |
| 118459 | 118684 | |
| | @@ -118693,11 +118918,11 @@ |
| 118693 | 118918 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 118694 | 118919 | } |
| 118695 | 118920 | isMatch = 1; |
| 118696 | 118921 | break; |
| 118697 | 118922 | } |
| 118698 | | - if( isMatch && (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
| 118923 | + if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){ |
| 118699 | 118924 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 118700 | 118925 | ** Sort order is irrelevant for a GROUP BY clause. */ |
| 118701 | 118926 | if( revSet ){ |
| 118702 | 118927 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; |
| 118703 | 118928 | }else{ |
| | @@ -119158,16 +119383,19 @@ |
| 119158 | 119383 | pWInfo->revMask = pFrom->revLoop; |
| 119159 | 119384 | } |
| 119160 | 119385 | if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) |
| 119161 | 119386 | && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr |
| 119162 | 119387 | ){ |
| 119163 | | - Bitmask notUsed = 0; |
| 119388 | + Bitmask revMask = 0; |
| 119164 | 119389 | int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, |
| 119165 | | - pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used |
| 119390 | + pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask |
| 119166 | 119391 | ); |
| 119167 | 119392 | assert( pWInfo->sorted==0 ); |
| 119168 | | - pWInfo->sorted = (nOrder==pWInfo->pOrderBy->nExpr); |
| 119393 | + if( nOrder==pWInfo->pOrderBy->nExpr ){ |
| 119394 | + pWInfo->sorted = 1; |
| 119395 | + pWInfo->revMask = revMask; |
| 119396 | + } |
| 119169 | 119397 | } |
| 119170 | 119398 | } |
| 119171 | 119399 | |
| 119172 | 119400 | |
| 119173 | 119401 | pWInfo->nRowOut = pFrom->nRow; |
| | @@ -119516,27 +119744,20 @@ |
| 119516 | 119744 | } |
| 119517 | 119745 | } |
| 119518 | 119746 | |
| 119519 | 119747 | /* Construct the WhereLoop objects */ |
| 119520 | 119748 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
| 119749 | +#if defined(WHERETRACE_ENABLED) |
| 119521 | 119750 | /* Display all terms of the WHERE clause */ |
| 119522 | | -#if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 119523 | 119751 | if( sqlite3WhereTrace & 0x100 ){ |
| 119524 | 119752 | int i; |
| 119525 | | - Vdbe *v = pParse->pVdbe; |
| 119526 | | - sqlite3ExplainBegin(v); |
| 119527 | 119753 | for(i=0; i<sWLB.pWC->nTerm; i++){ |
| 119528 | | - sqlite3ExplainPrintf(v, "#%-2d ", i); |
| 119529 | | - sqlite3ExplainPush(v); |
| 119530 | | - whereExplainTerm(v, &sWLB.pWC->a[i]); |
| 119531 | | - sqlite3ExplainPop(v); |
| 119532 | | - sqlite3ExplainNL(v); |
| 119533 | | - } |
| 119534 | | - sqlite3ExplainFinish(v); |
| 119535 | | - sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v)); |
| 119754 | + whereTermPrint(&sWLB.pWC->a[i], i); |
| 119755 | + } |
| 119536 | 119756 | } |
| 119537 | 119757 | #endif |
| 119758 | + |
| 119538 | 119759 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
| 119539 | 119760 | rc = whereLoopAddAll(&sWLB); |
| 119540 | 119761 | if( rc ) goto whereBeginError; |
| 119541 | 119762 | |
| 119542 | 119763 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
| | @@ -120059,11 +120280,11 @@ |
| 120059 | 120280 | |
| 120060 | 120281 | /* A routine to convert a binary TK_IS or TK_ISNOT expression into a |
| 120061 | 120282 | ** unary TK_ISNULL or TK_NOTNULL expression. */ |
| 120062 | 120283 | static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ |
| 120063 | 120284 | sqlite3 *db = pParse->db; |
| 120064 | | - if( db->mallocFailed==0 && pY->op==TK_NULL ){ |
| 120285 | + if( pY && pA && pY->op==TK_NULL ){ |
| 120065 | 120286 | pA->op = (u8)op; |
| 120066 | 120287 | sqlite3ExprDelete(db, pA->pRight); |
| 120067 | 120288 | pA->pRight = 0; |
| 120068 | 120289 | } |
| 120069 | 120290 | } |
| | @@ -122318,13 +122539,10 @@ |
| 122318 | 122539 | break; |
| 122319 | 122540 | case 111: /* cmd ::= select */ |
| 122320 | 122541 | { |
| 122321 | 122542 | SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0}; |
| 122322 | 122543 | sqlite3Select(pParse, yymsp[0].minor.yy3, &dest); |
| 122323 | | - sqlite3ExplainBegin(pParse->pVdbe); |
| 122324 | | - sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy3); |
| 122325 | | - sqlite3ExplainFinish(pParse->pVdbe); |
| 122326 | 122544 | sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3); |
| 122327 | 122545 | } |
| 122328 | 122546 | break; |
| 122329 | 122547 | case 112: /* select ::= with selectnowith */ |
| 122330 | 122548 | { |
| | @@ -122377,10 +122595,34 @@ |
| 122377 | 122595 | {yygotominor.yy328 = TK_ALL;} |
| 122378 | 122596 | break; |
| 122379 | 122597 | case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */ |
| 122380 | 122598 | { |
| 122381 | 122599 | yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy14,yymsp[-5].minor.yy65,yymsp[-4].minor.yy132,yymsp[-3].minor.yy14,yymsp[-2].minor.yy132,yymsp[-1].minor.yy14,yymsp[-7].minor.yy381,yymsp[0].minor.yy476.pLimit,yymsp[0].minor.yy476.pOffset); |
| 122600 | +#if SELECTTRACE_ENABLED |
| 122601 | + /* Populate the Select.zSelName[] string that is used to help with |
| 122602 | + ** query planner debugging, to differentiate between multiple Select |
| 122603 | + ** objects in a complex query. |
| 122604 | + ** |
| 122605 | + ** If the SELECT keyword is immediately followed by a C-style comment |
| 122606 | + ** then extract the first few alphanumeric characters from within that |
| 122607 | + ** comment to be the zSelName value. Otherwise, the label is #N where |
| 122608 | + ** is an integer that is incremented with each SELECT statement seen. |
| 122609 | + */ |
| 122610 | + if( yygotominor.yy3!=0 ){ |
| 122611 | + const char *z = yymsp[-8].minor.yy0.z+6; |
| 122612 | + int i; |
| 122613 | + sqlite3_snprintf(sizeof(yygotominor.yy3->zSelName), yygotominor.yy3->zSelName, "#%d", |
| 122614 | + ++pParse->nSelect); |
| 122615 | + while( z[0]==' ' ) z++; |
| 122616 | + if( z[0]=='/' && z[1]=='*' ){ |
| 122617 | + z += 2; |
| 122618 | + while( z[0]==' ' ) z++; |
| 122619 | + for(i=0; sqlite3Isalnum(z[i]); i++){} |
| 122620 | + sqlite3_snprintf(sizeof(yygotominor.yy3->zSelName), yygotominor.yy3->zSelName, "%.*s", i, z); |
| 122621 | + } |
| 122622 | + } |
| 122623 | +#endif /* SELECTRACE_ENABLED */ |
| 122382 | 122624 | } |
| 122383 | 122625 | break; |
| 122384 | 122626 | case 120: /* values ::= VALUES LP nexprlist RP */ |
| 122385 | 122627 | { |
| 122386 | 122628 | yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0); |
| | @@ -123868,10 +124110,11 @@ |
| 123868 | 124110 | 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ |
| 123869 | 124111 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ |
| 123870 | 124112 | }; |
| 123871 | 124113 | #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) |
| 123872 | 124114 | #endif |
| 124115 | +SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); } |
| 123873 | 124116 | |
| 123874 | 124117 | |
| 123875 | 124118 | /* |
| 123876 | 124119 | ** Return the length of the token that begins at z[0]. |
| 123877 | 124120 | ** Store the token type in *tokenType before returning. |
| | @@ -125138,10 +125381,15 @@ |
| 125138 | 125381 | sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t); |
| 125139 | 125382 | sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); |
| 125140 | 125383 | break; |
| 125141 | 125384 | } |
| 125142 | 125385 | |
| 125386 | + /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames |
| 125387 | + ** can be changed at start-time using the |
| 125388 | + ** sqlite3_config(SQLITE_CONFIG_URI,1) or |
| 125389 | + ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. |
| 125390 | + */ |
| 125143 | 125391 | case SQLITE_CONFIG_URI: { |
| 125144 | 125392 | sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); |
| 125145 | 125393 | break; |
| 125146 | 125394 | } |
| 125147 | 125395 | |
| | @@ -126875,11 +127123,11 @@ |
| 126875 | 127123 | int nUri = sqlite3Strlen30(zUri); |
| 126876 | 127124 | |
| 126877 | 127125 | assert( *pzErrMsg==0 ); |
| 126878 | 127126 | |
| 126879 | 127127 | if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) |
| 126880 | | - && nUri>=5 && memcmp(zUri, "file:", 5)==0 |
| 127128 | + && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ |
| 126881 | 127129 | ){ |
| 126882 | 127130 | char *zOpt; |
| 126883 | 127131 | int eState; /* Parser state when parsing URI */ |
| 126884 | 127132 | int iIn; /* Input character index */ |
| 126885 | 127133 | int iOut = 0; /* Output character index */ |
| | @@ -127105,11 +127353,13 @@ |
| 127105 | 127353 | assert( SQLITE_OPEN_READWRITE == 0x02 ); |
| 127106 | 127354 | assert( SQLITE_OPEN_CREATE == 0x04 ); |
| 127107 | 127355 | testcase( (1<<(flags&7))==0x02 ); /* READONLY */ |
| 127108 | 127356 | testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ |
| 127109 | 127357 | testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ |
| 127110 | | - if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT; |
| 127358 | + if( ((1<<(flags&7)) & 0x46)==0 ){ |
| 127359 | + return SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */ |
| 127360 | + } |
| 127111 | 127361 | |
| 127112 | 127362 | if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
| 127113 | 127363 | isThreadsafe = 0; |
| 127114 | 127364 | }else if( flags & SQLITE_OPEN_NOMUTEX ){ |
| 127115 | 127365 | isThreadsafe = 0; |
| | @@ -127989,26 +128239,10 @@ |
| 127989 | 128239 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: { |
| 127990 | 128240 | sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); |
| 127991 | 128241 | break; |
| 127992 | 128242 | } |
| 127993 | 128243 | |
| 127994 | | -#if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
| 127995 | | - /* sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, |
| 127996 | | - ** sqlite3_stmt*,const char**); |
| 127997 | | - ** |
| 127998 | | - ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds |
| 127999 | | - ** a string that describes the optimized parse tree. This test-control |
| 128000 | | - ** returns a pointer to that string. |
| 128001 | | - */ |
| 128002 | | - case SQLITE_TESTCTRL_EXPLAIN_STMT: { |
| 128003 | | - sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*); |
| 128004 | | - const char **pzRet = va_arg(ap, const char**); |
| 128005 | | - *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt); |
| 128006 | | - break; |
| 128007 | | - } |
| 128008 | | -#endif |
| 128009 | | - |
| 128010 | 128244 | /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int); |
| 128011 | 128245 | ** |
| 128012 | 128246 | ** Set or clear a flag that indicates that the database file is always well- |
| 128013 | 128247 | ** formed and never corrupt. This flag is clear by default, indicating that |
| 128014 | 128248 | ** database files might have arbitrary corruption. Setting the flag during |
| | @@ -132464,10 +132698,11 @@ |
| 132464 | 132698 | assert( iIdx==nVal ); |
| 132465 | 132699 | |
| 132466 | 132700 | /* In case the cursor has been used before, clear it now. */ |
| 132467 | 132701 | sqlite3_finalize(pCsr->pStmt); |
| 132468 | 132702 | sqlite3_free(pCsr->aDoclist); |
| 132703 | + sqlite3_free(pCsr->aMatchinfo); |
| 132469 | 132704 | sqlite3Fts3ExprFree(pCsr->pExpr); |
| 132470 | 132705 | memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor)); |
| 132471 | 132706 | |
| 132472 | 132707 | /* Set the lower and upper bounds on docids to return */ |
| 132473 | 132708 | pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64); |
| | @@ -133774,11 +134009,11 @@ |
| 133774 | 134009 | if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){ |
| 133775 | 134010 | iMax = a[i].iDocid; |
| 133776 | 134011 | bMaxSet = 1; |
| 133777 | 134012 | } |
| 133778 | 134013 | } |
| 133779 | | - assert( rc!=SQLITE_OK || a[p->nToken-1].bIgnore==0 ); |
| 134014 | + assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) ); |
| 133780 | 134015 | assert( rc!=SQLITE_OK || bMaxSet ); |
| 133781 | 134016 | |
| 133782 | 134017 | /* Keep advancing iterators until they all point to the same document */ |
| 133783 | 134018 | for(i=0; i<p->nToken; i++){ |
| 133784 | 134019 | while( rc==SQLITE_OK && bEof==0 |
| | @@ -135891,11 +136126,11 @@ |
| 135891 | 136126 | int i = 0; |
| 135892 | 136127 | |
| 135893 | 136128 | /* Set variable i to the maximum number of bytes of input to tokenize. */ |
| 135894 | 136129 | for(i=0; i<n; i++){ |
| 135895 | 136130 | if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break; |
| 135896 | | - if( z[i]=='*' || z[i]=='"' ) break; |
| 136131 | + if( z[i]=='"' ) break; |
| 135897 | 136132 | } |
| 135898 | 136133 | |
| 135899 | 136134 | *pnConsumed = i; |
| 135900 | 136135 | rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor); |
| 135901 | 136136 | if( rc==SQLITE_OK ){ |
| 135902 | 136137 | |