| | @@ -135,11 +135,11 @@ |
| 135 | 135 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 136 | 136 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 137 | 137 | */ |
| 138 | 138 | #define SQLITE_VERSION "3.8.2" |
| 139 | 139 | #define SQLITE_VERSION_NUMBER 3008002 |
| 140 | | -#define SQLITE_SOURCE_ID "2013-11-12 15:33:40 0f924c6ef6cf2ac5a61aafa8dd8e3309b3970499" |
| 140 | +#define SQLITE_SOURCE_ID "2013-11-19 13:55:34 17e8524fc05aa1e6074c19a8ccccc5ab5883103a" |
| 141 | 141 | |
| 142 | 142 | /* |
| 143 | 143 | ** CAPI3REF: Run-Time Library Version Numbers |
| 144 | 144 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 145 | 145 | ** |
| | @@ -396,11 +396,11 @@ |
| 396 | 396 | ** Restrictions: |
| 397 | 397 | ** |
| 398 | 398 | ** <ul> |
| 399 | 399 | ** <li> The application must insure that the 1st parameter to sqlite3_exec() |
| 400 | 400 | ** is a valid and open [database connection]. |
| 401 | | -** <li> The application must not close [database connection] specified by |
| 401 | +** <li> The application must not close the [database connection] specified by |
| 402 | 402 | ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running. |
| 403 | 403 | ** <li> The application must not modify the SQL statement text passed into |
| 404 | 404 | ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running. |
| 405 | 405 | ** </ul> |
| 406 | 406 | */ |
| | @@ -473,11 +473,11 @@ |
| 473 | 473 | ** about errors. The extended result codes are enabled or disabled |
| 474 | 474 | ** on a per database connection basis using the |
| 475 | 475 | ** [sqlite3_extended_result_codes()] API. |
| 476 | 476 | ** |
| 477 | 477 | ** Some of the available extended result codes are listed here. |
| 478 | | -** One may expect the number of extended result codes will be expand |
| 478 | +** One may expect the number of extended result codes will increase |
| 479 | 479 | ** over time. Software that uses extended result codes should expect |
| 480 | 480 | ** to see new result codes in future releases of SQLite. |
| 481 | 481 | ** |
| 482 | 482 | ** The SQLITE_OK result code will never be extended. It will always |
| 483 | 483 | ** be exactly zero. |
| | @@ -1411,11 +1411,11 @@ |
| 1411 | 1411 | ** of 8. Some allocators round up to a larger multiple or to a power of 2. |
| 1412 | 1412 | ** Every memory allocation request coming in through [sqlite3_malloc()] |
| 1413 | 1413 | ** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0, |
| 1414 | 1414 | ** that causes the corresponding memory allocation to fail. |
| 1415 | 1415 | ** |
| 1416 | | -** The xInit method initializes the memory allocator. (For example, |
| 1416 | +** The xInit method initializes the memory allocator. For example, |
| 1417 | 1417 | ** it might allocate any require mutexes or initialize internal data |
| 1418 | 1418 | ** structures. The xShutdown method is invoked (indirectly) by |
| 1419 | 1419 | ** [sqlite3_shutdown()] and should deallocate any resources acquired |
| 1420 | 1420 | ** by xInit. The pAppData pointer is used as the only parameter to |
| 1421 | 1421 | ** xInit and xShutdown. |
| | @@ -3137,11 +3137,10 @@ |
| 3137 | 3137 | ** to the [sqlite3_bind_text | bindings] of that [parameter]. |
| 3138 | 3138 | ** ^The specific value of WHERE-clause [parameter] might influence the |
| 3139 | 3139 | ** choice of query plan if the parameter is the left-hand side of a [LIKE] |
| 3140 | 3140 | ** or [GLOB] operator or if the parameter is compared to an indexed column |
| 3141 | 3141 | ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled. |
| 3142 | | -** the |
| 3143 | 3142 | ** </li> |
| 3144 | 3143 | ** </ol> |
| 3145 | 3144 | */ |
| 3146 | 3145 | SQLITE_API int sqlite3_prepare( |
| 3147 | 3146 | sqlite3 *db, /* Database handle */ |
| | @@ -3867,11 +3866,11 @@ |
| 3867 | 3866 | ** |
| 3868 | 3867 | ** ^The pointers returned are valid until a type conversion occurs as |
| 3869 | 3868 | ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or |
| 3870 | 3869 | ** [sqlite3_finalize()] is called. ^The memory space used to hold strings |
| 3871 | 3870 | ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned |
| 3872 | | -** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into |
| 3871 | +** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into |
| 3873 | 3872 | ** [sqlite3_free()]. |
| 3874 | 3873 | ** |
| 3875 | 3874 | ** ^(If a memory allocation error occurs during the evaluation of any |
| 3876 | 3875 | ** of these routines, a default value is returned. The default value |
| 3877 | 3876 | ** is either the integer 0, the floating point number 0.0, or a NULL |
| | @@ -4945,12 +4944,12 @@ |
| 4945 | 4944 | /* |
| 4946 | 4945 | ** CAPI3REF: Free Memory Used By A Database Connection |
| 4947 | 4946 | ** |
| 4948 | 4947 | ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap |
| 4949 | 4948 | ** memory as possible from database connection D. Unlike the |
| 4950 | | -** [sqlite3_release_memory()] interface, this interface is effect even |
| 4951 | | -** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is |
| 4949 | +** [sqlite3_release_memory()] interface, this interface is in effect even |
| 4950 | +** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is |
| 4952 | 4951 | ** omitted. |
| 4953 | 4952 | ** |
| 4954 | 4953 | ** See also: [sqlite3_release_memory()] |
| 4955 | 4954 | */ |
| 4956 | 4955 | SQLITE_API int sqlite3_db_release_memory(sqlite3*); |
| | @@ -9096,22 +9095,22 @@ |
| 9096 | 9095 | #define OP_NotNull 75 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */ |
| 9097 | 9096 | #define OP_Ne 76 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */ |
| 9098 | 9097 | #define OP_Eq 77 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */ |
| 9099 | 9098 | #define OP_Gt 78 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */ |
| 9100 | 9099 | #define OP_Le 79 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */ |
| 9101 | | -#define OP_Lt 80 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P3 */ |
| 9100 | +#define OP_Lt 80 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */ |
| 9102 | 9101 | #define OP_Ge 81 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */ |
| 9103 | 9102 | #define OP_RowKey 82 /* synopsis: r[P2]=key */ |
| 9104 | 9103 | #define OP_BitAnd 83 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */ |
| 9105 | 9104 | #define OP_BitOr 84 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */ |
| 9106 | 9105 | #define OP_ShiftLeft 85 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */ |
| 9107 | 9106 | #define OP_ShiftRight 86 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */ |
| 9108 | 9107 | #define OP_Add 87 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */ |
| 9109 | 9108 | #define OP_Subtract 88 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */ |
| 9110 | 9109 | #define OP_Multiply 89 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */ |
| 9111 | | -#define OP_Divide 90 /* same as TK_SLASH, synopsis: r[P3]=r[P1]/r[P2] */ |
| 9112 | | -#define OP_Remainder 91 /* same as TK_REM, synopsis: r[P3]=r[P1]%r[P2] */ |
| 9110 | +#define OP_Divide 90 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */ |
| 9111 | +#define OP_Remainder 91 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */ |
| 9113 | 9112 | #define OP_Concat 92 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */ |
| 9114 | 9113 | #define OP_RowData 93 /* synopsis: r[P2]=data */ |
| 9115 | 9114 | #define OP_BitNot 94 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */ |
| 9116 | 9115 | #define OP_String8 95 /* same as TK_STRING, synopsis: r[P2]='P4' */ |
| 9117 | 9116 | #define OP_Rowid 96 /* synopsis: r[P2]=rowid */ |
| | @@ -9237,11 +9236,10 @@ |
| 9237 | 9236 | SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*); |
| 9238 | 9237 | SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int); |
| 9239 | 9238 | SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*); |
| 9240 | 9239 | #ifdef SQLITE_DEBUG |
| 9241 | 9240 | SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int); |
| 9242 | | -SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe*,FILE*); |
| 9243 | 9241 | #endif |
| 9244 | 9242 | SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*); |
| 9245 | 9243 | SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*); |
| 9246 | 9244 | SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*); |
| 9247 | 9245 | SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int); |
| | @@ -10304,10 +10302,11 @@ |
| 10304 | 10302 | #define SQLITE_PreferBuiltin 0x00200000 /* Preference to built-in funcs */ |
| 10305 | 10303 | #define SQLITE_LoadExtension 0x00400000 /* Enable load_extension */ |
| 10306 | 10304 | #define SQLITE_EnableTrigger 0x00800000 /* True to enable triggers */ |
| 10307 | 10305 | #define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */ |
| 10308 | 10306 | #define SQLITE_QueryOnly 0x02000000 /* Disable database changes */ |
| 10307 | +#define SQLITE_VdbeEQP 0x04000000 /* Debug EXPLAIN QUERY PLAN */ |
| 10309 | 10308 | |
| 10310 | 10309 | |
| 10311 | 10310 | /* |
| 10312 | 10311 | ** Bits of the sqlite3.dbOptFlags field that are used by the |
| 10313 | 10312 | ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to |
| | @@ -11089,11 +11088,11 @@ |
| 11089 | 11088 | #define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */ |
| 11090 | 11089 | #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */ |
| 11091 | 11090 | #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */ |
| 11092 | 11091 | #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */ |
| 11093 | 11092 | #define EP_Collate 0x000100 /* Tree contains a TK_COLLATE opeartor */ |
| 11094 | | -#define EP_FixedDest 0x000200 /* Result needed in a specific register */ |
| 11093 | + /* unused 0x000200 */ |
| 11095 | 11094 | #define EP_IntValue 0x000400 /* Integer value contained in u.iValue */ |
| 11096 | 11095 | #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */ |
| 11097 | 11096 | #define EP_Skip 0x001000 /* COLLATE, AS, or UNLIKELY */ |
| 11098 | 11097 | #define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */ |
| 11099 | 11098 | #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */ |
| | @@ -11160,12 +11159,17 @@ |
| 11160 | 11159 | char *zName; /* Token associated with this expression */ |
| 11161 | 11160 | char *zSpan; /* Original text of the expression */ |
| 11162 | 11161 | u8 sortOrder; /* 1 for DESC or 0 for ASC */ |
| 11163 | 11162 | unsigned done :1; /* A flag to indicate when processing is finished */ |
| 11164 | 11163 | unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */ |
| 11165 | | - u16 iOrderByCol; /* For ORDER BY, column number in result set */ |
| 11166 | | - u16 iAlias; /* Index into Parse.aAlias[] for zName */ |
| 11164 | + union { |
| 11165 | + struct { |
| 11166 | + u16 iOrderByCol; /* For ORDER BY, column number in result set */ |
| 11167 | + u16 iAlias; /* Index into Parse.aAlias[] for zName */ |
| 11168 | + } x; |
| 11169 | + int iConstExprReg; /* Register in which Expr value is cached */ |
| 11170 | + } u; |
| 11167 | 11171 | } *a; /* Alloc a power of two greater or equal to nExpr */ |
| 11168 | 11172 | }; |
| 11169 | 11173 | |
| 11170 | 11174 | /* |
| 11171 | 11175 | ** An instance of this structure is used by the parser to record both |
| | @@ -11538,10 +11542,11 @@ |
| 11538 | 11542 | u8 tempReg; /* iReg is a temp register that needs to be freed */ |
| 11539 | 11543 | int iLevel; /* Nesting level */ |
| 11540 | 11544 | int iReg; /* Reg with value of this column. 0 means none. */ |
| 11541 | 11545 | int lru; /* Least recently used entry has the smallest value */ |
| 11542 | 11546 | } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */ |
| 11547 | + ExprList *pConstExpr;/* Constant expressions */ |
| 11543 | 11548 | yDbMask writeMask; /* Start a write transaction on these databases */ |
| 11544 | 11549 | yDbMask cookieMask; /* Bitmask of schema verified databases */ |
| 11545 | 11550 | int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */ |
| 11546 | 11551 | int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */ |
| 11547 | 11552 | int regRowid; /* Register holding rowid of CREATE TABLE entry */ |
| | @@ -12151,11 +12156,10 @@ |
| 12151 | 12156 | SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int); |
| 12152 | 12157 | SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int); |
| 12153 | 12158 | SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*); |
| 12154 | 12159 | SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int); |
| 12155 | 12160 | SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int); |
| 12156 | | -SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*); |
| 12157 | 12161 | SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int); |
| 12158 | 12162 | SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int); |
| 12159 | 12163 | SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int); |
| 12160 | 12164 | SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*); |
| 12161 | 12165 | SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*); |
| | @@ -12197,11 +12201,11 @@ |
| 12197 | 12201 | SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*); |
| 12198 | 12202 | SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*); |
| 12199 | 12203 | SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int, |
| 12200 | 12204 | u8,u8,int,int*); |
| 12201 | 12205 | SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int); |
| 12202 | | -SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, int*, int*); |
| 12206 | +SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*); |
| 12203 | 12207 | SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int); |
| 12204 | 12208 | SQLITE_PRIVATE void sqlite3MultiWrite(Parse*); |
| 12205 | 12209 | SQLITE_PRIVATE void sqlite3MayAbort(Parse*); |
| 12206 | 12210 | SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8); |
| 12207 | 12211 | SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*); |
| | @@ -12519,10 +12523,11 @@ |
| 12519 | 12523 | SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); |
| 12520 | 12524 | SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**); |
| 12521 | 12525 | SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); |
| 12522 | 12526 | SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); |
| 12523 | 12527 | SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); |
| 12528 | +SQLITE_PRIVATE void sqlite3ParserReset(Parse*); |
| 12524 | 12529 | SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*); |
| 12525 | 12530 | SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*); |
| 12526 | 12531 | SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *); |
| 12527 | 12532 | SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*); |
| 12528 | 12533 | SQLITE_PRIVATE const char *sqlite3JournalModename(int); |
| | @@ -13700,13 +13705,10 @@ |
| 13700 | 13705 | i64 nFkConstraint; /* Number of imm. FK constraints this VM */ |
| 13701 | 13706 | i64 nStmtDefCons; /* Number of def. constraints when stmt started */ |
| 13702 | 13707 | i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ |
| 13703 | 13708 | char *zSql; /* Text of the SQL statement that generated this */ |
| 13704 | 13709 | void *pFree; /* Free this when deleting the vdbe */ |
| 13705 | | -#ifdef SQLITE_DEBUG |
| 13706 | | - FILE *trace; /* Write an execution trace here, if not NULL */ |
| 13707 | | -#endif |
| 13708 | 13710 | #ifdef SQLITE_ENABLE_TREE_EXPLAIN |
| 13709 | 13711 | Explain *pExplain; /* The explainer */ |
| 13710 | 13712 | char *zExplain; /* Explanation of data structures */ |
| 13711 | 13713 | #endif |
| 13712 | 13714 | VdbeFrame *pFrame; /* Parent frame */ |
| | @@ -15796,20 +15798,10 @@ |
| 15796 | 15798 | ** This version of the memory allocator is the default. It is |
| 15797 | 15799 | ** used when no other memory allocator is specified using compile-time |
| 15798 | 15800 | ** macros. |
| 15799 | 15801 | */ |
| 15800 | 15802 | #ifdef SQLITE_SYSTEM_MALLOC |
| 15801 | | - |
| 15802 | | -/* |
| 15803 | | -** The MSVCRT has malloc_usable_size() but it is called _msize(). |
| 15804 | | -** The use of _msize() is automatic, but can be disabled by compiling |
| 15805 | | -** with -DSQLITE_WITHOUT_MSIZE |
| 15806 | | -*/ |
| 15807 | | -#if defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE) |
| 15808 | | -# define SQLITE_MALLOCSIZE _msize |
| 15809 | | -#endif |
| 15810 | | - |
| 15811 | 15803 | #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) |
| 15812 | 15804 | |
| 15813 | 15805 | /* |
| 15814 | 15806 | ** Use the zone allocator available on apple products unless the |
| 15815 | 15807 | ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined. |
| | @@ -15828,25 +15820,51 @@ |
| 15828 | 15820 | |
| 15829 | 15821 | /* |
| 15830 | 15822 | ** Use standard C library malloc and free on non-Apple systems. |
| 15831 | 15823 | ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined. |
| 15832 | 15824 | */ |
| 15833 | | -#define SQLITE_MALLOC(x) malloc(x) |
| 15834 | | -#define SQLITE_FREE(x) free(x) |
| 15835 | | -#define SQLITE_REALLOC(x,y) realloc((x),(y)) |
| 15836 | | - |
| 15837 | | -#if (defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)) \ |
| 15838 | | - || (defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE)) |
| 15839 | | -# include <malloc.h> /* Needed for malloc_usable_size on linux */ |
| 15840 | | -#endif |
| 15841 | | -#ifdef HAVE_MALLOC_USABLE_SIZE |
| 15842 | | -# ifndef SQLITE_MALLOCSIZE |
| 15843 | | -# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x) |
| 15844 | | -# endif |
| 15845 | | -#else |
| 15846 | | -# undef SQLITE_MALLOCSIZE |
| 15847 | | -#endif |
| 15825 | +#define SQLITE_MALLOC(x) malloc(x) |
| 15826 | +#define SQLITE_FREE(x) free(x) |
| 15827 | +#define SQLITE_REALLOC(x,y) realloc((x),(y)) |
| 15828 | + |
| 15829 | +/* |
| 15830 | +** The malloc.h header file is needed for malloc_usable_size() function |
| 15831 | +** on some systems (e.g. Linux). |
| 15832 | +*/ |
| 15833 | +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE) |
| 15834 | +# define SQLITE_USE_MALLOC_H |
| 15835 | +# define SQLITE_USE_MALLOC_USABLE_SIZE |
| 15836 | +/* |
| 15837 | +** The MSVCRT has malloc_usable_size(), but it is called _msize(). The |
| 15838 | +** use of _msize() is automatic, but can be disabled by compiling with |
| 15839 | +** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires |
| 15840 | +** the malloc.h header file. |
| 15841 | +*/ |
| 15842 | +#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE) |
| 15843 | +# define SQLITE_USE_MALLOC_H |
| 15844 | +# define SQLITE_USE_MSIZE |
| 15845 | +#endif |
| 15846 | + |
| 15847 | +/* |
| 15848 | +** Include the malloc.h header file, if necessary. Also set define macro |
| 15849 | +** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize() |
| 15850 | +** for MSVC and malloc_usable_size() for most other systems (e.g. Linux). |
| 15851 | +** The memory size function can always be overridden manually by defining |
| 15852 | +** the macro SQLITE_MALLOCSIZE to the desired function name. |
| 15853 | +*/ |
| 15854 | +#if defined(SQLITE_USE_MALLOC_H) |
| 15855 | +# include <malloc.h> |
| 15856 | +# if defined(SQLITE_USE_MALLOC_USABLE_SIZE) |
| 15857 | +# if !defined(SQLITE_MALLOCSIZE) |
| 15858 | +# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x) |
| 15859 | +# endif |
| 15860 | +# elif defined(SQLITE_USE_MSIZE) |
| 15861 | +# if !defined(SQLITE_MALLOCSIZE) |
| 15862 | +# define SQLITE_MALLOCSIZE _msize |
| 15863 | +# endif |
| 15864 | +# endif |
| 15865 | +#endif /* defined(SQLITE_USE_MALLOC_H) */ |
| 15848 | 15866 | |
| 15849 | 15867 | #endif /* __APPLE__ or not __APPLE__ */ |
| 15850 | 15868 | |
| 15851 | 15869 | /* |
| 15852 | 15870 | ** Like malloc(), but remember the size of the allocation |
| | @@ -22957,22 +22975,22 @@ |
| 22957 | 22975 | /* 75 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"), |
| 22958 | 22976 | /* 76 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"), |
| 22959 | 22977 | /* 77 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"), |
| 22960 | 22978 | /* 78 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"), |
| 22961 | 22979 | /* 79 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"), |
| 22962 | | - /* 80 */ "Lt" OpHelp("if r[P1]<r[P3] goto P3"), |
| 22980 | + /* 80 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"), |
| 22963 | 22981 | /* 81 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"), |
| 22964 | 22982 | /* 82 */ "RowKey" OpHelp("r[P2]=key"), |
| 22965 | 22983 | /* 83 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"), |
| 22966 | 22984 | /* 84 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"), |
| 22967 | 22985 | /* 85 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"), |
| 22968 | 22986 | /* 86 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"), |
| 22969 | 22987 | /* 87 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"), |
| 22970 | 22988 | /* 88 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"), |
| 22971 | 22989 | /* 89 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"), |
| 22972 | | - /* 90 */ "Divide" OpHelp("r[P3]=r[P1]/r[P2]"), |
| 22973 | | - /* 91 */ "Remainder" OpHelp("r[P3]=r[P1]%r[P2]"), |
| 22990 | + /* 90 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"), |
| 22991 | + /* 91 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"), |
| 22974 | 22992 | /* 92 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"), |
| 22975 | 22993 | /* 93 */ "RowData" OpHelp("r[P2]=data"), |
| 22976 | 22994 | /* 94 */ "BitNot" OpHelp("r[P1]= ~r[P1]"), |
| 22977 | 22995 | /* 95 */ "String8" OpHelp("r[P2]='P4'"), |
| 22978 | 22996 | /* 96 */ "Rowid" OpHelp("r[P2]=rowid"), |
| | @@ -58853,10 +58871,11 @@ |
| 58853 | 58871 | if( sqlite3OpenTempDatabase(pParse) ){ |
| 58854 | 58872 | sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg); |
| 58855 | 58873 | rc = SQLITE_ERROR; |
| 58856 | 58874 | } |
| 58857 | 58875 | sqlite3DbFree(pErrorDb, pParse->zErrMsg); |
| 58876 | + sqlite3ParserReset(pParse); |
| 58858 | 58877 | sqlite3StackFree(pErrorDb, pParse); |
| 58859 | 58878 | } |
| 58860 | 58879 | if( rc ){ |
| 58861 | 58880 | return 0; |
| 58862 | 58881 | } |
| | @@ -60997,19 +61016,10 @@ |
| 60997 | 61016 | pA->zSql = pB->zSql; |
| 60998 | 61017 | pB->zSql = zTmp; |
| 60999 | 61018 | pB->isPrepareV2 = pA->isPrepareV2; |
| 61000 | 61019 | } |
| 61001 | 61020 | |
| 61002 | | -#ifdef SQLITE_DEBUG |
| 61003 | | -/* |
| 61004 | | -** Turn tracing on or off |
| 61005 | | -*/ |
| 61006 | | -SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ |
| 61007 | | - p->trace = trace; |
| 61008 | | -} |
| 61009 | | -#endif |
| 61010 | | - |
| 61011 | 61021 | /* |
| 61012 | 61022 | ** Resize the Vdbe.aOp array so that it is at least one op larger than |
| 61013 | 61023 | ** it was. |
| 61014 | 61024 | ** |
| 61015 | 61025 | ** If an out-of-memory error occurs while resizing the array, return |
| | @@ -61901,11 +61911,11 @@ |
| 61901 | 61911 | assert( i<nTemp ); |
| 61902 | 61912 | break; |
| 61903 | 61913 | } |
| 61904 | 61914 | case P4_COLLSEQ: { |
| 61905 | 61915 | CollSeq *pColl = pOp->p4.pColl; |
| 61906 | | - sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); |
| 61916 | + sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName); |
| 61907 | 61917 | break; |
| 61908 | 61918 | } |
| 61909 | 61919 | case P4_FUNCDEF: { |
| 61910 | 61920 | FuncDef *pDef = pOp->p4.pFunc; |
| 61911 | 61921 | sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); |
| | @@ -62334,19 +62344,21 @@ |
| 62334 | 62344 | #ifdef SQLITE_DEBUG |
| 62335 | 62345 | /* |
| 62336 | 62346 | ** Print the SQL that was used to generate a VDBE program. |
| 62337 | 62347 | */ |
| 62338 | 62348 | SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){ |
| 62339 | | - int nOp = p->nOp; |
| 62340 | | - VdbeOp *pOp; |
| 62341 | | - if( nOp<1 ) return; |
| 62342 | | - pOp = &p->aOp[0]; |
| 62343 | | - if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ |
| 62344 | | - const char *z = pOp->p4.z; |
| 62345 | | - while( sqlite3Isspace(*z) ) z++; |
| 62346 | | - printf("SQL: [%s]\n", z); |
| 62347 | | - } |
| 62349 | + const char *z = 0; |
| 62350 | + if( p->zSql ){ |
| 62351 | + z = p->zSql; |
| 62352 | + }else if( p->nOp>=1 ){ |
| 62353 | + const VdbeOp *pOp = &p->aOp[0]; |
| 62354 | + if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ |
| 62355 | + z = pOp->p4.z; |
| 62356 | + while( sqlite3Isspace(*z) ) z++; |
| 62357 | + } |
| 62358 | + } |
| 62359 | + if( z ) printf("SQL: [%s]\n", z); |
| 62348 | 62360 | } |
| 62349 | 62361 | #endif |
| 62350 | 62362 | |
| 62351 | 62363 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
| 62352 | 62364 | /* |
| | @@ -63892,19 +63904,16 @@ |
| 63892 | 63904 | pMem->u.i = serial_type-8; |
| 63893 | 63905 | pMem->flags = MEM_Int; |
| 63894 | 63906 | return 0; |
| 63895 | 63907 | } |
| 63896 | 63908 | default: { |
| 63909 | + static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; |
| 63897 | 63910 | u32 len = (serial_type-12)/2; |
| 63898 | 63911 | pMem->z = (char *)buf; |
| 63899 | 63912 | pMem->n = len; |
| 63900 | 63913 | pMem->xDel = 0; |
| 63901 | | - if( serial_type&0x01 ){ |
| 63902 | | - pMem->flags = MEM_Str | MEM_Ephem; |
| 63903 | | - }else{ |
| 63904 | | - pMem->flags = MEM_Blob | MEM_Ephem; |
| 63905 | | - } |
| 63914 | + pMem->flags = aFlag[serial_type&1]; |
| 63906 | 63915 | return len; |
| 63907 | 63916 | } |
| 63908 | 63917 | } |
| 63909 | 63918 | return 0; |
| 63910 | 63919 | } |
| | @@ -66350,41 +66359,40 @@ |
| 66350 | 66359 | |
| 66351 | 66360 | #ifdef SQLITE_DEBUG |
| 66352 | 66361 | /* |
| 66353 | 66362 | ** Print the value of a register for tracing purposes: |
| 66354 | 66363 | */ |
| 66355 | | -static void memTracePrint(FILE *out, Mem *p){ |
| 66364 | +static void memTracePrint(Mem *p){ |
| 66356 | 66365 | if( p->flags & MEM_Invalid ){ |
| 66357 | | - fprintf(out, " undefined"); |
| 66366 | + printf(" undefined"); |
| 66358 | 66367 | }else if( p->flags & MEM_Null ){ |
| 66359 | | - fprintf(out, " NULL"); |
| 66368 | + printf(" NULL"); |
| 66360 | 66369 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
| 66361 | | - fprintf(out, " si:%lld", p->u.i); |
| 66370 | + printf(" si:%lld", p->u.i); |
| 66362 | 66371 | }else if( p->flags & MEM_Int ){ |
| 66363 | | - fprintf(out, " i:%lld", p->u.i); |
| 66372 | + printf(" i:%lld", p->u.i); |
| 66364 | 66373 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 66365 | 66374 | }else if( p->flags & MEM_Real ){ |
| 66366 | | - fprintf(out, " r:%g", p->r); |
| 66375 | + printf(" r:%g", p->r); |
| 66367 | 66376 | #endif |
| 66368 | 66377 | }else if( p->flags & MEM_RowSet ){ |
| 66369 | | - fprintf(out, " (rowset)"); |
| 66378 | + printf(" (rowset)"); |
| 66370 | 66379 | }else{ |
| 66371 | 66380 | char zBuf[200]; |
| 66372 | 66381 | sqlite3VdbeMemPrettyPrint(p, zBuf); |
| 66373 | | - fprintf(out, " "); |
| 66374 | | - fprintf(out, "%s", zBuf); |
| 66382 | + printf(" %s", zBuf); |
| 66375 | 66383 | } |
| 66376 | 66384 | } |
| 66377 | | -static void registerTrace(FILE *out, int iReg, Mem *p){ |
| 66378 | | - fprintf(out, "REG[%d] = ", iReg); |
| 66379 | | - memTracePrint(out, p); |
| 66380 | | - fprintf(out, "\n"); |
| 66385 | +static void registerTrace(int iReg, Mem *p){ |
| 66386 | + printf("REG[%d] = ", iReg); |
| 66387 | + memTracePrint(p); |
| 66388 | + printf("\n"); |
| 66381 | 66389 | } |
| 66382 | 66390 | #endif |
| 66383 | 66391 | |
| 66384 | 66392 | #ifdef SQLITE_DEBUG |
| 66385 | | -# define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M) |
| 66393 | +# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) |
| 66386 | 66394 | #else |
| 66387 | 66395 | # define REGISTER_TRACE(R,M) |
| 66388 | 66396 | #endif |
| 66389 | 66397 | |
| 66390 | 66398 | |
| | @@ -67035,17 +67043,32 @@ |
| 67035 | 67043 | } |
| 67036 | 67044 | } |
| 67037 | 67045 | #endif |
| 67038 | 67046 | #ifdef SQLITE_DEBUG |
| 67039 | 67047 | sqlite3BeginBenignMalloc(); |
| 67040 | | - if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){ |
| 67048 | + if( p->pc==0 |
| 67049 | + && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 |
| 67050 | + ){ |
| 67041 | 67051 | int i; |
| 67042 | | - printf("VDBE Program Listing:\n"); |
| 67052 | + int once = 1; |
| 67043 | 67053 | sqlite3VdbePrintSql(p); |
| 67044 | | - for(i=0; i<p->nOp; i++){ |
| 67045 | | - sqlite3VdbePrintOp(stdout, i, &aOp[i]); |
| 67054 | + if( p->db->flags & SQLITE_VdbeListing ){ |
| 67055 | + printf("VDBE Program Listing:\n"); |
| 67056 | + for(i=0; i<p->nOp; i++){ |
| 67057 | + sqlite3VdbePrintOp(stdout, i, &aOp[i]); |
| 67058 | + } |
| 67046 | 67059 | } |
| 67060 | + if( p->db->flags & SQLITE_VdbeEQP ){ |
| 67061 | + for(i=0; i<p->nOp; i++){ |
| 67062 | + if( aOp[i].opcode==OP_Explain ){ |
| 67063 | + if( once ) printf("VDBE Query Plan:\n"); |
| 67064 | + printf("%s\n", aOp[i].p4.z); |
| 67065 | + once = 0; |
| 67066 | + } |
| 67067 | + } |
| 67068 | + } |
| 67069 | + if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); |
| 67047 | 67070 | } |
| 67048 | 67071 | sqlite3EndBenignMalloc(); |
| 67049 | 67072 | #endif |
| 67050 | 67073 | for(pc=p->pc; rc==SQLITE_OK; pc++){ |
| 67051 | 67074 | assert( pc>=0 && pc<p->nOp ); |
| | @@ -67058,16 +67081,12 @@ |
| 67058 | 67081 | pOp = &aOp[pc]; |
| 67059 | 67082 | |
| 67060 | 67083 | /* Only allow tracing if SQLITE_DEBUG is defined. |
| 67061 | 67084 | */ |
| 67062 | 67085 | #ifdef SQLITE_DEBUG |
| 67063 | | - if( p->trace ){ |
| 67064 | | - if( pc==0 ){ |
| 67065 | | - printf("VDBE Execution Trace:\n"); |
| 67066 | | - sqlite3VdbePrintSql(p); |
| 67067 | | - } |
| 67068 | | - sqlite3VdbePrintOp(p->trace, pc, pOp); |
| 67086 | + if( db->flags & SQLITE_VdbeTrace ){ |
| 67087 | + sqlite3VdbePrintOp(stdout, pc, pOp); |
| 67069 | 67088 | } |
| 67070 | 67089 | #endif |
| 67071 | 67090 | |
| 67072 | 67091 | |
| 67073 | 67092 | /* Check to see if we need to simulate an interrupt. This only happens |
| | @@ -67194,19 +67213,16 @@ |
| 67194 | 67213 | ** sqlite3VdbeExec() or since last time the progress callback was called). |
| 67195 | 67214 | ** If the progress callback returns non-zero, exit the virtual machine with |
| 67196 | 67215 | ** a return code SQLITE_ABORT. |
| 67197 | 67216 | */ |
| 67198 | 67217 | if( db->xProgress!=0 && nVmStep>=nProgressLimit ){ |
| 67199 | | - int prc; |
| 67200 | | - prc = db->xProgress(db->pProgressArg); |
| 67201 | | - if( prc!=0 ){ |
| 67218 | + assert( db->nProgressOps!=0 ); |
| 67219 | + nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); |
| 67220 | + if( db->xProgress(db->pProgressArg) ){ |
| 67202 | 67221 | rc = SQLITE_INTERRUPT; |
| 67203 | 67222 | goto vdbe_error_halt; |
| 67204 | 67223 | } |
| 67205 | | - if( db->xProgress!=0 ){ |
| 67206 | | - nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); |
| 67207 | | - } |
| 67208 | 67224 | } |
| 67209 | 67225 | #endif |
| 67210 | 67226 | |
| 67211 | 67227 | break; |
| 67212 | 67228 | } |
| | @@ -67638,10 +67654,22 @@ |
| 67638 | 67654 | int i; |
| 67639 | 67655 | #endif /* local variables moved into u.ag */ |
| 67640 | 67656 | assert( p->nResColumn==pOp->p2 ); |
| 67641 | 67657 | assert( pOp->p1>0 ); |
| 67642 | 67658 | assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 ); |
| 67659 | + |
| 67660 | +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 67661 | + /* Run the progress counter just before returning. |
| 67662 | + */ |
| 67663 | + if( db->xProgress!=0 |
| 67664 | + && nVmStep>=nProgressLimit |
| 67665 | + && db->xProgress(db->pProgressArg)!=0 |
| 67666 | + ){ |
| 67667 | + rc = SQLITE_INTERRUPT; |
| 67668 | + goto vdbe_error_halt; |
| 67669 | + } |
| 67670 | +#endif |
| 67643 | 67671 | |
| 67644 | 67672 | /* If this statement has violated immediate foreign key constraints, do |
| 67645 | 67673 | ** not return the number of rows modified. And do not RELEASE the statement |
| 67646 | 67674 | ** transaction. It needs to be rolled back. */ |
| 67647 | 67675 | if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ |
| | @@ -67768,23 +67796,23 @@ |
| 67768 | 67796 | ** Subtract the value in register P1 from the value in register P2 |
| 67769 | 67797 | ** and store the result in register P3. |
| 67770 | 67798 | ** If either input is NULL, the result is NULL. |
| 67771 | 67799 | */ |
| 67772 | 67800 | /* Opcode: Divide P1 P2 P3 * * |
| 67773 | | -** Synopsis: r[P3]=r[P1]/r[P2] |
| 67801 | +** Synopsis: r[P3]=r[P2]/r[P1] |
| 67774 | 67802 | ** |
| 67775 | 67803 | ** Divide the value in register P1 by the value in register P2 |
| 67776 | 67804 | ** and store the result in register P3 (P3=P2/P1). If the value in |
| 67777 | 67805 | ** register P1 is zero, then the result is NULL. If either input is |
| 67778 | 67806 | ** NULL, the result is NULL. |
| 67779 | 67807 | */ |
| 67780 | 67808 | /* Opcode: Remainder P1 P2 P3 * * |
| 67781 | | -** Synopsis: r[P3]=r[P1]%r[P2] |
| 67809 | +** Synopsis: r[P3]=r[P2]%r[P1] |
| 67782 | 67810 | ** |
| 67783 | | -** Compute the remainder after integer division of the value in |
| 67784 | | -** register P1 by the value in register P2 and store the result in P3. |
| 67785 | | -** If the value in register P2 is zero the result is NULL. |
| 67811 | +** Compute the remainder after integer register P2 is divided by |
| 67812 | +** register P1 and store the result in register P3. |
| 67813 | +** If the value in register P1 is zero the result is NULL. |
| 67786 | 67814 | ** If either operand is NULL, the result is NULL. |
| 67787 | 67815 | */ |
| 67788 | 67816 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 67789 | 67817 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 67790 | 67818 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| | @@ -68249,11 +68277,11 @@ |
| 68249 | 68277 | break; |
| 68250 | 68278 | } |
| 68251 | 68279 | #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */ |
| 68252 | 68280 | |
| 68253 | 68281 | /* Opcode: Lt P1 P2 P3 P4 P5 |
| 68254 | | -** Synopsis: if r[P1]<r[P3] goto P3 |
| 68282 | +** Synopsis: if r[P1]<r[P3] goto P2 |
| 68255 | 68283 | ** |
| 68256 | 68284 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 68257 | 68285 | ** jump to address P2. |
| 68258 | 68286 | ** |
| 68259 | 68287 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| | @@ -72846,17 +72874,17 @@ |
| 72846 | 72874 | */ |
| 72847 | 72875 | #ifndef NDEBUG |
| 72848 | 72876 | assert( pc>=-1 && pc<p->nOp ); |
| 72849 | 72877 | |
| 72850 | 72878 | #ifdef SQLITE_DEBUG |
| 72851 | | - if( p->trace ){ |
| 72852 | | - if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc); |
| 72879 | + if( db->flags & SQLITE_VdbeTrace ){ |
| 72880 | + if( rc!=0 ) printf("rc=%d\n",rc); |
| 72853 | 72881 | if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){ |
| 72854 | | - registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]); |
| 72882 | + registerTrace(pOp->p2, &aMem[pOp->p2]); |
| 72855 | 72883 | } |
| 72856 | 72884 | if( pOp->opflags & OPFLG_OUT3 ){ |
| 72857 | | - registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]); |
| 72885 | + registerTrace(pOp->p3, &aMem[pOp->p3]); |
| 72858 | 72886 | } |
| 72859 | 72887 | } |
| 72860 | 72888 | #endif /* SQLITE_DEBUG */ |
| 72861 | 72889 | #endif /* NDEBUG */ |
| 72862 | 72890 | } /* The end of the for(;;) loop the loops through opcodes */ |
| | @@ -73253,10 +73281,11 @@ |
| 73253 | 73281 | if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt); |
| 73254 | 73282 | sqlite3DbFree(db, pBlob); |
| 73255 | 73283 | } |
| 73256 | 73284 | sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr); |
| 73257 | 73285 | sqlite3DbFree(db, zErr); |
| 73286 | + sqlite3ParserReset(pParse); |
| 73258 | 73287 | sqlite3StackFree(db, pParse); |
| 73259 | 73288 | rc = sqlite3ApiExit(db, rc); |
| 73260 | 73289 | sqlite3_mutex_leave(db->mutex); |
| 73261 | 73290 | return rc; |
| 73262 | 73291 | } |
| | @@ -75218,14 +75247,14 @@ |
| 75218 | 75247 | if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){ |
| 75219 | 75248 | incrAggFunctionDepth(pDup, nSubquery); |
| 75220 | 75249 | pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0); |
| 75221 | 75250 | if( pDup==0 ) return; |
| 75222 | 75251 | ExprSetProperty(pDup, EP_Skip); |
| 75223 | | - if( pEList->a[iCol].iAlias==0 ){ |
| 75224 | | - pEList->a[iCol].iAlias = (u16)(++pParse->nAlias); |
| 75252 | + if( pEList->a[iCol].u.x.iAlias==0 ){ |
| 75253 | + pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias); |
| 75225 | 75254 | } |
| 75226 | | - pDup->iTable = pEList->a[iCol].iAlias; |
| 75255 | + pDup->iTable = pEList->a[iCol].u.x.iAlias; |
| 75227 | 75256 | } |
| 75228 | 75257 | if( pExpr->op==TK_COLLATE ){ |
| 75229 | 75258 | pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken); |
| 75230 | 75259 | } |
| 75231 | 75260 | |
| | @@ -76086,11 +76115,11 @@ |
| 76086 | 76115 | assert( pItem->pExpr->op==TK_COLLATE ); |
| 76087 | 76116 | assert( pItem->pExpr->pLeft==pE ); |
| 76088 | 76117 | pItem->pExpr->pLeft = pNew; |
| 76089 | 76118 | } |
| 76090 | 76119 | sqlite3ExprDelete(db, pE); |
| 76091 | | - pItem->iOrderByCol = (u16)iCol; |
| 76120 | + pItem->u.x.iOrderByCol = (u16)iCol; |
| 76092 | 76121 | pItem->done = 1; |
| 76093 | 76122 | }else{ |
| 76094 | 76123 | moreToDo = 1; |
| 76095 | 76124 | } |
| 76096 | 76125 | } |
| | @@ -76107,12 +76136,12 @@ |
| 76107 | 76136 | } |
| 76108 | 76137 | |
| 76109 | 76138 | /* |
| 76110 | 76139 | ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of |
| 76111 | 76140 | ** the SELECT statement pSelect. If any term is reference to a |
| 76112 | | -** result set expression (as determined by the ExprList.a.iOrderByCol field) |
| 76113 | | -** then convert that term into a copy of the corresponding result set |
| 76141 | +** result set expression (as determined by the ExprList.a.u.x.iOrderByCol |
| 76142 | +** field) then convert that term into a copy of the corresponding result set |
| 76114 | 76143 | ** column. |
| 76115 | 76144 | ** |
| 76116 | 76145 | ** If any errors are detected, add an error message to pParse and |
| 76117 | 76146 | ** return non-zero. Return zero if no errors are seen. |
| 76118 | 76147 | */ |
| | @@ -76135,16 +76164,16 @@ |
| 76135 | 76164 | } |
| 76136 | 76165 | #endif |
| 76137 | 76166 | pEList = pSelect->pEList; |
| 76138 | 76167 | assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ |
| 76139 | 76168 | for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
| 76140 | | - if( pItem->iOrderByCol ){ |
| 76141 | | - if( pItem->iOrderByCol>pEList->nExpr ){ |
| 76169 | + if( pItem->u.x.iOrderByCol ){ |
| 76170 | + if( pItem->u.x.iOrderByCol>pEList->nExpr ){ |
| 76142 | 76171 | resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr); |
| 76143 | 76172 | return 1; |
| 76144 | 76173 | } |
| 76145 | | - resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0); |
| 76174 | + resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0); |
| 76146 | 76175 | } |
| 76147 | 76176 | } |
| 76148 | 76177 | return 0; |
| 76149 | 76178 | } |
| 76150 | 76179 | |
| | @@ -76189,11 +76218,11 @@ |
| 76189 | 76218 | if( iCol>0 ){ |
| 76190 | 76219 | /* If an AS-name match is found, mark this ORDER BY column as being |
| 76191 | 76220 | ** a copy of the iCol-th result-set column. The subsequent call to |
| 76192 | 76221 | ** sqlite3ResolveOrderGroupBy() will convert the expression to a |
| 76193 | 76222 | ** copy of the iCol-th result-set expression. */ |
| 76194 | | - pItem->iOrderByCol = (u16)iCol; |
| 76223 | + pItem->u.x.iOrderByCol = (u16)iCol; |
| 76195 | 76224 | continue; |
| 76196 | 76225 | } |
| 76197 | 76226 | } |
| 76198 | 76227 | if( sqlite3ExprIsInteger(pE2, &iCol) ){ |
| 76199 | 76228 | /* The ORDER BY term is an integer constant. Again, set the column |
| | @@ -76201,22 +76230,22 @@ |
| 76201 | 76230 | ** order-by term to a copy of the result-set expression */ |
| 76202 | 76231 | if( iCol<1 || iCol>0xffff ){ |
| 76203 | 76232 | resolveOutOfRangeError(pParse, zType, i+1, nResult); |
| 76204 | 76233 | return 1; |
| 76205 | 76234 | } |
| 76206 | | - pItem->iOrderByCol = (u16)iCol; |
| 76235 | + pItem->u.x.iOrderByCol = (u16)iCol; |
| 76207 | 76236 | continue; |
| 76208 | 76237 | } |
| 76209 | 76238 | |
| 76210 | 76239 | /* Otherwise, treat the ORDER BY term as an ordinary expression */ |
| 76211 | | - pItem->iOrderByCol = 0; |
| 76240 | + pItem->u.x.iOrderByCol = 0; |
| 76212 | 76241 | if( sqlite3ResolveExprNames(pNC, pE) ){ |
| 76213 | 76242 | return 1; |
| 76214 | 76243 | } |
| 76215 | 76244 | for(j=0; j<pSelect->pEList->nExpr; j++){ |
| 76216 | 76245 | if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){ |
| 76217 | | - pItem->iOrderByCol = j+1; |
| 76246 | + pItem->u.x.iOrderByCol = j+1; |
| 76218 | 76247 | } |
| 76219 | 76248 | } |
| 76220 | 76249 | } |
| 76221 | 76250 | return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType); |
| 76222 | 76251 | } |
| | @@ -77491,12 +77520,11 @@ |
| 77491 | 77520 | pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
| 77492 | 77521 | pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); |
| 77493 | 77522 | pItem->sortOrder = pOldItem->sortOrder; |
| 77494 | 77523 | pItem->done = 0; |
| 77495 | 77524 | pItem->bSpanIsTab = pOldItem->bSpanIsTab; |
| 77496 | | - pItem->iOrderByCol = pOldItem->iOrderByCol; |
| 77497 | | - pItem->iAlias = pOldItem->iAlias; |
| 77525 | + pItem->u = pOldItem->u; |
| 77498 | 77526 | } |
| 77499 | 77527 | return pNew; |
| 77500 | 77528 | } |
| 77501 | 77529 | |
| 77502 | 77530 | /* |
| | @@ -78917,10 +78945,11 @@ |
| 78917 | 78945 | int inReg = target; /* Results stored in register inReg */ |
| 78918 | 78946 | int regFree1 = 0; /* If non-zero free this temporary register */ |
| 78919 | 78947 | int regFree2 = 0; /* If non-zero free this temporary register */ |
| 78920 | 78948 | int r1, r2, r3, r4; /* Various register numbers */ |
| 78921 | 78949 | sqlite3 *db = pParse->db; /* The database connection */ |
| 78950 | + Expr tempX; /* Temporary expression node */ |
| 78922 | 78951 | |
| 78923 | 78952 | assert( target>0 && target<=pParse->nMem ); |
| 78924 | 78953 | if( v==0 ){ |
| 78925 | 78954 | assert( pParse->db->mallocFailed ); |
| 78926 | 78955 | return 0; |
| | @@ -79136,12 +79165,14 @@ |
| 79136 | 79165 | }else if( pLeft->op==TK_FLOAT ){ |
| 79137 | 79166 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 79138 | 79167 | codeReal(v, pLeft->u.zToken, 1, target); |
| 79139 | 79168 | #endif |
| 79140 | 79169 | }else{ |
| 79141 | | - regFree1 = r1 = sqlite3GetTempReg(pParse); |
| 79142 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, r1); |
| 79170 | + tempX.op = TK_INTEGER; |
| 79171 | + tempX.flags = EP_IntValue|EP_TokenOnly; |
| 79172 | + tempX.u.iValue = 0; |
| 79173 | + r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); |
| 79143 | 79174 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); |
| 79144 | 79175 | sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); |
| 79145 | 79176 | testcase( regFree2==0 ); |
| 79146 | 79177 | } |
| 79147 | 79178 | inReg = target; |
| | @@ -79453,11 +79484,10 @@ |
| 79453 | 79484 | int nExpr; /* 2x number of WHEN terms */ |
| 79454 | 79485 | int i; /* Loop counter */ |
| 79455 | 79486 | ExprList *pEList; /* List of WHEN terms */ |
| 79456 | 79487 | struct ExprList_item *aListelem; /* Array of WHEN terms */ |
| 79457 | 79488 | Expr opCompare; /* The X==Ei expression */ |
| 79458 | | - Expr cacheX; /* Cached expression X */ |
| 79459 | 79489 | Expr *pX; /* The X expression */ |
| 79460 | 79490 | Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ |
| 79461 | 79491 | VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) |
| 79462 | 79492 | |
| 79463 | 79493 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); |
| | @@ -79465,17 +79495,16 @@ |
| 79465 | 79495 | pEList = pExpr->x.pList; |
| 79466 | 79496 | aListelem = pEList->a; |
| 79467 | 79497 | nExpr = pEList->nExpr; |
| 79468 | 79498 | endLabel = sqlite3VdbeMakeLabel(v); |
| 79469 | 79499 | if( (pX = pExpr->pLeft)!=0 ){ |
| 79470 | | - cacheX = *pX; |
| 79500 | + tempX = *pX; |
| 79471 | 79501 | testcase( pX->op==TK_COLUMN ); |
| 79472 | | - testcase( pX->op==TK_REGISTER ); |
| 79473 | | - exprToRegister(&cacheX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); |
| 79502 | + exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); |
| 79474 | 79503 | testcase( regFree1==0 ); |
| 79475 | 79504 | opCompare.op = TK_EQ; |
| 79476 | | - opCompare.pLeft = &cacheX; |
| 79505 | + opCompare.pLeft = &tempX; |
| 79477 | 79506 | pTest = &opCompare; |
| 79478 | 79507 | /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: |
| 79479 | 79508 | ** The value in regFree1 might get SCopy-ed into the file result. |
| 79480 | 79509 | ** So make sure that the regFree1 register is not reused for other |
| 79481 | 79510 | ** purposes and possibly overwritten. */ |
| | @@ -79491,11 +79520,10 @@ |
| 79491 | 79520 | } |
| 79492 | 79521 | nextCase = sqlite3VdbeMakeLabel(v); |
| 79493 | 79522 | testcase( pTest->op==TK_COLUMN ); |
| 79494 | 79523 | sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); |
| 79495 | 79524 | testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); |
| 79496 | | - testcase( aListelem[i+1].pExpr->op==TK_REGISTER ); |
| 79497 | 79525 | sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); |
| 79498 | 79526 | sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); |
| 79499 | 79527 | sqlite3ExprCachePop(pParse, 1); |
| 79500 | 79528 | sqlite3VdbeResolveLabel(v, nextCase); |
| 79501 | 79529 | } |
| | @@ -79550,19 +79578,45 @@ |
| 79550 | 79578 | ** are stored. |
| 79551 | 79579 | ** |
| 79552 | 79580 | ** If the register is a temporary register that can be deallocated, |
| 79553 | 79581 | ** then write its number into *pReg. If the result register is not |
| 79554 | 79582 | ** a temporary, then set *pReg to zero. |
| 79583 | +** |
| 79584 | +** If pExpr is a constant, then this routine might generate this |
| 79585 | +** code to fill the register in the initialization section of the |
| 79586 | +** VDBE program, in order to factor it out of the evaluation loop. |
| 79555 | 79587 | */ |
| 79556 | 79588 | SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ |
| 79557 | | - int r1 = sqlite3GetTempReg(pParse); |
| 79558 | | - int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
| 79559 | | - if( r2==r1 ){ |
| 79560 | | - *pReg = r1; |
| 79589 | + int r2; |
| 79590 | + pExpr = sqlite3ExprSkipCollate(pExpr); |
| 79591 | + if( pParse->cookieGoto>0 |
| 79592 | + && pExpr->op!=TK_REGISTER |
| 79593 | + && sqlite3ExprIsConstantNotJoin(pExpr) |
| 79594 | + ){ |
| 79595 | + ExprList *p = pParse->pConstExpr; |
| 79596 | + int i; |
| 79597 | + *pReg = 0; |
| 79598 | + if( p ){ |
| 79599 | + for(i=0; i<p->nExpr; i++){ |
| 79600 | + if( sqlite3ExprCompare(p->a[i].pExpr, pExpr, -1)==0 ){ |
| 79601 | + return p->a[i].u.iConstExprReg; |
| 79602 | + } |
| 79603 | + } |
| 79604 | + } |
| 79605 | + p = sqlite3ExprListAppend(pParse, p, sqlite3ExprDup(pParse->db, pExpr, 0)); |
| 79606 | + pParse->pConstExpr = p; |
| 79607 | + r2 = ++pParse->nMem; |
| 79608 | + if( p ) p->a[p->nExpr-1].u.iConstExprReg = r2; |
| 79561 | 79609 | }else{ |
| 79562 | | - sqlite3ReleaseTempReg(pParse, r1); |
| 79563 | | - *pReg = 0; |
| 79610 | + int r1 = sqlite3GetTempReg(pParse); |
| 79611 | + r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
| 79612 | + if( r2==r1 ){ |
| 79613 | + *pReg = r1; |
| 79614 | + }else{ |
| 79615 | + sqlite3ReleaseTempReg(pParse, r1); |
| 79616 | + *pReg = 0; |
| 79617 | + } |
| 79564 | 79618 | } |
| 79565 | 79619 | return r2; |
| 79566 | 79620 | } |
| 79567 | 79621 | |
| 79568 | 79622 | /* |
| | @@ -79601,16 +79655,17 @@ |
| 79601 | 79655 | SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ |
| 79602 | 79656 | Vdbe *v = pParse->pVdbe; |
| 79603 | 79657 | int inReg; |
| 79604 | 79658 | inReg = sqlite3ExprCode(pParse, pExpr, target); |
| 79605 | 79659 | assert( target>0 ); |
| 79606 | | - /* This routine is called for terms to INSERT or UPDATE. And the only |
| 79607 | | - ** other place where expressions can be converted into TK_REGISTER is |
| 79608 | | - ** in WHERE clause processing. So as currently implemented, there is |
| 79609 | | - ** no way for a TK_REGISTER to exist here. But it seems prudent to |
| 79610 | | - ** keep the ALWAYS() in case the conditions above change with future |
| 79611 | | - ** modifications or enhancements. */ |
| 79660 | + /* The only place, other than this routine, where expressions can be |
| 79661 | + ** converted to TK_REGISTER is internal subexpressions in BETWEEN and |
| 79662 | + ** CASE operators. Neither ever calls this routine. And this routine |
| 79663 | + ** is never called twice on the same expression. Hence it is impossible |
| 79664 | + ** for the input to this routine to already be a register. Nevertheless, |
| 79665 | + ** it seems prudent to keep the ALWAYS() in case the conditions above |
| 79666 | + ** change with future modifications or enhancements. */ |
| 79612 | 79667 | if( ALWAYS(pExpr->op!=TK_REGISTER) ){ |
| 79613 | 79668 | int iMem; |
| 79614 | 79669 | iMem = ++pParse->nMem; |
| 79615 | 79670 | sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); |
| 79616 | 79671 | exprToRegister(pExpr, iMem); |
| | @@ -79888,144 +79943,10 @@ |
| 79888 | 79943 | } |
| 79889 | 79944 | sqlite3ExplainPop(pOut); |
| 79890 | 79945 | } |
| 79891 | 79946 | } |
| 79892 | 79947 | #endif /* SQLITE_DEBUG */ |
| 79893 | | - |
| 79894 | | -/* |
| 79895 | | -** Return TRUE if pExpr is an constant expression that is appropriate |
| 79896 | | -** for factoring out of a loop. Appropriate expressions are: |
| 79897 | | -** |
| 79898 | | -** * Any expression that evaluates to two or more opcodes. |
| 79899 | | -** |
| 79900 | | -** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, |
| 79901 | | -** or OP_Variable that does not need to be placed in a |
| 79902 | | -** specific register. |
| 79903 | | -** |
| 79904 | | -** There is no point in factoring out single-instruction constant |
| 79905 | | -** expressions that need to be placed in a particular register. |
| 79906 | | -** We could factor them out, but then we would end up adding an |
| 79907 | | -** OP_SCopy instruction to move the value into the correct register |
| 79908 | | -** later. We might as well just use the original instruction and |
| 79909 | | -** avoid the OP_SCopy. |
| 79910 | | -*/ |
| 79911 | | -static int isAppropriateForFactoring(Expr *p){ |
| 79912 | | - if( !sqlite3ExprIsConstantNotJoin(p) ){ |
| 79913 | | - return 0; /* Only constant expressions are appropriate for factoring */ |
| 79914 | | - } |
| 79915 | | - if( (p->flags & EP_FixedDest)==0 ){ |
| 79916 | | - return 1; /* Any constant without a fixed destination is appropriate */ |
| 79917 | | - } |
| 79918 | | - while( p->op==TK_UPLUS ) p = p->pLeft; |
| 79919 | | - switch( p->op ){ |
| 79920 | | -#ifndef SQLITE_OMIT_BLOB_LITERAL |
| 79921 | | - case TK_BLOB: |
| 79922 | | -#endif |
| 79923 | | - case TK_VARIABLE: |
| 79924 | | - case TK_INTEGER: |
| 79925 | | - case TK_FLOAT: |
| 79926 | | - case TK_NULL: |
| 79927 | | - case TK_STRING: { |
| 79928 | | - testcase( p->op==TK_BLOB ); |
| 79929 | | - testcase( p->op==TK_VARIABLE ); |
| 79930 | | - testcase( p->op==TK_INTEGER ); |
| 79931 | | - testcase( p->op==TK_FLOAT ); |
| 79932 | | - testcase( p->op==TK_NULL ); |
| 79933 | | - testcase( p->op==TK_STRING ); |
| 79934 | | - /* Single-instruction constants with a fixed destination are |
| 79935 | | - ** better done in-line. If we factor them, they will just end |
| 79936 | | - ** up generating an OP_SCopy to move the value to the destination |
| 79937 | | - ** register. */ |
| 79938 | | - return 0; |
| 79939 | | - } |
| 79940 | | - case TK_UMINUS: { |
| 79941 | | - if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){ |
| 79942 | | - return 0; |
| 79943 | | - } |
| 79944 | | - break; |
| 79945 | | - } |
| 79946 | | - default: { |
| 79947 | | - break; |
| 79948 | | - } |
| 79949 | | - } |
| 79950 | | - return 1; |
| 79951 | | -} |
| 79952 | | - |
| 79953 | | -/* |
| 79954 | | -** If pExpr is a constant expression that is appropriate for |
| 79955 | | -** factoring out of a loop, then evaluate the expression |
| 79956 | | -** into a register and convert the expression into a TK_REGISTER |
| 79957 | | -** expression. |
| 79958 | | -*/ |
| 79959 | | -static int evalConstExpr(Walker *pWalker, Expr *pExpr){ |
| 79960 | | - Parse *pParse = pWalker->pParse; |
| 79961 | | - switch( pExpr->op ){ |
| 79962 | | - case TK_IN: |
| 79963 | | - case TK_REGISTER: { |
| 79964 | | - return WRC_Prune; |
| 79965 | | - } |
| 79966 | | - case TK_COLLATE: { |
| 79967 | | - return WRC_Continue; |
| 79968 | | - } |
| 79969 | | - case TK_FUNCTION: |
| 79970 | | - case TK_AGG_FUNCTION: |
| 79971 | | - case TK_CONST_FUNC: { |
| 79972 | | - /* The arguments to a function have a fixed destination. |
| 79973 | | - ** Mark them this way to avoid generated unneeded OP_SCopy |
| 79974 | | - ** instructions. |
| 79975 | | - */ |
| 79976 | | - ExprList *pList = pExpr->x.pList; |
| 79977 | | - assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 79978 | | - if( pList ){ |
| 79979 | | - int i = pList->nExpr; |
| 79980 | | - struct ExprList_item *pItem = pList->a; |
| 79981 | | - for(; i>0; i--, pItem++){ |
| 79982 | | - if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest; |
| 79983 | | - } |
| 79984 | | - } |
| 79985 | | - break; |
| 79986 | | - } |
| 79987 | | - } |
| 79988 | | - if( isAppropriateForFactoring(pExpr) ){ |
| 79989 | | - int r1 = ++pParse->nMem; |
| 79990 | | - int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
| 79991 | | - /* If r2!=r1, it means that register r1 is never used. That is harmless |
| 79992 | | - ** but suboptimal, so we want to know about the situation to fix it. |
| 79993 | | - ** Hence the following assert: */ |
| 79994 | | - assert( r2==r1 ); |
| 79995 | | - exprToRegister(pExpr, r2); |
| 79996 | | - return WRC_Prune; |
| 79997 | | - } |
| 79998 | | - return WRC_Continue; |
| 79999 | | -} |
| 80000 | | - |
| 80001 | | -/* |
| 80002 | | -** Preevaluate constant subexpressions within pExpr and store the |
| 80003 | | -** results in registers. Modify pExpr so that the constant subexpresions |
| 80004 | | -** are TK_REGISTER opcodes that refer to the precomputed values. |
| 80005 | | -** |
| 80006 | | -** This routine is a no-op if the jump to the cookie-check code has |
| 80007 | | -** already occur. Since the cookie-check jump is generated prior to |
| 80008 | | -** any other serious processing, this check ensures that there is no |
| 80009 | | -** way to accidently bypass the constant initializations. |
| 80010 | | -** |
| 80011 | | -** This routine is also a no-op if the SQLITE_FactorOutConst optimization |
| 80012 | | -** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS) |
| 80013 | | -** interface. This allows test logic to verify that the same answer is |
| 80014 | | -** obtained for queries regardless of whether or not constants are |
| 80015 | | -** precomputed into registers or if they are inserted in-line. |
| 80016 | | -*/ |
| 80017 | | -SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ |
| 80018 | | - Walker w; |
| 80019 | | - if( pParse->cookieGoto ) return; |
| 80020 | | - if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return; |
| 80021 | | - memset(&w, 0, sizeof(w)); |
| 80022 | | - w.xExprCallback = evalConstExpr; |
| 80023 | | - w.pParse = pParse; |
| 80024 | | - sqlite3WalkExpr(&w, pExpr); |
| 80025 | | -} |
| 80026 | | - |
| 80027 | 79948 | |
| 80028 | 79949 | /* |
| 80029 | 79950 | ** Generate code that pushes the value of every element of the given |
| 80030 | 79951 | ** expression list into a sequence of registers beginning at target. |
| 80031 | 79952 | ** |
| | @@ -80400,44 +80321,46 @@ |
| 80400 | 80321 | ** this routine is used, it does not hurt to get an extra 2 - that |
| 80401 | 80322 | ** just might result in some slightly slower code. But returning |
| 80402 | 80323 | ** an incorrect 0 or 1 could lead to a malfunction. |
| 80403 | 80324 | */ |
| 80404 | 80325 | SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ |
| 80405 | | - if( pA==0||pB==0 ){ |
| 80326 | + u32 combinedFlags; |
| 80327 | + if( pA==0 || pB==0 ){ |
| 80406 | 80328 | return pB==pA ? 0 : 2; |
| 80407 | 80329 | } |
| 80408 | | - assert( !ExprHasProperty(pA, EP_TokenOnly|EP_Reduced) ); |
| 80409 | | - assert( !ExprHasProperty(pB, EP_TokenOnly|EP_Reduced) ); |
| 80410 | | - if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){ |
| 80330 | + combinedFlags = pA->flags | pB->flags; |
| 80331 | + if( combinedFlags & EP_IntValue ){ |
| 80332 | + if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ |
| 80333 | + return 0; |
| 80334 | + } |
| 80411 | 80335 | return 2; |
| 80412 | 80336 | } |
| 80413 | | - if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; |
| 80414 | | - if( pA->op!=pB->op && (pA->op!=TK_REGISTER || pA->op2!=pB->op) ){ |
| 80337 | + if( pA->op!=pB->op ){ |
| 80415 | 80338 | if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ |
| 80416 | 80339 | return 1; |
| 80417 | 80340 | } |
| 80418 | 80341 | if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ |
| 80419 | 80342 | return 1; |
| 80420 | 80343 | } |
| 80421 | 80344 | return 2; |
| 80422 | 80345 | } |
| 80423 | | - if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; |
| 80424 | | - if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; |
| 80425 | | - if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; |
| 80426 | | - if( pA->iColumn!=pB->iColumn ) return 2; |
| 80427 | | - if( pA->iTable!=pB->iTable |
| 80428 | | - && pA->op!=TK_REGISTER |
| 80429 | | - && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; |
| 80430 | | - if( ExprHasProperty(pA, EP_IntValue) ){ |
| 80431 | | - if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){ |
| 80432 | | - return 2; |
| 80433 | | - } |
| 80434 | | - }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){ |
| 80435 | | - if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2; |
| 80346 | + if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){ |
| 80436 | 80347 | if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ |
| 80437 | 80348 | return pA->op==TK_COLLATE ? 1 : 2; |
| 80438 | 80349 | } |
| 80350 | + } |
| 80351 | + if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; |
| 80352 | + if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ |
| 80353 | + if( combinedFlags & EP_xIsSelect ) return 2; |
| 80354 | + if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; |
| 80355 | + if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; |
| 80356 | + if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; |
| 80357 | + if( ALWAYS((combinedFlags & EP_Reduced)==0) ){ |
| 80358 | + if( pA->iColumn!=pB->iColumn ) return 2; |
| 80359 | + if( pA->iTable!=pB->iTable |
| 80360 | + && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; |
| 80361 | + } |
| 80439 | 80362 | } |
| 80440 | 80363 | return 0; |
| 80441 | 80364 | } |
| 80442 | 80365 | |
| 80443 | 80366 | /* |
| | @@ -83090,14 +83013,16 @@ |
| 83090 | 83013 | } |
| 83091 | 83014 | pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase); |
| 83092 | 83015 | if( pTable==0 ){ |
| 83093 | 83016 | return 0; |
| 83094 | 83017 | } |
| 83095 | | - if( argv[1] ){ |
| 83096 | | - pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); |
| 83097 | | - }else{ |
| 83018 | + if( argv[1]==0 ){ |
| 83098 | 83019 | pIndex = 0; |
| 83020 | + }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){ |
| 83021 | + pIndex = sqlite3PrimaryKeyIndex(pTable); |
| 83022 | + }else{ |
| 83023 | + pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); |
| 83099 | 83024 | } |
| 83100 | 83025 | z = argv[2]; |
| 83101 | 83026 | |
| 83102 | 83027 | if( pIndex ){ |
| 83103 | 83028 | decodeIntArray((char*)z, pIndex->nKeyCol+1, pIndex->aiRowEst, pIndex); |
| | @@ -84408,11 +84333,11 @@ |
| 84408 | 84333 | ** transaction on each used database and to verify the schema cookie |
| 84409 | 84334 | ** on each used database. |
| 84410 | 84335 | */ |
| 84411 | 84336 | if( pParse->cookieGoto>0 ){ |
| 84412 | 84337 | yDbMask mask; |
| 84413 | | - int iDb; |
| 84338 | + int iDb, i, addr; |
| 84414 | 84339 | sqlite3VdbeJumpHere(v, pParse->cookieGoto-1); |
| 84415 | 84340 | for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){ |
| 84416 | 84341 | if( (mask & pParse->cookieMask)==0 ) continue; |
| 84417 | 84342 | sqlite3VdbeUsesBtree(v, iDb); |
| 84418 | 84343 | sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0); |
| | @@ -84422,18 +84347,15 @@ |
| 84422 | 84347 | iDb, pParse->cookieValue[iDb], |
| 84423 | 84348 | db->aDb[iDb].pSchema->iGeneration); |
| 84424 | 84349 | } |
| 84425 | 84350 | } |
| 84426 | 84351 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 84427 | | - { |
| 84428 | | - int i; |
| 84429 | | - for(i=0; i<pParse->nVtabLock; i++){ |
| 84430 | | - char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
| 84431 | | - sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
| 84432 | | - } |
| 84433 | | - pParse->nVtabLock = 0; |
| 84434 | | - } |
| 84352 | + for(i=0; i<pParse->nVtabLock; i++){ |
| 84353 | + char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
| 84354 | + sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
| 84355 | + } |
| 84356 | + pParse->nVtabLock = 0; |
| 84435 | 84357 | #endif |
| 84436 | 84358 | |
| 84437 | 84359 | /* Once all the cookies have been verified and transactions opened, |
| 84438 | 84360 | ** obtain the required table-locks. This is a no-op unless the |
| 84439 | 84361 | ** shared-cache feature is enabled. |
| | @@ -84441,24 +84363,30 @@ |
| 84441 | 84363 | codeTableLocks(pParse); |
| 84442 | 84364 | |
| 84443 | 84365 | /* Initialize any AUTOINCREMENT data structures required. |
| 84444 | 84366 | */ |
| 84445 | 84367 | sqlite3AutoincrementBegin(pParse); |
| 84368 | + |
| 84369 | + /* Code constant expressions that where factored out of inner loops */ |
| 84370 | + addr = pParse->cookieGoto; |
| 84371 | + if( pParse->pConstExpr ){ |
| 84372 | + ExprList *pEL = pParse->pConstExpr; |
| 84373 | + pParse->cookieGoto = 0; |
| 84374 | + for(i=0; i<pEL->nExpr; i++){ |
| 84375 | + sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg); |
| 84376 | + } |
| 84377 | + } |
| 84446 | 84378 | |
| 84447 | 84379 | /* Finally, jump back to the beginning of the executable code. */ |
| 84448 | | - sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto); |
| 84380 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); |
| 84449 | 84381 | } |
| 84450 | 84382 | } |
| 84451 | 84383 | |
| 84452 | 84384 | |
| 84453 | 84385 | /* Get the VDBE program ready for execution |
| 84454 | 84386 | */ |
| 84455 | 84387 | if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){ |
| 84456 | | -#ifdef SQLITE_DEBUG |
| 84457 | | - FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; |
| 84458 | | - sqlite3VdbeTrace(v, trace); |
| 84459 | | -#endif |
| 84460 | 84388 | assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ |
| 84461 | 84389 | /* A minimum of one cursor is required if autoincrement is used |
| 84462 | 84390 | * See ticket [a696379c1f08866] */ |
| 84463 | 84391 | if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; |
| 84464 | 84392 | sqlite3VdbeMakeReady(v, pParse); |
| | @@ -89162,24 +89090,38 @@ |
| 89162 | 89090 | Expr *pWhere /* The WHERE clause. May be null */ |
| 89163 | 89091 | ){ |
| 89164 | 89092 | Vdbe *v; /* The virtual database engine */ |
| 89165 | 89093 | Table *pTab; /* The table from which records will be deleted */ |
| 89166 | 89094 | const char *zDb; /* Name of database holding pTab */ |
| 89167 | | - int end, addr = 0; /* A couple addresses of generated code */ |
| 89168 | 89095 | int i; /* Loop counter */ |
| 89169 | 89096 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 89170 | 89097 | Index *pIdx; /* For looping over indices of the table */ |
| 89171 | 89098 | int iTabCur; /* Cursor number for the table */ |
| 89172 | 89099 | int iDataCur; /* VDBE cursor for the canonical data source */ |
| 89173 | 89100 | int iIdxCur; /* Cursor number of the first index */ |
| 89101 | + int nIdx; /* Number of indices */ |
| 89174 | 89102 | sqlite3 *db; /* Main database structure */ |
| 89175 | 89103 | AuthContext sContext; /* Authorization context */ |
| 89176 | 89104 | NameContext sNC; /* Name context to resolve expressions in */ |
| 89177 | 89105 | int iDb; /* Database number */ |
| 89178 | 89106 | int memCnt = -1; /* Memory cell used for change counting */ |
| 89179 | 89107 | int rcauth; /* Value returned by authorization callback */ |
| 89180 | | - |
| 89108 | + int okOnePass; /* True for one-pass algorithm without the FIFO */ |
| 89109 | + int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */ |
| 89110 | + u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */ |
| 89111 | + Index *pPk; /* The PRIMARY KEY index on the table */ |
| 89112 | + int iPk; /* First of nPk registers holding PRIMARY KEY value */ |
| 89113 | + i16 nPk = 1; /* Number of columns in the PRIMARY KEY */ |
| 89114 | + int iKey; /* Memory cell holding key of row to be deleted */ |
| 89115 | + i16 nKey; /* Number of memory cells in the row key */ |
| 89116 | + int iEphCur = 0; /* Ephemeral table holding all primary key values */ |
| 89117 | + int iRowSet = 0; /* Register for rowset of rows to delete */ |
| 89118 | + int addrBypass = 0; /* Address of jump over the delete logic */ |
| 89119 | + int addrLoop = 0; /* Top of the delete loop */ |
| 89120 | + int addrDelete = 0; /* Jump directly to the delete logic */ |
| 89121 | + int addrEphOpen = 0; /* Instruction to open the Ephermeral table */ |
| 89122 | + |
| 89181 | 89123 | #ifndef SQLITE_OMIT_TRIGGER |
| 89182 | 89124 | int isView; /* True if attempting to delete from a view */ |
| 89183 | 89125 | Trigger *pTrigger; /* List of table triggers, if required */ |
| 89184 | 89126 | #endif |
| 89185 | 89127 | |
| | @@ -89230,15 +89172,15 @@ |
| 89230 | 89172 | if( rcauth==SQLITE_DENY ){ |
| 89231 | 89173 | goto delete_from_cleanup; |
| 89232 | 89174 | } |
| 89233 | 89175 | assert(!isView || pTrigger); |
| 89234 | 89176 | |
| 89235 | | - /* Assign cursor number to the table and all its indices. |
| 89177 | + /* Assign cursor numbers to the table and all its indices. |
| 89236 | 89178 | */ |
| 89237 | 89179 | assert( pTabList->nSrc==1 ); |
| 89238 | 89180 | iTabCur = pTabList->a[0].iCursor = pParse->nTab++; |
| 89239 | | - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 89181 | + for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ |
| 89240 | 89182 | pParse->nTab++; |
| 89241 | 89183 | } |
| 89242 | 89184 | |
| 89243 | 89185 | /* Start the view context |
| 89244 | 89186 | */ |
| | @@ -89300,132 +89242,162 @@ |
| 89300 | 89242 | assert( pIdx->pSchema==pTab->pSchema ); |
| 89301 | 89243 | sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); |
| 89302 | 89244 | } |
| 89303 | 89245 | }else |
| 89304 | 89246 | #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */ |
| 89305 | | - if( !HasRowid(pTab) ){ |
| 89306 | | - /* There is a WHERE clause on a WITHOUT ROWID table. |
| 89307 | | - */ |
| 89308 | | - Index *pPk; /* The PRIMARY KEY index on the table */ |
| 89309 | | - int iPk; /* First of nPk memory cells holding PRIMARY KEY value */ |
| 89310 | | - int iEph; /* Ephemeral table holding all primary key values */ |
| 89311 | | - int iKey; /* Key value inserting into iEph */ |
| 89312 | | - i16 nPk; /* Number of components of the PRIMARY KEY */ |
| 89313 | | - |
| 89314 | | - pPk = sqlite3PrimaryKeyIndex(pTab); |
| 89315 | | - assert( pPk!=0 ); |
| 89316 | | - nPk = pPk->nKeyCol; |
| 89317 | | - iPk = pParse->nMem+1; |
| 89318 | | - pParse->nMem += nPk; |
| 89319 | | - iKey = ++pParse->nMem; |
| 89320 | | - iEph = pParse->nTab++; |
| 89321 | | - |
| 89322 | | - sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk); |
| 89323 | | - sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 89324 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 0, 0); |
| 89325 | | - if( pWInfo==0 ) goto delete_from_cleanup; |
| 89326 | | - for(i=0; i<nPk; i++){ |
| 89327 | | - sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, pPk->aiColumn[i],iPk+i); |
| 89328 | | - } |
| 89329 | | - sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey, |
| 89330 | | - sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT); |
| 89331 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, iKey); |
| 89332 | | - if( db->flags & SQLITE_CountRows ){ |
| 89333 | | - sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); |
| 89334 | | - } |
| 89335 | | - sqlite3WhereEnd(pWInfo); |
| 89336 | | - |
| 89337 | | - /* Open cursors for all indices of the table. |
| 89338 | | - */ |
| 89339 | | - sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, |
| 89340 | | - iTabCur, &iDataCur, &iIdxCur); |
| 89341 | | - |
| 89342 | | - /* Loop over the primary keys to be deleted. */ |
| 89343 | | - addr = sqlite3VdbeAddOp1(v, OP_Rewind, iEph); |
| 89344 | | - sqlite3VdbeAddOp2(v, OP_RowKey, iEph, iPk); |
| 89345 | | - |
| 89346 | | - /* Delete the row */ |
| 89347 | | - sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
| 89348 | | - iPk, 0, 1, OE_Default, 0); |
| 89349 | | - |
| 89350 | | - /* End of the delete loop */ |
| 89351 | | - sqlite3VdbeAddOp2(v, OP_Next, iEph, addr+1); |
| 89352 | | - sqlite3VdbeJumpHere(v, addr); |
| 89353 | | - |
| 89354 | | - /* Close the cursors open on the table and its indexes. */ |
| 89355 | | - assert( iDataCur>=iIdxCur ); |
| 89356 | | - for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
| 89357 | | - sqlite3VdbeAddOp1(v, OP_Close, iIdxCur+i); |
| 89358 | | - } |
| 89359 | | - }else{ |
| 89360 | | - /* There is a WHERE clause on a rowid table. Run a loop that extracts |
| 89361 | | - ** all rowids to be deleted into a RowSet. |
| 89362 | | - */ |
| 89363 | | - int iRowSet = ++pParse->nMem; /* Register for rowset of rows to delete */ |
| 89364 | | - int iRowid = ++pParse->nMem; /* Used for storing rowid values. */ |
| 89365 | | - int regRowid; /* Actual register containing rowids */ |
| 89366 | | - |
| 89367 | | - /* Collect rowids of every row to be deleted. |
| 89368 | | - */ |
| 89369 | | - sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet); |
| 89370 | | - pWInfo = sqlite3WhereBegin( |
| 89371 | | - pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0 |
| 89372 | | - ); |
| 89373 | | - if( pWInfo==0 ) goto delete_from_cleanup; |
| 89374 | | - regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iRowid, 0); |
| 89375 | | - sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid); |
| 89376 | | - if( db->flags & SQLITE_CountRows ){ |
| 89377 | | - sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); |
| 89378 | | - } |
| 89379 | | - sqlite3WhereEnd(pWInfo); |
| 89380 | | - |
| 89381 | | - /* Delete every item whose key was written to the list during the |
| 89382 | | - ** database scan. We have to delete items after the scan is complete |
| 89383 | | - ** because deleting an item can change the scan order. */ |
| 89384 | | - end = sqlite3VdbeMakeLabel(v); |
| 89385 | | - |
| 89386 | | - /* Unless this is a view, open cursors for the table we are |
| 89387 | | - ** deleting from and all its indices. If this is a view, then the |
| 89388 | | - ** only effect this statement has is to fire the INSTEAD OF |
| 89389 | | - ** triggers. */ |
| 89390 | | - if( !isView ){ |
| 89391 | | - sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, |
| 89392 | | - &iDataCur, &iIdxCur); |
| 89393 | | - assert( iDataCur==iTabCur ); |
| 89394 | | - assert( iIdxCur==iDataCur+1 ); |
| 89395 | | - } |
| 89396 | | - |
| 89397 | | - addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid); |
| 89398 | | - |
| 89247 | + { |
| 89248 | + if( HasRowid(pTab) ){ |
| 89249 | + /* For a rowid table, initialize the RowSet to an empty set */ |
| 89250 | + pPk = 0; |
| 89251 | + nPk = 1; |
| 89252 | + iRowSet = ++pParse->nMem; |
| 89253 | + sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet); |
| 89254 | + }else{ |
| 89255 | + /* For a WITHOUT ROWID table, create an ephermeral table used to |
| 89256 | + ** hold all primary keys for rows to be deleted. */ |
| 89257 | + pPk = sqlite3PrimaryKeyIndex(pTab); |
| 89258 | + assert( pPk!=0 ); |
| 89259 | + nPk = pPk->nKeyCol; |
| 89260 | + iPk = pParse->nMem+1; |
| 89261 | + pParse->nMem += nPk; |
| 89262 | + iEphCur = pParse->nTab++; |
| 89263 | + addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk); |
| 89264 | + sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 89265 | + } |
| 89266 | + |
| 89267 | + /* Construct a query to find the rowid or primary key for every row |
| 89268 | + ** to be deleted, based on the WHERE clause. |
| 89269 | + */ |
| 89270 | + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, |
| 89271 | + WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK, |
| 89272 | + iTabCur+1); |
| 89273 | + if( pWInfo==0 ) goto delete_from_cleanup; |
| 89274 | + okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
| 89275 | + |
| 89276 | + /* Keep track of the number of rows to be deleted */ |
| 89277 | + if( db->flags & SQLITE_CountRows ){ |
| 89278 | + sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); |
| 89279 | + } |
| 89280 | + |
| 89281 | + /* Extract the rowid or primary key for the current row */ |
| 89282 | + if( pPk ){ |
| 89283 | + for(i=0; i<nPk; i++){ |
| 89284 | + sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, |
| 89285 | + pPk->aiColumn[i], iPk+i); |
| 89286 | + } |
| 89287 | + iKey = iPk; |
| 89288 | + }else{ |
| 89289 | + iKey = pParse->nMem + 1; |
| 89290 | + iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0); |
| 89291 | + if( iKey>pParse->nMem ) pParse->nMem = iKey; |
| 89292 | + } |
| 89293 | + |
| 89294 | + if( okOnePass ){ |
| 89295 | + /* For ONEPASS, no need to store the rowid/primary-key. There is only |
| 89296 | + ** one, so just keep it in its register(s) and fall through to the |
| 89297 | + ** delete code. |
| 89298 | + */ |
| 89299 | + nKey = nPk; /* OP_Found will use an unpacked key */ |
| 89300 | + aToOpen = sqlite3DbMallocRaw(db, nIdx+2); |
| 89301 | + if( aToOpen==0 ){ |
| 89302 | + sqlite3WhereEnd(pWInfo); |
| 89303 | + goto delete_from_cleanup; |
| 89304 | + } |
| 89305 | + memset(aToOpen, 1, nIdx+1); |
| 89306 | + aToOpen[nIdx+1] = 0; |
| 89307 | + if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0; |
| 89308 | + if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0; |
| 89309 | + if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen); |
| 89310 | + addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */ |
| 89311 | + }else if( pPk ){ |
| 89312 | + /* Construct a composite key for the row to be deleted and remember it */ |
| 89313 | + iKey = ++pParse->nMem; |
| 89314 | + nKey = 0; /* Zero tells OP_Found to use a composite key */ |
| 89315 | + sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey, |
| 89316 | + sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT); |
| 89317 | + sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey); |
| 89318 | + }else{ |
| 89319 | + /* Get the rowid of the row to be deleted and remember it in the RowSet */ |
| 89320 | + nKey = 1; /* OP_Seek always uses a single rowid */ |
| 89321 | + sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey); |
| 89322 | + } |
| 89323 | + |
| 89324 | + /* End of the WHERE loop */ |
| 89325 | + sqlite3WhereEnd(pWInfo); |
| 89326 | + if( okOnePass ){ |
| 89327 | + /* Bypass the delete logic below if the WHERE loop found zero rows */ |
| 89328 | + addrBypass = sqlite3VdbeMakeLabel(v); |
| 89329 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBypass); |
| 89330 | + sqlite3VdbeJumpHere(v, addrDelete); |
| 89331 | + } |
| 89332 | + |
| 89333 | + /* Unless this is a view, open cursors for the table we are |
| 89334 | + ** deleting from and all its indices. If this is a view, then the |
| 89335 | + ** only effect this statement has is to fire the INSTEAD OF |
| 89336 | + ** triggers. |
| 89337 | + */ |
| 89338 | + if( !isView ){ |
| 89339 | + sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen, |
| 89340 | + &iDataCur, &iIdxCur); |
| 89341 | + assert( pPk || iDataCur==iTabCur ); |
| 89342 | + assert( pPk || iIdxCur==iDataCur+1 ); |
| 89343 | + } |
| 89344 | + |
| 89345 | + /* Set up a loop over the rowids/primary-keys that were found in the |
| 89346 | + ** where-clause loop above. |
| 89347 | + */ |
| 89348 | + if( okOnePass ){ |
| 89349 | + /* Just one row. Hence the top-of-loop is a no-op */ |
| 89350 | + assert( nKey==nPk ); /* OP_Found will use an unpacked key */ |
| 89351 | + if( aToOpen[iDataCur-iTabCur] ){ |
| 89352 | + assert( pPk!=0 ); |
| 89353 | + sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey); |
| 89354 | + } |
| 89355 | + }else if( pPk ){ |
| 89356 | + addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); |
| 89357 | + sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey); |
| 89358 | + assert( nKey==0 ); /* OP_Found will use a composite key */ |
| 89359 | + }else{ |
| 89360 | + addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey); |
| 89361 | + assert( nKey==1 ); |
| 89362 | + } |
| 89363 | + |
| 89399 | 89364 | /* Delete the row */ |
| 89400 | 89365 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 89401 | 89366 | if( IsVirtual(pTab) ){ |
| 89402 | 89367 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 89403 | 89368 | sqlite3VtabMakeWritable(pParse, pTab); |
| 89404 | | - sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB); |
| 89369 | + sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB); |
| 89405 | 89370 | sqlite3VdbeChangeP5(v, OE_Abort); |
| 89406 | 89371 | sqlite3MayAbort(pParse); |
| 89407 | 89372 | }else |
| 89408 | 89373 | #endif |
| 89409 | 89374 | { |
| 89410 | 89375 | int count = (pParse->nested==0); /* True to count changes */ |
| 89411 | 89376 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
| 89412 | | - iRowid, 1, count, OE_Default, 0); |
| 89377 | + iKey, nKey, count, OE_Default, okOnePass); |
| 89413 | 89378 | } |
| 89414 | | - |
| 89415 | | - /* End of the delete loop */ |
| 89416 | | - sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); |
| 89417 | | - sqlite3VdbeResolveLabel(v, end); |
| 89418 | | - |
| 89379 | + |
| 89380 | + /* End of the loop over all rowids/primary-keys. */ |
| 89381 | + if( okOnePass ){ |
| 89382 | + sqlite3VdbeResolveLabel(v, addrBypass); |
| 89383 | + }else if( pPk ){ |
| 89384 | + sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); |
| 89385 | + sqlite3VdbeJumpHere(v, addrLoop); |
| 89386 | + }else{ |
| 89387 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop); |
| 89388 | + sqlite3VdbeJumpHere(v, addrLoop); |
| 89389 | + } |
| 89390 | + |
| 89419 | 89391 | /* Close the cursors open on the table and its indexes. */ |
| 89420 | 89392 | if( !isView && !IsVirtual(pTab) ){ |
| 89421 | | - sqlite3VdbeAddOp1(v, OP_Close, iDataCur); |
| 89393 | + if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur); |
| 89422 | 89394 | for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
| 89423 | 89395 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i); |
| 89424 | 89396 | } |
| 89425 | 89397 | } |
| 89426 | | - } |
| 89398 | + } /* End non-truncate path */ |
| 89427 | 89399 | |
| 89428 | 89400 | /* Update the sqlite_sequence table by storing the content of the |
| 89429 | 89401 | ** maximum rowid counter values recorded while inserting into |
| 89430 | 89402 | ** autoincrement tables. |
| 89431 | 89403 | */ |
| | @@ -89445,10 +89417,11 @@ |
| 89445 | 89417 | |
| 89446 | 89418 | delete_from_cleanup: |
| 89447 | 89419 | sqlite3AuthContextPop(&sContext); |
| 89448 | 89420 | sqlite3SrcListDelete(db, pTabList); |
| 89449 | 89421 | sqlite3ExprDelete(db, pWhere); |
| 89422 | + sqlite3DbFree(db, aToOpen); |
| 89450 | 89423 | return; |
| 89451 | 89424 | } |
| 89452 | 89425 | /* Make sure "isView" and other macros defined above are undefined. Otherwise |
| 89453 | 89426 | ** thely may interfere with compilation of other functions in this file |
| 89454 | 89427 | ** (or in another file, if this file becomes part of the amalgamation). */ |
| | @@ -89511,10 +89484,11 @@ |
| 89511 | 89484 | /* If there are any triggers to fire, allocate a range of registers to |
| 89512 | 89485 | ** use for the old.* references in the triggers. */ |
| 89513 | 89486 | if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){ |
| 89514 | 89487 | u32 mask; /* Mask of OLD.* columns in use */ |
| 89515 | 89488 | int iCol; /* Iterator used while populating OLD.* */ |
| 89489 | + int addrStart; /* Start of BEFORE trigger programs */ |
| 89516 | 89490 | |
| 89517 | 89491 | /* TODO: Could use temporary registers here. Also could attempt to |
| 89518 | 89492 | ** avoid copying the contents of the rowid register. */ |
| 89519 | 89493 | mask = sqlite3TriggerColmask( |
| 89520 | 89494 | pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf |
| | @@ -89531,19 +89505,23 @@ |
| 89531 | 89505 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1); |
| 89532 | 89506 | } |
| 89533 | 89507 | } |
| 89534 | 89508 | |
| 89535 | 89509 | /* Invoke BEFORE DELETE trigger programs. */ |
| 89510 | + addrStart = sqlite3VdbeCurrentAddr(v); |
| 89536 | 89511 | sqlite3CodeRowTrigger(pParse, pTrigger, |
| 89537 | 89512 | TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel |
| 89538 | 89513 | ); |
| 89539 | 89514 | |
| 89540 | | - /* Seek the cursor to the row to be deleted again. It may be that |
| 89541 | | - ** the BEFORE triggers coded above have already removed the row |
| 89542 | | - ** being deleted. Do not attempt to delete the row a second time, and |
| 89543 | | - ** do not fire AFTER triggers. */ |
| 89544 | | - sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk); |
| 89515 | + /* If any BEFORE triggers were coded, then seek the cursor to the |
| 89516 | + ** row to be deleted again. It may be that the BEFORE triggers moved |
| 89517 | + ** the cursor or of already deleted the row that the cursor was |
| 89518 | + ** pointing to. |
| 89519 | + */ |
| 89520 | + if( addrStart<sqlite3VdbeCurrentAddr(v) ){ |
| 89521 | + sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk); |
| 89522 | + } |
| 89545 | 89523 | |
| 89546 | 89524 | /* Do FK processing. This call checks that any FK constraints that |
| 89547 | 89525 | ** refer to this table (i.e. constraints attached to other tables) |
| 89548 | 89526 | ** are not violated by deleting this row. */ |
| 89549 | 89527 | sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0); |
| | @@ -91969,10 +91947,11 @@ |
| 91969 | 91947 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 91970 | 91948 | |
| 91971 | 91949 | assert( pIdx==0 || pIdx->pTable==pTab ); |
| 91972 | 91950 | assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol ); |
| 91973 | 91951 | assert( pIdx!=0 || pFKey->nCol==1 ); |
| 91952 | + assert( pIdx!=0 || HasRowid(pTab) ); |
| 91974 | 91953 | |
| 91975 | 91954 | if( nIncr<0 ){ |
| 91976 | 91955 | iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0); |
| 91977 | 91956 | } |
| 91978 | 91957 | |
| | @@ -92021,10 +92000,11 @@ |
| 92021 | 92000 | pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1); |
| 92022 | 92001 | pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0); |
| 92023 | 92002 | }else{ |
| 92024 | 92003 | Expr *pEq, *pAll = 0; |
| 92025 | 92004 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 92005 | + assert( pIdx!=0 ); |
| 92026 | 92006 | for(i=0; i<pPk->nKeyCol; i++){ |
| 92027 | 92007 | i16 iCol = pIdx->aiColumn[i]; |
| 92028 | 92008 | pLeft = exprTableRegister(pParse, pTab, regData, iCol); |
| 92029 | 92009 | pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol); |
| 92030 | 92010 | pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0); |
| | @@ -93599,11 +93579,11 @@ |
| 93599 | 93579 | } |
| 93600 | 93580 | |
| 93601 | 93581 | /* If this is not a view, open the table and and all indices */ |
| 93602 | 93582 | if( !isView ){ |
| 93603 | 93583 | int nIdx; |
| 93604 | | - nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, |
| 93584 | + nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0, |
| 93605 | 93585 | &iDataCur, &iIdxCur); |
| 93606 | 93586 | aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1)); |
| 93607 | 93587 | if( aRegIdx==0 ){ |
| 93608 | 93588 | goto insert_cleanup; |
| 93609 | 93589 | } |
| | @@ -94459,42 +94439,50 @@ |
| 94459 | 94439 | SQLITE_PRIVATE int sqlite3OpenTableAndIndices( |
| 94460 | 94440 | Parse *pParse, /* Parsing context */ |
| 94461 | 94441 | Table *pTab, /* Table to be opened */ |
| 94462 | 94442 | int op, /* OP_OpenRead or OP_OpenWrite */ |
| 94463 | 94443 | int iBase, /* Use this for the table cursor, if there is one */ |
| 94444 | + u8 *aToOpen, /* If not NULL: boolean for each table and index */ |
| 94464 | 94445 | int *piDataCur, /* Write the database source cursor number here */ |
| 94465 | 94446 | int *piIdxCur /* Write the first index cursor number here */ |
| 94466 | 94447 | ){ |
| 94467 | 94448 | int i; |
| 94468 | 94449 | int iDb; |
| 94450 | + int iDataCur; |
| 94469 | 94451 | Index *pIdx; |
| 94470 | 94452 | Vdbe *v; |
| 94471 | 94453 | |
| 94472 | 94454 | assert( op==OP_OpenRead || op==OP_OpenWrite ); |
| 94473 | 94455 | if( IsVirtual(pTab) ){ |
| 94456 | + assert( aToOpen==0 ); |
| 94474 | 94457 | *piDataCur = 0; |
| 94475 | 94458 | *piIdxCur = 1; |
| 94476 | 94459 | return 0; |
| 94477 | 94460 | } |
| 94478 | 94461 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 94479 | 94462 | v = sqlite3GetVdbe(pParse); |
| 94480 | 94463 | assert( v!=0 ); |
| 94481 | 94464 | if( iBase<0 ) iBase = pParse->nTab; |
| 94482 | | - if( HasRowid(pTab) ){ |
| 94483 | | - *piDataCur = iBase++; |
| 94484 | | - sqlite3OpenTable(pParse, *piDataCur, iDb, pTab, op); |
| 94465 | + iDataCur = iBase++; |
| 94466 | + if( piDataCur ) *piDataCur = iDataCur; |
| 94467 | + if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){ |
| 94468 | + sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op); |
| 94485 | 94469 | }else{ |
| 94486 | 94470 | sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName); |
| 94487 | 94471 | } |
| 94488 | | - *piIdxCur = iBase; |
| 94472 | + if( piIdxCur ) *piIdxCur = iBase; |
| 94489 | 94473 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 94490 | 94474 | int iIdxCur = iBase++; |
| 94491 | 94475 | assert( pIdx->pSchema==pTab->pSchema ); |
| 94492 | | - if( pIdx->autoIndex==2 && !HasRowid(pTab) ) *piDataCur = iIdxCur; |
| 94493 | | - sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); |
| 94494 | | - sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 94495 | | - VdbeComment((v, "%s", pIdx->zName)); |
| 94476 | + if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){ |
| 94477 | + *piDataCur = iIdxCur; |
| 94478 | + } |
| 94479 | + if( aToOpen==0 || aToOpen[i+1] ){ |
| 94480 | + sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); |
| 94481 | + sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 94482 | + VdbeComment((v, "%s", pIdx->zName)); |
| 94483 | + } |
| 94496 | 94484 | } |
| 94497 | 94485 | if( iBase>pParse->nTab ) pParse->nTab = iBase; |
| 94498 | 94486 | return i; |
| 94499 | 94487 | } |
| 94500 | 94488 | |
| | @@ -96664,10 +96652,14 @@ |
| 96664 | 96652 | /* iArg: */ SQLITE_VdbeAddopTrace }, |
| 96665 | 96653 | { /* zName: */ "vdbe_debug", |
| 96666 | 96654 | /* ePragTyp: */ PragTyp_FLAG, |
| 96667 | 96655 | /* ePragFlag: */ 0, |
| 96668 | 96656 | /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace }, |
| 96657 | + { /* zName: */ "vdbe_eqp", |
| 96658 | + /* ePragTyp: */ PragTyp_FLAG, |
| 96659 | + /* ePragFlag: */ 0, |
| 96660 | + /* iArg: */ SQLITE_VdbeEQP }, |
| 96669 | 96661 | { /* zName: */ "vdbe_listing", |
| 96670 | 96662 | /* ePragTyp: */ PragTyp_FLAG, |
| 96671 | 96663 | /* ePragFlag: */ 0, |
| 96672 | 96664 | /* iArg: */ SQLITE_VdbeListing }, |
| 96673 | 96665 | { /* zName: */ "vdbe_trace", |
| | @@ -96691,11 +96683,11 @@ |
| 96691 | 96683 | /* ePragTyp: */ PragTyp_FLAG, |
| 96692 | 96684 | /* ePragFlag: */ 0, |
| 96693 | 96685 | /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode }, |
| 96694 | 96686 | #endif |
| 96695 | 96687 | }; |
| 96696 | | -/* Number of pragmas: 56 on by default, 68 total. */ |
| 96688 | +/* Number of pragmas: 56 on by default, 69 total. */ |
| 96697 | 96689 | /* End of the automatically generated pragma table. |
| 96698 | 96690 | ***************************************************************************/ |
| 96699 | 96691 | |
| 96700 | 96692 | /* |
| 96701 | 96693 | ** Interpret the given string as a safety level. Return 0 for OFF, |
| | @@ -98117,11 +98109,11 @@ |
| 98117 | 98109 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */ |
| 98118 | 98110 | sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); |
| 98119 | 98111 | sqlite3VdbeJumpHere(v, addr); |
| 98120 | 98112 | sqlite3ExprCacheClear(pParse); |
| 98121 | 98113 | sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, |
| 98122 | | - 1, &iDataCur, &iIdxCur); |
| 98114 | + 1, 0, &iDataCur, &iIdxCur); |
| 98123 | 98115 | sqlite3VdbeAddOp2(v, OP_Integer, 0, 7); |
| 98124 | 98116 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 98125 | 98117 | sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */ |
| 98126 | 98118 | } |
| 98127 | 98119 | pParse->nMem = MAX(pParse->nMem, 8+j); |
| | @@ -99052,10 +99044,17 @@ |
| 99052 | 99044 | } |
| 99053 | 99045 | assert( i>=0 && i<db->nDb ); |
| 99054 | 99046 | } |
| 99055 | 99047 | return i; |
| 99056 | 99048 | } |
| 99049 | + |
| 99050 | +/* |
| 99051 | +** Free all memory allocations in the pParse object |
| 99052 | +*/ |
| 99053 | +SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){ |
| 99054 | + if( pParse ) sqlite3ExprListDelete(pParse->db, pParse->pConstExpr); |
| 99055 | +} |
| 99057 | 99056 | |
| 99058 | 99057 | /* |
| 99059 | 99058 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 99060 | 99059 | */ |
| 99061 | 99060 | static int sqlite3Prepare( |
| | @@ -99210,10 +99209,11 @@ |
| 99210 | 99209 | sqlite3DbFree(db, pT); |
| 99211 | 99210 | } |
| 99212 | 99211 | |
| 99213 | 99212 | end_prepare: |
| 99214 | 99213 | |
| 99214 | + sqlite3ParserReset(pParse); |
| 99215 | 99215 | sqlite3StackFree(db, pParse); |
| 99216 | 99216 | rc = sqlite3ApiExit(db, rc); |
| 99217 | 99217 | assert( (rc&db->errMask)==rc ); |
| 99218 | 99218 | return rc; |
| 99219 | 99219 | } |
| | @@ -101776,20 +101776,20 @@ |
| 101776 | 101776 | */ |
| 101777 | 101777 | if( op!=TK_ALL ){ |
| 101778 | 101778 | for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){ |
| 101779 | 101779 | struct ExprList_item *pItem; |
| 101780 | 101780 | for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){ |
| 101781 | | - assert( pItem->iOrderByCol>0 ); |
| 101782 | | - if( pItem->iOrderByCol==i ) break; |
| 101781 | + assert( pItem->u.x.iOrderByCol>0 ); |
| 101782 | + if( pItem->u.x.iOrderByCol==i ) break; |
| 101783 | 101783 | } |
| 101784 | 101784 | if( j==nOrderBy ){ |
| 101785 | 101785 | Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); |
| 101786 | 101786 | if( pNew==0 ) return SQLITE_NOMEM; |
| 101787 | 101787 | pNew->flags |= EP_IntValue; |
| 101788 | 101788 | pNew->u.iValue = i; |
| 101789 | 101789 | pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew); |
| 101790 | | - if( pOrderBy ) pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i; |
| 101790 | + if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i; |
| 101791 | 101791 | } |
| 101792 | 101792 | } |
| 101793 | 101793 | } |
| 101794 | 101794 | |
| 101795 | 101795 | /* Compute the comparison permutation and keyinfo that is used with |
| | @@ -101801,12 +101801,13 @@ |
| 101801 | 101801 | */ |
| 101802 | 101802 | aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy); |
| 101803 | 101803 | if( aPermute ){ |
| 101804 | 101804 | struct ExprList_item *pItem; |
| 101805 | 101805 | for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){ |
| 101806 | | - assert( pItem->iOrderByCol>0 && pItem->iOrderByCol<=p->pEList->nExpr ); |
| 101807 | | - aPermute[i] = pItem->iOrderByCol - 1; |
| 101806 | + assert( pItem->u.x.iOrderByCol>0 |
| 101807 | + && pItem->u.x.iOrderByCol<=p->pEList->nExpr ); |
| 101808 | + aPermute[i] = pItem->u.x.iOrderByCol - 1; |
| 101808 | 101809 | } |
| 101809 | 101810 | pKeyMerge = sqlite3KeyInfoAlloc(db, nOrderBy, 1); |
| 101810 | 101811 | if( pKeyMerge ){ |
| 101811 | 101812 | for(i=0; i<nOrderBy; i++){ |
| 101812 | 101813 | CollSeq *pColl; |
| | @@ -102382,11 +102383,11 @@ |
| 102382 | 102383 | |
| 102383 | 102384 | /* Restriction 18. */ |
| 102384 | 102385 | if( p->pOrderBy ){ |
| 102385 | 102386 | int ii; |
| 102386 | 102387 | for(ii=0; ii<p->pOrderBy->nExpr; ii++){ |
| 102387 | | - if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0; |
| 102388 | + if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0; |
| 102388 | 102389 | } |
| 102389 | 102390 | } |
| 102390 | 102391 | } |
| 102391 | 102392 | |
| 102392 | 102393 | /***** If we reach this point, flattening is permitted. *****/ |
| | @@ -103789,14 +103790,14 @@ |
| 103789 | 103790 | if( pGroupBy ){ |
| 103790 | 103791 | int k; /* Loop counter */ |
| 103791 | 103792 | struct ExprList_item *pItem; /* For looping over expression in a list */ |
| 103792 | 103793 | |
| 103793 | 103794 | for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){ |
| 103794 | | - pItem->iAlias = 0; |
| 103795 | + pItem->u.x.iAlias = 0; |
| 103795 | 103796 | } |
| 103796 | 103797 | for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ |
| 103797 | | - pItem->iAlias = 0; |
| 103798 | + pItem->u.x.iAlias = 0; |
| 103798 | 103799 | } |
| 103799 | 103800 | if( p->nSelectRow>100 ) p->nSelectRow = 100; |
| 103800 | 103801 | }else{ |
| 103801 | 103802 | p->nSelectRow = 1; |
| 103802 | 103803 | } |
| | @@ -105443,10 +105444,11 @@ |
| 105443 | 105444 | sqlite3VdbeDelete(v); |
| 105444 | 105445 | } |
| 105445 | 105446 | |
| 105446 | 105447 | assert( !pSubParse->pAinc && !pSubParse->pZombieTab ); |
| 105447 | 105448 | assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); |
| 105449 | + sqlite3ParserReset(pSubParse); |
| 105448 | 105450 | sqlite3StackFree(db, pSubParse); |
| 105449 | 105451 | |
| 105450 | 105452 | return pPrg; |
| 105451 | 105453 | } |
| 105452 | 105454 | |
| | @@ -105757,22 +105759,23 @@ |
| 105757 | 105759 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 105758 | 105760 | Vdbe *v; /* The virtual database engine */ |
| 105759 | 105761 | Index *pIdx; /* For looping over indices */ |
| 105760 | 105762 | Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */ |
| 105761 | 105763 | int nIdx; /* Number of indices that need updating */ |
| 105764 | + int iBaseCur; /* Base cursor number */ |
| 105762 | 105765 | int iDataCur; /* Cursor for the canonical data btree */ |
| 105763 | 105766 | int iIdxCur; /* Cursor for the first index */ |
| 105764 | 105767 | sqlite3 *db; /* The database structure */ |
| 105765 | 105768 | int *aRegIdx = 0; /* One register assigned to each index to be updated */ |
| 105766 | 105769 | int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the |
| 105767 | 105770 | ** an expression for the i-th column of the table. |
| 105768 | 105771 | ** aXRef[i]==-1 if the i-th column is not changed. */ |
| 105772 | + u8 *aToOpen; /* 1 for tables and indices to be opened */ |
| 105769 | 105773 | u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */ |
| 105770 | 105774 | u8 chngRowid; /* Rowid changed in a normal table */ |
| 105771 | 105775 | u8 chngKey; /* Either chngPk or chngRowid */ |
| 105772 | 105776 | Expr *pRowidExpr = 0; /* Expression defining the new record number */ |
| 105773 | | - int openAll = 0; /* True if all indices need to be opened */ |
| 105774 | 105777 | AuthContext sContext; /* The authorization context */ |
| 105775 | 105778 | NameContext sNC; /* The name-context to resolve expressions in */ |
| 105776 | 105779 | int iDb; /* Database containing the table being updated */ |
| 105777 | 105780 | int okOnePass; /* True for one-pass algorithm without the FIFO */ |
| 105778 | 105781 | int hasFK; /* True if foreign key processing is required */ |
| | @@ -105832,29 +105835,37 @@ |
| 105832 | 105835 | goto update_cleanup; |
| 105833 | 105836 | } |
| 105834 | 105837 | if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ |
| 105835 | 105838 | goto update_cleanup; |
| 105836 | 105839 | } |
| 105837 | | - aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol ); |
| 105838 | | - if( aXRef==0 ) goto update_cleanup; |
| 105839 | | - for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; |
| 105840 | 105840 | |
| 105841 | 105841 | /* Allocate a cursors for the main database table and for all indices. |
| 105842 | 105842 | ** The index cursors might not be used, but if they are used they |
| 105843 | 105843 | ** need to occur right after the database cursor. So go ahead and |
| 105844 | 105844 | ** allocate enough space, just in case. |
| 105845 | 105845 | */ |
| 105846 | | - pTabList->a[0].iCursor = iDataCur = pParse->nTab++; |
| 105846 | + pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++; |
| 105847 | 105847 | iIdxCur = iDataCur+1; |
| 105848 | 105848 | pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); |
| 105849 | 105849 | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ |
| 105850 | 105850 | if( pIdx->autoIndex==2 && pPk!=0 ){ |
| 105851 | 105851 | iDataCur = pParse->nTab; |
| 105852 | 105852 | pTabList->a[0].iCursor = iDataCur; |
| 105853 | 105853 | } |
| 105854 | 105854 | pParse->nTab++; |
| 105855 | 105855 | } |
| 105856 | + |
| 105857 | + /* Allocate space for aXRef[], aRegIdx[], and aToOpen[]. |
| 105858 | + ** Initialize aXRef[] and aToOpen[] to their default values. |
| 105859 | + */ |
| 105860 | + aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 ); |
| 105861 | + if( aXRef==0 ) goto update_cleanup; |
| 105862 | + aRegIdx = aXRef+pTab->nCol; |
| 105863 | + aToOpen = (u8*)(aRegIdx+nIdx); |
| 105864 | + memset(aToOpen, 1, nIdx+1); |
| 105865 | + aToOpen[nIdx+1] = 0; |
| 105866 | + for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; |
| 105856 | 105867 | |
| 105857 | 105868 | /* Initialize the name-context */ |
| 105858 | 105869 | memset(&sNC, 0, sizeof(sNC)); |
| 105859 | 105870 | sNC.pParse = pParse; |
| 105860 | 105871 | sNC.pSrcList = pTabList; |
| | @@ -105909,22 +105920,22 @@ |
| 105909 | 105920 | } |
| 105910 | 105921 | assert( (chngRowid & chngPk)==0 ); |
| 105911 | 105922 | assert( chngRowid==0 || chngRowid==1 ); |
| 105912 | 105923 | assert( chngPk==0 || chngPk==1 ); |
| 105913 | 105924 | chngKey = chngRowid + chngPk; |
| 105925 | + |
| 105926 | + /* The SET expressions are not actually used inside the WHERE loop. |
| 105927 | + ** So reset the colUsed mask |
| 105928 | + */ |
| 105929 | + pTabList->a[0].colUsed = 0; |
| 105914 | 105930 | |
| 105915 | 105931 | hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey); |
| 105916 | 105932 | |
| 105917 | | - /* Allocate memory for the array aRegIdx[]. There is one entry in the |
| 105918 | | - ** array for each index associated with table being updated. Fill in |
| 105919 | | - ** the value with a register number for indices that are to be used |
| 105920 | | - ** and with zero for unused indices. |
| 105933 | + /* There is one entry in the aRegIdx[] array for each index on the table |
| 105934 | + ** being updated. Fill in aRegIdx[] with a register number that will hold |
| 105935 | + ** the key for accessing each index. |
| 105921 | 105936 | */ |
| 105922 | | - if( nIdx>0 ){ |
| 105923 | | - aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx ); |
| 105924 | | - if( aRegIdx==0 ) goto update_cleanup; |
| 105925 | | - } |
| 105926 | 105937 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 105927 | 105938 | int reg; |
| 105928 | 105939 | if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){ |
| 105929 | 105940 | reg = ++pParse->nMem; |
| 105930 | 105941 | }else{ |
| | @@ -105934,10 +105945,11 @@ |
| 105934 | 105945 | reg = ++pParse->nMem; |
| 105935 | 105946 | break; |
| 105936 | 105947 | } |
| 105937 | 105948 | } |
| 105938 | 105949 | } |
| 105950 | + if( reg==0 ) aToOpen[j+1] = 0; |
| 105939 | 105951 | aRegIdx[j] = reg; |
| 105940 | 105952 | } |
| 105941 | 105953 | |
| 105942 | 105954 | /* Begin generating code. */ |
| 105943 | 105955 | v = sqlite3GetVdbe(pParse); |
| | @@ -106057,46 +106069,34 @@ |
| 106057 | 106069 | ** Open every index that needs updating. Note that if any |
| 106058 | 106070 | ** index could potentially invoke a REPLACE conflict resolution |
| 106059 | 106071 | ** action, then we need to open all indices because we might need |
| 106060 | 106072 | ** to be deleting some records. |
| 106061 | 106073 | */ |
| 106062 | | - if( !okOnePass && HasRowid(pTab) ){ |
| 106063 | | - sqlite3OpenTable(pParse, iDataCur, iDb, pTab, OP_OpenWrite); |
| 106064 | | - } |
| 106065 | | - sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); |
| 106066 | 106074 | if( onError==OE_Replace ){ |
| 106067 | | - openAll = 1; |
| 106075 | + memset(aToOpen, 1, nIdx+1); |
| 106068 | 106076 | }else{ |
| 106069 | | - openAll = 0; |
| 106070 | 106077 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 106071 | 106078 | if( pIdx->onError==OE_Replace ){ |
| 106072 | | - openAll = 1; |
| 106079 | + memset(aToOpen, 1, nIdx+1); |
| 106073 | 106080 | break; |
| 106074 | 106081 | } |
| 106075 | 106082 | } |
| 106076 | 106083 | } |
| 106077 | | - for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 106078 | | - int iThisCur = iIdxCur+i; |
| 106079 | | - assert( aRegIdx ); |
| 106080 | | - if( (openAll || aRegIdx[i]>0) |
| 106081 | | - && iThisCur!=aiCurOnePass[1] |
| 106082 | | - ){ |
| 106083 | | - assert( iThisCur!=aiCurOnePass[0] ); |
| 106084 | | - sqlite3VdbeAddOp3(v, OP_OpenWrite, iThisCur, pIdx->tnum, iDb); |
| 106085 | | - sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 106086 | | - assert( pParse->nTab>iThisCur ); |
| 106087 | | - VdbeComment((v, "%s", pIdx->zName)); |
| 106088 | | - if( okOnePass && pPk && iThisCur==iDataCur ){ |
| 106089 | | - sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, |
| 106090 | | - regKey, nKey); |
| 106091 | | - } |
| 106092 | | - } |
| 106093 | | - } |
| 106084 | + if( okOnePass ){ |
| 106085 | + if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0; |
| 106086 | + if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0; |
| 106087 | + } |
| 106088 | + sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen, |
| 106089 | + 0, 0); |
| 106094 | 106090 | } |
| 106095 | 106091 | |
| 106096 | 106092 | /* Top of the update loop */ |
| 106097 | 106093 | if( okOnePass ){ |
| 106094 | + if( aToOpen[iDataCur-iBaseCur] ){ |
| 106095 | + assert( pPk!=0 ); |
| 106096 | + sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey); |
| 106097 | + } |
| 106098 | 106098 | labelContinue = labelBreak; |
| 106099 | 106099 | sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak); |
| 106100 | 106100 | }else if( pPk ){ |
| 106101 | 106101 | labelContinue = sqlite3VdbeMakeLabel(v); |
| 106102 | 106102 | sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); |
| | @@ -106285,11 +106285,11 @@ |
| 106285 | 106285 | sqlite3VdbeResolveLabel(v, labelBreak); |
| 106286 | 106286 | |
| 106287 | 106287 | /* Close all tables */ |
| 106288 | 106288 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 106289 | 106289 | assert( aRegIdx ); |
| 106290 | | - if( openAll || aRegIdx[i]>0 ){ |
| 106290 | + if( aToOpen[i+1] ){ |
| 106291 | 106291 | sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0); |
| 106292 | 106292 | } |
| 106293 | 106293 | } |
| 106294 | 106294 | if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0); |
| 106295 | 106295 | |
| | @@ -106312,12 +106312,11 @@ |
| 106312 | 106312 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC); |
| 106313 | 106313 | } |
| 106314 | 106314 | |
| 106315 | 106315 | update_cleanup: |
| 106316 | 106316 | sqlite3AuthContextPop(&sContext); |
| 106317 | | - sqlite3DbFree(db, aRegIdx); |
| 106318 | | - sqlite3DbFree(db, aXRef); |
| 106317 | + sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */ |
| 106319 | 106318 | sqlite3SrcListDelete(db, pTabList); |
| 106320 | 106319 | sqlite3ExprListDelete(db, pChanges); |
| 106321 | 106320 | sqlite3ExprDelete(db, pWhere); |
| 106322 | 106321 | return; |
| 106323 | 106322 | } |
| | @@ -107536,10 +107535,11 @@ |
| 107536 | 107535 | |
| 107537 | 107536 | if( pParse->pVdbe ){ |
| 107538 | 107537 | sqlite3VdbeFinalize(pParse->pVdbe); |
| 107539 | 107538 | } |
| 107540 | 107539 | sqlite3DeleteTable(db, pParse->pNewTable); |
| 107540 | + sqlite3ParserReset(pParse); |
| 107541 | 107541 | sqlite3StackFree(db, pParse); |
| 107542 | 107542 | } |
| 107543 | 107543 | |
| 107544 | 107544 | assert( (rc&0xff)==rc ); |
| 107545 | 107545 | rc = sqlite3ApiExit(db, rc); |
| | @@ -107913,11 +107913,28 @@ |
| 107913 | 107913 | ** generating the code that loops through a table looking for applicable |
| 107914 | 107914 | ** rows. Indices are selected and used to speed the search when doing |
| 107915 | 107915 | ** so is applicable. Because this module is responsible for selecting |
| 107916 | 107916 | ** indices, you might also think of this module as the "query optimizer". |
| 107917 | 107917 | */ |
| 107918 | | - |
| 107918 | +/************** Include whereInt.h in the middle of where.c ******************/ |
| 107919 | +/************** Begin file whereInt.h ****************************************/ |
| 107920 | +/* |
| 107921 | +** 2013-11-12 |
| 107922 | +** |
| 107923 | +** The author disclaims copyright to this source code. In place of |
| 107924 | +** a legal notice, here is a blessing: |
| 107925 | +** |
| 107926 | +** May you do good and not evil. |
| 107927 | +** May you find forgiveness for yourself and forgive others. |
| 107928 | +** May you share freely, never taking more than you give. |
| 107929 | +** |
| 107930 | +************************************************************************* |
| 107931 | +** |
| 107932 | +** This file contains structure and macro definitions for the query |
| 107933 | +** planner logic in "where.c". These definitions are broken out into |
| 107934 | +** a separate source file for easier editing. |
| 107935 | +*/ |
| 107919 | 107936 | |
| 107920 | 107937 | /* |
| 107921 | 107938 | ** Trace output macros |
| 107922 | 107939 | */ |
| 107923 | 107940 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| | @@ -107965,10 +107982,11 @@ |
| 107965 | 107982 | int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 107966 | 107983 | int iTabCur; /* The VDBE cursor used to access the table */ |
| 107967 | 107984 | int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 107968 | 107985 | int addrBrk; /* Jump here to break out of the loop */ |
| 107969 | 107986 | int addrNxt; /* Jump here to start the next IN combination */ |
| 107987 | + int addrSkip; /* Jump here for next iteration of skip-scan */ |
| 107970 | 107988 | int addrCont; /* Jump here to continue with the next loop cycle */ |
| 107971 | 107989 | int addrFirst; /* First instruction of interior of the loop */ |
| 107972 | 107990 | int addrBody; /* Beginning of the body of this loop */ |
| 107973 | 107991 | u8 iFrom; /* Which entry in the FROM clause */ |
| 107974 | 107992 | u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */ |
| | @@ -108014,11 +108032,11 @@ |
| 108014 | 108032 | LogEst rRun; /* Cost of running each loop */ |
| 108015 | 108033 | LogEst nOut; /* Estimated number of output rows */ |
| 108016 | 108034 | union { |
| 108017 | 108035 | struct { /* Information for internal btree tables */ |
| 108018 | 108036 | u16 nEq; /* Number of equality constraints */ |
| 108019 | | - u16 nSkip; /* Number of initial index columns skipped */ |
| 108037 | + u16 nSkip; /* Number of initial index columns to skip */ |
| 108020 | 108038 | Index *pIndex; /* Index used, or NULL */ |
| 108021 | 108039 | } btree; |
| 108022 | 108040 | struct { /* Information for virtual tables */ |
| 108023 | 108041 | int idxNum; /* Index number */ |
| 108024 | 108042 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| | @@ -108355,10 +108373,14 @@ |
| 108355 | 108373 | #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 108356 | 108374 | #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
| 108357 | 108375 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
| 108358 | 108376 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 108359 | 108377 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 108378 | +#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
| 108379 | + |
| 108380 | +/************** End of whereInt.h ********************************************/ |
| 108381 | +/************** Continuing where we left off in where.c **********************/ |
| 108360 | 108382 | |
| 108361 | 108383 | /* |
| 108362 | 108384 | ** Return the estimated number of output rows from a WHERE clause |
| 108363 | 108385 | */ |
| 108364 | 108386 | SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| | @@ -109006,13 +109028,10 @@ |
| 109006 | 109028 | } |
| 109007 | 109029 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
| 109008 | 109030 | |
| 109009 | 109031 | pRight = pList->a[0].pExpr; |
| 109010 | 109032 | op = pRight->op; |
| 109011 | | - if( op==TK_REGISTER ){ |
| 109012 | | - op = pRight->op2; |
| 109013 | | - } |
| 109014 | 109033 | if( op==TK_VARIABLE ){ |
| 109015 | 109034 | Vdbe *pReprepare = pParse->pReprepare; |
| 109016 | 109035 | int iCol = pRight->iColumn; |
| 109017 | 109036 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE); |
| 109018 | 109037 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| | @@ -110759,11 +110778,11 @@ |
| 110759 | 110778 | return iReg; |
| 110760 | 110779 | } |
| 110761 | 110780 | |
| 110762 | 110781 | /* |
| 110763 | 110782 | ** Generate code that will evaluate all == and IN constraints for an |
| 110764 | | -** index. |
| 110783 | +** index scan. |
| 110765 | 110784 | ** |
| 110766 | 110785 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 110767 | 110786 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 110768 | 110787 | ** The index has as many as three equality constraints, but in this |
| 110769 | 110788 | ** example, the third "c" value is an inequality. So only two |
| | @@ -110774,13 +110793,19 @@ |
| 110774 | 110793 | ** In the example above nEq==2. But this subroutine works for any value |
| 110775 | 110794 | ** of nEq including 0. If nEq==0, this routine is nearly a no-op. |
| 110776 | 110795 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 110777 | 110796 | ** compute the affinity string. |
| 110778 | 110797 | ** |
| 110779 | | -** This routine always allocates at least one memory cell and returns |
| 110780 | | -** the index of that memory cell. The code that |
| 110781 | | -** calls this routine will use that memory cell to store the termination |
| 110798 | +** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints |
| 110799 | +** are == or IN and are covered by the nEq. nExtraReg is 1 if there is |
| 110800 | +** an inequality constraint (such as the "c>=5 AND c<10" in the example) that |
| 110801 | +** occurs after the nEq quality constraints. |
| 110802 | +** |
| 110803 | +** This routine allocates a range of nEq+nExtraReg memory cells and returns |
| 110804 | +** the index of the first memory cell in that range. The code that |
| 110805 | +** calls this routine will use that memory range to store keys for |
| 110806 | +** start and termination conditions of the loop. |
| 110782 | 110807 | ** key value of the loop. If one or more IN operators appear, then |
| 110783 | 110808 | ** this routine allocates an additional nEq memory cells for internal |
| 110784 | 110809 | ** use. |
| 110785 | 110810 | ** |
| 110786 | 110811 | ** Before returning, *pzAff is set to point to a buffer containing a |
| | @@ -110803,11 +110828,12 @@ |
| 110803 | 110828 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 110804 | 110829 | int bRev, /* Reverse the order of IN operators */ |
| 110805 | 110830 | int nExtraReg, /* Number of extra registers to allocate */ |
| 110806 | 110831 | char **pzAff /* OUT: Set to point to affinity string */ |
| 110807 | 110832 | ){ |
| 110808 | | - int nEq; /* The number of == or IN constraints to code */ |
| 110833 | + u16 nEq; /* The number of == or IN constraints to code */ |
| 110834 | + u16 nSkip; /* Number of left-most columns to skip */ |
| 110809 | 110835 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 110810 | 110836 | Index *pIdx; /* The index being used for this loop */ |
| 110811 | 110837 | WhereTerm *pTerm; /* A single constraint term */ |
| 110812 | 110838 | WhereLoop *pLoop; /* The WhereLoop object */ |
| 110813 | 110839 | int j; /* Loop counter */ |
| | @@ -110817,10 +110843,11 @@ |
| 110817 | 110843 | |
| 110818 | 110844 | /* This module is only called on query plans that use an index. */ |
| 110819 | 110845 | pLoop = pLevel->pWLoop; |
| 110820 | 110846 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 110821 | 110847 | nEq = pLoop->u.btree.nEq; |
| 110848 | + nSkip = pLoop->u.btree.nSkip; |
| 110822 | 110849 | pIdx = pLoop->u.btree.pIndex; |
| 110823 | 110850 | assert( pIdx!=0 ); |
| 110824 | 110851 | |
| 110825 | 110852 | /* Figure out how many memory cells we will need then allocate them. |
| 110826 | 110853 | */ |
| | @@ -110830,19 +110857,34 @@ |
| 110830 | 110857 | |
| 110831 | 110858 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 110832 | 110859 | if( !zAff ){ |
| 110833 | 110860 | pParse->db->mallocFailed = 1; |
| 110834 | 110861 | } |
| 110862 | + |
| 110863 | + if( nSkip ){ |
| 110864 | + int iIdxCur = pLevel->iIdxCur; |
| 110865 | + sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); |
| 110866 | + VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); |
| 110867 | + j = sqlite3VdbeAddOp0(v, OP_Goto); |
| 110868 | + pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLt:OP_SeekGt), |
| 110869 | + iIdxCur, 0, regBase, nSkip); |
| 110870 | + sqlite3VdbeJumpHere(v, j); |
| 110871 | + for(j=0; j<nSkip; j++){ |
| 110872 | + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j); |
| 110873 | + assert( pIdx->aiColumn[j]>=0 ); |
| 110874 | + VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName)); |
| 110875 | + } |
| 110876 | + } |
| 110835 | 110877 | |
| 110836 | 110878 | /* Evaluate the equality constraints |
| 110837 | 110879 | */ |
| 110838 | 110880 | assert( zAff==0 || (int)strlen(zAff)>=nEq ); |
| 110839 | | - for(j=0; j<nEq; j++){ |
| 110881 | + for(j=nSkip; j<nEq; j++){ |
| 110840 | 110882 | int r1; |
| 110841 | 110883 | pTerm = pLoop->aLTerm[j]; |
| 110842 | 110884 | assert( pTerm!=0 ); |
| 110843 | | - /* The following true for indices with redundant columns. |
| 110885 | + /* The following testcase is true for indices with redundant columns. |
| 110844 | 110886 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 110845 | 110887 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
| 110846 | 110888 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 110847 | 110889 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
| 110848 | 110890 | if( r1!=regBase+j ){ |
| | @@ -110912,11 +110954,12 @@ |
| 110912 | 110954 | ** It is the responsibility of the caller to free the buffer when it is |
| 110913 | 110955 | ** no longer required. |
| 110914 | 110956 | */ |
| 110915 | 110957 | static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 110916 | 110958 | Index *pIndex = pLoop->u.btree.pIndex; |
| 110917 | | - int nEq = pLoop->u.btree.nEq; |
| 110959 | + u16 nEq = pLoop->u.btree.nEq; |
| 110960 | + u16 nSkip = pLoop->u.btree.nSkip; |
| 110918 | 110961 | int i, j; |
| 110919 | 110962 | Column *aCol = pTab->aCol; |
| 110920 | 110963 | i16 *aiColumn = pIndex->aiColumn; |
| 110921 | 110964 | StrAccum txt; |
| 110922 | 110965 | |
| | @@ -110926,11 +110969,18 @@ |
| 110926 | 110969 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
| 110927 | 110970 | txt.db = db; |
| 110928 | 110971 | sqlite3StrAccumAppend(&txt, " (", 2); |
| 110929 | 110972 | for(i=0; i<nEq; i++){ |
| 110930 | 110973 | char *z = (i==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[i]].zName; |
| 110931 | | - explainAppendTerm(&txt, i, z, "="); |
| 110974 | + if( i>=nSkip ){ |
| 110975 | + explainAppendTerm(&txt, i, z, "="); |
| 110976 | + }else{ |
| 110977 | + if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5); |
| 110978 | + sqlite3StrAccumAppend(&txt, "ANY(", 4); |
| 110979 | + sqlite3StrAccumAppend(&txt, z, -1); |
| 110980 | + sqlite3StrAccumAppend(&txt, ")", 1); |
| 110981 | + } |
| 110932 | 110982 | } |
| 110933 | 110983 | |
| 110934 | 110984 | j = i; |
| 110935 | 110985 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
| 110936 | 110986 | char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName; |
| | @@ -110956,11 +111006,14 @@ |
| 110956 | 111006 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 110957 | 111007 | int iLevel, /* Value for "level" column of output */ |
| 110958 | 111008 | int iFrom, /* Value for "from" column of output */ |
| 110959 | 111009 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 110960 | 111010 | ){ |
| 110961 | | - if( pParse->explain==2 ){ |
| 111011 | +#ifndef SQLITE_DEBUG |
| 111012 | + if( pParse->explain==2 ) |
| 111013 | +#endif |
| 111014 | + { |
| 110962 | 111015 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 110963 | 111016 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 110964 | 111017 | sqlite3 *db = pParse->db; /* Database handle */ |
| 110965 | 111018 | char *zMsg; /* Text to add to EQP output */ |
| 110966 | 111019 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| | @@ -111062,11 +111115,11 @@ |
| 111062 | 111115 | iCur = pTabItem->iCursor; |
| 111063 | 111116 | pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
| 111064 | 111117 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 111065 | 111118 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
| 111066 | 111119 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
| 111067 | | - VdbeNoopComment((v, "Begin WHERE-Loop %d: %s", iLevel,pTabItem->pTab->zName)); |
| 111120 | + VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); |
| 111068 | 111121 | |
| 111069 | 111122 | /* Create labels for the "break" and "continue" instructions |
| 111070 | 111123 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 111071 | 111124 | ** Jump to cont to go immediately to the next iteration of the |
| 111072 | 111125 | ** loop. |
| | @@ -111289,12 +111342,12 @@ |
| 111289 | 111342 | static const u8 aEndOp[] = { |
| 111290 | 111343 | OP_Noop, /* 0: (!end_constraints) */ |
| 111291 | 111344 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 111292 | 111345 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 111293 | 111346 | }; |
| 111294 | | - int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 111295 | | - int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
| 111347 | + u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 111348 | + int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
| 111296 | 111349 | int regBase; /* Base register holding constraint values */ |
| 111297 | 111350 | int r1; /* Temp register */ |
| 111298 | 111351 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 111299 | 111352 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 111300 | 111353 | int startEq; /* True if range start uses ==, >= or <= */ |
| | @@ -111304,14 +111357,15 @@ |
| 111304 | 111357 | Index *pIdx; /* The index we will be using */ |
| 111305 | 111358 | int iIdxCur; /* The VDBE cursor for the index */ |
| 111306 | 111359 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 111307 | 111360 | int op; /* Instruction opcode */ |
| 111308 | 111361 | char *zStartAff; /* Affinity for start of range constraint */ |
| 111309 | | - char *zEndAff; /* Affinity for end of range constraint */ |
| 111362 | + char cEndAff = 0; /* Affinity for end of range constraint */ |
| 111310 | 111363 | |
| 111311 | 111364 | pIdx = pLoop->u.btree.pIndex; |
| 111312 | 111365 | iIdxCur = pLevel->iIdxCur; |
| 111366 | + assert( nEq>=pLoop->u.btree.nSkip ); |
| 111313 | 111367 | |
| 111314 | 111368 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 111315 | 111369 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 111316 | 111370 | ** query, then the caller will only allow the loop to run for |
| 111317 | 111371 | ** a single iteration. This means that the first row returned |
| | @@ -111321,12 +111375,11 @@ |
| 111321 | 111375 | */ |
| 111322 | 111376 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 111323 | 111377 | && (pWInfo->bOBSat!=0) |
| 111324 | 111378 | && (pIdx->nKeyCol>nEq) |
| 111325 | 111379 | ){ |
| 111326 | | - /* assert( pOrderBy->nExpr==1 ); */ |
| 111327 | | - /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 111380 | + assert( pLoop->u.btree.nSkip==0 ); |
| 111328 | 111381 | isMinQuery = 1; |
| 111329 | 111382 | nExtraReg = 1; |
| 111330 | 111383 | } |
| 111331 | 111384 | |
| 111332 | 111385 | /* Find any inequality constraint terms for the start and end |
| | @@ -111345,11 +111398,12 @@ |
| 111345 | 111398 | /* Generate code to evaluate all constraint terms using == or IN |
| 111346 | 111399 | ** and store the values of those terms in an array of registers |
| 111347 | 111400 | ** starting at regBase. |
| 111348 | 111401 | */ |
| 111349 | 111402 | regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
| 111350 | | - zEndAff = sqlite3DbStrDup(db, zStartAff); |
| 111403 | + assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); |
| 111404 | + if( zStartAff ) cEndAff = zStartAff[nEq]; |
| 111351 | 111405 | addrNxt = pLevel->addrNxt; |
| 111352 | 111406 | |
| 111353 | 111407 | /* If we are doing a reverse order scan on an ascending index, or |
| 111354 | 111408 | ** a forward order scan on a descending index, interchange the |
| 111355 | 111409 | ** start and end terms (pRangeStart and pRangeEnd). |
| | @@ -111415,27 +111469,19 @@ |
| 111415 | 111469 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
| 111416 | 111470 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
| 111417 | 111471 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){ |
| 111418 | 111472 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 111419 | 111473 | } |
| 111420 | | - if( zEndAff ){ |
| 111421 | | - if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){ |
| 111422 | | - /* Since the comparison is to be performed with no conversions |
| 111423 | | - ** applied to the operands, set the affinity to apply to pRight to |
| 111424 | | - ** SQLITE_AFF_NONE. */ |
| 111425 | | - zEndAff[nEq] = SQLITE_AFF_NONE; |
| 111426 | | - } |
| 111427 | | - if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){ |
| 111428 | | - zEndAff[nEq] = SQLITE_AFF_NONE; |
| 111429 | | - } |
| 111430 | | - } |
| 111431 | | - codeApplyAffinity(pParse, regBase, nEq+1, zEndAff); |
| 111474 | + if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE |
| 111475 | + && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff) |
| 111476 | + ){ |
| 111477 | + codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff); |
| 111478 | + } |
| 111432 | 111479 | nConstraint++; |
| 111433 | 111480 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
| 111434 | 111481 | } |
| 111435 | 111482 | sqlite3DbFree(db, zStartAff); |
| 111436 | | - sqlite3DbFree(db, zEndAff); |
| 111437 | 111483 | |
| 111438 | 111484 | /* Top of the loop body */ |
| 111439 | 111485 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 111440 | 111486 | |
| 111441 | 111487 | /* Check if the index cursor is past the end of the range. */ |
| | @@ -111453,12 +111499,17 @@ |
| 111453 | 111499 | ** If it is, jump to the next iteration of the loop. |
| 111454 | 111500 | */ |
| 111455 | 111501 | r1 = sqlite3GetTempReg(pParse); |
| 111456 | 111502 | testcase( pLoop->wsFlags & WHERE_BTM_LIMIT ); |
| 111457 | 111503 | testcase( pLoop->wsFlags & WHERE_TOP_LIMIT ); |
| 111458 | | - if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ |
| 111504 | + if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 111505 | + && (j = pIdx->aiColumn[nEq])>=0 |
| 111506 | + && pIdx->pTable->aCol[j].notNull==0 |
| 111507 | + && (nEq || (pLoop->wsFlags & WHERE_BTM_LIMIT)==0) |
| 111508 | + ){ |
| 111459 | 111509 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 111510 | + VdbeComment((v, "%s", pIdx->pTable->aCol[j].zName)); |
| 111460 | 111511 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 111461 | 111512 | } |
| 111462 | 111513 | sqlite3ReleaseTempReg(pParse, r1); |
| 111463 | 111514 | |
| 111464 | 111515 | /* Seek the table cursor, if required */ |
| | @@ -111769,11 +111820,11 @@ |
| 111769 | 111820 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 111770 | 111821 | if( pAlt==0 ) continue; |
| 111771 | 111822 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
| 111772 | 111823 | testcase( pAlt->eOperator & WO_EQ ); |
| 111773 | 111824 | testcase( pAlt->eOperator & WO_IN ); |
| 111774 | | - VdbeNoopComment((v, "begin transitive constraint")); |
| 111825 | + VdbeModuleComment((v, "begin transitive constraint")); |
| 111775 | 111826 | pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 111776 | 111827 | if( pEAlt ){ |
| 111777 | 111828 | *pEAlt = *pAlt->pExpr; |
| 111778 | 111829 | pEAlt->pLeft = pE->pLeft; |
| 111779 | 111830 | sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); |
| | @@ -111842,16 +111893,11 @@ |
| 111842 | 111893 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 111843 | 111894 | int i = sqlite3Strlen30(zName) - 1; |
| 111844 | 111895 | while( zName[i]!='_' ) i--; |
| 111845 | 111896 | zName += i; |
| 111846 | 111897 | } |
| 111847 | | - if( p->u.btree.nSkip ){ |
| 111848 | | - sqlite3DebugPrintf(".%-15s %d+%d", zName, |
| 111849 | | - p->u.btree.nSkip, p->u.btree.nEq); |
| 111850 | | - }else{ |
| 111851 | | - sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
| 111852 | | - } |
| 111898 | + sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
| 111853 | 111899 | }else{ |
| 111854 | 111900 | sqlite3DebugPrintf("%20s",""); |
| 111855 | 111901 | } |
| 111856 | 111902 | }else{ |
| 111857 | 111903 | char *z; |
| | @@ -111874,10 +111920,11 @@ |
| 111874 | 111920 | int i; |
| 111875 | 111921 | Vdbe *v = pWInfo->pParse->pVdbe; |
| 111876 | 111922 | sqlite3ExplainBegin(v); |
| 111877 | 111923 | for(i=0; i<p->nLTerm; i++){ |
| 111878 | 111924 | WhereTerm *pTerm = p->aLTerm[i]; |
| 111925 | + if( pTerm==0 ) continue; |
| 111879 | 111926 | sqlite3ExplainPrintf(v, " (%d) #%-2d ", i+1, (int)(pTerm-pWC->a)); |
| 111880 | 111927 | sqlite3ExplainPush(v); |
| 111881 | 111928 | whereExplainTerm(v, pTerm); |
| 111882 | 111929 | sqlite3ExplainPop(v); |
| 111883 | 111930 | sqlite3ExplainNL(v); |
| | @@ -112157,10 +112204,11 @@ |
| 112157 | 112204 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
| 112158 | 112205 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 112159 | 112206 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
| 112160 | 112207 | for(j=pLoop->nLTerm-1; j>=0; j--){ |
| 112161 | 112208 | pX = pLoop->aLTerm[j]; |
| 112209 | + if( pX==0 ) continue; |
| 112162 | 112210 | if( pX==pTerm ) break; |
| 112163 | 112211 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 112164 | 112212 | } |
| 112165 | 112213 | if( j<0 ) pLoop->nOut += pTerm->truthProb; |
| 112166 | 112214 | } |
| | @@ -112186,11 +112234,12 @@ |
| 112186 | 112234 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
| 112187 | 112235 | int opMask; /* Valid operators for constraints */ |
| 112188 | 112236 | WhereScan scan; /* Iterator for WHERE terms */ |
| 112189 | 112237 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 112190 | 112238 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 112191 | | - int saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 112239 | + u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 112240 | + u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */ |
| 112192 | 112241 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 112193 | 112242 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
| 112194 | 112243 | int iCol; /* Index of the column in the table */ |
| 112195 | 112244 | int rc = SQLITE_OK; /* Return code */ |
| 112196 | 112245 | LogEst nRowEst; /* Estimated index selectivity */ |
| | @@ -112221,16 +112270,35 @@ |
| 112221 | 112270 | nRowEst = 0; |
| 112222 | 112271 | } |
| 112223 | 112272 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
| 112224 | 112273 | opMask, pProbe); |
| 112225 | 112274 | saved_nEq = pNew->u.btree.nEq; |
| 112275 | + saved_nSkip = pNew->u.btree.nSkip; |
| 112226 | 112276 | saved_nLTerm = pNew->nLTerm; |
| 112227 | 112277 | saved_wsFlags = pNew->wsFlags; |
| 112228 | 112278 | saved_prereq = pNew->prereq; |
| 112229 | 112279 | saved_nOut = pNew->nOut; |
| 112230 | 112280 | pNew->rSetup = 0; |
| 112231 | 112281 | rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0])); |
| 112282 | + |
| 112283 | + /* Consider using a skip-scan if there are no WHERE clause constraints |
| 112284 | + ** available for the left-most terms of the index, and if the average |
| 112285 | + ** number of repeats in the left-most terms is at least 50. |
| 112286 | + */ |
| 112287 | + if( pTerm==0 |
| 112288 | + && saved_nEq==saved_nSkip |
| 112289 | + && saved_nEq+1<pProbe->nKeyCol |
| 112290 | + && pProbe->aiRowEst[saved_nEq+1]>50 /* TUNING: Minimum for skip-scan */ |
| 112291 | + ){ |
| 112292 | + LogEst nIter; |
| 112293 | + pNew->u.btree.nEq++; |
| 112294 | + pNew->u.btree.nSkip++; |
| 112295 | + pNew->aLTerm[pNew->nLTerm++] = 0; |
| 112296 | + pNew->wsFlags |= WHERE_SKIPSCAN; |
| 112297 | + nIter = sqlite3LogEst(pProbe->aiRowEst[0]/pProbe->aiRowEst[saved_nEq+1]); |
| 112298 | + whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter); |
| 112299 | + } |
| 112232 | 112300 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
| 112233 | 112301 | int nIn = 0; |
| 112234 | 112302 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 112235 | 112303 | int nRecValid = pBuilder->nRecValid; |
| 112236 | 112304 | #endif |
| | @@ -112262,12 +112330,14 @@ |
| 112262 | 112330 | } |
| 112263 | 112331 | pNew->rRun += nIn; |
| 112264 | 112332 | pNew->u.btree.nEq++; |
| 112265 | 112333 | pNew->nOut = nRowEst + nInMul + nIn; |
| 112266 | 112334 | }else if( pTerm->eOperator & (WO_EQ) ){ |
| 112267 | | - assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 |
| 112268 | | - || nInMul==0 ); |
| 112335 | + assert( |
| 112336 | + (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN|WHERE_SKIPSCAN))!=0 |
| 112337 | + || nInMul==0 |
| 112338 | + ); |
| 112269 | 112339 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
| 112270 | 112340 | if( iCol<0 |
| 112271 | 112341 | || (pProbe->onError!=OE_None && nInMul==0 |
| 112272 | 112342 | && pNew->u.btree.nEq==pProbe->nKeyCol-1) |
| 112273 | 112343 | ){ |
| | @@ -112344,10 +112414,11 @@ |
| 112344 | 112414 | pBuilder->nRecValid = nRecValid; |
| 112345 | 112415 | #endif |
| 112346 | 112416 | } |
| 112347 | 112417 | pNew->prereq = saved_prereq; |
| 112348 | 112418 | pNew->u.btree.nEq = saved_nEq; |
| 112419 | + pNew->u.btree.nSkip = saved_nSkip; |
| 112349 | 112420 | pNew->wsFlags = saved_wsFlags; |
| 112350 | 112421 | pNew->nOut = saved_nOut; |
| 112351 | 112422 | pNew->nLTerm = saved_nLTerm; |
| 112352 | 112423 | return rc; |
| 112353 | 112424 | } |
| | @@ -112490,10 +112561,11 @@ |
| 112490 | 112561 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 112491 | 112562 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
| 112492 | 112563 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 112493 | 112564 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 112494 | 112565 | pNew->u.btree.nEq = 1; |
| 112566 | + pNew->u.btree.nSkip = 0; |
| 112495 | 112567 | pNew->u.btree.pIndex = 0; |
| 112496 | 112568 | pNew->nLTerm = 1; |
| 112497 | 112569 | pNew->aLTerm[0] = pTerm; |
| 112498 | 112570 | /* TUNING: One-time cost for computing the automatic index is |
| 112499 | 112571 | ** approximately 7*N*log2(N) where N is the number of rows in |
| | @@ -112519,10 +112591,11 @@ |
| 112519 | 112591 | if( pProbe->pPartIdxWhere!=0 |
| 112520 | 112592 | && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){ |
| 112521 | 112593 | continue; /* Partial index inappropriate for this query */ |
| 112522 | 112594 | } |
| 112523 | 112595 | pNew->u.btree.nEq = 0; |
| 112596 | + pNew->u.btree.nSkip = 0; |
| 112524 | 112597 | pNew->nLTerm = 0; |
| 112525 | 112598 | pNew->iSortIdx = 0; |
| 112526 | 112599 | pNew->rSetup = 0; |
| 112527 | 112600 | pNew->prereq = mExtra; |
| 112528 | 112601 | pNew->nOut = rSize; |
| | @@ -112604,11 +112677,12 @@ |
| 112604 | 112677 | /* |
| 112605 | 112678 | ** Add all WhereLoop objects for a table of the join identified by |
| 112606 | 112679 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
| 112607 | 112680 | */ |
| 112608 | 112681 | static int whereLoopAddVirtual( |
| 112609 | | - WhereLoopBuilder *pBuilder /* WHERE clause information */ |
| 112682 | + WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 112683 | + Bitmask mExtra |
| 112610 | 112684 | ){ |
| 112611 | 112685 | WhereInfo *pWInfo; /* WHERE analysis context */ |
| 112612 | 112686 | Parse *pParse; /* The parsing context */ |
| 112613 | 112687 | WhereClause *pWC; /* The WHERE clause */ |
| 112614 | 112688 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| | @@ -112694,11 +112768,11 @@ |
| 112694 | 112768 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
| 112695 | 112769 | pIdxInfo->estimatedRows = 25; |
| 112696 | 112770 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 112697 | 112771 | if( rc ) goto whereLoopAddVtab_exit; |
| 112698 | 112772 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 112699 | | - pNew->prereq = 0; |
| 112773 | + pNew->prereq = mExtra; |
| 112700 | 112774 | mxTerm = -1; |
| 112701 | 112775 | assert( pNew->nLSlot>=nConstraint ); |
| 112702 | 112776 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
| 112703 | 112777 | pNew->u.vtab.omitMask = 0; |
| 112704 | 112778 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
| | @@ -112821,12 +112895,11 @@ |
| 112821 | 112895 | continue; |
| 112822 | 112896 | } |
| 112823 | 112897 | sCur.n = 0; |
| 112824 | 112898 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 112825 | 112899 | if( IsVirtual(pItem->pTab) ){ |
| 112826 | | - rc = whereLoopAddVirtual(&sSubBuild); |
| 112827 | | - for(i=0; i<sCur.n; i++) sCur.a[i].prereq |= mExtra; |
| 112900 | + rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
| 112828 | 112901 | }else |
| 112829 | 112902 | #endif |
| 112830 | 112903 | { |
| 112831 | 112904 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 112832 | 112905 | } |
| | @@ -112892,11 +112965,11 @@ |
| 112892 | 112965 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
| 112893 | 112966 | mExtra = mPrior; |
| 112894 | 112967 | } |
| 112895 | 112968 | priorJoinType = pItem->jointype; |
| 112896 | 112969 | if( IsVirtual(pItem->pTab) ){ |
| 112897 | | - rc = whereLoopAddVirtual(pBuilder); |
| 112970 | + rc = whereLoopAddVirtual(pBuilder, mExtra); |
| 112898 | 112971 | }else{ |
| 112899 | 112972 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 112900 | 112973 | } |
| 112901 | 112974 | if( rc==SQLITE_OK ){ |
| 112902 | 112975 | rc = whereLoopAddOr(pBuilder, mExtra); |
| | @@ -113053,10 +113126,11 @@ |
| 113053 | 113126 | for(j=0; j<nColumn; j++){ |
| 113054 | 113127 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 113055 | 113128 | |
| 113056 | 113129 | /* Skip over == and IS NULL terms */ |
| 113057 | 113130 | if( j<pLoop->u.btree.nEq |
| 113131 | + && pLoop->u.btree.nSkip==0 |
| 113058 | 113132 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
| 113059 | 113133 | ){ |
| 113060 | 113134 | if( i & WO_ISNULL ){ |
| 113061 | 113135 | testcase( isOrderDistinct ); |
| 113062 | 113136 | isOrderDistinct = 0; |
| | @@ -113478,10 +113552,11 @@ |
| 113478 | 113552 | if( pItem->zIndex ) return 0; |
| 113479 | 113553 | iCur = pItem->iCursor; |
| 113480 | 113554 | pWC = &pWInfo->sWC; |
| 113481 | 113555 | pLoop = pBuilder->pNew; |
| 113482 | 113556 | pLoop->wsFlags = 0; |
| 113557 | + pLoop->u.btree.nSkip = 0; |
| 113483 | 113558 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
| 113484 | 113559 | if( pTerm ){ |
| 113485 | 113560 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 113486 | 113561 | pLoop->aLTerm[0] = pTerm; |
| 113487 | 113562 | pLoop->nLTerm = 1; |
| | @@ -113706,11 +113781,10 @@ |
| 113706 | 113781 | /* Split the WHERE clause into separate subexpressions where each |
| 113707 | 113782 | ** subexpression is separated by an AND operator. |
| 113708 | 113783 | */ |
| 113709 | 113784 | initMaskSet(pMaskSet); |
| 113710 | 113785 | whereClauseInit(&pWInfo->sWC, pWInfo); |
| 113711 | | - sqlite3ExprCodeConstants(pParse, pWhere); |
| 113712 | 113786 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); |
| 113713 | 113787 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
| 113714 | 113788 | |
| 113715 | 113789 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 113716 | 113790 | ** expression and either jump over all of the code or fall thru. |
| | @@ -114021,10 +114095,11 @@ |
| 114021 | 114095 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
| 114022 | 114096 | pWInfo->iContinue = pLevel->addrCont; |
| 114023 | 114097 | } |
| 114024 | 114098 | |
| 114025 | 114099 | /* Done. */ |
| 114100 | + VdbeModuleComment((v, "Begin WHERE-core")); |
| 114026 | 114101 | return pWInfo; |
| 114027 | 114102 | |
| 114028 | 114103 | /* Jump here if malloc fails */ |
| 114029 | 114104 | whereBeginError: |
| 114030 | 114105 | if( pWInfo ){ |
| | @@ -114047,12 +114122,14 @@ |
| 114047 | 114122 | SrcList *pTabList = pWInfo->pTabList; |
| 114048 | 114123 | sqlite3 *db = pParse->db; |
| 114049 | 114124 | |
| 114050 | 114125 | /* Generate loop termination code. |
| 114051 | 114126 | */ |
| 114127 | + VdbeModuleComment((v, "End WHERE-core")); |
| 114052 | 114128 | sqlite3ExprCacheClear(pParse); |
| 114053 | 114129 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
| 114130 | + int addr; |
| 114054 | 114131 | pLevel = &pWInfo->a[i]; |
| 114055 | 114132 | pLoop = pLevel->pWLoop; |
| 114056 | 114133 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
| 114057 | 114134 | if( pLevel->op!=OP_Noop ){ |
| 114058 | 114135 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
| | @@ -114068,12 +114145,17 @@ |
| 114068 | 114145 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
| 114069 | 114146 | } |
| 114070 | 114147 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
| 114071 | 114148 | } |
| 114072 | 114149 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
| 114150 | + if( pLevel->addrSkip ){ |
| 114151 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip); |
| 114152 | + VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); |
| 114153 | + sqlite3VdbeJumpHere(v, pLevel->addrSkip); |
| 114154 | + sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); |
| 114155 | + } |
| 114073 | 114156 | if( pLevel->iLeftJoin ){ |
| 114074 | | - int addr; |
| 114075 | 114157 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); |
| 114076 | 114158 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 114077 | 114159 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 114078 | 114160 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
| 114079 | 114161 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| | @@ -114086,11 +114168,11 @@ |
| 114086 | 114168 | }else{ |
| 114087 | 114169 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 114088 | 114170 | } |
| 114089 | 114171 | sqlite3VdbeJumpHere(v, addr); |
| 114090 | 114172 | } |
| 114091 | | - VdbeNoopComment((v, "End WHERE-Loop %d: %s", i, |
| 114173 | + VdbeModuleComment((v, "End WHERE-loop%d: %s", i, |
| 114092 | 114174 | pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); |
| 114093 | 114175 | } |
| 114094 | 114176 | |
| 114095 | 114177 | /* The "break" point is here, just past the end of the outer loop. |
| 114096 | 114178 | ** Set it. |
| | @@ -123671,10 +123753,14 @@ |
| 123671 | 123753 | char *aDoclist; /* Pointer to doclist buffer */ |
| 123672 | 123754 | int nDoclist; /* Size of aDoclist[] in bytes */ |
| 123673 | 123755 | }; |
| 123674 | 123756 | |
| 123675 | 123757 | SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int); |
| 123758 | + |
| 123759 | +#define fts3GetVarint32(p, piVal) ( \ |
| 123760 | + (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \ |
| 123761 | +) |
| 123676 | 123762 | |
| 123677 | 123763 | /* fts3.c */ |
| 123678 | 123764 | SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64); |
| 123679 | 123765 | SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *); |
| 123680 | 123766 | SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *); |
| | @@ -123779,36 +123865,63 @@ |
| 123779 | 123865 | q[-1] &= 0x7f; /* turn off high bit in final byte */ |
| 123780 | 123866 | assert( q - (unsigned char *)p <= FTS3_VARINT_MAX ); |
| 123781 | 123867 | return (int) (q - (unsigned char *)p); |
| 123782 | 123868 | } |
| 123783 | 123869 | |
| 123870 | +#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \ |
| 123871 | + v = (v & mask1) | ( (*ptr++) << shift ); \ |
| 123872 | + if( (v & mask2)==0 ){ var = v; return ret; } |
| 123873 | +#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \ |
| 123874 | + v = (*ptr++); \ |
| 123875 | + if( (v & mask2)==0 ){ var = v; return ret; } |
| 123876 | + |
| 123784 | 123877 | /* |
| 123785 | 123878 | ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| 123786 | 123879 | ** Return the number of bytes read, or 0 on error. |
| 123787 | 123880 | ** The value is stored in *v. |
| 123788 | 123881 | */ |
| 123789 | 123882 | SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){ |
| 123790 | | - const unsigned char *q = (const unsigned char *) p; |
| 123791 | | - sqlite_uint64 x = 0, y = 1; |
| 123792 | | - while( (*q&0x80)==0x80 && q-(unsigned char *)p<FTS3_VARINT_MAX ){ |
| 123793 | | - x += y * (*q++ & 0x7f); |
| 123794 | | - y <<= 7; |
| 123795 | | - } |
| 123796 | | - x += y * (*q++); |
| 123797 | | - *v = (sqlite_int64) x; |
| 123798 | | - return (int) (q - (unsigned char *)p); |
| 123883 | + const char *pStart = p; |
| 123884 | + u32 a; |
| 123885 | + u64 b; |
| 123886 | + int shift; |
| 123887 | + |
| 123888 | + GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1); |
| 123889 | + GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2); |
| 123890 | + GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3); |
| 123891 | + GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4); |
| 123892 | + b = (a & 0x0FFFFFFF ); |
| 123893 | + |
| 123894 | + for(shift=28; shift<=63; shift+=7){ |
| 123895 | + u64 c = *p++; |
| 123896 | + b += (c&0x7F) << shift; |
| 123897 | + if( (c & 0x80)==0 ) break; |
| 123898 | + } |
| 123899 | + *v = b; |
| 123900 | + return (int)(p - pStart); |
| 123799 | 123901 | } |
| 123800 | 123902 | |
| 123801 | 123903 | /* |
| 123802 | 123904 | ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a |
| 123803 | 123905 | ** 32-bit integer before it is returned. |
| 123804 | 123906 | */ |
| 123805 | 123907 | SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){ |
| 123806 | | - sqlite_int64 i; |
| 123807 | | - int ret = sqlite3Fts3GetVarint(p, &i); |
| 123808 | | - *pi = (int) i; |
| 123809 | | - return ret; |
| 123908 | + u32 a; |
| 123909 | + |
| 123910 | +#ifndef fts3GetVarint32 |
| 123911 | + GETVARINT_INIT(a, p, 0, 0x00, 0x80, *pi, 1); |
| 123912 | +#else |
| 123913 | + a = (*p++); |
| 123914 | + assert( a & 0x80 ); |
| 123915 | +#endif |
| 123916 | + |
| 123917 | + GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *pi, 2); |
| 123918 | + GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *pi, 3); |
| 123919 | + GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *pi, 4); |
| 123920 | + a = (a & 0x0FFFFFFF ); |
| 123921 | + *pi = (int)(a | ((u32)(*p & 0x0F) << 28)); |
| 123922 | + return 5; |
| 123810 | 123923 | } |
| 123811 | 123924 | |
| 123812 | 123925 | /* |
| 123813 | 123926 | ** Return the number of bytes required to encode v as a varint |
| 123814 | 123927 | */ |
| | @@ -125164,14 +125277,14 @@ |
| 125164 | 125277 | int nBuffer; /* Total term size */ |
| 125165 | 125278 | |
| 125166 | 125279 | /* Load the next term on the node into zBuffer. Use realloc() to expand |
| 125167 | 125280 | ** the size of zBuffer if required. */ |
| 125168 | 125281 | if( !isFirstTerm ){ |
| 125169 | | - zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix); |
| 125282 | + zCsr += fts3GetVarint32(zCsr, &nPrefix); |
| 125170 | 125283 | } |
| 125171 | 125284 | isFirstTerm = 0; |
| 125172 | | - zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix); |
| 125285 | + zCsr += fts3GetVarint32(zCsr, &nSuffix); |
| 125173 | 125286 | |
| 125174 | 125287 | if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){ |
| 125175 | 125288 | rc = FTS_CORRUPT_VTAB; |
| 125176 | 125289 | goto finish_scan; |
| 125177 | 125290 | } |
| | @@ -125255,11 +125368,11 @@ |
| 125255 | 125368 | int rc; /* Return code */ |
| 125256 | 125369 | int iHeight; /* Height of this node in tree */ |
| 125257 | 125370 | |
| 125258 | 125371 | assert( piLeaf || piLeaf2 ); |
| 125259 | 125372 | |
| 125260 | | - sqlite3Fts3GetVarint32(zNode, &iHeight); |
| 125373 | + fts3GetVarint32(zNode, &iHeight); |
| 125261 | 125374 | rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2); |
| 125262 | 125375 | assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) ); |
| 125263 | 125376 | |
| 125264 | 125377 | if( rc==SQLITE_OK && iHeight>1 ){ |
| 125265 | 125378 | char *zBlob = 0; /* Blob read from %_segments table */ |
| | @@ -125457,15 +125570,15 @@ |
| 125457 | 125570 | |
| 125458 | 125571 | while( *p1 || *p2 ){ |
| 125459 | 125572 | int iCol1; /* The current column index in pp1 */ |
| 125460 | 125573 | int iCol2; /* The current column index in pp2 */ |
| 125461 | 125574 | |
| 125462 | | - if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1); |
| 125575 | + if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1); |
| 125463 | 125576 | else if( *p1==POS_END ) iCol1 = POSITION_LIST_END; |
| 125464 | 125577 | else iCol1 = 0; |
| 125465 | 125578 | |
| 125466 | | - if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2); |
| 125579 | + if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2); |
| 125467 | 125580 | else if( *p2==POS_END ) iCol2 = POSITION_LIST_END; |
| 125468 | 125581 | else iCol2 = 0; |
| 125469 | 125582 | |
| 125470 | 125583 | if( iCol1==iCol2 ){ |
| 125471 | 125584 | sqlite3_int64 i1 = 0; /* Last position from pp1 */ |
| | @@ -125554,15 +125667,15 @@ |
| 125554 | 125667 | assert( isSaveLeft==0 || isExact==0 ); |
| 125555 | 125668 | |
| 125556 | 125669 | assert( p!=0 && *p1!=0 && *p2!=0 ); |
| 125557 | 125670 | if( *p1==POS_COLUMN ){ |
| 125558 | 125671 | p1++; |
| 125559 | | - p1 += sqlite3Fts3GetVarint32(p1, &iCol1); |
| 125672 | + p1 += fts3GetVarint32(p1, &iCol1); |
| 125560 | 125673 | } |
| 125561 | 125674 | if( *p2==POS_COLUMN ){ |
| 125562 | 125675 | p2++; |
| 125563 | | - p2 += sqlite3Fts3GetVarint32(p2, &iCol2); |
| 125676 | + p2 += fts3GetVarint32(p2, &iCol2); |
| 125564 | 125677 | } |
| 125565 | 125678 | |
| 125566 | 125679 | while( 1 ){ |
| 125567 | 125680 | if( iCol1==iCol2 ){ |
| 125568 | 125681 | char *pSave = p; |
| | @@ -125608,13 +125721,13 @@ |
| 125608 | 125721 | fts3ColumnlistCopy(0, &p2); |
| 125609 | 125722 | assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 ); |
| 125610 | 125723 | if( 0==*p1 || 0==*p2 ) break; |
| 125611 | 125724 | |
| 125612 | 125725 | p1++; |
| 125613 | | - p1 += sqlite3Fts3GetVarint32(p1, &iCol1); |
| 125726 | + p1 += fts3GetVarint32(p1, &iCol1); |
| 125614 | 125727 | p2++; |
| 125615 | | - p2 += sqlite3Fts3GetVarint32(p2, &iCol2); |
| 125728 | + p2 += fts3GetVarint32(p2, &iCol2); |
| 125616 | 125729 | } |
| 125617 | 125730 | |
| 125618 | 125731 | /* Advance pointer p1 or p2 (whichever corresponds to the smaller of |
| 125619 | 125732 | ** iCol1 and iCol2) so that it points to either the 0x00 that marks the |
| 125620 | 125733 | ** end of the position list, or the 0x01 that precedes the next |
| | @@ -125622,16 +125735,16 @@ |
| 125622 | 125735 | */ |
| 125623 | 125736 | else if( iCol1<iCol2 ){ |
| 125624 | 125737 | fts3ColumnlistCopy(0, &p1); |
| 125625 | 125738 | if( 0==*p1 ) break; |
| 125626 | 125739 | p1++; |
| 125627 | | - p1 += sqlite3Fts3GetVarint32(p1, &iCol1); |
| 125740 | + p1 += fts3GetVarint32(p1, &iCol1); |
| 125628 | 125741 | }else{ |
| 125629 | 125742 | fts3ColumnlistCopy(0, &p2); |
| 125630 | 125743 | if( 0==*p2 ) break; |
| 125631 | 125744 | p2++; |
| 125632 | | - p2 += sqlite3Fts3GetVarint32(p2, &iCol2); |
| 125745 | + p2 += fts3GetVarint32(p2, &iCol2); |
| 125633 | 125746 | } |
| 125634 | 125747 | } |
| 125635 | 125748 | |
| 125636 | 125749 | fts3PoslistCopy(0, &p2); |
| 125637 | 125750 | fts3PoslistCopy(0, &p1); |
| | @@ -128794,11 +128907,11 @@ |
| 128794 | 128907 | */ |
| 128795 | 128908 | pExpr->aMI[iCol*3 + 1] += iCnt; |
| 128796 | 128909 | pExpr->aMI[iCol*3 + 2] += (iCnt>0); |
| 128797 | 128910 | if( *p==0x00 ) break; |
| 128798 | 128911 | p++; |
| 128799 | | - p += sqlite3Fts3GetVarint32(p, &iCol); |
| 128912 | + p += fts3GetVarint32(p, &iCol); |
| 128800 | 128913 | } |
| 128801 | 128914 | } |
| 128802 | 128915 | |
| 128803 | 128916 | fts3EvalUpdateCounts(pExpr->pLeft); |
| 128804 | 128917 | fts3EvalUpdateCounts(pExpr->pRight); |
| | @@ -129095,19 +129208,19 @@ |
| 129095 | 129208 | } |
| 129096 | 129209 | if( pIter==0 ) return SQLITE_OK; |
| 129097 | 129210 | |
| 129098 | 129211 | if( *pIter==0x01 ){ |
| 129099 | 129212 | pIter++; |
| 129100 | | - pIter += sqlite3Fts3GetVarint32(pIter, &iThis); |
| 129213 | + pIter += fts3GetVarint32(pIter, &iThis); |
| 129101 | 129214 | }else{ |
| 129102 | 129215 | iThis = 0; |
| 129103 | 129216 | } |
| 129104 | 129217 | while( iThis<iCol ){ |
| 129105 | 129218 | fts3ColumnlistCopy(0, &pIter); |
| 129106 | 129219 | if( *pIter==0x00 ) return 0; |
| 129107 | 129220 | pIter++; |
| 129108 | | - pIter += sqlite3Fts3GetVarint32(pIter, &iThis); |
| 129221 | + pIter += fts3GetVarint32(pIter, &iThis); |
| 129109 | 129222 | } |
| 129110 | 129223 | |
| 129111 | 129224 | *ppOut = ((iCol==iThis)?pIter:0); |
| 129112 | 129225 | return SQLITE_OK; |
| 129113 | 129226 | } |
| | @@ -134552,12 +134665,12 @@ |
| 134552 | 134665 | rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2); |
| 134553 | 134666 | if( rc!=SQLITE_OK ) return rc; |
| 134554 | 134667 | |
| 134555 | 134668 | /* Because of the FTS3_NODE_PADDING bytes of padding, the following is |
| 134556 | 134669 | ** safe (no risk of overread) even if the node data is corrupted. */ |
| 134557 | | - pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix); |
| 134558 | | - pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix); |
| 134670 | + pNext += fts3GetVarint32(pNext, &nPrefix); |
| 134671 | + pNext += fts3GetVarint32(pNext, &nSuffix); |
| 134559 | 134672 | if( nPrefix<0 || nSuffix<=0 |
| 134560 | 134673 | || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] |
| 134561 | 134674 | ){ |
| 134562 | 134675 | return FTS_CORRUPT_VTAB; |
| 134563 | 134676 | } |
| | @@ -134576,11 +134689,11 @@ |
| 134576 | 134689 | if( rc!=SQLITE_OK ) return rc; |
| 134577 | 134690 | |
| 134578 | 134691 | memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix); |
| 134579 | 134692 | pReader->nTerm = nPrefix+nSuffix; |
| 134580 | 134693 | pNext += nSuffix; |
| 134581 | | - pNext += sqlite3Fts3GetVarint32(pNext, &pReader->nDoclist); |
| 134694 | + pNext += fts3GetVarint32(pNext, &pReader->nDoclist); |
| 134582 | 134695 | pReader->aDoclist = pNext; |
| 134583 | 134696 | pReader->pOffsetList = 0; |
| 134584 | 134697 | |
| 134585 | 134698 | /* Check that the doclist does not appear to extend past the end of the |
| 134586 | 134699 | ** b-tree node. And that the final byte of the doclist is 0x00. If either |
| | @@ -135737,11 +135850,11 @@ |
| 135737 | 135850 | pList = p; |
| 135738 | 135851 | if( nList==0 ){ |
| 135739 | 135852 | break; |
| 135740 | 135853 | } |
| 135741 | 135854 | p = &pList[1]; |
| 135742 | | - p += sqlite3Fts3GetVarint32(p, &iCurrent); |
| 135855 | + p += fts3GetVarint32(p, &iCurrent); |
| 135743 | 135856 | } |
| 135744 | 135857 | |
| 135745 | 135858 | if( bZero && &pList[nList]!=pEnd ){ |
| 135746 | 135859 | memset(&pList[nList], 0, pEnd - &pList[nList]); |
| 135747 | 135860 | } |
| | @@ -136702,21 +136815,21 @@ |
| 136702 | 136815 | if( p->iOff>=p->nNode ){ |
| 136703 | 136816 | /* EOF */ |
| 136704 | 136817 | p->aNode = 0; |
| 136705 | 136818 | }else{ |
| 136706 | 136819 | if( bFirst==0 ){ |
| 136707 | | - p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &nPrefix); |
| 136820 | + p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix); |
| 136708 | 136821 | } |
| 136709 | | - p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &nSuffix); |
| 136822 | + p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix); |
| 136710 | 136823 | |
| 136711 | 136824 | blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc); |
| 136712 | 136825 | if( rc==SQLITE_OK ){ |
| 136713 | 136826 | memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix); |
| 136714 | 136827 | p->term.n = nPrefix+nSuffix; |
| 136715 | 136828 | p->iOff += nSuffix; |
| 136716 | 136829 | if( p->iChild==0 ){ |
| 136717 | | - p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist); |
| 136830 | + p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist); |
| 136718 | 136831 | p->aDoclist = &p->aNode[p->iOff]; |
| 136719 | 136832 | p->iOff += p->nDoclist; |
| 136720 | 136833 | } |
| 136721 | 136834 | } |
| 136722 | 136835 | } |
| | @@ -137764,11 +137877,11 @@ |
| 137764 | 137877 | while( i>0 && (pHint->a[i-1] & 0x80) ) i--; |
| 137765 | 137878 | while( i>0 && (pHint->a[i-1] & 0x80) ) i--; |
| 137766 | 137879 | |
| 137767 | 137880 | pHint->n = i; |
| 137768 | 137881 | i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel); |
| 137769 | | - i += sqlite3Fts3GetVarint32(&pHint->a[i], pnInput); |
| 137882 | + i += fts3GetVarint32(&pHint->a[i], pnInput); |
| 137770 | 137883 | if( i!=nHint ) return SQLITE_CORRUPT_VTAB; |
| 137771 | 137884 | |
| 137772 | 137885 | return SQLITE_OK; |
| 137773 | 137886 | } |
| 137774 | 137887 | |
| | @@ -138757,11 +138870,11 @@ |
| 138757 | 138870 | ** After it returns, *piPos contains the value of the next element of the |
| 138758 | 138871 | ** list and *pp is advanced to the following varint. |
| 138759 | 138872 | */ |
| 138760 | 138873 | static void fts3GetDeltaPosition(char **pp, int *piPos){ |
| 138761 | 138874 | int iVal; |
| 138762 | | - *pp += sqlite3Fts3GetVarint32(*pp, &iVal); |
| 138875 | + *pp += fts3GetVarint32(*pp, &iVal); |
| 138763 | 138876 | *piPos += (iVal-2); |
| 138764 | 138877 | } |
| 138765 | 138878 | |
| 138766 | 138879 | /* |
| 138767 | 138880 | ** Helper function for fts3ExprIterate() (see below). |
| 138768 | 138881 | |