| | @@ -1150,11 +1150,11 @@ |
| 1150 | 1150 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1151 | 1151 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1152 | 1152 | */ |
| 1153 | 1153 | #define SQLITE_VERSION "3.24.0" |
| 1154 | 1154 | #define SQLITE_VERSION_NUMBER 3024000 |
| 1155 | | -#define SQLITE_SOURCE_ID "2018-04-26 18:34:26 9fd0faf517993587d2f54212638545fc85fbbc84a031bcfae8c1e5894825d83b" |
| 1155 | +#define SQLITE_SOURCE_ID "2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba71eda" |
| 1156 | 1156 | |
| 1157 | 1157 | /* |
| 1158 | 1158 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1159 | 1159 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1160 | 1160 | ** |
| | @@ -3137,10 +3137,25 @@ |
| 3137 | 3137 | ** or negative to leave the setting unchanged. |
| 3138 | 3138 | ** The second parameter is a pointer to an integer into which is written |
| 3139 | 3139 | ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if |
| 3140 | 3140 | ** it is not disabled, 1 if it is. |
| 3141 | 3141 | ** </dd> |
| 3142 | +** |
| 3143 | +** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt> |
| 3144 | +** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run |
| 3145 | +** [VACUUM] in order to reset a database back to an empty database |
| 3146 | +** with no schema and no content. The following process works even for |
| 3147 | +** a badly corrupted database file: |
| 3148 | +** <ol> |
| 3149 | +** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0); |
| 3150 | +** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0); |
| 3151 | +** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); |
| 3152 | +** </ol> |
| 3153 | +** Because resetting a database is destructive and irreversible, the |
| 3154 | +** process requires the use of this obscure API and multiple steps to help |
| 3155 | +** ensure that it does not happen by accident. |
| 3156 | +** </dd> |
| 3142 | 3157 | ** </dl> |
| 3143 | 3158 | */ |
| 3144 | 3159 | #define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */ |
| 3145 | 3160 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ |
| 3146 | 3161 | #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */ |
| | @@ -3148,11 +3163,12 @@ |
| 3148 | 3163 | #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */ |
| 3149 | 3164 | #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */ |
| 3150 | 3165 | #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */ |
| 3151 | 3166 | #define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */ |
| 3152 | 3167 | #define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */ |
| 3153 | | -#define SQLITE_DBCONFIG_MAX 1008 /* Largest DBCONFIG */ |
| 3168 | +#define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */ |
| 3169 | +#define SQLITE_DBCONFIG_MAX 1009 /* Largest DBCONFIG */ |
| 3154 | 3170 | |
| 3155 | 3171 | /* |
| 3156 | 3172 | ** CAPI3REF: Enable Or Disable Extended Result Codes |
| 3157 | 3173 | ** METHOD: sqlite3 |
| 3158 | 3174 | ** |
| | @@ -6534,10 +6550,45 @@ |
| 6534 | 6550 | ** made NULL or made to point to memory obtained from [sqlite3_malloc] |
| 6535 | 6551 | ** or else the use of the [data_store_directory pragma] should be avoided. |
| 6536 | 6552 | */ |
| 6537 | 6553 | SQLITE_API char *sqlite3_data_directory; |
| 6538 | 6554 | |
| 6555 | +/* |
| 6556 | +** CAPI3REF: Win32 Specific Interface |
| 6557 | +** |
| 6558 | +** These interfaces are available only on Windows. The |
| 6559 | +** [sqlite3_win32_set_directory] interface is used to set the value associated |
| 6560 | +** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to |
| 6561 | +** zValue, depending on the value of the type parameter. The zValue parameter |
| 6562 | +** should be NULL to cause the previous value to be freed via [sqlite3_free]; |
| 6563 | +** a non-NULL value will be copied into memory obtained from [sqlite3_malloc] |
| 6564 | +** prior to being used. The [sqlite3_win32_set_directory] interface returns |
| 6565 | +** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported, |
| 6566 | +** or [SQLITE_NOMEM] if memory could not be allocated. The value of the |
| 6567 | +** [sqlite3_data_directory] variable is intended to act as a replacement for |
| 6568 | +** the current directory on the sub-platforms of Win32 where that concept is |
| 6569 | +** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and |
| 6570 | +** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the |
| 6571 | +** sqlite3_win32_set_directory interface except the string parameter must be |
| 6572 | +** UTF-8 or UTF-16, respectively. |
| 6573 | +*/ |
| 6574 | +SQLITE_API int sqlite3_win32_set_directory( |
| 6575 | + unsigned long type, /* Identifier for directory being set or reset */ |
| 6576 | + void *zValue /* New value for directory being set or reset */ |
| 6577 | +); |
| 6578 | +SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue); |
| 6579 | +SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue); |
| 6580 | + |
| 6581 | +/* |
| 6582 | +** CAPI3REF: Win32 Directory Types |
| 6583 | +** |
| 6584 | +** These macros are only available on Windows. They define the allowed values |
| 6585 | +** for the type argument to the [sqlite3_win32_set_directory] interface. |
| 6586 | +*/ |
| 6587 | +#define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1 |
| 6588 | +#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2 |
| 6589 | + |
| 6539 | 6590 | /* |
| 6540 | 6591 | ** CAPI3REF: Test For Auto-Commit Mode |
| 6541 | 6592 | ** KEYWORDS: {autocommit mode} |
| 6542 | 6593 | ** METHOD: sqlite3 |
| 6543 | 6594 | ** |
| | @@ -14517,11 +14568,23 @@ |
| 14517 | 14568 | SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p); |
| 14518 | 14569 | #else |
| 14519 | 14570 | # define sqlite3VdbeVerifyNoMallocRequired(A,B) |
| 14520 | 14571 | # define sqlite3VdbeVerifyNoResultRow(A) |
| 14521 | 14572 | #endif |
| 14522 | | -SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno); |
| 14573 | +SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno); |
| 14574 | +#ifndef SQLITE_OMIT_EXPLAIN |
| 14575 | +SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...); |
| 14576 | +SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*); |
| 14577 | +SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*); |
| 14578 | +# define ExplainQueryPlan(P) sqlite3VdbeExplain P |
| 14579 | +# define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P) |
| 14580 | +# define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P) |
| 14581 | +#else |
| 14582 | +# define ExplainQueryPlan(P) |
| 14583 | +# define ExplainQueryPlanPop(P) |
| 14584 | +# define ExplainQueryPlanParent(P) 0 |
| 14585 | +#endif |
| 14523 | 14586 | SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*); |
| 14524 | 14587 | SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8); |
| 14525 | 14588 | SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1); |
| 14526 | 14589 | SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2); |
| 14527 | 14590 | SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3); |
| | @@ -15821,10 +15884,11 @@ |
| 15821 | 15884 | #define SQLITE_QueryOnly 0x00100000 /* Disable database changes */ |
| 15822 | 15885 | #define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */ |
| 15823 | 15886 | #define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */ |
| 15824 | 15887 | #define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/ |
| 15825 | 15888 | #define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */ |
| 15889 | +#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */ |
| 15826 | 15890 | |
| 15827 | 15891 | /* Flags used only if debugging */ |
| 15828 | 15892 | #ifdef SQLITE_DEBUG |
| 15829 | 15893 | #define SQLITE_SqlTrace 0x08000000 /* Debug print SQL as it executes */ |
| 15830 | 15894 | #define SQLITE_VdbeListing 0x10000000 /* Debug listings of VDBE programs */ |
| | @@ -16923,13 +16987,10 @@ |
| 16923 | 16987 | unsigned isTabFunc :1; /* True if table-valued-function syntax */ |
| 16924 | 16988 | unsigned isCorrelated :1; /* True if sub-query is correlated */ |
| 16925 | 16989 | unsigned viaCoroutine :1; /* Implemented as a co-routine */ |
| 16926 | 16990 | unsigned isRecursive :1; /* True for recursive reference in WITH */ |
| 16927 | 16991 | } fg; |
| 16928 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 16929 | | - u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */ |
| 16930 | | -#endif |
| 16931 | 16992 | int iCursor; /* The VDBE cursor number used to access this table */ |
| 16932 | 16993 | Expr *pOn; /* The ON clause of a join */ |
| 16933 | 16994 | IdList *pUsing; /* The USING clause of a join */ |
| 16934 | 16995 | Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */ |
| 16935 | 16996 | union { |
| | @@ -17073,15 +17134,12 @@ |
| 17073 | 17134 | |
| 17074 | 17135 | /* |
| 17075 | 17136 | ** An instance of the following structure contains all information |
| 17076 | 17137 | ** needed to generate code for a single SELECT statement. |
| 17077 | 17138 | ** |
| 17078 | | -** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0. |
| 17079 | | -** If there is a LIMIT clause, the parser sets nLimit to the value of the |
| 17080 | | -** limit and nOffset to the value of the offset (or 0 if there is not |
| 17081 | | -** offset). But later on, nLimit and nOffset become the memory locations |
| 17082 | | -** in the VDBE that record the limit and offset counters. |
| 17139 | +** See the header comment on the computeLimitRegisters() routine for a |
| 17140 | +** detailed description of the meaning of the iLimit and iOffset fields. |
| 17083 | 17141 | ** |
| 17084 | 17142 | ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes. |
| 17085 | 17143 | ** These addresses must be stored so that we can go back and fill in |
| 17086 | 17144 | ** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor |
| 17087 | 17145 | ** the number of columns in P2 can be computed at the same time |
| | @@ -17097,11 +17155,10 @@ |
| 17097 | 17155 | LogEst nSelectRow; /* Estimated number of result rows */ |
| 17098 | 17156 | u32 selFlags; /* Various SF_* values */ |
| 17099 | 17157 | int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ |
| 17100 | 17158 | #if SELECTTRACE_ENABLED |
| 17101 | 17159 | char zSelName[12]; /* Symbolic name of this SELECT use for debugging */ |
| 17102 | | - u32 iSelectId; /* EXPLAIN QUERY PLAN select ID */ |
| 17103 | 17160 | #endif |
| 17104 | 17161 | int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */ |
| 17105 | 17162 | SrcList *pSrc; /* The FROM clause */ |
| 17106 | 17163 | Expr *pWhere; /* The WHERE clause */ |
| 17107 | 17164 | ExprList *pGroupBy; /* The GROUP BY clause */ |
| | @@ -17138,12 +17195,11 @@ |
| 17138 | 17195 | #define SF_Recursive 0x02000 /* The recursive part of a recursive CTE */ |
| 17139 | 17196 | #define SF_FixedLimit 0x04000 /* nSelectRow set by a constant LIMIT */ |
| 17140 | 17197 | #define SF_MaybeConvert 0x08000 /* Need convertCompoundSelectToSubquery() */ |
| 17141 | 17198 | #define SF_Converted 0x10000 /* By convertCompoundSelectToSubquery() */ |
| 17142 | 17199 | #define SF_IncludeHidden 0x20000 /* Include hidden columns in output */ |
| 17143 | | -#define SF_ComplexResult 0x40000 /* Result set contains subquery or function */ |
| 17144 | | - |
| 17200 | +#define SF_ComplexResult 0x40000 /* Result contains subquery or function */ |
| 17145 | 17201 | |
| 17146 | 17202 | /* |
| 17147 | 17203 | ** The results of a SELECT can be distributed in several ways, as defined |
| 17148 | 17204 | ** by one of the following macros. The "SRT" prefix means "SELECT Result |
| 17149 | 17205 | ** Type". |
| | @@ -17409,12 +17465,11 @@ |
| 17409 | 17465 | u8 declareVtab; /* True if inside sqlite3_declare_vtab() */ |
| 17410 | 17466 | int nVtabLock; /* Number of virtual tables to lock */ |
| 17411 | 17467 | #endif |
| 17412 | 17468 | int nHeight; /* Expression tree height of current sub-select */ |
| 17413 | 17469 | #ifndef SQLITE_OMIT_EXPLAIN |
| 17414 | | - int iSelectId; /* ID of current select for EXPLAIN output */ |
| 17415 | | - int iNextSelectId; /* Next available select ID for EXPLAIN output */ |
| 17470 | + int addrExplain; /* Address of current OP_Explain opcode */ |
| 17416 | 17471 | #endif |
| 17417 | 17472 | VList *pVList; /* Mapping between variable names and numbers */ |
| 17418 | 17473 | Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */ |
| 17419 | 17474 | const char *zTail; /* All SQL text past the last semicolon parsed */ |
| 17420 | 17475 | Table *pNewTable; /* A table being constructed by CREATE TABLE */ |
| | @@ -27718,14 +27773,14 @@ |
| 27718 | 27773 | sqlite3TreeViewPush(pView, 1); |
| 27719 | 27774 | } |
| 27720 | 27775 | do{ |
| 27721 | 27776 | #if SELECTTRACE_ENABLED |
| 27722 | 27777 | sqlite3TreeViewLine(pView, |
| 27723 | | - "SELECT%s%s (%s/%d/%p) selFlags=0x%x nSelectRow=%d", |
| 27778 | + "SELECT%s%s (%s/%p) selFlags=0x%x nSelectRow=%d", |
| 27724 | 27779 | ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), |
| 27725 | 27780 | ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), |
| 27726 | | - p->zSelName, p->iSelectId, p, p->selFlags, |
| 27781 | + p->zSelName, p, p->selFlags, |
| 27727 | 27782 | (int)p->nSelectRow |
| 27728 | 27783 | ); |
| 27729 | 27784 | #else |
| 27730 | 27785 | sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d", |
| 27731 | 27786 | ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), |
| | @@ -39693,26 +39748,10 @@ |
| 39693 | 39748 | */ |
| 39694 | 39749 | #ifndef SQLITE_WIN32_DBG_BUF_SIZE |
| 39695 | 39750 | # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD))) |
| 39696 | 39751 | #endif |
| 39697 | 39752 | |
| 39698 | | -/* |
| 39699 | | - * The value used with sqlite3_win32_set_directory() to specify that |
| 39700 | | - * the data directory should be changed. |
| 39701 | | - */ |
| 39702 | | -#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE |
| 39703 | | -# define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1) |
| 39704 | | -#endif |
| 39705 | | - |
| 39706 | | -/* |
| 39707 | | - * The value used with sqlite3_win32_set_directory() to specify that |
| 39708 | | - * the temporary directory should be changed. |
| 39709 | | - */ |
| 39710 | | -#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE |
| 39711 | | -# define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2) |
| 39712 | | -#endif |
| 39713 | | - |
| 39714 | 39753 | /* |
| 39715 | 39754 | * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the |
| 39716 | 39755 | * various Win32 API heap functions instead of our own. |
| 39717 | 39756 | */ |
| 39718 | 39757 | #ifdef SQLITE_WIN32_MALLOC |
| | @@ -41305,17 +41344,17 @@ |
| 41305 | 41344 | #endif |
| 41306 | 41345 | return winUtf8ToMbcs(zText, useAnsi); |
| 41307 | 41346 | } |
| 41308 | 41347 | |
| 41309 | 41348 | /* |
| 41310 | | -** This function sets the data directory or the temporary directory based on |
| 41311 | | -** the provided arguments. The type argument must be 1 in order to set the |
| 41312 | | -** data directory or 2 in order to set the temporary directory. The zValue |
| 41313 | | -** argument is the name of the directory to use. The return value will be |
| 41314 | | -** SQLITE_OK if successful. |
| 41349 | +** This function is the same as sqlite3_win32_set_directory (below); however, |
| 41350 | +** it accepts a UTF-8 string. |
| 41315 | 41351 | */ |
| 41316 | | -SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){ |
| 41352 | +SQLITE_API int sqlite3_win32_set_directory8( |
| 41353 | + unsigned long type, /* Identifier for directory being set or reset */ |
| 41354 | + const char *zValue /* New value for directory being set or reset */ |
| 41355 | +){ |
| 41317 | 41356 | char **ppDirectory = 0; |
| 41318 | 41357 | #ifndef SQLITE_OMIT_AUTOINIT |
| 41319 | 41358 | int rc = sqlite3_initialize(); |
| 41320 | 41359 | if( rc ) return rc; |
| 41321 | 41360 | #endif |
| | @@ -41327,23 +41366,56 @@ |
| 41327 | 41366 | assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE |
| 41328 | 41367 | || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE |
| 41329 | 41368 | ); |
| 41330 | 41369 | assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) ); |
| 41331 | 41370 | if( ppDirectory ){ |
| 41332 | | - char *zValueUtf8 = 0; |
| 41371 | + char *zCopy = 0; |
| 41333 | 41372 | if( zValue && zValue[0] ){ |
| 41334 | | - zValueUtf8 = winUnicodeToUtf8(zValue); |
| 41335 | | - if ( zValueUtf8==0 ){ |
| 41373 | + zCopy = sqlite3_mprintf("%s", zValue); |
| 41374 | + if ( zCopy==0 ){ |
| 41336 | 41375 | return SQLITE_NOMEM_BKPT; |
| 41337 | 41376 | } |
| 41338 | 41377 | } |
| 41339 | 41378 | sqlite3_free(*ppDirectory); |
| 41340 | | - *ppDirectory = zValueUtf8; |
| 41379 | + *ppDirectory = zCopy; |
| 41341 | 41380 | return SQLITE_OK; |
| 41342 | 41381 | } |
| 41343 | 41382 | return SQLITE_ERROR; |
| 41344 | 41383 | } |
| 41384 | + |
| 41385 | +/* |
| 41386 | +** This function is the same as sqlite3_win32_set_directory (below); however, |
| 41387 | +** it accepts a UTF-16 string. |
| 41388 | +*/ |
| 41389 | +SQLITE_API int sqlite3_win32_set_directory16( |
| 41390 | + unsigned long type, /* Identifier for directory being set or reset */ |
| 41391 | + const void *zValue /* New value for directory being set or reset */ |
| 41392 | +){ |
| 41393 | + int rc; |
| 41394 | + char *zUtf8 = 0; |
| 41395 | + if( zValue ){ |
| 41396 | + zUtf8 = sqlite3_win32_unicode_to_utf8(zValue); |
| 41397 | + if( zUtf8==0 ) return SQLITE_NOMEM_BKPT; |
| 41398 | + } |
| 41399 | + rc = sqlite3_win32_set_directory8(type, zUtf8); |
| 41400 | + if( zUtf8 ) sqlite3_free(zUtf8); |
| 41401 | + return rc; |
| 41402 | +} |
| 41403 | + |
| 41404 | +/* |
| 41405 | +** This function sets the data directory or the temporary directory based on |
| 41406 | +** the provided arguments. The type argument must be 1 in order to set the |
| 41407 | +** data directory or 2 in order to set the temporary directory. The zValue |
| 41408 | +** argument is the name of the directory to use. The return value will be |
| 41409 | +** SQLITE_OK if successful. |
| 41410 | +*/ |
| 41411 | +SQLITE_API int sqlite3_win32_set_directory( |
| 41412 | + unsigned long type, /* Identifier for directory being set or reset */ |
| 41413 | + void *zValue /* New value for directory being set or reset */ |
| 41414 | +){ |
| 41415 | + return sqlite3_win32_set_directory16(type, zValue); |
| 41416 | +} |
| 41345 | 41417 | |
| 41346 | 41418 | /* |
| 41347 | 41419 | ** The return value of winGetLastErrorMsg |
| 41348 | 41420 | ** is zero if the error message fits in the buffer, or non-zero |
| 41349 | 41421 | ** otherwise (if the message was truncated). |
| | @@ -64714,10 +64786,14 @@ |
| 64714 | 64786 | } |
| 64715 | 64787 | } |
| 64716 | 64788 | #else |
| 64717 | 64789 | # define setDefaultSyncFlag(pBt,safety_level) |
| 64718 | 64790 | #endif |
| 64791 | + |
| 64792 | +/* Forward declaration */ |
| 64793 | +static int newDatabase(BtShared*); |
| 64794 | + |
| 64719 | 64795 | |
| 64720 | 64796 | /* |
| 64721 | 64797 | ** Get a reference to pPage1 of the database file. This will |
| 64722 | 64798 | ** also acquire a readlock on that file. |
| 64723 | 64799 | ** |
| | @@ -64745,10 +64821,13 @@ |
| 64745 | 64821 | */ |
| 64746 | 64822 | nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); |
| 64747 | 64823 | sqlite3PagerPagecount(pBt->pPager, &nPageFile); |
| 64748 | 64824 | if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){ |
| 64749 | 64825 | nPage = nPageFile; |
| 64826 | + } |
| 64827 | + if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){ |
| 64828 | + nPage = 0; |
| 64750 | 64829 | } |
| 64751 | 64830 | if( nPage>0 ){ |
| 64752 | 64831 | u32 pageSize; |
| 64753 | 64832 | u32 usableSize; |
| 64754 | 64833 | u8 *page1 = pPage1->aData; |
| | @@ -67962,11 +68041,13 @@ |
| 67962 | 68041 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 67963 | 68042 | pPage->xParseCell(pPage, pCell, pInfo); |
| 67964 | 68043 | if( pInfo->nLocal==pInfo->nPayload ){ |
| 67965 | 68044 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
| 67966 | 68045 | } |
| 67967 | | - if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){ |
| 68046 | + testcase( pCell + pInfo->nSize == pPage->aDataEnd ); |
| 68047 | + testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd ); |
| 68048 | + if( pCell + pInfo->nSize > pPage->aDataEnd ){ |
| 67968 | 68049 | /* Cell extends past end of page */ |
| 67969 | 68050 | return SQLITE_CORRUPT_PAGE(pPage); |
| 67970 | 68051 | } |
| 67971 | 68052 | ovflPgno = get4byte(pCell + pInfo->nSize - 4); |
| 67972 | 68053 | pBt = pPage->pBt; |
| | @@ -74686,10 +74767,53 @@ |
| 74686 | 74767 | char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8); |
| 74687 | 74768 | if( p4copy ) memcpy(p4copy, zP4, 8); |
| 74688 | 74769 | return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type); |
| 74689 | 74770 | } |
| 74690 | 74771 | |
| 74772 | +#ifndef SQLITE_OMIT_EXPLAIN |
| 74773 | +/* |
| 74774 | +** Return the address of the current EXPLAIN QUERY PLAN baseline. |
| 74775 | +** 0 means "none". |
| 74776 | +*/ |
| 74777 | +SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){ |
| 74778 | + VdbeOp *pOp; |
| 74779 | + if( pParse->addrExplain==0 ) return 0; |
| 74780 | + pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain); |
| 74781 | + return pOp->p2; |
| 74782 | +} |
| 74783 | + |
| 74784 | +/* |
| 74785 | +** Add a new OP_Explain opcode. |
| 74786 | +** |
| 74787 | +** If the bPush flag is true, then make this opcode the parent for |
| 74788 | +** subsequent Explains until sqlite3VdbeExplainPop() is called. |
| 74789 | +*/ |
| 74790 | +SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){ |
| 74791 | + if( pParse->explain==2 ){ |
| 74792 | + char *zMsg; |
| 74793 | + Vdbe *v = pParse->pVdbe; |
| 74794 | + va_list ap; |
| 74795 | + int iThis; |
| 74796 | + va_start(ap, zFmt); |
| 74797 | + zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap); |
| 74798 | + va_end(ap); |
| 74799 | + v = pParse->pVdbe; |
| 74800 | + iThis = v->nOp; |
| 74801 | + sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0, |
| 74802 | + zMsg, P4_DYNAMIC); |
| 74803 | + if( bPush) pParse->addrExplain = iThis; |
| 74804 | + } |
| 74805 | +} |
| 74806 | + |
| 74807 | +/* |
| 74808 | +** Pop the EXPLAIN QUERY PLAN stack one level. |
| 74809 | +*/ |
| 74810 | +SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){ |
| 74811 | + pParse->addrExplain = sqlite3VdbeExplainParent(pParse); |
| 74812 | +} |
| 74813 | +#endif /* SQLITE_OMIT_EXPLAIN */ |
| 74814 | + |
| 74691 | 74815 | /* |
| 74692 | 74816 | ** Add an OP_ParseSchema opcode. This routine is broken out from |
| 74693 | 74817 | ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees |
| 74694 | 74818 | ** as having been used. |
| 74695 | 74819 | ** |
| | @@ -96622,15 +96746,12 @@ |
| 96622 | 96746 | |
| 96623 | 96747 | assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) ); |
| 96624 | 96748 | if( colUsed==(MASKBIT(nExpr)-1) ){ |
| 96625 | 96749 | /* If we reach this point, that means the index pIdx is usable */ |
| 96626 | 96750 | int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
| 96627 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 96628 | | - sqlite3VdbeAddOp4(v, OP_Explain, 0, 0, 0, |
| 96629 | | - sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName), |
| 96630 | | - P4_DYNAMIC); |
| 96631 | | -#endif |
| 96751 | + ExplainQueryPlan((pParse, 0, |
| 96752 | + "USING INDEX %s FOR IN-OPERATOR",pIdx->zName)); |
| 96632 | 96753 | sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); |
| 96633 | 96754 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 96634 | 96755 | VdbeComment((v, "%s", pIdx->zName)); |
| 96635 | 96756 | assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); |
| 96636 | 96757 | eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; |
| | @@ -96858,21 +96979,13 @@ |
| 96858 | 96979 | ** table allocated and opened above. |
| 96859 | 96980 | */ |
| 96860 | 96981 | Select *pSelect = pExpr->x.pSelect; |
| 96861 | 96982 | ExprList *pEList = pSelect->pEList; |
| 96862 | 96983 | |
| 96863 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 96864 | | - if( pParse->explain==2 ){ |
| 96865 | | - char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %sLIST SUBQUERY %d", |
| 96866 | | - jmpIfDynamic>=0?"":"CORRELATED ", |
| 96867 | | - pParse->iNextSelectId |
| 96868 | | - ); |
| 96869 | | - sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, |
| 96870 | | - P4_DYNAMIC); |
| 96871 | | - } |
| 96872 | | -#endif |
| 96873 | | - |
| 96984 | + ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY", |
| 96985 | + jmpIfDynamic>=0?"":"CORRELATED " |
| 96986 | + )); |
| 96874 | 96987 | assert( !isRowid ); |
| 96875 | 96988 | /* If the LHS and RHS of the IN operator do not match, that |
| 96876 | 96989 | ** error will have been caught long before we reach this point. */ |
| 96877 | 96990 | if( ALWAYS(pEList->nExpr==nVal) ){ |
| 96878 | 96991 | SelectDest dest; |
| | @@ -96989,22 +97102,13 @@ |
| 96989 | 97102 | testcase( pExpr->op==TK_EXISTS ); |
| 96990 | 97103 | testcase( pExpr->op==TK_SELECT ); |
| 96991 | 97104 | assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); |
| 96992 | 97105 | assert( ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 96993 | 97106 | |
| 96994 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 96995 | | - if( pParse->explain==2 ){ |
| 96996 | | - char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %sSCALAR SUBQUERY %d", |
| 96997 | | - jmpIfDynamic>=0?"":"CORRELATED ", |
| 96998 | | - pParse->iNextSelectId |
| 96999 | | - ); |
| 97000 | | - sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, |
| 97001 | | - P4_DYNAMIC); |
| 97002 | | - } |
| 97003 | | -#endif |
| 97004 | | - |
| 97005 | 97107 | pSel = pExpr->x.pSelect; |
| 97108 | + ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY", |
| 97109 | + jmpIfDynamic>=0?"":"CORRELATED ")); |
| 97006 | 97110 | nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1; |
| 97007 | 97111 | sqlite3SelectDestInit(&dest, 0, pParse->nMem+1); |
| 97008 | 97112 | pParse->nMem += nReg; |
| 97009 | 97113 | if( pExpr->op==TK_SELECT ){ |
| 97010 | 97114 | dest.eDest = SRT_Mem; |
| | @@ -97763,10 +97867,11 @@ |
| 97763 | 97867 | if( v==0 ){ |
| 97764 | 97868 | assert( pParse->db->mallocFailed ); |
| 97765 | 97869 | return 0; |
| 97766 | 97870 | } |
| 97767 | 97871 | |
| 97872 | +expr_code_doover: |
| 97768 | 97873 | if( pExpr==0 ){ |
| 97769 | 97874 | op = TK_NULL; |
| 97770 | 97875 | }else{ |
| 97771 | 97876 | op = pExpr->op; |
| 97772 | 97877 | } |
| | @@ -98223,11 +98328,12 @@ |
| 98223 | 98328 | return target; |
| 98224 | 98329 | } |
| 98225 | 98330 | case TK_SPAN: |
| 98226 | 98331 | case TK_COLLATE: |
| 98227 | 98332 | case TK_UPLUS: { |
| 98228 | | - return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
| 98333 | + pExpr = pExpr->pLeft; |
| 98334 | + goto expr_code_doover; |
| 98229 | 98335 | } |
| 98230 | 98336 | |
| 98231 | 98337 | case TK_TRIGGER: { |
| 98232 | 98338 | /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 98233 | 98339 | ** to a column in the new.* or old.* pseudo-tables available to |
| | @@ -106427,11 +106533,15 @@ |
| 106427 | 106533 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
| 106428 | 106534 | && db->init.busy==0 |
| 106429 | 106535 | #if SQLITE_USER_AUTHENTICATION |
| 106430 | 106536 | && sqlite3UserAuthTable(pTab->zName)==0 |
| 106431 | 106537 | #endif |
| 106432 | | - && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){ |
| 106538 | +#ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX |
| 106539 | + && sqlite3StrICmp(&pTab->zName[7],"master")!=0 |
| 106540 | +#endif |
| 106541 | + && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 |
| 106542 | + ){ |
| 106433 | 106543 | sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
| 106434 | 106544 | goto exit_create_index; |
| 106435 | 106545 | } |
| 106436 | 106546 | #ifndef SQLITE_OMIT_VIEW |
| 106437 | 106547 | if( pTab->pSelect ){ |
| | @@ -120002,10 +120112,13 @@ |
| 120002 | 120112 | ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to |
| 120003 | 120113 | ** the possible values of meta[4]. |
| 120004 | 120114 | */ |
| 120005 | 120115 | for(i=0; i<ArraySize(meta); i++){ |
| 120006 | 120116 | sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]); |
| 120117 | + } |
| 120118 | + if( (db->flags & SQLITE_ResetDatabase)!=0 ){ |
| 120119 | + memset(meta, 0, sizeof(meta)); |
| 120007 | 120120 | } |
| 120008 | 120121 | pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1]; |
| 120009 | 120122 | |
| 120010 | 120123 | /* If opening a non-empty database, check the text encoding. For the |
| 120011 | 120124 | ** main database, set sqlite3.enc to the encoding of the main database. |
| | @@ -120400,11 +120513,11 @@ |
| 120400 | 120513 | |
| 120401 | 120514 | #ifndef SQLITE_OMIT_EXPLAIN |
| 120402 | 120515 | if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){ |
| 120403 | 120516 | static const char * const azColName[] = { |
| 120404 | 120517 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", |
| 120405 | | - "selectid", "order", "from", "detail" |
| 120518 | + "id", "parent", "notused", "detail" |
| 120406 | 120519 | }; |
| 120407 | 120520 | int iFirst, mx; |
| 120408 | 120521 | if( sParse.explain==2 ){ |
| 120409 | 120522 | sqlite3VdbeSetNumCols(sParse.pVdbe, 4); |
| 120410 | 120523 | iFirst = 8; |
| | @@ -120714,11 +120827,11 @@ |
| 120714 | 120827 | */ |
| 120715 | 120828 | #if SELECTTRACE_ENABLED |
| 120716 | 120829 | /***/ int sqlite3SelectTrace = 0; |
| 120717 | 120830 | # define SELECTTRACE(K,P,S,X) \ |
| 120718 | 120831 | if(sqlite3SelectTrace&(K)) \ |
| 120719 | | - sqlite3DebugPrintf("%s/%d/%p: ",(S)->zSelName,(P)->iSelectId,(S)),\ |
| 120832 | + sqlite3DebugPrintf("%s/%d/%p: ",(S)->zSelName,(P)->addrExplain,(S)),\ |
| 120720 | 120833 | sqlite3DebugPrintf X |
| 120721 | 120834 | #else |
| 120722 | 120835 | # define SELECTTRACE(K,P,S,X) |
| 120723 | 120836 | #endif |
| 120724 | 120837 | |
| | @@ -120771,10 +120884,11 @@ |
| 120771 | 120884 | Table *pTab; /* Table definition */ |
| 120772 | 120885 | int iCsr; /* Cursor number for table */ |
| 120773 | 120886 | int nKey; /* Number of PK columns for table pTab (>=1) */ |
| 120774 | 120887 | } aDefer[4]; |
| 120775 | 120888 | #endif |
| 120889 | + struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */ |
| 120776 | 120890 | }; |
| 120777 | 120891 | #define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */ |
| 120778 | 120892 | |
| 120779 | 120893 | /* |
| 120780 | 120894 | ** Delete all the content of a Select structure. Deallocate the structure |
| | @@ -121228,10 +121342,66 @@ |
| 121228 | 121342 | Parse *pParse, /* Parsing context */ |
| 121229 | 121343 | ExprList *pList, /* Form the KeyInfo object from this ExprList */ |
| 121230 | 121344 | int iStart, /* Begin with this column of pList */ |
| 121231 | 121345 | int nExtra /* Add this many extra columns to the end */ |
| 121232 | 121346 | ); |
| 121347 | + |
| 121348 | +/* |
| 121349 | +** An instance of this object holds information (beyond pParse and pSelect) |
| 121350 | +** needed to load the next result row that is to be added to the sorter. |
| 121351 | +*/ |
| 121352 | +typedef struct RowLoadInfo RowLoadInfo; |
| 121353 | +struct RowLoadInfo { |
| 121354 | + int regResult; /* Store results in array of registers here */ |
| 121355 | + u8 ecelFlags; /* Flag argument to ExprCodeExprList() */ |
| 121356 | +#ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 121357 | + ExprList *pExtra; /* Extra columns needed by sorter refs */ |
| 121358 | + int regExtraResult; /* Where to load the extra columns */ |
| 121359 | +#endif |
| 121360 | +}; |
| 121361 | + |
| 121362 | +/* |
| 121363 | +** This routine does the work of loading query data into an array of |
| 121364 | +** registers so that it can be added to the sorter. |
| 121365 | +*/ |
| 121366 | +static void innerLoopLoadRow( |
| 121367 | + Parse *pParse, /* Statement under construction */ |
| 121368 | + Select *pSelect, /* The query being coded */ |
| 121369 | + RowLoadInfo *pInfo /* Info needed to complete the row load */ |
| 121370 | +){ |
| 121371 | + sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult, |
| 121372 | + 0, pInfo->ecelFlags); |
| 121373 | +#ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 121374 | + if( pInfo->pExtra ){ |
| 121375 | + sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0); |
| 121376 | + sqlite3ExprListDelete(pParse->db, pInfo->pExtra); |
| 121377 | + } |
| 121378 | +#endif |
| 121379 | +} |
| 121380 | + |
| 121381 | +/* |
| 121382 | +** Code the OP_MakeRecord instruction that generates the entry to be |
| 121383 | +** added into the sorter. |
| 121384 | +** |
| 121385 | +** Return the register in which the result is stored. |
| 121386 | +*/ |
| 121387 | +static int makeSorterRecord( |
| 121388 | + Parse *pParse, |
| 121389 | + SortCtx *pSort, |
| 121390 | + Select *pSelect, |
| 121391 | + int regBase, |
| 121392 | + int nBase |
| 121393 | +){ |
| 121394 | + int nOBSat = pSort->nOBSat; |
| 121395 | + Vdbe *v = pParse->pVdbe; |
| 121396 | + int regOut = ++pParse->nMem; |
| 121397 | + if( pSort->pDeferredRowLoad ){ |
| 121398 | + innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad); |
| 121399 | + } |
| 121400 | + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut); |
| 121401 | + return regOut; |
| 121402 | +} |
| 121233 | 121403 | |
| 121234 | 121404 | /* |
| 121235 | 121405 | ** Generate code that will push the record in registers regData |
| 121236 | 121406 | ** through regData+nData-1 onto the sorter. |
| 121237 | 121407 | */ |
| | @@ -121239,28 +121409,43 @@ |
| 121239 | 121409 | Parse *pParse, /* Parser context */ |
| 121240 | 121410 | SortCtx *pSort, /* Information about the ORDER BY clause */ |
| 121241 | 121411 | Select *pSelect, /* The whole SELECT statement */ |
| 121242 | 121412 | int regData, /* First register holding data to be sorted */ |
| 121243 | 121413 | int regOrigData, /* First register holding data before packing */ |
| 121244 | | - int nData, /* Number of elements in the data array */ |
| 121414 | + int nData, /* Number of elements in the regData data array */ |
| 121245 | 121415 | int nPrefixReg /* No. of reg prior to regData available for use */ |
| 121246 | 121416 | ){ |
| 121247 | 121417 | Vdbe *v = pParse->pVdbe; /* Stmt under construction */ |
| 121248 | 121418 | int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0); |
| 121249 | 121419 | int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */ |
| 121250 | 121420 | int nBase = nExpr + bSeq + nData; /* Fields in sorter record */ |
| 121251 | 121421 | int regBase; /* Regs for sorter record */ |
| 121252 | | - int regRecord = ++pParse->nMem; /* Assembled sorter record */ |
| 121422 | + int regRecord = 0; /* Assembled sorter record */ |
| 121253 | 121423 | int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */ |
| 121254 | 121424 | int op; /* Opcode to add sorter record to sorter */ |
| 121255 | 121425 | int iLimit; /* LIMIT counter */ |
| 121426 | + int iSkip = 0; /* End of the sorter insert loop */ |
| 121256 | 121427 | |
| 121257 | 121428 | assert( bSeq==0 || bSeq==1 ); |
| 121429 | + |
| 121430 | + /* Three cases: |
| 121431 | + ** (1) The data to be sorted has already been packed into a Record |
| 121432 | + ** by a prior OP_MakeRecord. In this case nData==1 and regData |
| 121433 | + ** will be completely unrelated to regOrigData. |
| 121434 | + ** (2) All output columns are included in the sort record. In that |
| 121435 | + ** case regData==regOrigData. |
| 121436 | + ** (3) Some output columns are omitted from the sort record due to |
| 121437 | + ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the |
| 121438 | + ** SQLITE_ECEL_OMITREF optimization. In that case, regOrigData==0 |
| 121439 | + ** to prevent this routine from trying to copy values that might |
| 121440 | + ** not exist. |
| 121441 | + */ |
| 121258 | 121442 | assert( nData==1 || regData==regOrigData || regOrigData==0 ); |
| 121443 | + |
| 121259 | 121444 | if( nPrefixReg ){ |
| 121260 | 121445 | assert( nPrefixReg==nExpr+bSeq ); |
| 121261 | | - regBase = regData - nExpr - bSeq; |
| 121446 | + regBase = regData - nPrefixReg; |
| 121262 | 121447 | }else{ |
| 121263 | 121448 | regBase = pParse->nMem + 1; |
| 121264 | 121449 | pParse->nMem += nBase; |
| 121265 | 121450 | } |
| 121266 | 121451 | assert( pSelect->iOffset==0 || pSelect->iLimit!=0 ); |
| | @@ -121280,11 +121465,11 @@ |
| 121280 | 121465 | int addrJmp; /* Address of the OP_Jump opcode */ |
| 121281 | 121466 | VdbeOp *pOp; /* Opcode that opens the sorter */ |
| 121282 | 121467 | int nKey; /* Number of sorting key columns, including OP_Sequence */ |
| 121283 | 121468 | KeyInfo *pKI; /* Original KeyInfo on the sorter table */ |
| 121284 | 121469 | |
| 121285 | | - sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat,regRecord); |
| 121470 | + regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase); |
| 121286 | 121471 | regPrevKey = pParse->nMem+1; |
| 121287 | 121472 | pParse->nMem += pSort->nOBSat; |
| 121288 | 121473 | nKey = nExpr - pSort->nOBSat + bSeq; |
| 121289 | 121474 | if( bSeq ){ |
| 121290 | 121475 | addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr); |
| | @@ -121331,29 +121516,33 @@ |
| 121331 | 121516 | ** pSort->bOrderedInnerLoop flag is set to indicate that the inner |
| 121332 | 121517 | ** loop delivers items in sorted order, jump to the next iteration |
| 121333 | 121518 | ** of the outer loop. |
| 121334 | 121519 | */ |
| 121335 | 121520 | int iCsr = pSort->iECursor; |
| 121336 | | - int iJmp = sqlite3VdbeCurrentAddr(v)+5+(nOBSat<=0)+pSort->bOrderedInnerLoop; |
| 121337 | | - assert( pSort->bOrderedInnerLoop==0 || pSort->bOrderedInnerLoop==1 ); |
| 121338 | 121521 | sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4); |
| 121339 | 121522 | VdbeCoverage(v); |
| 121340 | 121523 | sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0); |
| 121341 | | - sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp, regBase+nOBSat, nExpr-nOBSat); |
| 121524 | + iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE, |
| 121525 | + iCsr, 0, regBase+nOBSat, nExpr-nOBSat); |
| 121342 | 121526 | VdbeCoverage(v); |
| 121343 | 121527 | sqlite3VdbeAddOp1(v, OP_Delete, iCsr); |
| 121344 | 121528 | } |
| 121345 | | - if( nOBSat<=0 ){ |
| 121346 | | - sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat,regRecord); |
| 121529 | + if( regRecord==0 ){ |
| 121530 | + regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase); |
| 121347 | 121531 | } |
| 121348 | 121532 | if( pSort->sortFlags & SORTFLAG_UseSorter ){ |
| 121349 | 121533 | op = OP_SorterInsert; |
| 121350 | 121534 | }else{ |
| 121351 | 121535 | op = OP_IdxInsert; |
| 121352 | 121536 | } |
| 121353 | 121537 | sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord, |
| 121354 | 121538 | regBase+nOBSat, nBase-nOBSat); |
| 121539 | + if( iSkip ){ |
| 121540 | + assert( pSort->bOrderedInnerLoop==0 || pSort->bOrderedInnerLoop==1 ); |
| 121541 | + sqlite3VdbeChangeP2(v, iSkip, |
| 121542 | + sqlite3VdbeCurrentAddr(v) + pSort->bOrderedInnerLoop); |
| 121543 | + } |
| 121355 | 121544 | } |
| 121356 | 121545 | |
| 121357 | 121546 | /* |
| 121358 | 121547 | ** Add code to implement the OFFSET |
| 121359 | 121548 | */ |
| | @@ -121435,13 +121624,10 @@ |
| 121435 | 121624 | if( pItem->u.x.iOrderByCol==0 ){ |
| 121436 | 121625 | Expr *pExpr = pItem->pExpr; |
| 121437 | 121626 | Table *pTab = pExpr->pTab; |
| 121438 | 121627 | if( pExpr->op==TK_COLUMN && pTab && !IsVirtual(pTab) |
| 121439 | 121628 | && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF) |
| 121440 | | -#if 0 |
| 121441 | | - && pTab->pSchema && pTab->pSelect==0 && !IsVirtual(pTab) |
| 121442 | | -#endif |
| 121443 | 121629 | ){ |
| 121444 | 121630 | int j; |
| 121445 | 121631 | for(j=0; j<nDefer; j++){ |
| 121446 | 121632 | if( pSort->aDefer[j].iCsr==pExpr->iTable ) break; |
| 121447 | 121633 | } |
| | @@ -121504,10 +121690,11 @@ |
| 121504 | 121690 | int hasDistinct; /* True if the DISTINCT keyword is present */ |
| 121505 | 121691 | int eDest = pDest->eDest; /* How to dispose of results */ |
| 121506 | 121692 | int iParm = pDest->iSDParm; /* First argument to disposal method */ |
| 121507 | 121693 | int nResultCol; /* Number of result columns */ |
| 121508 | 121694 | int nPrefixReg = 0; /* Number of extra registers before regResult */ |
| 121695 | + RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */ |
| 121509 | 121696 | |
| 121510 | 121697 | /* Usually, regResult is the first cell in an array of memory cells |
| 121511 | 121698 | ** containing the current result row. In this case regOrig is set to the |
| 121512 | 121699 | ** same value. However, if the results are being sent to the sorter, the |
| 121513 | 121700 | ** values for any expressions that are also part of the sort-key are omitted |
| | @@ -121556,11 +121743,12 @@ |
| 121556 | 121743 | ExprList *pExtra = 0; |
| 121557 | 121744 | #endif |
| 121558 | 121745 | /* If the destination is an EXISTS(...) expression, the actual |
| 121559 | 121746 | ** values returned by the SELECT are not required. |
| 121560 | 121747 | */ |
| 121561 | | - u8 ecelFlags; |
| 121748 | + u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */ |
| 121749 | + ExprList *pEList; |
| 121562 | 121750 | if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){ |
| 121563 | 121751 | ecelFlags = SQLITE_ECEL_DUP; |
| 121564 | 121752 | }else{ |
| 121565 | 121753 | ecelFlags = 0; |
| 121566 | 121754 | } |
| | @@ -121570,10 +121758,11 @@ |
| 121570 | 121758 | ** iOrderByCol value to one more than the index of the ORDER BY |
| 121571 | 121759 | ** expression within the sort-key that pushOntoSorter() will generate. |
| 121572 | 121760 | ** This allows the p->pEList field to be omitted from the sorted record, |
| 121573 | 121761 | ** saving space and CPU cycles. */ |
| 121574 | 121762 | ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF); |
| 121763 | + |
| 121575 | 121764 | for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){ |
| 121576 | 121765 | int j; |
| 121577 | 121766 | if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){ |
| 121578 | 121767 | p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat; |
| 121579 | 121768 | } |
| | @@ -121590,24 +121779,50 @@ |
| 121590 | 121779 | pOp->p2 += (pExtra->nExpr - pSort->nDefer); |
| 121591 | 121780 | pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer); |
| 121592 | 121781 | pParse->nMem += pExtra->nExpr; |
| 121593 | 121782 | } |
| 121594 | 121783 | #endif |
| 121595 | | - regOrig = 0; |
| 121784 | + |
| 121785 | + /* Adjust nResultCol to account for columns that are omitted |
| 121786 | + ** from the sorter by the optimizations in this branch */ |
| 121787 | + pEList = p->pEList; |
| 121788 | + for(i=0; i<pEList->nExpr; i++){ |
| 121789 | + if( pEList->a[i].u.x.iOrderByCol>0 |
| 121790 | +#ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 121791 | + || pEList->a[i].bSorterRef |
| 121792 | +#endif |
| 121793 | + ){ |
| 121794 | + nResultCol--; |
| 121795 | + regOrig = 0; |
| 121796 | + } |
| 121797 | + } |
| 121798 | + |
| 121799 | + testcase( regOrig ); |
| 121800 | + testcase( eDest==SRT_Set ); |
| 121801 | + testcase( eDest==SRT_Mem ); |
| 121802 | + testcase( eDest==SRT_Coroutine ); |
| 121803 | + testcase( eDest==SRT_Output ); |
| 121596 | 121804 | assert( eDest==SRT_Set || eDest==SRT_Mem |
| 121597 | 121805 | || eDest==SRT_Coroutine || eDest==SRT_Output ); |
| 121598 | 121806 | } |
| 121599 | | - nResultCol = sqlite3ExprCodeExprList(pParse,p->pEList,regResult, |
| 121600 | | - 0,ecelFlags); |
| 121807 | + sRowLoadInfo.regResult = regResult; |
| 121808 | + sRowLoadInfo.ecelFlags = ecelFlags; |
| 121601 | 121809 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 121602 | | - if( pExtra ){ |
| 121603 | | - nResultCol += sqlite3ExprCodeExprList( |
| 121604 | | - pParse, pExtra, regResult + nResultCol, 0, 0 |
| 121605 | | - ); |
| 121606 | | - sqlite3ExprListDelete(pParse->db, pExtra); |
| 121810 | + sRowLoadInfo.pExtra = pExtra; |
| 121811 | + sRowLoadInfo.regExtraResult = regResult + nResultCol; |
| 121812 | + if( pExtra ) nResultCol += pExtra->nExpr; |
| 121813 | +#endif |
| 121814 | + if( p->iLimit |
| 121815 | + && (ecelFlags & SQLITE_ECEL_OMITREF)!=0 |
| 121816 | + && nPrefixReg>0 |
| 121817 | + ){ |
| 121818 | + assert( pSort!=0 ); |
| 121819 | + assert( hasDistinct==0 ); |
| 121820 | + pSort->pDeferredRowLoad = &sRowLoadInfo; |
| 121821 | + }else{ |
| 121822 | + innerLoopLoadRow(pParse, p, &sRowLoadInfo); |
| 121607 | 121823 | } |
| 121608 | | -#endif |
| 121609 | 121824 | } |
| 121610 | 121825 | |
| 121611 | 121826 | /* If the DISTINCT keyword was present on the SELECT statement |
| 121612 | 121827 | ** and this row has been seen before, then do not make this row |
| 121613 | 121828 | ** part of the result. |
| | @@ -121719,11 +121934,12 @@ |
| 121719 | 121934 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol); |
| 121720 | 121935 | assert( pSort==0 ); |
| 121721 | 121936 | } |
| 121722 | 121937 | #endif |
| 121723 | 121938 | if( pSort ){ |
| 121724 | | - pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg); |
| 121939 | + assert( regResult==regOrig ); |
| 121940 | + pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg); |
| 121725 | 121941 | }else{ |
| 121726 | 121942 | int r2 = sqlite3GetTempReg(pParse); |
| 121727 | 121943 | sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2); |
| 121728 | 121944 | sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2); |
| 121729 | 121945 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| | @@ -121986,15 +122202,11 @@ |
| 121986 | 122202 | ** |
| 121987 | 122203 | ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which |
| 121988 | 122204 | ** is determined by the zUsage argument. |
| 121989 | 122205 | */ |
| 121990 | 122206 | static void explainTempTable(Parse *pParse, const char *zUsage){ |
| 121991 | | - if( pParse->explain==2 ){ |
| 121992 | | - Vdbe *v = pParse->pVdbe; |
| 121993 | | - char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage); |
| 121994 | | - sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); |
| 121995 | | - } |
| 122207 | + ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage)); |
| 121996 | 122208 | } |
| 121997 | 122209 | |
| 121998 | 122210 | /* |
| 121999 | 122211 | ** Assign expression b to lvalue a. A second, no-op, version of this macro |
| 122000 | 122212 | ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code |
| | @@ -122008,46 +122220,10 @@ |
| 122008 | 122220 | /* No-op versions of the explainXXX() functions and macros. */ |
| 122009 | 122221 | # define explainTempTable(y,z) |
| 122010 | 122222 | # define explainSetInteger(y,z) |
| 122011 | 122223 | #endif |
| 122012 | 122224 | |
| 122013 | | -#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT) |
| 122014 | | -/* |
| 122015 | | -** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function |
| 122016 | | -** is a no-op. Otherwise, it adds a single row of output to the EQP result, |
| 122017 | | -** where the caption is of one of the two forms: |
| 122018 | | -** |
| 122019 | | -** "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)" |
| 122020 | | -** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)" |
| 122021 | | -** |
| 122022 | | -** where iSub1 and iSub2 are the integers passed as the corresponding |
| 122023 | | -** function parameters, and op is the text representation of the parameter |
| 122024 | | -** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT, |
| 122025 | | -** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is |
| 122026 | | -** false, or the second form if it is true. |
| 122027 | | -*/ |
| 122028 | | -static void explainComposite( |
| 122029 | | - Parse *pParse, /* Parse context */ |
| 122030 | | - int op, /* One of TK_UNION, TK_EXCEPT etc. */ |
| 122031 | | - int iSub1, /* Subquery id 1 */ |
| 122032 | | - int iSub2, /* Subquery id 2 */ |
| 122033 | | - int bUseTmp /* True if a temp table was used */ |
| 122034 | | -){ |
| 122035 | | - assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL ); |
| 122036 | | - if( pParse->explain==2 ){ |
| 122037 | | - Vdbe *v = pParse->pVdbe; |
| 122038 | | - char *zMsg = sqlite3MPrintf( |
| 122039 | | - pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2, |
| 122040 | | - bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op) |
| 122041 | | - ); |
| 122042 | | - sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); |
| 122043 | | - } |
| 122044 | | -} |
| 122045 | | -#else |
| 122046 | | -/* No-op versions of the explainXXX() functions and macros. */ |
| 122047 | | -# define explainComposite(v,w,x,y,z) |
| 122048 | | -#endif |
| 122049 | 122225 | |
| 122050 | 122226 | /* |
| 122051 | 122227 | ** If the inner loop was generated using a non-null pOrderBy argument, |
| 122052 | 122228 | ** then the results were placed in a sorter. After the loop is terminated |
| 122053 | 122229 | ** we need to run the sorter and output the results. The following |
| | @@ -122493,11 +122669,11 @@ |
| 122493 | 122669 | if( pParse->explain ){ |
| 122494 | 122670 | return; |
| 122495 | 122671 | } |
| 122496 | 122672 | #endif |
| 122497 | 122673 | |
| 122498 | | - if( pParse->colNamesSet || db->mallocFailed ) return; |
| 122674 | + if( pParse->colNamesSet ) return; |
| 122499 | 122675 | /* Column names are determined by the left-most term of a compound select */ |
| 122500 | 122676 | while( pSelect->pPrior ) pSelect = pSelect->pPrior; |
| 122501 | 122677 | SELECTTRACE(1,pParse,pSelect,("generating column names\n")); |
| 122502 | 122678 | pTabList = pSelect->pSrc; |
| 122503 | 122679 | pEList = pSelect->pEList; |
| | @@ -123020,10 +123196,11 @@ |
| 123020 | 123196 | /* Detach the ORDER BY clause from the compound SELECT */ |
| 123021 | 123197 | p->pOrderBy = 0; |
| 123022 | 123198 | |
| 123023 | 123199 | /* Store the results of the setup-query in Queue. */ |
| 123024 | 123200 | pSetup->pNext = 0; |
| 123201 | + ExplainQueryPlan((pParse, 1, "SETUP")); |
| 123025 | 123202 | rc = sqlite3Select(pParse, pSetup, &destQueue); |
| 123026 | 123203 | pSetup->pNext = p; |
| 123027 | 123204 | if( rc ) goto end_of_recursive_query; |
| 123028 | 123205 | |
| 123029 | 123206 | /* Find the next row in the Queue and output that row */ |
| | @@ -123054,10 +123231,11 @@ |
| 123054 | 123231 | */ |
| 123055 | 123232 | if( p->selFlags & SF_Aggregate ){ |
| 123056 | 123233 | sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported"); |
| 123057 | 123234 | }else{ |
| 123058 | 123235 | p->pPrior = 0; |
| 123236 | + ExplainQueryPlan((pParse, 1, "RECURSIVE STEP")); |
| 123059 | 123237 | sqlite3Select(pParse, p, &destQueue); |
| 123060 | 123238 | assert( p->pPrior==0 ); |
| 123061 | 123239 | p->pPrior = pSetup; |
| 123062 | 123240 | } |
| 123063 | 123241 | |
| | @@ -123099,30 +123277,28 @@ |
| 123099 | 123277 | static int multiSelectValues( |
| 123100 | 123278 | Parse *pParse, /* Parsing context */ |
| 123101 | 123279 | Select *p, /* The right-most of SELECTs to be coded */ |
| 123102 | 123280 | SelectDest *pDest /* What to do with query results */ |
| 123103 | 123281 | ){ |
| 123104 | | - Select *pPrior; |
| 123105 | | - Select *pRightmost = p; |
| 123106 | 123282 | int nRow = 1; |
| 123107 | 123283 | int rc = 0; |
| 123284 | + int bShowAll = p->pLimit==0; |
| 123108 | 123285 | assert( p->selFlags & SF_MultiValue ); |
| 123109 | 123286 | do{ |
| 123110 | 123287 | assert( p->selFlags & SF_Values ); |
| 123111 | 123288 | assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) ); |
| 123112 | 123289 | assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr ); |
| 123113 | 123290 | if( p->pPrior==0 ) break; |
| 123114 | 123291 | assert( p->pPrior->pNext==p ); |
| 123115 | 123292 | p = p->pPrior; |
| 123116 | | - nRow++; |
| 123293 | + nRow += bShowAll; |
| 123117 | 123294 | }while(1); |
| 123295 | + ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow, |
| 123296 | + nRow==1 ? "" : "S")); |
| 123118 | 123297 | while( p ){ |
| 123119 | | - pPrior = p->pPrior; |
| 123120 | | - p->pPrior = 0; |
| 123121 | | - rc = sqlite3Select(pParse, p, pDest); |
| 123122 | | - p->pPrior = pPrior; |
| 123123 | | - if( rc || pRightmost->pLimit ) break; |
| 123298 | + selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1); |
| 123299 | + if( !bShowAll ) break; |
| 123124 | 123300 | p->nSelectRow = nRow; |
| 123125 | 123301 | p = p->pNext; |
| 123126 | 123302 | } |
| 123127 | 123303 | return rc; |
| 123128 | 123304 | } |
| | @@ -123167,14 +123343,10 @@ |
| 123167 | 123343 | Select *pPrior; /* Another SELECT immediately to our left */ |
| 123168 | 123344 | Vdbe *v; /* Generate code to this VDBE */ |
| 123169 | 123345 | SelectDest dest; /* Alternative data destination */ |
| 123170 | 123346 | Select *pDelete = 0; /* Chain of simple selects to delete */ |
| 123171 | 123347 | sqlite3 *db; /* Database connection */ |
| 123172 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 123173 | | - int iSub1 = 0; /* EQP id of left-hand query */ |
| 123174 | | - int iSub2 = 0; /* EQP id of right-hand query */ |
| 123175 | | -#endif |
| 123176 | 123348 | |
| 123177 | 123349 | /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only |
| 123178 | 123350 | ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. |
| 123179 | 123351 | */ |
| 123180 | 123352 | assert( p && p->pPrior ); /* Calling function guarantees this much */ |
| | @@ -123221,221 +123393,235 @@ |
| 123221 | 123393 | |
| 123222 | 123394 | /* Compound SELECTs that have an ORDER BY clause are handled separately. |
| 123223 | 123395 | */ |
| 123224 | 123396 | if( p->pOrderBy ){ |
| 123225 | 123397 | return multiSelectOrderBy(pParse, p, pDest); |
| 123226 | | - }else |
| 123227 | | - |
| 123228 | | - /* Generate code for the left and right SELECT statements. |
| 123229 | | - */ |
| 123230 | | - switch( p->op ){ |
| 123231 | | - case TK_ALL: { |
| 123232 | | - int addr = 0; |
| 123233 | | - int nLimit; |
| 123234 | | - assert( !pPrior->pLimit ); |
| 123235 | | - pPrior->iLimit = p->iLimit; |
| 123236 | | - pPrior->iOffset = p->iOffset; |
| 123237 | | - pPrior->pLimit = p->pLimit; |
| 123238 | | - explainSetInteger(iSub1, pParse->iNextSelectId); |
| 123239 | | - rc = sqlite3Select(pParse, pPrior, &dest); |
| 123240 | | - p->pLimit = 0; |
| 123241 | | - if( rc ){ |
| 123242 | | - goto multi_select_end; |
| 123243 | | - } |
| 123244 | | - p->pPrior = 0; |
| 123245 | | - p->iLimit = pPrior->iLimit; |
| 123246 | | - p->iOffset = pPrior->iOffset; |
| 123247 | | - if( p->iLimit ){ |
| 123248 | | - addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v); |
| 123249 | | - VdbeComment((v, "Jump ahead if LIMIT reached")); |
| 123250 | | - if( p->iOffset ){ |
| 123251 | | - sqlite3VdbeAddOp3(v, OP_OffsetLimit, |
| 123252 | | - p->iLimit, p->iOffset+1, p->iOffset); |
| 123253 | | - } |
| 123254 | | - } |
| 123255 | | - explainSetInteger(iSub2, pParse->iNextSelectId); |
| 123256 | | - rc = sqlite3Select(pParse, p, &dest); |
| 123257 | | - testcase( rc!=SQLITE_OK ); |
| 123258 | | - pDelete = p->pPrior; |
| 123259 | | - p->pPrior = pPrior; |
| 123260 | | - p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
| 123261 | | - if( pPrior->pLimit |
| 123262 | | - && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit) |
| 123263 | | - && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) |
| 123264 | | - ){ |
| 123265 | | - p->nSelectRow = sqlite3LogEst((u64)nLimit); |
| 123266 | | - } |
| 123267 | | - if( addr ){ |
| 123268 | | - sqlite3VdbeJumpHere(v, addr); |
| 123269 | | - } |
| 123270 | | - break; |
| 123271 | | - } |
| 123272 | | - case TK_EXCEPT: |
| 123273 | | - case TK_UNION: { |
| 123274 | | - int unionTab; /* Cursor number of the temporary table holding result */ |
| 123275 | | - u8 op = 0; /* One of the SRT_ operations to apply to self */ |
| 123276 | | - int priorOp; /* The SRT_ operation to apply to prior selects */ |
| 123277 | | - Expr *pLimit; /* Saved values of p->nLimit */ |
| 123278 | | - int addr; |
| 123279 | | - SelectDest uniondest; |
| 123280 | | - |
| 123281 | | - testcase( p->op==TK_EXCEPT ); |
| 123282 | | - testcase( p->op==TK_UNION ); |
| 123283 | | - priorOp = SRT_Union; |
| 123284 | | - if( dest.eDest==priorOp ){ |
| 123285 | | - /* We can reuse a temporary table generated by a SELECT to our |
| 123286 | | - ** right. |
| 123287 | | - */ |
| 123288 | | - assert( p->pLimit==0 ); /* Not allowed on leftward elements */ |
| 123289 | | - unionTab = dest.iSDParm; |
| 123290 | | - }else{ |
| 123291 | | - /* We will need to create our own temporary table to hold the |
| 123292 | | - ** intermediate results. |
| 123293 | | - */ |
| 123294 | | - unionTab = pParse->nTab++; |
| 123295 | | - assert( p->pOrderBy==0 ); |
| 123296 | | - addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0); |
| 123297 | | - assert( p->addrOpenEphm[0] == -1 ); |
| 123298 | | - p->addrOpenEphm[0] = addr; |
| 123299 | | - findRightmost(p)->selFlags |= SF_UsesEphemeral; |
| 123300 | | - assert( p->pEList ); |
| 123301 | | - } |
| 123302 | | - |
| 123303 | | - /* Code the SELECT statements to our left |
| 123304 | | - */ |
| 123305 | | - assert( !pPrior->pOrderBy ); |
| 123306 | | - sqlite3SelectDestInit(&uniondest, priorOp, unionTab); |
| 123307 | | - explainSetInteger(iSub1, pParse->iNextSelectId); |
| 123308 | | - rc = sqlite3Select(pParse, pPrior, &uniondest); |
| 123309 | | - if( rc ){ |
| 123310 | | - goto multi_select_end; |
| 123311 | | - } |
| 123312 | | - |
| 123313 | | - /* Code the current SELECT statement |
| 123314 | | - */ |
| 123315 | | - if( p->op==TK_EXCEPT ){ |
| 123316 | | - op = SRT_Except; |
| 123317 | | - }else{ |
| 123318 | | - assert( p->op==TK_UNION ); |
| 123319 | | - op = SRT_Union; |
| 123320 | | - } |
| 123321 | | - p->pPrior = 0; |
| 123322 | | - pLimit = p->pLimit; |
| 123323 | | - p->pLimit = 0; |
| 123324 | | - uniondest.eDest = op; |
| 123325 | | - explainSetInteger(iSub2, pParse->iNextSelectId); |
| 123326 | | - rc = sqlite3Select(pParse, p, &uniondest); |
| 123327 | | - testcase( rc!=SQLITE_OK ); |
| 123328 | | - /* Query flattening in sqlite3Select() might refill p->pOrderBy. |
| 123329 | | - ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */ |
| 123330 | | - sqlite3ExprListDelete(db, p->pOrderBy); |
| 123331 | | - pDelete = p->pPrior; |
| 123332 | | - p->pPrior = pPrior; |
| 123333 | | - p->pOrderBy = 0; |
| 123334 | | - if( p->op==TK_UNION ){ |
| 123335 | | - p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
| 123336 | | - } |
| 123337 | | - sqlite3ExprDelete(db, p->pLimit); |
| 123338 | | - p->pLimit = pLimit; |
| 123339 | | - p->iLimit = 0; |
| 123340 | | - p->iOffset = 0; |
| 123341 | | - |
| 123342 | | - /* Convert the data in the temporary table into whatever form |
| 123343 | | - ** it is that we currently need. |
| 123344 | | - */ |
| 123345 | | - assert( unionTab==dest.iSDParm || dest.eDest!=priorOp ); |
| 123346 | | - if( dest.eDest!=priorOp ){ |
| 123347 | | - int iCont, iBreak, iStart; |
| 123348 | | - assert( p->pEList ); |
| 123349 | | - iBreak = sqlite3VdbeMakeLabel(v); |
| 123350 | | - iCont = sqlite3VdbeMakeLabel(v); |
| 123351 | | - computeLimitRegisters(pParse, p, iBreak); |
| 123352 | | - sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v); |
| 123353 | | - iStart = sqlite3VdbeCurrentAddr(v); |
| 123354 | | - selectInnerLoop(pParse, p, unionTab, |
| 123355 | | - 0, 0, &dest, iCont, iBreak); |
| 123356 | | - sqlite3VdbeResolveLabel(v, iCont); |
| 123357 | | - sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v); |
| 123358 | | - sqlite3VdbeResolveLabel(v, iBreak); |
| 123359 | | - sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0); |
| 123360 | | - } |
| 123361 | | - break; |
| 123362 | | - } |
| 123363 | | - default: assert( p->op==TK_INTERSECT ); { |
| 123364 | | - int tab1, tab2; |
| 123365 | | - int iCont, iBreak, iStart; |
| 123366 | | - Expr *pLimit; |
| 123367 | | - int addr; |
| 123368 | | - SelectDest intersectdest; |
| 123369 | | - int r1; |
| 123370 | | - |
| 123371 | | - /* INTERSECT is different from the others since it requires |
| 123372 | | - ** two temporary tables. Hence it has its own case. Begin |
| 123373 | | - ** by allocating the tables we will need. |
| 123374 | | - */ |
| 123375 | | - tab1 = pParse->nTab++; |
| 123376 | | - tab2 = pParse->nTab++; |
| 123377 | | - assert( p->pOrderBy==0 ); |
| 123378 | | - |
| 123379 | | - addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0); |
| 123380 | | - assert( p->addrOpenEphm[0] == -1 ); |
| 123381 | | - p->addrOpenEphm[0] = addr; |
| 123382 | | - findRightmost(p)->selFlags |= SF_UsesEphemeral; |
| 123383 | | - assert( p->pEList ); |
| 123384 | | - |
| 123385 | | - /* Code the SELECTs to our left into temporary table "tab1". |
| 123386 | | - */ |
| 123387 | | - sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1); |
| 123388 | | - explainSetInteger(iSub1, pParse->iNextSelectId); |
| 123389 | | - rc = sqlite3Select(pParse, pPrior, &intersectdest); |
| 123390 | | - if( rc ){ |
| 123391 | | - goto multi_select_end; |
| 123392 | | - } |
| 123393 | | - |
| 123394 | | - /* Code the current SELECT into temporary table "tab2" |
| 123395 | | - */ |
| 123396 | | - addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0); |
| 123397 | | - assert( p->addrOpenEphm[1] == -1 ); |
| 123398 | | - p->addrOpenEphm[1] = addr; |
| 123399 | | - p->pPrior = 0; |
| 123400 | | - pLimit = p->pLimit; |
| 123401 | | - p->pLimit = 0; |
| 123402 | | - intersectdest.iSDParm = tab2; |
| 123403 | | - explainSetInteger(iSub2, pParse->iNextSelectId); |
| 123404 | | - rc = sqlite3Select(pParse, p, &intersectdest); |
| 123405 | | - testcase( rc!=SQLITE_OK ); |
| 123406 | | - pDelete = p->pPrior; |
| 123407 | | - p->pPrior = pPrior; |
| 123408 | | - if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; |
| 123409 | | - sqlite3ExprDelete(db, p->pLimit); |
| 123410 | | - p->pLimit = pLimit; |
| 123411 | | - |
| 123412 | | - /* Generate code to take the intersection of the two temporary |
| 123413 | | - ** tables. |
| 123414 | | - */ |
| 123415 | | - assert( p->pEList ); |
| 123416 | | - iBreak = sqlite3VdbeMakeLabel(v); |
| 123417 | | - iCont = sqlite3VdbeMakeLabel(v); |
| 123418 | | - computeLimitRegisters(pParse, p, iBreak); |
| 123419 | | - sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v); |
| 123420 | | - r1 = sqlite3GetTempReg(pParse); |
| 123421 | | - iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1); |
| 123422 | | - sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v); |
| 123423 | | - sqlite3ReleaseTempReg(pParse, r1); |
| 123424 | | - selectInnerLoop(pParse, p, tab1, |
| 123425 | | - 0, 0, &dest, iCont, iBreak); |
| 123426 | | - sqlite3VdbeResolveLabel(v, iCont); |
| 123427 | | - sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v); |
| 123428 | | - sqlite3VdbeResolveLabel(v, iBreak); |
| 123429 | | - sqlite3VdbeAddOp2(v, OP_Close, tab2, 0); |
| 123430 | | - sqlite3VdbeAddOp2(v, OP_Close, tab1, 0); |
| 123431 | | - break; |
| 123432 | | - } |
| 123433 | | - } |
| 123434 | | - |
| 123435 | | - explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL); |
| 123436 | | - |
| 123398 | + }else{ |
| 123399 | + |
| 123400 | +#ifndef SQLITE_OMIT_EXPLAIN |
| 123401 | + if( pPrior->pPrior==0 ){ |
| 123402 | + ExplainQueryPlan((pParse, 1, "COMPOUND QUERY")); |
| 123403 | + ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY")); |
| 123404 | + } |
| 123405 | +#endif |
| 123406 | + |
| 123407 | + /* Generate code for the left and right SELECT statements. |
| 123408 | + */ |
| 123409 | + switch( p->op ){ |
| 123410 | + case TK_ALL: { |
| 123411 | + int addr = 0; |
| 123412 | + int nLimit; |
| 123413 | + assert( !pPrior->pLimit ); |
| 123414 | + pPrior->iLimit = p->iLimit; |
| 123415 | + pPrior->iOffset = p->iOffset; |
| 123416 | + pPrior->pLimit = p->pLimit; |
| 123417 | + rc = sqlite3Select(pParse, pPrior, &dest); |
| 123418 | + p->pLimit = 0; |
| 123419 | + if( rc ){ |
| 123420 | + goto multi_select_end; |
| 123421 | + } |
| 123422 | + p->pPrior = 0; |
| 123423 | + p->iLimit = pPrior->iLimit; |
| 123424 | + p->iOffset = pPrior->iOffset; |
| 123425 | + if( p->iLimit ){ |
| 123426 | + addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v); |
| 123427 | + VdbeComment((v, "Jump ahead if LIMIT reached")); |
| 123428 | + if( p->iOffset ){ |
| 123429 | + sqlite3VdbeAddOp3(v, OP_OffsetLimit, |
| 123430 | + p->iLimit, p->iOffset+1, p->iOffset); |
| 123431 | + } |
| 123432 | + } |
| 123433 | + ExplainQueryPlan((pParse, 1, "UNION ALL")); |
| 123434 | + rc = sqlite3Select(pParse, p, &dest); |
| 123435 | + testcase( rc!=SQLITE_OK ); |
| 123436 | + pDelete = p->pPrior; |
| 123437 | + p->pPrior = pPrior; |
| 123438 | + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
| 123439 | + if( pPrior->pLimit |
| 123440 | + && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit) |
| 123441 | + && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) |
| 123442 | + ){ |
| 123443 | + p->nSelectRow = sqlite3LogEst((u64)nLimit); |
| 123444 | + } |
| 123445 | + if( addr ){ |
| 123446 | + sqlite3VdbeJumpHere(v, addr); |
| 123447 | + } |
| 123448 | + break; |
| 123449 | + } |
| 123450 | + case TK_EXCEPT: |
| 123451 | + case TK_UNION: { |
| 123452 | + int unionTab; /* Cursor number of the temp table holding result */ |
| 123453 | + u8 op = 0; /* One of the SRT_ operations to apply to self */ |
| 123454 | + int priorOp; /* The SRT_ operation to apply to prior selects */ |
| 123455 | + Expr *pLimit; /* Saved values of p->nLimit */ |
| 123456 | + int addr; |
| 123457 | + SelectDest uniondest; |
| 123458 | + |
| 123459 | + testcase( p->op==TK_EXCEPT ); |
| 123460 | + testcase( p->op==TK_UNION ); |
| 123461 | + priorOp = SRT_Union; |
| 123462 | + if( dest.eDest==priorOp ){ |
| 123463 | + /* We can reuse a temporary table generated by a SELECT to our |
| 123464 | + ** right. |
| 123465 | + */ |
| 123466 | + assert( p->pLimit==0 ); /* Not allowed on leftward elements */ |
| 123467 | + unionTab = dest.iSDParm; |
| 123468 | + }else{ |
| 123469 | + /* We will need to create our own temporary table to hold the |
| 123470 | + ** intermediate results. |
| 123471 | + */ |
| 123472 | + unionTab = pParse->nTab++; |
| 123473 | + assert( p->pOrderBy==0 ); |
| 123474 | + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0); |
| 123475 | + assert( p->addrOpenEphm[0] == -1 ); |
| 123476 | + p->addrOpenEphm[0] = addr; |
| 123477 | + findRightmost(p)->selFlags |= SF_UsesEphemeral; |
| 123478 | + assert( p->pEList ); |
| 123479 | + } |
| 123480 | + |
| 123481 | + /* Code the SELECT statements to our left |
| 123482 | + */ |
| 123483 | + assert( !pPrior->pOrderBy ); |
| 123484 | + sqlite3SelectDestInit(&uniondest, priorOp, unionTab); |
| 123485 | + rc = sqlite3Select(pParse, pPrior, &uniondest); |
| 123486 | + if( rc ){ |
| 123487 | + goto multi_select_end; |
| 123488 | + } |
| 123489 | + |
| 123490 | + /* Code the current SELECT statement |
| 123491 | + */ |
| 123492 | + if( p->op==TK_EXCEPT ){ |
| 123493 | + op = SRT_Except; |
| 123494 | + }else{ |
| 123495 | + assert( p->op==TK_UNION ); |
| 123496 | + op = SRT_Union; |
| 123497 | + } |
| 123498 | + p->pPrior = 0; |
| 123499 | + pLimit = p->pLimit; |
| 123500 | + p->pLimit = 0; |
| 123501 | + uniondest.eDest = op; |
| 123502 | + ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE", |
| 123503 | + selectOpName(p->op))); |
| 123504 | + rc = sqlite3Select(pParse, p, &uniondest); |
| 123505 | + testcase( rc!=SQLITE_OK ); |
| 123506 | + /* Query flattening in sqlite3Select() might refill p->pOrderBy. |
| 123507 | + ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */ |
| 123508 | + sqlite3ExprListDelete(db, p->pOrderBy); |
| 123509 | + pDelete = p->pPrior; |
| 123510 | + p->pPrior = pPrior; |
| 123511 | + p->pOrderBy = 0; |
| 123512 | + if( p->op==TK_UNION ){ |
| 123513 | + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
| 123514 | + } |
| 123515 | + sqlite3ExprDelete(db, p->pLimit); |
| 123516 | + p->pLimit = pLimit; |
| 123517 | + p->iLimit = 0; |
| 123518 | + p->iOffset = 0; |
| 123519 | + |
| 123520 | + /* Convert the data in the temporary table into whatever form |
| 123521 | + ** it is that we currently need. |
| 123522 | + */ |
| 123523 | + assert( unionTab==dest.iSDParm || dest.eDest!=priorOp ); |
| 123524 | + if( dest.eDest!=priorOp ){ |
| 123525 | + int iCont, iBreak, iStart; |
| 123526 | + assert( p->pEList ); |
| 123527 | + iBreak = sqlite3VdbeMakeLabel(v); |
| 123528 | + iCont = sqlite3VdbeMakeLabel(v); |
| 123529 | + computeLimitRegisters(pParse, p, iBreak); |
| 123530 | + sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v); |
| 123531 | + iStart = sqlite3VdbeCurrentAddr(v); |
| 123532 | + selectInnerLoop(pParse, p, unionTab, |
| 123533 | + 0, 0, &dest, iCont, iBreak); |
| 123534 | + sqlite3VdbeResolveLabel(v, iCont); |
| 123535 | + sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v); |
| 123536 | + sqlite3VdbeResolveLabel(v, iBreak); |
| 123537 | + sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0); |
| 123538 | + } |
| 123539 | + break; |
| 123540 | + } |
| 123541 | + default: assert( p->op==TK_INTERSECT ); { |
| 123542 | + int tab1, tab2; |
| 123543 | + int iCont, iBreak, iStart; |
| 123544 | + Expr *pLimit; |
| 123545 | + int addr; |
| 123546 | + SelectDest intersectdest; |
| 123547 | + int r1; |
| 123548 | + |
| 123549 | + /* INTERSECT is different from the others since it requires |
| 123550 | + ** two temporary tables. Hence it has its own case. Begin |
| 123551 | + ** by allocating the tables we will need. |
| 123552 | + */ |
| 123553 | + tab1 = pParse->nTab++; |
| 123554 | + tab2 = pParse->nTab++; |
| 123555 | + assert( p->pOrderBy==0 ); |
| 123556 | + |
| 123557 | + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0); |
| 123558 | + assert( p->addrOpenEphm[0] == -1 ); |
| 123559 | + p->addrOpenEphm[0] = addr; |
| 123560 | + findRightmost(p)->selFlags |= SF_UsesEphemeral; |
| 123561 | + assert( p->pEList ); |
| 123562 | + |
| 123563 | + /* Code the SELECTs to our left into temporary table "tab1". |
| 123564 | + */ |
| 123565 | + sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1); |
| 123566 | + rc = sqlite3Select(pParse, pPrior, &intersectdest); |
| 123567 | + if( rc ){ |
| 123568 | + goto multi_select_end; |
| 123569 | + } |
| 123570 | + |
| 123571 | + /* Code the current SELECT into temporary table "tab2" |
| 123572 | + */ |
| 123573 | + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0); |
| 123574 | + assert( p->addrOpenEphm[1] == -1 ); |
| 123575 | + p->addrOpenEphm[1] = addr; |
| 123576 | + p->pPrior = 0; |
| 123577 | + pLimit = p->pLimit; |
| 123578 | + p->pLimit = 0; |
| 123579 | + intersectdest.iSDParm = tab2; |
| 123580 | + ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE", |
| 123581 | + selectOpName(p->op))); |
| 123582 | + rc = sqlite3Select(pParse, p, &intersectdest); |
| 123583 | + testcase( rc!=SQLITE_OK ); |
| 123584 | + pDelete = p->pPrior; |
| 123585 | + p->pPrior = pPrior; |
| 123586 | + if( p->nSelectRow>pPrior->nSelectRow ){ |
| 123587 | + p->nSelectRow = pPrior->nSelectRow; |
| 123588 | + } |
| 123589 | + sqlite3ExprDelete(db, p->pLimit); |
| 123590 | + p->pLimit = pLimit; |
| 123591 | + |
| 123592 | + /* Generate code to take the intersection of the two temporary |
| 123593 | + ** tables. |
| 123594 | + */ |
| 123595 | + assert( p->pEList ); |
| 123596 | + iBreak = sqlite3VdbeMakeLabel(v); |
| 123597 | + iCont = sqlite3VdbeMakeLabel(v); |
| 123598 | + computeLimitRegisters(pParse, p, iBreak); |
| 123599 | + sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v); |
| 123600 | + r1 = sqlite3GetTempReg(pParse); |
| 123601 | + iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1); |
| 123602 | + sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); |
| 123603 | + VdbeCoverage(v); |
| 123604 | + sqlite3ReleaseTempReg(pParse, r1); |
| 123605 | + selectInnerLoop(pParse, p, tab1, |
| 123606 | + 0, 0, &dest, iCont, iBreak); |
| 123607 | + sqlite3VdbeResolveLabel(v, iCont); |
| 123608 | + sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v); |
| 123609 | + sqlite3VdbeResolveLabel(v, iBreak); |
| 123610 | + sqlite3VdbeAddOp2(v, OP_Close, tab2, 0); |
| 123611 | + sqlite3VdbeAddOp2(v, OP_Close, tab1, 0); |
| 123612 | + break; |
| 123613 | + } |
| 123614 | + } |
| 123615 | + |
| 123616 | + #ifndef SQLITE_OMIT_EXPLAIN |
| 123617 | + if( p->pNext==0 ){ |
| 123618 | + ExplainQueryPlanPop(pParse); |
| 123619 | + } |
| 123620 | + #endif |
| 123621 | + } |
| 123622 | + |
| 123437 | 123623 | /* Compute collating sequences used by |
| 123438 | 123624 | ** temporary tables needed to implement the compound select. |
| 123439 | 123625 | ** Attach the KeyInfo structure to all temporary tables. |
| 123440 | 123626 | ** |
| 123441 | 123627 | ** This section is run by the right-most SELECT statement only. |
| | @@ -123769,14 +123955,10 @@ |
| 123769 | 123955 | KeyInfo *pKeyMerge; /* Comparison information for merging rows */ |
| 123770 | 123956 | sqlite3 *db; /* Database connection */ |
| 123771 | 123957 | ExprList *pOrderBy; /* The ORDER BY clause */ |
| 123772 | 123958 | int nOrderBy; /* Number of terms in the ORDER BY clause */ |
| 123773 | 123959 | int *aPermute; /* Mapping from ORDER BY terms to result set columns */ |
| 123774 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 123775 | | - int iSub1; /* EQP id of left-hand query */ |
| 123776 | | - int iSub2; /* EQP id of right-hand query */ |
| 123777 | | -#endif |
| 123778 | 123960 | |
| 123779 | 123961 | assert( p->pOrderBy!=0 ); |
| 123780 | 123962 | assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */ |
| 123781 | 123963 | db = pParse->db; |
| 123782 | 123964 | v = pParse->pVdbe; |
| | @@ -123892,18 +124074,20 @@ |
| 123892 | 124074 | regOutA = ++pParse->nMem; |
| 123893 | 124075 | regOutB = ++pParse->nMem; |
| 123894 | 124076 | sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA); |
| 123895 | 124077 | sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB); |
| 123896 | 124078 | |
| 124079 | + ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op))); |
| 124080 | + |
| 123897 | 124081 | /* Generate a coroutine to evaluate the SELECT statement to the |
| 123898 | 124082 | ** left of the compound operator - the "A" select. |
| 123899 | 124083 | */ |
| 123900 | 124084 | addrSelectA = sqlite3VdbeCurrentAddr(v) + 1; |
| 123901 | 124085 | addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA); |
| 123902 | 124086 | VdbeComment((v, "left SELECT")); |
| 123903 | 124087 | pPrior->iLimit = regLimitA; |
| 123904 | | - explainSetInteger(iSub1, pParse->iNextSelectId); |
| 124088 | + ExplainQueryPlan((pParse, 1, "LEFT")); |
| 123905 | 124089 | sqlite3Select(pParse, pPrior, &destA); |
| 123906 | 124090 | sqlite3VdbeEndCoroutine(v, regAddrA); |
| 123907 | 124091 | sqlite3VdbeJumpHere(v, addr1); |
| 123908 | 124092 | |
| 123909 | 124093 | /* Generate a coroutine to evaluate the SELECT statement on |
| | @@ -123914,11 +124098,11 @@ |
| 123914 | 124098 | VdbeComment((v, "right SELECT")); |
| 123915 | 124099 | savedLimit = p->iLimit; |
| 123916 | 124100 | savedOffset = p->iOffset; |
| 123917 | 124101 | p->iLimit = regLimitB; |
| 123918 | 124102 | p->iOffset = 0; |
| 123919 | | - explainSetInteger(iSub2, pParse->iNextSelectId); |
| 124103 | + ExplainQueryPlan((pParse, 1, "RIGHT")); |
| 123920 | 124104 | sqlite3Select(pParse, p, &destB); |
| 123921 | 124105 | p->iLimit = savedLimit; |
| 123922 | 124106 | p->iOffset = savedOffset; |
| 123923 | 124107 | sqlite3VdbeEndCoroutine(v, regAddrB); |
| 123924 | 124108 | |
| | @@ -124026,11 +124210,11 @@ |
| 124026 | 124210 | p->pPrior = pPrior; |
| 124027 | 124211 | pPrior->pNext = p; |
| 124028 | 124212 | |
| 124029 | 124213 | /*** TBD: Insert subroutine calls to close cursors on incomplete |
| 124030 | 124214 | **** subqueries ****/ |
| 124031 | | - explainComposite(pParse, p->op, iSub1, iSub2, 0); |
| 124215 | + ExplainQueryPlanPop(pParse); |
| 124032 | 124216 | return pParse->nErr!=0; |
| 124033 | 124217 | } |
| 124034 | 124218 | #endif |
| 124035 | 124219 | |
| 124036 | 124220 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| | @@ -125814,18 +125998,15 @@ |
| 125814 | 125998 | Table *pTab, /* Table being queried */ |
| 125815 | 125999 | Index *pIdx /* Index used to optimize scan, or NULL */ |
| 125816 | 126000 | ){ |
| 125817 | 126001 | if( pParse->explain==2 ){ |
| 125818 | 126002 | int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx))); |
| 125819 | | - char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s", |
| 126003 | + sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s", |
| 125820 | 126004 | pTab->zName, |
| 125821 | 126005 | bCover ? " USING COVERING INDEX " : "", |
| 125822 | 126006 | bCover ? pIdx->zName : "" |
| 125823 | 126007 | ); |
| 125824 | | - sqlite3VdbeAddOp4( |
| 125825 | | - pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC |
| 125826 | | - ); |
| 125827 | 126008 | } |
| 125828 | 126009 | } |
| 125829 | 126010 | #else |
| 125830 | 126011 | # define explainSimpleCount(a,b,c) |
| 125831 | 126012 | #endif |
| | @@ -126034,26 +126215,19 @@ |
| 126034 | 126215 | int iEnd; /* Address of the end of the query */ |
| 126035 | 126216 | sqlite3 *db; /* The database connection */ |
| 126036 | 126217 | ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */ |
| 126037 | 126218 | u8 minMaxFlag; /* Flag for min/max queries */ |
| 126038 | 126219 | |
| 126039 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 126040 | | - int iRestoreSelectId = pParse->iSelectId; |
| 126041 | | - pParse->iSelectId = pParse->iNextSelectId++; |
| 126042 | | -#endif |
| 126043 | | - |
| 126044 | 126220 | db = pParse->db; |
| 126221 | + v = sqlite3GetVdbe(pParse); |
| 126045 | 126222 | if( p==0 || db->mallocFailed || pParse->nErr ){ |
| 126046 | 126223 | return 1; |
| 126047 | 126224 | } |
| 126048 | 126225 | if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; |
| 126049 | 126226 | memset(&sAggInfo, 0, sizeof(sAggInfo)); |
| 126050 | 126227 | #if SELECTTRACE_ENABLED |
| 126051 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 126052 | | - p->iSelectId = pParse->iSelectId; |
| 126053 | | -#endif |
| 126054 | | - SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->iSelectId)); |
| 126228 | + SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain)); |
| 126055 | 126229 | if( sqlite3SelectTrace & 0x100 ){ |
| 126056 | 126230 | sqlite3TreeViewSelect(0, p, 0); |
| 126057 | 126231 | } |
| 126058 | 126232 | #endif |
| 126059 | 126233 | |
| | @@ -126086,14 +126260,10 @@ |
| 126086 | 126260 | SELECTTRACE(0x104,pParse,p, ("after name resolution:\n")); |
| 126087 | 126261 | sqlite3TreeViewSelect(0, p, 0); |
| 126088 | 126262 | } |
| 126089 | 126263 | #endif |
| 126090 | 126264 | |
| 126091 | | - /* Get a pointer the VDBE under construction, allocating a new VDBE if one |
| 126092 | | - ** does not already exist */ |
| 126093 | | - v = sqlite3GetVdbe(pParse); |
| 126094 | | - if( v==0 ) goto select_end; |
| 126095 | 126265 | if( pDest->eDest==SRT_Output ){ |
| 126096 | 126266 | generateColumnNames(pParse, p); |
| 126097 | 126267 | } |
| 126098 | 126268 | |
| 126099 | 126269 | /* Try to various optimizations (flattening subqueries, and strength |
| | @@ -126184,15 +126354,15 @@ |
| 126184 | 126354 | */ |
| 126185 | 126355 | if( p->pPrior ){ |
| 126186 | 126356 | rc = multiSelect(pParse, p, pDest); |
| 126187 | 126357 | #if SELECTTRACE_ENABLED |
| 126188 | 126358 | SELECTTRACE(0x1,pParse,p,("end compound-select processing\n")); |
| 126189 | | - if( pParse->iSelectId==0 && (sqlite3SelectTrace & 0x2000)!=0 ){ |
| 126359 | + if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){ |
| 126190 | 126360 | sqlite3TreeViewSelect(0, p, 0); |
| 126191 | 126361 | } |
| 126192 | 126362 | #endif |
| 126193 | | - explainSetInteger(pParse->iSelectId, iRestoreSelectId); |
| 126363 | + if( p->pNext==0 ) ExplainQueryPlanPop(pParse); |
| 126194 | 126364 | return rc; |
| 126195 | 126365 | } |
| 126196 | 126366 | #endif |
| 126197 | 126367 | |
| 126198 | 126368 | /* For each term in the FROM clause, do two things: |
| | @@ -126300,11 +126470,11 @@ |
| 126300 | 126470 | pItem->regReturn = ++pParse->nMem; |
| 126301 | 126471 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop); |
| 126302 | 126472 | VdbeComment((v, "%s", pItem->pTab->zName)); |
| 126303 | 126473 | pItem->addrFillSub = addrTop; |
| 126304 | 126474 | sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn); |
| 126305 | | - explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); |
| 126475 | + ExplainQueryPlan((pParse, 1, "CO-ROUTINE 0x%p", pSub)); |
| 126306 | 126476 | sqlite3Select(pParse, pSub, &dest); |
| 126307 | 126477 | pItem->pTab->nRowLogEst = pSub->nSelectRow; |
| 126308 | 126478 | pItem->fg.viaCoroutine = 1; |
| 126309 | 126479 | pItem->regResult = dest.iSdst; |
| 126310 | 126480 | sqlite3VdbeEndCoroutine(v, pItem->regReturn); |
| | @@ -126335,16 +126505,15 @@ |
| 126335 | 126505 | VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName)); |
| 126336 | 126506 | } |
| 126337 | 126507 | pPrior = isSelfJoinView(pTabList, pItem); |
| 126338 | 126508 | if( pPrior ){ |
| 126339 | 126509 | sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor); |
| 126340 | | - explainSetInteger(pItem->iSelectId, pPrior->iSelectId); |
| 126341 | 126510 | assert( pPrior->pSelect!=0 ); |
| 126342 | 126511 | pSub->nSelectRow = pPrior->pSelect->nSelectRow; |
| 126343 | 126512 | }else{ |
| 126344 | 126513 | sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); |
| 126345 | | - explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); |
| 126514 | + ExplainQueryPlan((pParse, 1, "MATERIALIZE 0x%p", pSub)); |
| 126346 | 126515 | sqlite3Select(pParse, pSub, &dest); |
| 126347 | 126516 | } |
| 126348 | 126517 | pItem->pTab->nRowLogEst = pSub->nSelectRow; |
| 126349 | 126518 | if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr); |
| 126350 | 126519 | retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn); |
| | @@ -126978,15 +127147,15 @@ |
| 126978 | 127147 | sqlite3ExprListDelete(db, pMinMaxOrderBy); |
| 126979 | 127148 | sqlite3DbFree(db, sAggInfo.aCol); |
| 126980 | 127149 | sqlite3DbFree(db, sAggInfo.aFunc); |
| 126981 | 127150 | #if SELECTTRACE_ENABLED |
| 126982 | 127151 | SELECTTRACE(0x1,pParse,p,("end processing\n")); |
| 126983 | | - if( pParse->iSelectId==0 && (sqlite3SelectTrace & 0x2000)!=0 ){ |
| 127152 | + if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){ |
| 126984 | 127153 | sqlite3TreeViewSelect(0, p, 0); |
| 126985 | 127154 | } |
| 126986 | 127155 | #endif |
| 126987 | | - explainSetInteger(pParse->iSelectId, iRestoreSelectId); |
| 127156 | + ExplainQueryPlanPop(pParse); |
| 126988 | 127157 | return rc; |
| 126989 | 127158 | } |
| 126990 | 127159 | |
| 126991 | 127160 | /************** End of select.c **********************************************/ |
| 126992 | 127161 | /************** Begin file table.c *******************************************/ |
| | @@ -129574,12 +129743,18 @@ |
| 129574 | 129743 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 129575 | 129744 | if( rc!=SQLITE_OK ) return rc; |
| 129576 | 129745 | while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){ |
| 129577 | 129746 | const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0); |
| 129578 | 129747 | assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 ); |
| 129579 | | - assert( sqlite3_strnicmp(zSubSql,"SELECT",6)!=0 || CORRUPT_DB ); |
| 129580 | | - if( zSubSql && zSubSql[0]!='S' ){ |
| 129748 | + /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX, |
| 129749 | + ** or INSERT. Historically there have been attacks that first |
| 129750 | + ** corrupt the sqlite_master.sql field with other kinds of statements |
| 129751 | + ** then run VACUUM to get those statements to execute at inappropriate |
| 129752 | + ** times. */ |
| 129753 | + if( zSubSql |
| 129754 | + && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0) |
| 129755 | + ){ |
| 129581 | 129756 | rc = execSql(db, pzErrMsg, zSubSql); |
| 129582 | 129757 | if( rc!=SQLITE_OK ) break; |
| 129583 | 129758 | } |
| 129584 | 129759 | } |
| 129585 | 129760 | assert( rc!=SQLITE_ROW ); |
| | @@ -129788,11 +129963,11 @@ |
| 129788 | 129963 | zDbMain |
| 129789 | 129964 | ); |
| 129790 | 129965 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 129791 | 129966 | rc = execSqlF(db, pzErrMsg, |
| 129792 | 129967 | "SELECT sql FROM \"%w\".sqlite_master" |
| 129793 | | - " WHERE type='index' AND length(sql)>10", |
| 129968 | + " WHERE type='index'", |
| 129794 | 129969 | zDbMain |
| 129795 | 129970 | ); |
| 129796 | 129971 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 129797 | 129972 | db->init.iDb = 0; |
| 129798 | 129973 | |
| | @@ -131852,11 +132027,10 @@ |
| 131852 | 132027 | #endif |
| 131853 | 132028 | { |
| 131854 | 132029 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 131855 | 132030 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 131856 | 132031 | sqlite3 *db = pParse->db; /* Database handle */ |
| 131857 | | - int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| 131858 | 132032 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 131859 | 132033 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 131860 | 132034 | u32 flags; /* Flags that describe this loop */ |
| 131861 | 132035 | char *zMsg; /* Text to add to EQP output */ |
| 131862 | 132036 | StrAccum str; /* EQP output string */ |
| | @@ -131871,11 +132045,11 @@ |
| 131871 | 132045 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 131872 | 132046 | |
| 131873 | 132047 | sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
| 131874 | 132048 | sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); |
| 131875 | 132049 | if( pItem->pSelect ){ |
| 131876 | | - sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId); |
| 132050 | + sqlite3XPrintf(&str, " SUBQUERY 0x%p", pItem->pSelect); |
| 131877 | 132051 | }else{ |
| 131878 | 132052 | sqlite3XPrintf(&str, " TABLE %s", pItem->zName); |
| 131879 | 132053 | } |
| 131880 | 132054 | |
| 131881 | 132055 | if( pItem->zAlias ){ |
| | @@ -131932,11 +132106,12 @@ |
| 131932 | 132106 | }else{ |
| 131933 | 132107 | sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 131934 | 132108 | } |
| 131935 | 132109 | #endif |
| 131936 | 132110 | zMsg = sqlite3StrAccumFinish(&str); |
| 131937 | | - ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); |
| 132111 | + ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v), |
| 132112 | + pParse->addrExplain, 0, zMsg,P4_DYNAMIC); |
| 131938 | 132113 | } |
| 131939 | 132114 | return ret; |
| 131940 | 132115 | } |
| 131941 | 132116 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 131942 | 132117 | |
| | @@ -133652,10 +133827,11 @@ |
| 133652 | 133827 | /* Run a separate WHERE clause for each term of the OR clause. After |
| 133653 | 133828 | ** eliminating duplicates from other WHERE clauses, the action for each |
| 133654 | 133829 | ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 133655 | 133830 | */ |
| 133656 | 133831 | wctrlFlags = WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE); |
| 133832 | + ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR")); |
| 133657 | 133833 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 133658 | 133834 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
| 133659 | 133835 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 133660 | 133836 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 133661 | 133837 | Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
| | @@ -133772,10 +133948,11 @@ |
| 133772 | 133948 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 133773 | 133949 | sqlite3WhereEnd(pSubWInfo); |
| 133774 | 133950 | } |
| 133775 | 133951 | } |
| 133776 | 133952 | } |
| 133953 | + ExplainQueryPlanPop(pParse); |
| 133777 | 133954 | pLevel->u.pCovidx = pCov; |
| 133778 | 133955 | if( pCov ) pLevel->iIdxCur = iCovCur; |
| 133779 | 133956 | if( pAndExpr ){ |
| 133780 | 133957 | pAndExpr->pLeft = 0; |
| 133781 | 133958 | sqlite3ExprDelete(db, pAndExpr); |
| | @@ -140079,10 +140256,11 @@ |
| 140079 | 140256 | if( nTabList==0 ){ |
| 140080 | 140257 | if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr; |
| 140081 | 140258 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 140082 | 140259 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 140083 | 140260 | } |
| 140261 | + ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW")); |
| 140084 | 140262 | }else{ |
| 140085 | 140263 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 140086 | 140264 | ** |
| 140087 | 140265 | ** The N-th term of the FROM clause is assigned a bitmask of 1<<N. |
| 140088 | 140266 | ** |
| | @@ -146939,10 +147117,11 @@ |
| 146939 | 147117 | { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer }, |
| 146940 | 147118 | { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension }, |
| 146941 | 147119 | { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose }, |
| 146942 | 147120 | { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG }, |
| 146943 | 147121 | { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP }, |
| 147122 | + { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase }, |
| 146944 | 147123 | }; |
| 146945 | 147124 | unsigned int i; |
| 146946 | 147125 | rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ |
| 146947 | 147126 | for(i=0; i<ArraySize(aFlagOp); i++){ |
| 146948 | 147127 | if( aFlagOp[i].op==op ){ |
| | @@ -151832,11 +152011,11 @@ |
| 151832 | 152011 | SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int, |
| 151833 | 152012 | char **, int, int, int, const char *, int, Fts3Expr **, char ** |
| 151834 | 152013 | ); |
| 151835 | 152014 | SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *); |
| 151836 | 152015 | #ifdef SQLITE_TEST |
| 151837 | | -SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db); |
| 152016 | +SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*); |
| 151838 | 152017 | SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db); |
| 151839 | 152018 | #endif |
| 151840 | 152019 | |
| 151841 | 152020 | SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int, |
| 151842 | 152021 | sqlite3_tokenizer_cursor ** |
| | @@ -155542,11 +155721,11 @@ |
| 155542 | 155721 | } |
| 155543 | 155722 | } |
| 155544 | 155723 | |
| 155545 | 155724 | #ifdef SQLITE_TEST |
| 155546 | 155725 | if( rc==SQLITE_OK ){ |
| 155547 | | - rc = sqlite3Fts3ExprInitTestInterface(db); |
| 155726 | + rc = sqlite3Fts3ExprInitTestInterface(db, pHash); |
| 155548 | 155727 | } |
| 155549 | 155728 | #endif |
| 155550 | 155729 | |
| 155551 | 155730 | /* Create the virtual table wrapper around the hash-table and overload |
| 155552 | 155731 | ** the four scalar functions. If this is successful, register the |
| | @@ -159202,38 +159381,10 @@ |
| 159202 | 159381 | |
| 159203 | 159382 | #ifdef SQLITE_TEST |
| 159204 | 159383 | |
| 159205 | 159384 | /* #include <stdio.h> */ |
| 159206 | 159385 | |
| 159207 | | -/* |
| 159208 | | -** Function to query the hash-table of tokenizers (see README.tokenizers). |
| 159209 | | -*/ |
| 159210 | | -static int queryTestTokenizer( |
| 159211 | | - sqlite3 *db, |
| 159212 | | - const char *zName, |
| 159213 | | - const sqlite3_tokenizer_module **pp |
| 159214 | | -){ |
| 159215 | | - int rc; |
| 159216 | | - sqlite3_stmt *pStmt; |
| 159217 | | - const char zSql[] = "SELECT fts3_tokenizer(?)"; |
| 159218 | | - |
| 159219 | | - *pp = 0; |
| 159220 | | - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 159221 | | - if( rc!=SQLITE_OK ){ |
| 159222 | | - return rc; |
| 159223 | | - } |
| 159224 | | - |
| 159225 | | - sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); |
| 159226 | | - if( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 159227 | | - if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ |
| 159228 | | - memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); |
| 159229 | | - } |
| 159230 | | - } |
| 159231 | | - |
| 159232 | | - return sqlite3_finalize(pStmt); |
| 159233 | | -} |
| 159234 | | - |
| 159235 | 159386 | /* |
| 159236 | 159387 | ** Return a pointer to a buffer containing a text representation of the |
| 159237 | 159388 | ** expression passed as the first argument. The buffer is obtained from |
| 159238 | 159389 | ** sqlite3_malloc(). It is the responsibility of the caller to use |
| 159239 | 159390 | ** sqlite3_free() to release the memory. If an OOM condition is encountered, |
| | @@ -159297,51 +159448,47 @@ |
| 159297 | 159448 | ** of a column of the fts3 table that the query expression may refer to. |
| 159298 | 159449 | ** For example: |
| 159299 | 159450 | ** |
| 159300 | 159451 | ** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2'); |
| 159301 | 159452 | */ |
| 159302 | | -static void fts3ExprTest( |
| 159453 | +static void fts3ExprTestCommon( |
| 159454 | + int bRebalance, |
| 159303 | 159455 | sqlite3_context *context, |
| 159304 | 159456 | int argc, |
| 159305 | 159457 | sqlite3_value **argv |
| 159306 | 159458 | ){ |
| 159307 | | - sqlite3_tokenizer_module const *pModule = 0; |
| 159308 | 159459 | sqlite3_tokenizer *pTokenizer = 0; |
| 159309 | 159460 | int rc; |
| 159310 | 159461 | char **azCol = 0; |
| 159311 | 159462 | const char *zExpr; |
| 159312 | 159463 | int nExpr; |
| 159313 | 159464 | int nCol; |
| 159314 | 159465 | int ii; |
| 159315 | 159466 | Fts3Expr *pExpr; |
| 159316 | 159467 | char *zBuf = 0; |
| 159317 | | - sqlite3 *db = sqlite3_context_db_handle(context); |
| 159468 | + Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context); |
| 159469 | + const char *zTokenizer = 0; |
| 159470 | + char *zErr = 0; |
| 159318 | 159471 | |
| 159319 | 159472 | if( argc<3 ){ |
| 159320 | 159473 | sqlite3_result_error(context, |
| 159321 | 159474 | "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1 |
| 159322 | 159475 | ); |
| 159323 | 159476 | return; |
| 159324 | 159477 | } |
| 159325 | 159478 | |
| 159326 | | - rc = queryTestTokenizer(db, |
| 159327 | | - (const char *)sqlite3_value_text(argv[0]), &pModule); |
| 159328 | | - if( rc==SQLITE_NOMEM ){ |
| 159329 | | - sqlite3_result_error_nomem(context); |
| 159330 | | - goto exprtest_out; |
| 159331 | | - }else if( !pModule ){ |
| 159332 | | - sqlite3_result_error(context, "No such tokenizer module", -1); |
| 159333 | | - goto exprtest_out; |
| 159334 | | - } |
| 159335 | | - |
| 159336 | | - rc = pModule->xCreate(0, 0, &pTokenizer); |
| 159337 | | - assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); |
| 159338 | | - if( rc==SQLITE_NOMEM ){ |
| 159339 | | - sqlite3_result_error_nomem(context); |
| 159340 | | - goto exprtest_out; |
| 159341 | | - } |
| 159342 | | - pTokenizer->pModule = pModule; |
| 159479 | + zTokenizer = (const char*)sqlite3_value_text(argv[0]); |
| 159480 | + rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr); |
| 159481 | + if( rc!=SQLITE_OK ){ |
| 159482 | + if( rc==SQLITE_NOMEM ){ |
| 159483 | + sqlite3_result_error_nomem(context); |
| 159484 | + }else{ |
| 159485 | + sqlite3_result_error(context, zErr, -1); |
| 159486 | + } |
| 159487 | + sqlite3_free(zErr); |
| 159488 | + return; |
| 159489 | + } |
| 159343 | 159490 | |
| 159344 | 159491 | zExpr = (const char *)sqlite3_value_text(argv[1]); |
| 159345 | 159492 | nExpr = sqlite3_value_bytes(argv[1]); |
| 159346 | 159493 | nCol = argc-2; |
| 159347 | 159494 | azCol = (char **)sqlite3_malloc(nCol*sizeof(char *)); |
| | @@ -159351,11 +159498,11 @@ |
| 159351 | 159498 | } |
| 159352 | 159499 | for(ii=0; ii<nCol; ii++){ |
| 159353 | 159500 | azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]); |
| 159354 | 159501 | } |
| 159355 | 159502 | |
| 159356 | | - if( sqlite3_user_data(context) ){ |
| 159503 | + if( bRebalance ){ |
| 159357 | 159504 | char *zDummy = 0; |
| 159358 | 159505 | rc = sqlite3Fts3ExprParse( |
| 159359 | 159506 | pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy |
| 159360 | 159507 | ); |
| 159361 | 159508 | assert( rc==SQLITE_OK || pExpr==0 ); |
| | @@ -159377,27 +159524,42 @@ |
| 159377 | 159524 | } |
| 159378 | 159525 | |
| 159379 | 159526 | sqlite3Fts3ExprFree(pExpr); |
| 159380 | 159527 | |
| 159381 | 159528 | exprtest_out: |
| 159382 | | - if( pModule && pTokenizer ){ |
| 159383 | | - rc = pModule->xDestroy(pTokenizer); |
| 159529 | + if( pTokenizer ){ |
| 159530 | + rc = pTokenizer->pModule->xDestroy(pTokenizer); |
| 159384 | 159531 | } |
| 159385 | 159532 | sqlite3_free(azCol); |
| 159386 | 159533 | } |
| 159534 | + |
| 159535 | +static void fts3ExprTest( |
| 159536 | + sqlite3_context *context, |
| 159537 | + int argc, |
| 159538 | + sqlite3_value **argv |
| 159539 | +){ |
| 159540 | + fts3ExprTestCommon(0, context, argc, argv); |
| 159541 | +} |
| 159542 | +static void fts3ExprTestRebalance( |
| 159543 | + sqlite3_context *context, |
| 159544 | + int argc, |
| 159545 | + sqlite3_value **argv |
| 159546 | +){ |
| 159547 | + fts3ExprTestCommon(1, context, argc, argv); |
| 159548 | +} |
| 159387 | 159549 | |
| 159388 | 159550 | /* |
| 159389 | 159551 | ** Register the query expression parser test function fts3_exprtest() |
| 159390 | 159552 | ** with database connection db. |
| 159391 | 159553 | */ |
| 159392 | | -SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){ |
| 159554 | +SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){ |
| 159393 | 159555 | int rc = sqlite3_create_function( |
| 159394 | | - db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0 |
| 159556 | + db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0 |
| 159395 | 159557 | ); |
| 159396 | 159558 | if( rc==SQLITE_OK ){ |
| 159397 | 159559 | rc = sqlite3_create_function(db, "fts3_exprtest_rebalance", |
| 159398 | | - -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0 |
| 159560 | + -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0 |
| 159399 | 159561 | ); |
| 159400 | 159562 | } |
| 159401 | 159563 | return rc; |
| 159402 | 159564 | } |
| 159403 | 159565 | |
| | @@ -175650,10 +175812,14 @@ |
| 175650 | 175812 | ** Valid if STAGE==1. The current change-counter cookie value in the |
| 175651 | 175813 | ** target db file. |
| 175652 | 175814 | ** |
| 175653 | 175815 | ** RBU_STATE_OALSZ: |
| 175654 | 175816 | ** Valid if STAGE==1. The size in bytes of the *-oal file. |
| 175817 | +** |
| 175818 | +** RBU_STATE_DATATBL: |
| 175819 | +** Only valid if STAGE==1. The RBU database name of the table |
| 175820 | +** currently being read. |
| 175655 | 175821 | */ |
| 175656 | 175822 | #define RBU_STATE_STAGE 1 |
| 175657 | 175823 | #define RBU_STATE_TBL 2 |
| 175658 | 175824 | #define RBU_STATE_IDX 3 |
| 175659 | 175825 | #define RBU_STATE_ROW 4 |
| | @@ -175660,10 +175826,11 @@ |
| 175660 | 175826 | #define RBU_STATE_PROGRESS 5 |
| 175661 | 175827 | #define RBU_STATE_CKPT 6 |
| 175662 | 175828 | #define RBU_STATE_COOKIE 7 |
| 175663 | 175829 | #define RBU_STATE_OALSZ 8 |
| 175664 | 175830 | #define RBU_STATE_PHASEONESTEP 9 |
| 175831 | +#define RBU_STATE_DATATBL 10 |
| 175665 | 175832 | |
| 175666 | 175833 | #define RBU_STAGE_OAL 1 |
| 175667 | 175834 | #define RBU_STAGE_MOVE 2 |
| 175668 | 175835 | #define RBU_STAGE_CAPTURE 3 |
| 175669 | 175836 | #define RBU_STAGE_CKPT 4 |
| | @@ -175702,10 +175869,11 @@ |
| 175702 | 175869 | ** A structure to store values read from the rbu_state table in memory. |
| 175703 | 175870 | */ |
| 175704 | 175871 | struct RbuState { |
| 175705 | 175872 | int eStage; |
| 175706 | 175873 | char *zTbl; |
| 175874 | + char *zDataTbl; |
| 175707 | 175875 | char *zIdx; |
| 175708 | 175876 | i64 iWalCksum; |
| 175709 | 175877 | int nRow; |
| 175710 | 175878 | i64 nProgress; |
| 175711 | 175879 | u32 iCookie; |
| | @@ -177765,10 +177933,11 @@ |
| 177765 | 177933 | ** Free an RbuState object allocated by rbuLoadState(). |
| 177766 | 177934 | */ |
| 177767 | 177935 | static void rbuFreeState(RbuState *p){ |
| 177768 | 177936 | if( p ){ |
| 177769 | 177937 | sqlite3_free(p->zTbl); |
| 177938 | + sqlite3_free(p->zDataTbl); |
| 177770 | 177939 | sqlite3_free(p->zIdx); |
| 177771 | 177940 | sqlite3_free(p); |
| 177772 | 177941 | } |
| 177773 | 177942 | } |
| 177774 | 177943 | |
| | @@ -177834,10 +178003,14 @@ |
| 177834 | 178003 | break; |
| 177835 | 178004 | |
| 177836 | 178005 | case RBU_STATE_PHASEONESTEP: |
| 177837 | 178006 | pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1); |
| 177838 | 178007 | break; |
| 178008 | + |
| 178009 | + case RBU_STATE_DATATBL: |
| 178010 | + pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc); |
| 178011 | + break; |
| 177839 | 178012 | |
| 177840 | 178013 | default: |
| 177841 | 178014 | rc = SQLITE_CORRUPT; |
| 177842 | 178015 | break; |
| 177843 | 178016 | } |
| | @@ -178609,21 +178782,23 @@ |
| 178609 | 178782 | "(%d, %d), " |
| 178610 | 178783 | "(%d, %d), " |
| 178611 | 178784 | "(%d, %lld), " |
| 178612 | 178785 | "(%d, %lld), " |
| 178613 | 178786 | "(%d, %lld), " |
| 178614 | | - "(%d, %lld) ", |
| 178787 | + "(%d, %lld), " |
| 178788 | + "(%d, %Q) ", |
| 178615 | 178789 | p->zStateDb, |
| 178616 | 178790 | RBU_STATE_STAGE, eStage, |
| 178617 | 178791 | RBU_STATE_TBL, p->objiter.zTbl, |
| 178618 | 178792 | RBU_STATE_IDX, p->objiter.zIdx, |
| 178619 | 178793 | RBU_STATE_ROW, p->nStep, |
| 178620 | 178794 | RBU_STATE_PROGRESS, p->nProgress, |
| 178621 | 178795 | RBU_STATE_CKPT, p->iWalCksum, |
| 178622 | 178796 | RBU_STATE_COOKIE, (i64)pFd->iCookie, |
| 178623 | 178797 | RBU_STATE_OALSZ, p->iOalSz, |
| 178624 | | - RBU_STATE_PHASEONESTEP, p->nPhaseOneStep |
| 178798 | + RBU_STATE_PHASEONESTEP, p->nPhaseOneStep, |
| 178799 | + RBU_STATE_DATATBL, p->objiter.zDataTbl |
| 178625 | 178800 | ) |
| 178626 | 178801 | ); |
| 178627 | 178802 | assert( pInsert==0 || rc==SQLITE_OK ); |
| 178628 | 178803 | |
| 178629 | 178804 | if( rc==SQLITE_OK ){ |
| | @@ -178875,11 +179050,12 @@ |
| 178875 | 179050 | RbuObjIter *pIter = &p->objiter; |
| 178876 | 179051 | int rc = SQLITE_OK; |
| 178877 | 179052 | |
| 178878 | 179053 | while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup |
| 178879 | 179054 | || rbuStrCompare(pIter->zIdx, pState->zIdx) |
| 178880 | | - || rbuStrCompare(pIter->zTbl, pState->zTbl) |
| 179055 | + || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl)) |
| 179056 | + || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl)) |
| 178881 | 179057 | )){ |
| 178882 | 179058 | rc = rbuObjIterNext(p, pIter); |
| 178883 | 179059 | } |
| 178884 | 179060 | |
| 178885 | 179061 | if( rc==SQLITE_OK && !pIter->zTbl ){ |
| | @@ -206725,11 +206901,11 @@ |
| 206725 | 206901 | int nArg, /* Number of args */ |
| 206726 | 206902 | sqlite3_value **apUnused /* Function arguments */ |
| 206727 | 206903 | ){ |
| 206728 | 206904 | assert( nArg==0 ); |
| 206729 | 206905 | UNUSED_PARAM2(nArg, apUnused); |
| 206730 | | - sqlite3_result_text(pCtx, "fts5: 2018-04-26 12:27:03 368c14da868a843767344f6cc17c499fddd83244c0510337ed9a918e64ee2413", -1, SQLITE_TRANSIENT); |
| 206906 | + sqlite3_result_text(pCtx, "fts5: 2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba71eda", -1, SQLITE_TRANSIENT); |
| 206731 | 206907 | } |
| 206732 | 206908 | |
| 206733 | 206909 | static int fts5Init(sqlite3 *db){ |
| 206734 | 206910 | static const sqlite3_module fts5Mod = { |
| 206735 | 206911 | /* iVersion */ 2, |
| | @@ -210995,12 +211171,12 @@ |
| 210995 | 211171 | } |
| 210996 | 211172 | #endif /* SQLITE_CORE */ |
| 210997 | 211173 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 210998 | 211174 | |
| 210999 | 211175 | /************** End of stmt.c ************************************************/ |
| 211000 | | -#if __LINE__!=211000 |
| 211176 | +#if __LINE__!=211176 |
| 211001 | 211177 | #undef SQLITE_SOURCE_ID |
| 211002 | | -#define SQLITE_SOURCE_ID "2018-04-26 18:34:26 9fd0faf517993587d2f54212638545fc85fbbc84a031bcfae8c1e5894825alt2" |
| 211178 | +#define SQLITE_SOURCE_ID "2018-05-05 01:23:28 9191ff670cb7f36e0b2dac4a22888679b639845687aef8edcc3c05e35ba7alt2" |
| 211003 | 211179 | #endif |
| 211004 | 211180 | /* Return the source-id for this library */ |
| 211005 | 211181 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 211006 | 211182 | /************************** End of sqlite3.c ******************************/ |
| 211007 | 211183 | |