| | @@ -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-14 19:34:10 10d59226382adcb8016fc2d927e5a0c0b36f3980" |
| 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 */ |
| | @@ -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); |
| | @@ -22970,22 +22975,22 @@ |
| 22970 | 22975 | /* 75 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"), |
| 22971 | 22976 | /* 76 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"), |
| 22972 | 22977 | /* 77 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"), |
| 22973 | 22978 | /* 78 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"), |
| 22974 | 22979 | /* 79 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"), |
| 22975 | | - /* 80 */ "Lt" OpHelp("if r[P1]<r[P3] goto P3"), |
| 22980 | + /* 80 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"), |
| 22976 | 22981 | /* 81 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"), |
| 22977 | 22982 | /* 82 */ "RowKey" OpHelp("r[P2]=key"), |
| 22978 | 22983 | /* 83 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"), |
| 22979 | 22984 | /* 84 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"), |
| 22980 | 22985 | /* 85 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"), |
| 22981 | 22986 | /* 86 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"), |
| 22982 | 22987 | /* 87 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"), |
| 22983 | 22988 | /* 88 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"), |
| 22984 | 22989 | /* 89 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"), |
| 22985 | | - /* 90 */ "Divide" OpHelp("r[P3]=r[P1]/r[P2]"), |
| 22986 | | - /* 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]"), |
| 22987 | 22992 | /* 92 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"), |
| 22988 | 22993 | /* 93 */ "RowData" OpHelp("r[P2]=data"), |
| 22989 | 22994 | /* 94 */ "BitNot" OpHelp("r[P1]= ~r[P1]"), |
| 22990 | 22995 | /* 95 */ "String8" OpHelp("r[P2]='P4'"), |
| 22991 | 22996 | /* 96 */ "Rowid" OpHelp("r[P2]=rowid"), |
| | @@ -58866,10 +58871,11 @@ |
| 58866 | 58871 | if( sqlite3OpenTempDatabase(pParse) ){ |
| 58867 | 58872 | sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg); |
| 58868 | 58873 | rc = SQLITE_ERROR; |
| 58869 | 58874 | } |
| 58870 | 58875 | sqlite3DbFree(pErrorDb, pParse->zErrMsg); |
| 58876 | + sqlite3ParserReset(pParse); |
| 58871 | 58877 | sqlite3StackFree(pErrorDb, pParse); |
| 58872 | 58878 | } |
| 58873 | 58879 | if( rc ){ |
| 58874 | 58880 | return 0; |
| 58875 | 58881 | } |
| | @@ -63898,19 +63904,16 @@ |
| 63898 | 63904 | pMem->u.i = serial_type-8; |
| 63899 | 63905 | pMem->flags = MEM_Int; |
| 63900 | 63906 | return 0; |
| 63901 | 63907 | } |
| 63902 | 63908 | default: { |
| 63909 | + static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; |
| 63903 | 63910 | u32 len = (serial_type-12)/2; |
| 63904 | 63911 | pMem->z = (char *)buf; |
| 63905 | 63912 | pMem->n = len; |
| 63906 | 63913 | pMem->xDel = 0; |
| 63907 | | - if( serial_type&0x01 ){ |
| 63908 | | - pMem->flags = MEM_Str | MEM_Ephem; |
| 63909 | | - }else{ |
| 63910 | | - pMem->flags = MEM_Blob | MEM_Ephem; |
| 63911 | | - } |
| 63914 | + pMem->flags = aFlag[serial_type&1]; |
| 63912 | 63915 | return len; |
| 63913 | 63916 | } |
| 63914 | 63917 | } |
| 63915 | 63918 | return 0; |
| 63916 | 63919 | } |
| | @@ -67793,23 +67796,23 @@ |
| 67793 | 67796 | ** Subtract the value in register P1 from the value in register P2 |
| 67794 | 67797 | ** and store the result in register P3. |
| 67795 | 67798 | ** If either input is NULL, the result is NULL. |
| 67796 | 67799 | */ |
| 67797 | 67800 | /* Opcode: Divide P1 P2 P3 * * |
| 67798 | | -** Synopsis: r[P3]=r[P1]/r[P2] |
| 67801 | +** Synopsis: r[P3]=r[P2]/r[P1] |
| 67799 | 67802 | ** |
| 67800 | 67803 | ** Divide the value in register P1 by the value in register P2 |
| 67801 | 67804 | ** and store the result in register P3 (P3=P2/P1). If the value in |
| 67802 | 67805 | ** register P1 is zero, then the result is NULL. If either input is |
| 67803 | 67806 | ** NULL, the result is NULL. |
| 67804 | 67807 | */ |
| 67805 | 67808 | /* Opcode: Remainder P1 P2 P3 * * |
| 67806 | | -** Synopsis: r[P3]=r[P1]%r[P2] |
| 67809 | +** Synopsis: r[P3]=r[P2]%r[P1] |
| 67807 | 67810 | ** |
| 67808 | | -** Compute the remainder after integer division of the value in |
| 67809 | | -** register P1 by the value in register P2 and store the result in P3. |
| 67810 | | -** 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. |
| 67811 | 67814 | ** If either operand is NULL, the result is NULL. |
| 67812 | 67815 | */ |
| 67813 | 67816 | case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ |
| 67814 | 67817 | case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ |
| 67815 | 67818 | case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ |
| | @@ -68274,11 +68277,11 @@ |
| 68274 | 68277 | break; |
| 68275 | 68278 | } |
| 68276 | 68279 | #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */ |
| 68277 | 68280 | |
| 68278 | 68281 | /* Opcode: Lt P1 P2 P3 P4 P5 |
| 68279 | | -** Synopsis: if r[P1]<r[P3] goto P3 |
| 68282 | +** Synopsis: if r[P1]<r[P3] goto P2 |
| 68280 | 68283 | ** |
| 68281 | 68284 | ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then |
| 68282 | 68285 | ** jump to address P2. |
| 68283 | 68286 | ** |
| 68284 | 68287 | ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or |
| | @@ -73278,10 +73281,11 @@ |
| 73278 | 73281 | if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt); |
| 73279 | 73282 | sqlite3DbFree(db, pBlob); |
| 73280 | 73283 | } |
| 73281 | 73284 | sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr); |
| 73282 | 73285 | sqlite3DbFree(db, zErr); |
| 73286 | + sqlite3ParserReset(pParse); |
| 73283 | 73287 | sqlite3StackFree(db, pParse); |
| 73284 | 73288 | rc = sqlite3ApiExit(db, rc); |
| 73285 | 73289 | sqlite3_mutex_leave(db->mutex); |
| 73286 | 73290 | return rc; |
| 73287 | 73291 | } |
| | @@ -75243,14 +75247,14 @@ |
| 75243 | 75247 | if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){ |
| 75244 | 75248 | incrAggFunctionDepth(pDup, nSubquery); |
| 75245 | 75249 | pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0); |
| 75246 | 75250 | if( pDup==0 ) return; |
| 75247 | 75251 | ExprSetProperty(pDup, EP_Skip); |
| 75248 | | - if( pEList->a[iCol].iAlias==0 ){ |
| 75249 | | - 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); |
| 75250 | 75254 | } |
| 75251 | | - pDup->iTable = pEList->a[iCol].iAlias; |
| 75255 | + pDup->iTable = pEList->a[iCol].u.x.iAlias; |
| 75252 | 75256 | } |
| 75253 | 75257 | if( pExpr->op==TK_COLLATE ){ |
| 75254 | 75258 | pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken); |
| 75255 | 75259 | } |
| 75256 | 75260 | |
| | @@ -76111,11 +76115,11 @@ |
| 76111 | 76115 | assert( pItem->pExpr->op==TK_COLLATE ); |
| 76112 | 76116 | assert( pItem->pExpr->pLeft==pE ); |
| 76113 | 76117 | pItem->pExpr->pLeft = pNew; |
| 76114 | 76118 | } |
| 76115 | 76119 | sqlite3ExprDelete(db, pE); |
| 76116 | | - pItem->iOrderByCol = (u16)iCol; |
| 76120 | + pItem->u.x.iOrderByCol = (u16)iCol; |
| 76117 | 76121 | pItem->done = 1; |
| 76118 | 76122 | }else{ |
| 76119 | 76123 | moreToDo = 1; |
| 76120 | 76124 | } |
| 76121 | 76125 | } |
| | @@ -76132,12 +76136,12 @@ |
| 76132 | 76136 | } |
| 76133 | 76137 | |
| 76134 | 76138 | /* |
| 76135 | 76139 | ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of |
| 76136 | 76140 | ** the SELECT statement pSelect. If any term is reference to a |
| 76137 | | -** result set expression (as determined by the ExprList.a.iOrderByCol field) |
| 76138 | | -** 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 |
| 76139 | 76143 | ** column. |
| 76140 | 76144 | ** |
| 76141 | 76145 | ** If any errors are detected, add an error message to pParse and |
| 76142 | 76146 | ** return non-zero. Return zero if no errors are seen. |
| 76143 | 76147 | */ |
| | @@ -76160,16 +76164,16 @@ |
| 76160 | 76164 | } |
| 76161 | 76165 | #endif |
| 76162 | 76166 | pEList = pSelect->pEList; |
| 76163 | 76167 | assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ |
| 76164 | 76168 | for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
| 76165 | | - if( pItem->iOrderByCol ){ |
| 76166 | | - if( pItem->iOrderByCol>pEList->nExpr ){ |
| 76169 | + if( pItem->u.x.iOrderByCol ){ |
| 76170 | + if( pItem->u.x.iOrderByCol>pEList->nExpr ){ |
| 76167 | 76171 | resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr); |
| 76168 | 76172 | return 1; |
| 76169 | 76173 | } |
| 76170 | | - resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0); |
| 76174 | + resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0); |
| 76171 | 76175 | } |
| 76172 | 76176 | } |
| 76173 | 76177 | return 0; |
| 76174 | 76178 | } |
| 76175 | 76179 | |
| | @@ -76214,11 +76218,11 @@ |
| 76214 | 76218 | if( iCol>0 ){ |
| 76215 | 76219 | /* If an AS-name match is found, mark this ORDER BY column as being |
| 76216 | 76220 | ** a copy of the iCol-th result-set column. The subsequent call to |
| 76217 | 76221 | ** sqlite3ResolveOrderGroupBy() will convert the expression to a |
| 76218 | 76222 | ** copy of the iCol-th result-set expression. */ |
| 76219 | | - pItem->iOrderByCol = (u16)iCol; |
| 76223 | + pItem->u.x.iOrderByCol = (u16)iCol; |
| 76220 | 76224 | continue; |
| 76221 | 76225 | } |
| 76222 | 76226 | } |
| 76223 | 76227 | if( sqlite3ExprIsInteger(pE2, &iCol) ){ |
| 76224 | 76228 | /* The ORDER BY term is an integer constant. Again, set the column |
| | @@ -76226,22 +76230,22 @@ |
| 76226 | 76230 | ** order-by term to a copy of the result-set expression */ |
| 76227 | 76231 | if( iCol<1 || iCol>0xffff ){ |
| 76228 | 76232 | resolveOutOfRangeError(pParse, zType, i+1, nResult); |
| 76229 | 76233 | return 1; |
| 76230 | 76234 | } |
| 76231 | | - pItem->iOrderByCol = (u16)iCol; |
| 76235 | + pItem->u.x.iOrderByCol = (u16)iCol; |
| 76232 | 76236 | continue; |
| 76233 | 76237 | } |
| 76234 | 76238 | |
| 76235 | 76239 | /* Otherwise, treat the ORDER BY term as an ordinary expression */ |
| 76236 | | - pItem->iOrderByCol = 0; |
| 76240 | + pItem->u.x.iOrderByCol = 0; |
| 76237 | 76241 | if( sqlite3ResolveExprNames(pNC, pE) ){ |
| 76238 | 76242 | return 1; |
| 76239 | 76243 | } |
| 76240 | 76244 | for(j=0; j<pSelect->pEList->nExpr; j++){ |
| 76241 | 76245 | if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){ |
| 76242 | | - pItem->iOrderByCol = j+1; |
| 76246 | + pItem->u.x.iOrderByCol = j+1; |
| 76243 | 76247 | } |
| 76244 | 76248 | } |
| 76245 | 76249 | } |
| 76246 | 76250 | return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType); |
| 76247 | 76251 | } |
| | @@ -77516,12 +77520,11 @@ |
| 77516 | 77520 | pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
| 77517 | 77521 | pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); |
| 77518 | 77522 | pItem->sortOrder = pOldItem->sortOrder; |
| 77519 | 77523 | pItem->done = 0; |
| 77520 | 77524 | pItem->bSpanIsTab = pOldItem->bSpanIsTab; |
| 77521 | | - pItem->iOrderByCol = pOldItem->iOrderByCol; |
| 77522 | | - pItem->iAlias = pOldItem->iAlias; |
| 77525 | + pItem->u = pOldItem->u; |
| 77523 | 77526 | } |
| 77524 | 77527 | return pNew; |
| 77525 | 77528 | } |
| 77526 | 77529 | |
| 77527 | 77530 | /* |
| | @@ -78942,10 +78945,11 @@ |
| 78942 | 78945 | int inReg = target; /* Results stored in register inReg */ |
| 78943 | 78946 | int regFree1 = 0; /* If non-zero free this temporary register */ |
| 78944 | 78947 | int regFree2 = 0; /* If non-zero free this temporary register */ |
| 78945 | 78948 | int r1, r2, r3, r4; /* Various register numbers */ |
| 78946 | 78949 | sqlite3 *db = pParse->db; /* The database connection */ |
| 78950 | + Expr tempX; /* Temporary expression node */ |
| 78947 | 78951 | |
| 78948 | 78952 | assert( target>0 && target<=pParse->nMem ); |
| 78949 | 78953 | if( v==0 ){ |
| 78950 | 78954 | assert( pParse->db->mallocFailed ); |
| 78951 | 78955 | return 0; |
| | @@ -79161,12 +79165,14 @@ |
| 79161 | 79165 | }else if( pLeft->op==TK_FLOAT ){ |
| 79162 | 79166 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 79163 | 79167 | codeReal(v, pLeft->u.zToken, 1, target); |
| 79164 | 79168 | #endif |
| 79165 | 79169 | }else{ |
| 79166 | | - regFree1 = r1 = sqlite3GetTempReg(pParse); |
| 79167 | | - 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); |
| 79168 | 79174 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); |
| 79169 | 79175 | sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); |
| 79170 | 79176 | testcase( regFree2==0 ); |
| 79171 | 79177 | } |
| 79172 | 79178 | inReg = target; |
| | @@ -79478,11 +79484,10 @@ |
| 79478 | 79484 | int nExpr; /* 2x number of WHEN terms */ |
| 79479 | 79485 | int i; /* Loop counter */ |
| 79480 | 79486 | ExprList *pEList; /* List of WHEN terms */ |
| 79481 | 79487 | struct ExprList_item *aListelem; /* Array of WHEN terms */ |
| 79482 | 79488 | Expr opCompare; /* The X==Ei expression */ |
| 79483 | | - Expr cacheX; /* Cached expression X */ |
| 79484 | 79489 | Expr *pX; /* The X expression */ |
| 79485 | 79490 | Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ |
| 79486 | 79491 | VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) |
| 79487 | 79492 | |
| 79488 | 79493 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); |
| | @@ -79490,17 +79495,16 @@ |
| 79490 | 79495 | pEList = pExpr->x.pList; |
| 79491 | 79496 | aListelem = pEList->a; |
| 79492 | 79497 | nExpr = pEList->nExpr; |
| 79493 | 79498 | endLabel = sqlite3VdbeMakeLabel(v); |
| 79494 | 79499 | if( (pX = pExpr->pLeft)!=0 ){ |
| 79495 | | - cacheX = *pX; |
| 79500 | + tempX = *pX; |
| 79496 | 79501 | testcase( pX->op==TK_COLUMN ); |
| 79497 | | - testcase( pX->op==TK_REGISTER ); |
| 79498 | | - exprToRegister(&cacheX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); |
| 79502 | + exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); |
| 79499 | 79503 | testcase( regFree1==0 ); |
| 79500 | 79504 | opCompare.op = TK_EQ; |
| 79501 | | - opCompare.pLeft = &cacheX; |
| 79505 | + opCompare.pLeft = &tempX; |
| 79502 | 79506 | pTest = &opCompare; |
| 79503 | 79507 | /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: |
| 79504 | 79508 | ** The value in regFree1 might get SCopy-ed into the file result. |
| 79505 | 79509 | ** So make sure that the regFree1 register is not reused for other |
| 79506 | 79510 | ** purposes and possibly overwritten. */ |
| | @@ -79516,11 +79520,10 @@ |
| 79516 | 79520 | } |
| 79517 | 79521 | nextCase = sqlite3VdbeMakeLabel(v); |
| 79518 | 79522 | testcase( pTest->op==TK_COLUMN ); |
| 79519 | 79523 | sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); |
| 79520 | 79524 | testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); |
| 79521 | | - testcase( aListelem[i+1].pExpr->op==TK_REGISTER ); |
| 79522 | 79525 | sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); |
| 79523 | 79526 | sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); |
| 79524 | 79527 | sqlite3ExprCachePop(pParse, 1); |
| 79525 | 79528 | sqlite3VdbeResolveLabel(v, nextCase); |
| 79526 | 79529 | } |
| | @@ -79575,19 +79578,45 @@ |
| 79575 | 79578 | ** are stored. |
| 79576 | 79579 | ** |
| 79577 | 79580 | ** If the register is a temporary register that can be deallocated, |
| 79578 | 79581 | ** then write its number into *pReg. If the result register is not |
| 79579 | 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. |
| 79580 | 79587 | */ |
| 79581 | 79588 | SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ |
| 79582 | | - int r1 = sqlite3GetTempReg(pParse); |
| 79583 | | - int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
| 79584 | | - if( r2==r1 ){ |
| 79585 | | - *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; |
| 79586 | 79609 | }else{ |
| 79587 | | - sqlite3ReleaseTempReg(pParse, r1); |
| 79588 | | - *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 | + } |
| 79589 | 79618 | } |
| 79590 | 79619 | return r2; |
| 79591 | 79620 | } |
| 79592 | 79621 | |
| 79593 | 79622 | /* |
| | @@ -79626,16 +79655,17 @@ |
| 79626 | 79655 | SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ |
| 79627 | 79656 | Vdbe *v = pParse->pVdbe; |
| 79628 | 79657 | int inReg; |
| 79629 | 79658 | inReg = sqlite3ExprCode(pParse, pExpr, target); |
| 79630 | 79659 | assert( target>0 ); |
| 79631 | | - /* This routine is called for terms to INSERT or UPDATE. And the only |
| 79632 | | - ** other place where expressions can be converted into TK_REGISTER is |
| 79633 | | - ** in WHERE clause processing. So as currently implemented, there is |
| 79634 | | - ** no way for a TK_REGISTER to exist here. But it seems prudent to |
| 79635 | | - ** keep the ALWAYS() in case the conditions above change with future |
| 79636 | | - ** 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. */ |
| 79637 | 79667 | if( ALWAYS(pExpr->op!=TK_REGISTER) ){ |
| 79638 | 79668 | int iMem; |
| 79639 | 79669 | iMem = ++pParse->nMem; |
| 79640 | 79670 | sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); |
| 79641 | 79671 | exprToRegister(pExpr, iMem); |
| | @@ -79913,144 +79943,10 @@ |
| 79913 | 79943 | } |
| 79914 | 79944 | sqlite3ExplainPop(pOut); |
| 79915 | 79945 | } |
| 79916 | 79946 | } |
| 79917 | 79947 | #endif /* SQLITE_DEBUG */ |
| 79918 | | - |
| 79919 | | -/* |
| 79920 | | -** Return TRUE if pExpr is an constant expression that is appropriate |
| 79921 | | -** for factoring out of a loop. Appropriate expressions are: |
| 79922 | | -** |
| 79923 | | -** * Any expression that evaluates to two or more opcodes. |
| 79924 | | -** |
| 79925 | | -** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, |
| 79926 | | -** or OP_Variable that does not need to be placed in a |
| 79927 | | -** specific register. |
| 79928 | | -** |
| 79929 | | -** There is no point in factoring out single-instruction constant |
| 79930 | | -** expressions that need to be placed in a particular register. |
| 79931 | | -** We could factor them out, but then we would end up adding an |
| 79932 | | -** OP_SCopy instruction to move the value into the correct register |
| 79933 | | -** later. We might as well just use the original instruction and |
| 79934 | | -** avoid the OP_SCopy. |
| 79935 | | -*/ |
| 79936 | | -static int isAppropriateForFactoring(Expr *p){ |
| 79937 | | - if( !sqlite3ExprIsConstantNotJoin(p) ){ |
| 79938 | | - return 0; /* Only constant expressions are appropriate for factoring */ |
| 79939 | | - } |
| 79940 | | - if( (p->flags & EP_FixedDest)==0 ){ |
| 79941 | | - return 1; /* Any constant without a fixed destination is appropriate */ |
| 79942 | | - } |
| 79943 | | - while( p->op==TK_UPLUS ) p = p->pLeft; |
| 79944 | | - switch( p->op ){ |
| 79945 | | -#ifndef SQLITE_OMIT_BLOB_LITERAL |
| 79946 | | - case TK_BLOB: |
| 79947 | | -#endif |
| 79948 | | - case TK_VARIABLE: |
| 79949 | | - case TK_INTEGER: |
| 79950 | | - case TK_FLOAT: |
| 79951 | | - case TK_NULL: |
| 79952 | | - case TK_STRING: { |
| 79953 | | - testcase( p->op==TK_BLOB ); |
| 79954 | | - testcase( p->op==TK_VARIABLE ); |
| 79955 | | - testcase( p->op==TK_INTEGER ); |
| 79956 | | - testcase( p->op==TK_FLOAT ); |
| 79957 | | - testcase( p->op==TK_NULL ); |
| 79958 | | - testcase( p->op==TK_STRING ); |
| 79959 | | - /* Single-instruction constants with a fixed destination are |
| 79960 | | - ** better done in-line. If we factor them, they will just end |
| 79961 | | - ** up generating an OP_SCopy to move the value to the destination |
| 79962 | | - ** register. */ |
| 79963 | | - return 0; |
| 79964 | | - } |
| 79965 | | - case TK_UMINUS: { |
| 79966 | | - if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){ |
| 79967 | | - return 0; |
| 79968 | | - } |
| 79969 | | - break; |
| 79970 | | - } |
| 79971 | | - default: { |
| 79972 | | - break; |
| 79973 | | - } |
| 79974 | | - } |
| 79975 | | - return 1; |
| 79976 | | -} |
| 79977 | | - |
| 79978 | | -/* |
| 79979 | | -** If pExpr is a constant expression that is appropriate for |
| 79980 | | -** factoring out of a loop, then evaluate the expression |
| 79981 | | -** into a register and convert the expression into a TK_REGISTER |
| 79982 | | -** expression. |
| 79983 | | -*/ |
| 79984 | | -static int evalConstExpr(Walker *pWalker, Expr *pExpr){ |
| 79985 | | - Parse *pParse = pWalker->pParse; |
| 79986 | | - switch( pExpr->op ){ |
| 79987 | | - case TK_IN: |
| 79988 | | - case TK_REGISTER: { |
| 79989 | | - return WRC_Prune; |
| 79990 | | - } |
| 79991 | | - case TK_COLLATE: { |
| 79992 | | - return WRC_Continue; |
| 79993 | | - } |
| 79994 | | - case TK_FUNCTION: |
| 79995 | | - case TK_AGG_FUNCTION: |
| 79996 | | - case TK_CONST_FUNC: { |
| 79997 | | - /* The arguments to a function have a fixed destination. |
| 79998 | | - ** Mark them this way to avoid generated unneeded OP_SCopy |
| 79999 | | - ** instructions. |
| 80000 | | - */ |
| 80001 | | - ExprList *pList = pExpr->x.pList; |
| 80002 | | - assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 80003 | | - if( pList ){ |
| 80004 | | - int i = pList->nExpr; |
| 80005 | | - struct ExprList_item *pItem = pList->a; |
| 80006 | | - for(; i>0; i--, pItem++){ |
| 80007 | | - if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest; |
| 80008 | | - } |
| 80009 | | - } |
| 80010 | | - break; |
| 80011 | | - } |
| 80012 | | - } |
| 80013 | | - if( isAppropriateForFactoring(pExpr) ){ |
| 80014 | | - int r1 = ++pParse->nMem; |
| 80015 | | - int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
| 80016 | | - /* If r2!=r1, it means that register r1 is never used. That is harmless |
| 80017 | | - ** but suboptimal, so we want to know about the situation to fix it. |
| 80018 | | - ** Hence the following assert: */ |
| 80019 | | - assert( r2==r1 ); |
| 80020 | | - exprToRegister(pExpr, r2); |
| 80021 | | - return WRC_Prune; |
| 80022 | | - } |
| 80023 | | - return WRC_Continue; |
| 80024 | | -} |
| 80025 | | - |
| 80026 | | -/* |
| 80027 | | -** Preevaluate constant subexpressions within pExpr and store the |
| 80028 | | -** results in registers. Modify pExpr so that the constant subexpresions |
| 80029 | | -** are TK_REGISTER opcodes that refer to the precomputed values. |
| 80030 | | -** |
| 80031 | | -** This routine is a no-op if the jump to the cookie-check code has |
| 80032 | | -** already occur. Since the cookie-check jump is generated prior to |
| 80033 | | -** any other serious processing, this check ensures that there is no |
| 80034 | | -** way to accidently bypass the constant initializations. |
| 80035 | | -** |
| 80036 | | -** This routine is also a no-op if the SQLITE_FactorOutConst optimization |
| 80037 | | -** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS) |
| 80038 | | -** interface. This allows test logic to verify that the same answer is |
| 80039 | | -** obtained for queries regardless of whether or not constants are |
| 80040 | | -** precomputed into registers or if they are inserted in-line. |
| 80041 | | -*/ |
| 80042 | | -SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ |
| 80043 | | - Walker w; |
| 80044 | | - if( pParse->cookieGoto ) return; |
| 80045 | | - if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return; |
| 80046 | | - memset(&w, 0, sizeof(w)); |
| 80047 | | - w.xExprCallback = evalConstExpr; |
| 80048 | | - w.pParse = pParse; |
| 80049 | | - sqlite3WalkExpr(&w, pExpr); |
| 80050 | | -} |
| 80051 | | - |
| 80052 | 79948 | |
| 80053 | 79949 | /* |
| 80054 | 79950 | ** Generate code that pushes the value of every element of the given |
| 80055 | 79951 | ** expression list into a sequence of registers beginning at target. |
| 80056 | 79952 | ** |
| | @@ -80425,44 +80321,46 @@ |
| 80425 | 80321 | ** this routine is used, it does not hurt to get an extra 2 - that |
| 80426 | 80322 | ** just might result in some slightly slower code. But returning |
| 80427 | 80323 | ** an incorrect 0 or 1 could lead to a malfunction. |
| 80428 | 80324 | */ |
| 80429 | 80325 | SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ |
| 80430 | | - if( pA==0||pB==0 ){ |
| 80326 | + u32 combinedFlags; |
| 80327 | + if( pA==0 || pB==0 ){ |
| 80431 | 80328 | return pB==pA ? 0 : 2; |
| 80432 | 80329 | } |
| 80433 | | - assert( !ExprHasProperty(pA, EP_TokenOnly|EP_Reduced) ); |
| 80434 | | - assert( !ExprHasProperty(pB, EP_TokenOnly|EP_Reduced) ); |
| 80435 | | - 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 | + } |
| 80436 | 80335 | return 2; |
| 80437 | 80336 | } |
| 80438 | | - if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; |
| 80439 | | - if( pA->op!=pB->op && (pA->op!=TK_REGISTER || pA->op2!=pB->op) ){ |
| 80337 | + if( pA->op!=pB->op ){ |
| 80440 | 80338 | if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ |
| 80441 | 80339 | return 1; |
| 80442 | 80340 | } |
| 80443 | 80341 | if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ |
| 80444 | 80342 | return 1; |
| 80445 | 80343 | } |
| 80446 | 80344 | return 2; |
| 80447 | 80345 | } |
| 80448 | | - if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; |
| 80449 | | - if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; |
| 80450 | | - if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; |
| 80451 | | - if( pA->iColumn!=pB->iColumn ) return 2; |
| 80452 | | - if( pA->iTable!=pB->iTable |
| 80453 | | - && pA->op!=TK_REGISTER |
| 80454 | | - && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; |
| 80455 | | - if( ExprHasProperty(pA, EP_IntValue) ){ |
| 80456 | | - if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){ |
| 80457 | | - return 2; |
| 80458 | | - } |
| 80459 | | - }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){ |
| 80460 | | - 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 ){ |
| 80461 | 80347 | if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ |
| 80462 | 80348 | return pA->op==TK_COLLATE ? 1 : 2; |
| 80463 | 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 | + } |
| 80464 | 80362 | } |
| 80465 | 80363 | return 0; |
| 80466 | 80364 | } |
| 80467 | 80365 | |
| 80468 | 80366 | /* |
| | @@ -84435,11 +84333,11 @@ |
| 84435 | 84333 | ** transaction on each used database and to verify the schema cookie |
| 84436 | 84334 | ** on each used database. |
| 84437 | 84335 | */ |
| 84438 | 84336 | if( pParse->cookieGoto>0 ){ |
| 84439 | 84337 | yDbMask mask; |
| 84440 | | - int iDb; |
| 84338 | + int iDb, i, addr; |
| 84441 | 84339 | sqlite3VdbeJumpHere(v, pParse->cookieGoto-1); |
| 84442 | 84340 | for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){ |
| 84443 | 84341 | if( (mask & pParse->cookieMask)==0 ) continue; |
| 84444 | 84342 | sqlite3VdbeUsesBtree(v, iDb); |
| 84445 | 84343 | sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0); |
| | @@ -84449,18 +84347,15 @@ |
| 84449 | 84347 | iDb, pParse->cookieValue[iDb], |
| 84450 | 84348 | db->aDb[iDb].pSchema->iGeneration); |
| 84451 | 84349 | } |
| 84452 | 84350 | } |
| 84453 | 84351 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 84454 | | - { |
| 84455 | | - int i; |
| 84456 | | - for(i=0; i<pParse->nVtabLock; i++){ |
| 84457 | | - char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
| 84458 | | - sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
| 84459 | | - } |
| 84460 | | - pParse->nVtabLock = 0; |
| 84461 | | - } |
| 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; |
| 84462 | 84357 | #endif |
| 84463 | 84358 | |
| 84464 | 84359 | /* Once all the cookies have been verified and transactions opened, |
| 84465 | 84360 | ** obtain the required table-locks. This is a no-op unless the |
| 84466 | 84361 | ** shared-cache feature is enabled. |
| | @@ -84468,13 +84363,23 @@ |
| 84468 | 84363 | codeTableLocks(pParse); |
| 84469 | 84364 | |
| 84470 | 84365 | /* Initialize any AUTOINCREMENT data structures required. |
| 84471 | 84366 | */ |
| 84472 | 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 | + } |
| 84473 | 84378 | |
| 84474 | 84379 | /* Finally, jump back to the beginning of the executable code. */ |
| 84475 | | - sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto); |
| 84380 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); |
| 84476 | 84381 | } |
| 84477 | 84382 | } |
| 84478 | 84383 | |
| 84479 | 84384 | |
| 84480 | 84385 | /* Get the VDBE program ready for execution |
| | @@ -89185,24 +89090,38 @@ |
| 89185 | 89090 | Expr *pWhere /* The WHERE clause. May be null */ |
| 89186 | 89091 | ){ |
| 89187 | 89092 | Vdbe *v; /* The virtual database engine */ |
| 89188 | 89093 | Table *pTab; /* The table from which records will be deleted */ |
| 89189 | 89094 | const char *zDb; /* Name of database holding pTab */ |
| 89190 | | - int end, addr = 0; /* A couple addresses of generated code */ |
| 89191 | 89095 | int i; /* Loop counter */ |
| 89192 | 89096 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 89193 | 89097 | Index *pIdx; /* For looping over indices of the table */ |
| 89194 | 89098 | int iTabCur; /* Cursor number for the table */ |
| 89195 | 89099 | int iDataCur; /* VDBE cursor for the canonical data source */ |
| 89196 | 89100 | int iIdxCur; /* Cursor number of the first index */ |
| 89101 | + int nIdx; /* Number of indices */ |
| 89197 | 89102 | sqlite3 *db; /* Main database structure */ |
| 89198 | 89103 | AuthContext sContext; /* Authorization context */ |
| 89199 | 89104 | NameContext sNC; /* Name context to resolve expressions in */ |
| 89200 | 89105 | int iDb; /* Database number */ |
| 89201 | 89106 | int memCnt = -1; /* Memory cell used for change counting */ |
| 89202 | 89107 | int rcauth; /* Value returned by authorization callback */ |
| 89203 | | - |
| 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 | + |
| 89204 | 89123 | #ifndef SQLITE_OMIT_TRIGGER |
| 89205 | 89124 | int isView; /* True if attempting to delete from a view */ |
| 89206 | 89125 | Trigger *pTrigger; /* List of table triggers, if required */ |
| 89207 | 89126 | #endif |
| 89208 | 89127 | |
| | @@ -89253,15 +89172,15 @@ |
| 89253 | 89172 | if( rcauth==SQLITE_DENY ){ |
| 89254 | 89173 | goto delete_from_cleanup; |
| 89255 | 89174 | } |
| 89256 | 89175 | assert(!isView || pTrigger); |
| 89257 | 89176 | |
| 89258 | | - /* Assign cursor number to the table and all its indices. |
| 89177 | + /* Assign cursor numbers to the table and all its indices. |
| 89259 | 89178 | */ |
| 89260 | 89179 | assert( pTabList->nSrc==1 ); |
| 89261 | 89180 | iTabCur = pTabList->a[0].iCursor = pParse->nTab++; |
| 89262 | | - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 89181 | + for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ |
| 89263 | 89182 | pParse->nTab++; |
| 89264 | 89183 | } |
| 89265 | 89184 | |
| 89266 | 89185 | /* Start the view context |
| 89267 | 89186 | */ |
| | @@ -89323,132 +89242,162 @@ |
| 89323 | 89242 | assert( pIdx->pSchema==pTab->pSchema ); |
| 89324 | 89243 | sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); |
| 89325 | 89244 | } |
| 89326 | 89245 | }else |
| 89327 | 89246 | #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */ |
| 89328 | | - if( !HasRowid(pTab) ){ |
| 89329 | | - /* There is a WHERE clause on a WITHOUT ROWID table. |
| 89330 | | - */ |
| 89331 | | - Index *pPk; /* The PRIMARY KEY index on the table */ |
| 89332 | | - int iPk; /* First of nPk memory cells holding PRIMARY KEY value */ |
| 89333 | | - int iEph; /* Ephemeral table holding all primary key values */ |
| 89334 | | - int iKey; /* Key value inserting into iEph */ |
| 89335 | | - i16 nPk; /* Number of components of the PRIMARY KEY */ |
| 89336 | | - |
| 89337 | | - pPk = sqlite3PrimaryKeyIndex(pTab); |
| 89338 | | - assert( pPk!=0 ); |
| 89339 | | - nPk = pPk->nKeyCol; |
| 89340 | | - iPk = pParse->nMem+1; |
| 89341 | | - pParse->nMem += nPk; |
| 89342 | | - iKey = ++pParse->nMem; |
| 89343 | | - iEph = pParse->nTab++; |
| 89344 | | - |
| 89345 | | - sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk); |
| 89346 | | - sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 89347 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 0, 0); |
| 89348 | | - if( pWInfo==0 ) goto delete_from_cleanup; |
| 89349 | | - for(i=0; i<nPk; i++){ |
| 89350 | | - sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, pPk->aiColumn[i],iPk+i); |
| 89351 | | - } |
| 89352 | | - sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey, |
| 89353 | | - sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT); |
| 89354 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, iKey); |
| 89355 | | - if( db->flags & SQLITE_CountRows ){ |
| 89356 | | - sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); |
| 89357 | | - } |
| 89358 | | - sqlite3WhereEnd(pWInfo); |
| 89359 | | - |
| 89360 | | - /* Open cursors for all indices of the table. |
| 89361 | | - */ |
| 89362 | | - sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, |
| 89363 | | - iTabCur, &iDataCur, &iIdxCur); |
| 89364 | | - |
| 89365 | | - /* Loop over the primary keys to be deleted. */ |
| 89366 | | - addr = sqlite3VdbeAddOp1(v, OP_Rewind, iEph); |
| 89367 | | - sqlite3VdbeAddOp2(v, OP_RowKey, iEph, iPk); |
| 89368 | | - |
| 89369 | | - /* Delete the row */ |
| 89370 | | - sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
| 89371 | | - iPk, 0, 1, OE_Default, 0); |
| 89372 | | - |
| 89373 | | - /* End of the delete loop */ |
| 89374 | | - sqlite3VdbeAddOp2(v, OP_Next, iEph, addr+1); |
| 89375 | | - sqlite3VdbeJumpHere(v, addr); |
| 89376 | | - |
| 89377 | | - /* Close the cursors open on the table and its indexes. */ |
| 89378 | | - assert( iDataCur>=iIdxCur ); |
| 89379 | | - for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
| 89380 | | - sqlite3VdbeAddOp1(v, OP_Close, iIdxCur+i); |
| 89381 | | - } |
| 89382 | | - }else{ |
| 89383 | | - /* There is a WHERE clause on a rowid table. Run a loop that extracts |
| 89384 | | - ** all rowids to be deleted into a RowSet. |
| 89385 | | - */ |
| 89386 | | - int iRowSet = ++pParse->nMem; /* Register for rowset of rows to delete */ |
| 89387 | | - int iRowid = ++pParse->nMem; /* Used for storing rowid values. */ |
| 89388 | | - int regRowid; /* Actual register containing rowids */ |
| 89389 | | - |
| 89390 | | - /* Collect rowids of every row to be deleted. |
| 89391 | | - */ |
| 89392 | | - sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet); |
| 89393 | | - pWInfo = sqlite3WhereBegin( |
| 89394 | | - pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0 |
| 89395 | | - ); |
| 89396 | | - if( pWInfo==0 ) goto delete_from_cleanup; |
| 89397 | | - regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iRowid, 0); |
| 89398 | | - sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid); |
| 89399 | | - if( db->flags & SQLITE_CountRows ){ |
| 89400 | | - sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); |
| 89401 | | - } |
| 89402 | | - sqlite3WhereEnd(pWInfo); |
| 89403 | | - |
| 89404 | | - /* Delete every item whose key was written to the list during the |
| 89405 | | - ** database scan. We have to delete items after the scan is complete |
| 89406 | | - ** because deleting an item can change the scan order. */ |
| 89407 | | - end = sqlite3VdbeMakeLabel(v); |
| 89408 | | - |
| 89409 | | - /* Unless this is a view, open cursors for the table we are |
| 89410 | | - ** deleting from and all its indices. If this is a view, then the |
| 89411 | | - ** only effect this statement has is to fire the INSTEAD OF |
| 89412 | | - ** triggers. */ |
| 89413 | | - if( !isView ){ |
| 89414 | | - sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, |
| 89415 | | - &iDataCur, &iIdxCur); |
| 89416 | | - assert( iDataCur==iTabCur ); |
| 89417 | | - assert( iIdxCur==iDataCur+1 ); |
| 89418 | | - } |
| 89419 | | - |
| 89420 | | - addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid); |
| 89421 | | - |
| 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 | + |
| 89422 | 89364 | /* Delete the row */ |
| 89423 | 89365 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 89424 | 89366 | if( IsVirtual(pTab) ){ |
| 89425 | 89367 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 89426 | 89368 | sqlite3VtabMakeWritable(pParse, pTab); |
| 89427 | | - sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB); |
| 89369 | + sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB); |
| 89428 | 89370 | sqlite3VdbeChangeP5(v, OE_Abort); |
| 89429 | 89371 | sqlite3MayAbort(pParse); |
| 89430 | 89372 | }else |
| 89431 | 89373 | #endif |
| 89432 | 89374 | { |
| 89433 | 89375 | int count = (pParse->nested==0); /* True to count changes */ |
| 89434 | 89376 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
| 89435 | | - iRowid, 1, count, OE_Default, 0); |
| 89377 | + iKey, nKey, count, OE_Default, okOnePass); |
| 89436 | 89378 | } |
| 89437 | | - |
| 89438 | | - /* End of the delete loop */ |
| 89439 | | - sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); |
| 89440 | | - sqlite3VdbeResolveLabel(v, end); |
| 89441 | | - |
| 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 | + |
| 89442 | 89391 | /* Close the cursors open on the table and its indexes. */ |
| 89443 | 89392 | if( !isView && !IsVirtual(pTab) ){ |
| 89444 | | - sqlite3VdbeAddOp1(v, OP_Close, iDataCur); |
| 89393 | + if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur); |
| 89445 | 89394 | for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ |
| 89446 | 89395 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i); |
| 89447 | 89396 | } |
| 89448 | 89397 | } |
| 89449 | | - } |
| 89398 | + } /* End non-truncate path */ |
| 89450 | 89399 | |
| 89451 | 89400 | /* Update the sqlite_sequence table by storing the content of the |
| 89452 | 89401 | ** maximum rowid counter values recorded while inserting into |
| 89453 | 89402 | ** autoincrement tables. |
| 89454 | 89403 | */ |
| | @@ -89468,10 +89417,11 @@ |
| 89468 | 89417 | |
| 89469 | 89418 | delete_from_cleanup: |
| 89470 | 89419 | sqlite3AuthContextPop(&sContext); |
| 89471 | 89420 | sqlite3SrcListDelete(db, pTabList); |
| 89472 | 89421 | sqlite3ExprDelete(db, pWhere); |
| 89422 | + sqlite3DbFree(db, aToOpen); |
| 89473 | 89423 | return; |
| 89474 | 89424 | } |
| 89475 | 89425 | /* Make sure "isView" and other macros defined above are undefined. Otherwise |
| 89476 | 89426 | ** thely may interfere with compilation of other functions in this file |
| 89477 | 89427 | ** (or in another file, if this file becomes part of the amalgamation). */ |
| | @@ -89534,10 +89484,11 @@ |
| 89534 | 89484 | /* If there are any triggers to fire, allocate a range of registers to |
| 89535 | 89485 | ** use for the old.* references in the triggers. */ |
| 89536 | 89486 | if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){ |
| 89537 | 89487 | u32 mask; /* Mask of OLD.* columns in use */ |
| 89538 | 89488 | int iCol; /* Iterator used while populating OLD.* */ |
| 89489 | + int addrStart; /* Start of BEFORE trigger programs */ |
| 89539 | 89490 | |
| 89540 | 89491 | /* TODO: Could use temporary registers here. Also could attempt to |
| 89541 | 89492 | ** avoid copying the contents of the rowid register. */ |
| 89542 | 89493 | mask = sqlite3TriggerColmask( |
| 89543 | 89494 | pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf |
| | @@ -89554,19 +89505,23 @@ |
| 89554 | 89505 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1); |
| 89555 | 89506 | } |
| 89556 | 89507 | } |
| 89557 | 89508 | |
| 89558 | 89509 | /* Invoke BEFORE DELETE trigger programs. */ |
| 89510 | + addrStart = sqlite3VdbeCurrentAddr(v); |
| 89559 | 89511 | sqlite3CodeRowTrigger(pParse, pTrigger, |
| 89560 | 89512 | TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel |
| 89561 | 89513 | ); |
| 89562 | 89514 | |
| 89563 | | - /* Seek the cursor to the row to be deleted again. It may be that |
| 89564 | | - ** the BEFORE triggers coded above have already removed the row |
| 89565 | | - ** being deleted. Do not attempt to delete the row a second time, and |
| 89566 | | - ** do not fire AFTER triggers. */ |
| 89567 | | - 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 | + } |
| 89568 | 89523 | |
| 89569 | 89524 | /* Do FK processing. This call checks that any FK constraints that |
| 89570 | 89525 | ** refer to this table (i.e. constraints attached to other tables) |
| 89571 | 89526 | ** are not violated by deleting this row. */ |
| 89572 | 89527 | sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0); |
| | @@ -91992,10 +91947,11 @@ |
| 91992 | 91947 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 91993 | 91948 | |
| 91994 | 91949 | assert( pIdx==0 || pIdx->pTable==pTab ); |
| 91995 | 91950 | assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol ); |
| 91996 | 91951 | assert( pIdx!=0 || pFKey->nCol==1 ); |
| 91952 | + assert( pIdx!=0 || HasRowid(pTab) ); |
| 91997 | 91953 | |
| 91998 | 91954 | if( nIncr<0 ){ |
| 91999 | 91955 | iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0); |
| 92000 | 91956 | } |
| 92001 | 91957 | |
| | @@ -92044,10 +92000,11 @@ |
| 92044 | 92000 | pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1); |
| 92045 | 92001 | pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0); |
| 92046 | 92002 | }else{ |
| 92047 | 92003 | Expr *pEq, *pAll = 0; |
| 92048 | 92004 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 92005 | + assert( pIdx!=0 ); |
| 92049 | 92006 | for(i=0; i<pPk->nKeyCol; i++){ |
| 92050 | 92007 | i16 iCol = pIdx->aiColumn[i]; |
| 92051 | 92008 | pLeft = exprTableRegister(pParse, pTab, regData, iCol); |
| 92052 | 92009 | pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol); |
| 92053 | 92010 | pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0); |
| | @@ -93622,11 +93579,11 @@ |
| 93622 | 93579 | } |
| 93623 | 93580 | |
| 93624 | 93581 | /* If this is not a view, open the table and and all indices */ |
| 93625 | 93582 | if( !isView ){ |
| 93626 | 93583 | int nIdx; |
| 93627 | | - nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, |
| 93584 | + nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0, |
| 93628 | 93585 | &iDataCur, &iIdxCur); |
| 93629 | 93586 | aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1)); |
| 93630 | 93587 | if( aRegIdx==0 ){ |
| 93631 | 93588 | goto insert_cleanup; |
| 93632 | 93589 | } |
| | @@ -94482,42 +94439,50 @@ |
| 94482 | 94439 | SQLITE_PRIVATE int sqlite3OpenTableAndIndices( |
| 94483 | 94440 | Parse *pParse, /* Parsing context */ |
| 94484 | 94441 | Table *pTab, /* Table to be opened */ |
| 94485 | 94442 | int op, /* OP_OpenRead or OP_OpenWrite */ |
| 94486 | 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 */ |
| 94487 | 94445 | int *piDataCur, /* Write the database source cursor number here */ |
| 94488 | 94446 | int *piIdxCur /* Write the first index cursor number here */ |
| 94489 | 94447 | ){ |
| 94490 | 94448 | int i; |
| 94491 | 94449 | int iDb; |
| 94450 | + int iDataCur; |
| 94492 | 94451 | Index *pIdx; |
| 94493 | 94452 | Vdbe *v; |
| 94494 | 94453 | |
| 94495 | 94454 | assert( op==OP_OpenRead || op==OP_OpenWrite ); |
| 94496 | 94455 | if( IsVirtual(pTab) ){ |
| 94456 | + assert( aToOpen==0 ); |
| 94497 | 94457 | *piDataCur = 0; |
| 94498 | 94458 | *piIdxCur = 1; |
| 94499 | 94459 | return 0; |
| 94500 | 94460 | } |
| 94501 | 94461 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 94502 | 94462 | v = sqlite3GetVdbe(pParse); |
| 94503 | 94463 | assert( v!=0 ); |
| 94504 | 94464 | if( iBase<0 ) iBase = pParse->nTab; |
| 94505 | | - if( HasRowid(pTab) ){ |
| 94506 | | - *piDataCur = iBase++; |
| 94507 | | - 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); |
| 94508 | 94469 | }else{ |
| 94509 | 94470 | sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName); |
| 94510 | 94471 | } |
| 94511 | | - *piIdxCur = iBase; |
| 94472 | + if( piIdxCur ) *piIdxCur = iBase; |
| 94512 | 94473 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 94513 | 94474 | int iIdxCur = iBase++; |
| 94514 | 94475 | assert( pIdx->pSchema==pTab->pSchema ); |
| 94515 | | - if( pIdx->autoIndex==2 && !HasRowid(pTab) ) *piDataCur = iIdxCur; |
| 94516 | | - sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); |
| 94517 | | - sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 94518 | | - 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 | + } |
| 94519 | 94484 | } |
| 94520 | 94485 | if( iBase>pParse->nTab ) pParse->nTab = iBase; |
| 94521 | 94486 | return i; |
| 94522 | 94487 | } |
| 94523 | 94488 | |
| | @@ -98144,11 +98109,11 @@ |
| 98144 | 98109 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */ |
| 98145 | 98110 | sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); |
| 98146 | 98111 | sqlite3VdbeJumpHere(v, addr); |
| 98147 | 98112 | sqlite3ExprCacheClear(pParse); |
| 98148 | 98113 | sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, |
| 98149 | | - 1, &iDataCur, &iIdxCur); |
| 98114 | + 1, 0, &iDataCur, &iIdxCur); |
| 98150 | 98115 | sqlite3VdbeAddOp2(v, OP_Integer, 0, 7); |
| 98151 | 98116 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 98152 | 98117 | sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */ |
| 98153 | 98118 | } |
| 98154 | 98119 | pParse->nMem = MAX(pParse->nMem, 8+j); |
| | @@ -99079,10 +99044,17 @@ |
| 99079 | 99044 | } |
| 99080 | 99045 | assert( i>=0 && i<db->nDb ); |
| 99081 | 99046 | } |
| 99082 | 99047 | return i; |
| 99083 | 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 | +} |
| 99084 | 99056 | |
| 99085 | 99057 | /* |
| 99086 | 99058 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 99087 | 99059 | */ |
| 99088 | 99060 | static int sqlite3Prepare( |
| | @@ -99237,10 +99209,11 @@ |
| 99237 | 99209 | sqlite3DbFree(db, pT); |
| 99238 | 99210 | } |
| 99239 | 99211 | |
| 99240 | 99212 | end_prepare: |
| 99241 | 99213 | |
| 99214 | + sqlite3ParserReset(pParse); |
| 99242 | 99215 | sqlite3StackFree(db, pParse); |
| 99243 | 99216 | rc = sqlite3ApiExit(db, rc); |
| 99244 | 99217 | assert( (rc&db->errMask)==rc ); |
| 99245 | 99218 | return rc; |
| 99246 | 99219 | } |
| | @@ -101803,20 +101776,20 @@ |
| 101803 | 101776 | */ |
| 101804 | 101777 | if( op!=TK_ALL ){ |
| 101805 | 101778 | for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){ |
| 101806 | 101779 | struct ExprList_item *pItem; |
| 101807 | 101780 | for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){ |
| 101808 | | - assert( pItem->iOrderByCol>0 ); |
| 101809 | | - if( pItem->iOrderByCol==i ) break; |
| 101781 | + assert( pItem->u.x.iOrderByCol>0 ); |
| 101782 | + if( pItem->u.x.iOrderByCol==i ) break; |
| 101810 | 101783 | } |
| 101811 | 101784 | if( j==nOrderBy ){ |
| 101812 | 101785 | Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); |
| 101813 | 101786 | if( pNew==0 ) return SQLITE_NOMEM; |
| 101814 | 101787 | pNew->flags |= EP_IntValue; |
| 101815 | 101788 | pNew->u.iValue = i; |
| 101816 | 101789 | pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew); |
| 101817 | | - if( pOrderBy ) pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i; |
| 101790 | + if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i; |
| 101818 | 101791 | } |
| 101819 | 101792 | } |
| 101820 | 101793 | } |
| 101821 | 101794 | |
| 101822 | 101795 | /* Compute the comparison permutation and keyinfo that is used with |
| | @@ -101828,12 +101801,13 @@ |
| 101828 | 101801 | */ |
| 101829 | 101802 | aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy); |
| 101830 | 101803 | if( aPermute ){ |
| 101831 | 101804 | struct ExprList_item *pItem; |
| 101832 | 101805 | for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){ |
| 101833 | | - assert( pItem->iOrderByCol>0 && pItem->iOrderByCol<=p->pEList->nExpr ); |
| 101834 | | - 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; |
| 101835 | 101809 | } |
| 101836 | 101810 | pKeyMerge = sqlite3KeyInfoAlloc(db, nOrderBy, 1); |
| 101837 | 101811 | if( pKeyMerge ){ |
| 101838 | 101812 | for(i=0; i<nOrderBy; i++){ |
| 101839 | 101813 | CollSeq *pColl; |
| | @@ -102409,11 +102383,11 @@ |
| 102409 | 102383 | |
| 102410 | 102384 | /* Restriction 18. */ |
| 102411 | 102385 | if( p->pOrderBy ){ |
| 102412 | 102386 | int ii; |
| 102413 | 102387 | for(ii=0; ii<p->pOrderBy->nExpr; ii++){ |
| 102414 | | - if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0; |
| 102388 | + if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0; |
| 102415 | 102389 | } |
| 102416 | 102390 | } |
| 102417 | 102391 | } |
| 102418 | 102392 | |
| 102419 | 102393 | /***** If we reach this point, flattening is permitted. *****/ |
| | @@ -103816,14 +103790,14 @@ |
| 103816 | 103790 | if( pGroupBy ){ |
| 103817 | 103791 | int k; /* Loop counter */ |
| 103818 | 103792 | struct ExprList_item *pItem; /* For looping over expression in a list */ |
| 103819 | 103793 | |
| 103820 | 103794 | for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){ |
| 103821 | | - pItem->iAlias = 0; |
| 103795 | + pItem->u.x.iAlias = 0; |
| 103822 | 103796 | } |
| 103823 | 103797 | for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ |
| 103824 | | - pItem->iAlias = 0; |
| 103798 | + pItem->u.x.iAlias = 0; |
| 103825 | 103799 | } |
| 103826 | 103800 | if( p->nSelectRow>100 ) p->nSelectRow = 100; |
| 103827 | 103801 | }else{ |
| 103828 | 103802 | p->nSelectRow = 1; |
| 103829 | 103803 | } |
| | @@ -105470,10 +105444,11 @@ |
| 105470 | 105444 | sqlite3VdbeDelete(v); |
| 105471 | 105445 | } |
| 105472 | 105446 | |
| 105473 | 105447 | assert( !pSubParse->pAinc && !pSubParse->pZombieTab ); |
| 105474 | 105448 | assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); |
| 105449 | + sqlite3ParserReset(pSubParse); |
| 105475 | 105450 | sqlite3StackFree(db, pSubParse); |
| 105476 | 105451 | |
| 105477 | 105452 | return pPrg; |
| 105478 | 105453 | } |
| 105479 | 105454 | |
| | @@ -105784,22 +105759,23 @@ |
| 105784 | 105759 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 105785 | 105760 | Vdbe *v; /* The virtual database engine */ |
| 105786 | 105761 | Index *pIdx; /* For looping over indices */ |
| 105787 | 105762 | Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */ |
| 105788 | 105763 | int nIdx; /* Number of indices that need updating */ |
| 105764 | + int iBaseCur; /* Base cursor number */ |
| 105789 | 105765 | int iDataCur; /* Cursor for the canonical data btree */ |
| 105790 | 105766 | int iIdxCur; /* Cursor for the first index */ |
| 105791 | 105767 | sqlite3 *db; /* The database structure */ |
| 105792 | 105768 | int *aRegIdx = 0; /* One register assigned to each index to be updated */ |
| 105793 | 105769 | int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the |
| 105794 | 105770 | ** an expression for the i-th column of the table. |
| 105795 | 105771 | ** aXRef[i]==-1 if the i-th column is not changed. */ |
| 105772 | + u8 *aToOpen; /* 1 for tables and indices to be opened */ |
| 105796 | 105773 | u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */ |
| 105797 | 105774 | u8 chngRowid; /* Rowid changed in a normal table */ |
| 105798 | 105775 | u8 chngKey; /* Either chngPk or chngRowid */ |
| 105799 | 105776 | Expr *pRowidExpr = 0; /* Expression defining the new record number */ |
| 105800 | | - int openAll = 0; /* True if all indices need to be opened */ |
| 105801 | 105777 | AuthContext sContext; /* The authorization context */ |
| 105802 | 105778 | NameContext sNC; /* The name-context to resolve expressions in */ |
| 105803 | 105779 | int iDb; /* Database containing the table being updated */ |
| 105804 | 105780 | int okOnePass; /* True for one-pass algorithm without the FIFO */ |
| 105805 | 105781 | int hasFK; /* True if foreign key processing is required */ |
| | @@ -105859,29 +105835,37 @@ |
| 105859 | 105835 | goto update_cleanup; |
| 105860 | 105836 | } |
| 105861 | 105837 | if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ |
| 105862 | 105838 | goto update_cleanup; |
| 105863 | 105839 | } |
| 105864 | | - aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol ); |
| 105865 | | - if( aXRef==0 ) goto update_cleanup; |
| 105866 | | - for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; |
| 105867 | 105840 | |
| 105868 | 105841 | /* Allocate a cursors for the main database table and for all indices. |
| 105869 | 105842 | ** The index cursors might not be used, but if they are used they |
| 105870 | 105843 | ** need to occur right after the database cursor. So go ahead and |
| 105871 | 105844 | ** allocate enough space, just in case. |
| 105872 | 105845 | */ |
| 105873 | | - pTabList->a[0].iCursor = iDataCur = pParse->nTab++; |
| 105846 | + pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++; |
| 105874 | 105847 | iIdxCur = iDataCur+1; |
| 105875 | 105848 | pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); |
| 105876 | 105849 | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ |
| 105877 | 105850 | if( pIdx->autoIndex==2 && pPk!=0 ){ |
| 105878 | 105851 | iDataCur = pParse->nTab; |
| 105879 | 105852 | pTabList->a[0].iCursor = iDataCur; |
| 105880 | 105853 | } |
| 105881 | 105854 | pParse->nTab++; |
| 105882 | 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; |
| 105883 | 105867 | |
| 105884 | 105868 | /* Initialize the name-context */ |
| 105885 | 105869 | memset(&sNC, 0, sizeof(sNC)); |
| 105886 | 105870 | sNC.pParse = pParse; |
| 105887 | 105871 | sNC.pSrcList = pTabList; |
| | @@ -105936,22 +105920,22 @@ |
| 105936 | 105920 | } |
| 105937 | 105921 | assert( (chngRowid & chngPk)==0 ); |
| 105938 | 105922 | assert( chngRowid==0 || chngRowid==1 ); |
| 105939 | 105923 | assert( chngPk==0 || chngPk==1 ); |
| 105940 | 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; |
| 105941 | 105930 | |
| 105942 | 105931 | hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey); |
| 105943 | 105932 | |
| 105944 | | - /* Allocate memory for the array aRegIdx[]. There is one entry in the |
| 105945 | | - ** array for each index associated with table being updated. Fill in |
| 105946 | | - ** the value with a register number for indices that are to be used |
| 105947 | | - ** 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. |
| 105948 | 105936 | */ |
| 105949 | | - if( nIdx>0 ){ |
| 105950 | | - aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx ); |
| 105951 | | - if( aRegIdx==0 ) goto update_cleanup; |
| 105952 | | - } |
| 105953 | 105937 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 105954 | 105938 | int reg; |
| 105955 | 105939 | if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){ |
| 105956 | 105940 | reg = ++pParse->nMem; |
| 105957 | 105941 | }else{ |
| | @@ -105961,10 +105945,11 @@ |
| 105961 | 105945 | reg = ++pParse->nMem; |
| 105962 | 105946 | break; |
| 105963 | 105947 | } |
| 105964 | 105948 | } |
| 105965 | 105949 | } |
| 105950 | + if( reg==0 ) aToOpen[j+1] = 0; |
| 105966 | 105951 | aRegIdx[j] = reg; |
| 105967 | 105952 | } |
| 105968 | 105953 | |
| 105969 | 105954 | /* Begin generating code. */ |
| 105970 | 105955 | v = sqlite3GetVdbe(pParse); |
| | @@ -106084,46 +106069,34 @@ |
| 106084 | 106069 | ** Open every index that needs updating. Note that if any |
| 106085 | 106070 | ** index could potentially invoke a REPLACE conflict resolution |
| 106086 | 106071 | ** action, then we need to open all indices because we might need |
| 106087 | 106072 | ** to be deleting some records. |
| 106088 | 106073 | */ |
| 106089 | | - if( !okOnePass && HasRowid(pTab) ){ |
| 106090 | | - sqlite3OpenTable(pParse, iDataCur, iDb, pTab, OP_OpenWrite); |
| 106091 | | - } |
| 106092 | | - sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); |
| 106093 | 106074 | if( onError==OE_Replace ){ |
| 106094 | | - openAll = 1; |
| 106075 | + memset(aToOpen, 1, nIdx+1); |
| 106095 | 106076 | }else{ |
| 106096 | | - openAll = 0; |
| 106097 | 106077 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 106098 | 106078 | if( pIdx->onError==OE_Replace ){ |
| 106099 | | - openAll = 1; |
| 106079 | + memset(aToOpen, 1, nIdx+1); |
| 106100 | 106080 | break; |
| 106101 | 106081 | } |
| 106102 | 106082 | } |
| 106103 | 106083 | } |
| 106104 | | - for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 106105 | | - int iThisCur = iIdxCur+i; |
| 106106 | | - assert( aRegIdx ); |
| 106107 | | - if( (openAll || aRegIdx[i]>0) |
| 106108 | | - && iThisCur!=aiCurOnePass[1] |
| 106109 | | - ){ |
| 106110 | | - assert( iThisCur!=aiCurOnePass[0] ); |
| 106111 | | - sqlite3VdbeAddOp3(v, OP_OpenWrite, iThisCur, pIdx->tnum, iDb); |
| 106112 | | - sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 106113 | | - assert( pParse->nTab>iThisCur ); |
| 106114 | | - VdbeComment((v, "%s", pIdx->zName)); |
| 106115 | | - if( okOnePass && pPk && iThisCur==iDataCur ){ |
| 106116 | | - sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, |
| 106117 | | - regKey, nKey); |
| 106118 | | - } |
| 106119 | | - } |
| 106120 | | - } |
| 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); |
| 106121 | 106090 | } |
| 106122 | 106091 | |
| 106123 | 106092 | /* Top of the update loop */ |
| 106124 | 106093 | if( okOnePass ){ |
| 106094 | + if( aToOpen[iDataCur-iBaseCur] ){ |
| 106095 | + assert( pPk!=0 ); |
| 106096 | + sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey); |
| 106097 | + } |
| 106125 | 106098 | labelContinue = labelBreak; |
| 106126 | 106099 | sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak); |
| 106127 | 106100 | }else if( pPk ){ |
| 106128 | 106101 | labelContinue = sqlite3VdbeMakeLabel(v); |
| 106129 | 106102 | sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); |
| | @@ -106312,11 +106285,11 @@ |
| 106312 | 106285 | sqlite3VdbeResolveLabel(v, labelBreak); |
| 106313 | 106286 | |
| 106314 | 106287 | /* Close all tables */ |
| 106315 | 106288 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 106316 | 106289 | assert( aRegIdx ); |
| 106317 | | - if( openAll || aRegIdx[i]>0 ){ |
| 106290 | + if( aToOpen[i+1] ){ |
| 106318 | 106291 | sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0); |
| 106319 | 106292 | } |
| 106320 | 106293 | } |
| 106321 | 106294 | if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0); |
| 106322 | 106295 | |
| | @@ -106339,12 +106312,11 @@ |
| 106339 | 106312 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC); |
| 106340 | 106313 | } |
| 106341 | 106314 | |
| 106342 | 106315 | update_cleanup: |
| 106343 | 106316 | sqlite3AuthContextPop(&sContext); |
| 106344 | | - sqlite3DbFree(db, aRegIdx); |
| 106345 | | - sqlite3DbFree(db, aXRef); |
| 106317 | + sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */ |
| 106346 | 106318 | sqlite3SrcListDelete(db, pTabList); |
| 106347 | 106319 | sqlite3ExprListDelete(db, pChanges); |
| 106348 | 106320 | sqlite3ExprDelete(db, pWhere); |
| 106349 | 106321 | return; |
| 106350 | 106322 | } |
| | @@ -107563,10 +107535,11 @@ |
| 107563 | 107535 | |
| 107564 | 107536 | if( pParse->pVdbe ){ |
| 107565 | 107537 | sqlite3VdbeFinalize(pParse->pVdbe); |
| 107566 | 107538 | } |
| 107567 | 107539 | sqlite3DeleteTable(db, pParse->pNewTable); |
| 107540 | + sqlite3ParserReset(pParse); |
| 107568 | 107541 | sqlite3StackFree(db, pParse); |
| 107569 | 107542 | } |
| 107570 | 107543 | |
| 107571 | 107544 | assert( (rc&0xff)==rc ); |
| 107572 | 107545 | rc = sqlite3ApiExit(db, rc); |
| | @@ -109055,13 +109028,10 @@ |
| 109055 | 109028 | } |
| 109056 | 109029 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
| 109057 | 109030 | |
| 109058 | 109031 | pRight = pList->a[0].pExpr; |
| 109059 | 109032 | op = pRight->op; |
| 109060 | | - if( op==TK_REGISTER ){ |
| 109061 | | - op = pRight->op2; |
| 109062 | | - } |
| 109063 | 109033 | if( op==TK_VARIABLE ){ |
| 109064 | 109034 | Vdbe *pReprepare = pParse->pReprepare; |
| 109065 | 109035 | int iCol = pRight->iColumn; |
| 109066 | 109036 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE); |
| 109067 | 109037 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| | @@ -111145,11 +111115,11 @@ |
| 111145 | 111115 | iCur = pTabItem->iCursor; |
| 111146 | 111116 | pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
| 111147 | 111117 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 111148 | 111118 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
| 111149 | 111119 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
| 111150 | | - VdbeNoopComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); |
| 111120 | + VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); |
| 111151 | 111121 | |
| 111152 | 111122 | /* Create labels for the "break" and "continue" instructions |
| 111153 | 111123 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 111154 | 111124 | ** Jump to cont to go immediately to the next iteration of the |
| 111155 | 111125 | ** loop. |
| | @@ -111387,11 +111357,11 @@ |
| 111387 | 111357 | Index *pIdx; /* The index we will be using */ |
| 111388 | 111358 | int iIdxCur; /* The VDBE cursor for the index */ |
| 111389 | 111359 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 111390 | 111360 | int op; /* Instruction opcode */ |
| 111391 | 111361 | char *zStartAff; /* Affinity for start of range constraint */ |
| 111392 | | - char *zEndAff; /* Affinity for end of range constraint */ |
| 111362 | + char cEndAff = 0; /* Affinity for end of range constraint */ |
| 111393 | 111363 | |
| 111394 | 111364 | pIdx = pLoop->u.btree.pIndex; |
| 111395 | 111365 | iIdxCur = pLevel->iIdxCur; |
| 111396 | 111366 | assert( nEq>=pLoop->u.btree.nSkip ); |
| 111397 | 111367 | |
| | @@ -111428,11 +111398,12 @@ |
| 111428 | 111398 | /* Generate code to evaluate all constraint terms using == or IN |
| 111429 | 111399 | ** and store the values of those terms in an array of registers |
| 111430 | 111400 | ** starting at regBase. |
| 111431 | 111401 | */ |
| 111432 | 111402 | regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
| 111433 | | - zEndAff = sqlite3DbStrDup(db, zStartAff); |
| 111403 | + assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); |
| 111404 | + if( zStartAff ) cEndAff = zStartAff[nEq]; |
| 111434 | 111405 | addrNxt = pLevel->addrNxt; |
| 111435 | 111406 | |
| 111436 | 111407 | /* If we are doing a reverse order scan on an ascending index, or |
| 111437 | 111408 | ** a forward order scan on a descending index, interchange the |
| 111438 | 111409 | ** start and end terms (pRangeStart and pRangeEnd). |
| | @@ -111498,27 +111469,19 @@ |
| 111498 | 111469 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
| 111499 | 111470 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
| 111500 | 111471 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){ |
| 111501 | 111472 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 111502 | 111473 | } |
| 111503 | | - if( zEndAff ){ |
| 111504 | | - if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){ |
| 111505 | | - /* Since the comparison is to be performed with no conversions |
| 111506 | | - ** applied to the operands, set the affinity to apply to pRight to |
| 111507 | | - ** SQLITE_AFF_NONE. */ |
| 111508 | | - zEndAff[nEq] = SQLITE_AFF_NONE; |
| 111509 | | - } |
| 111510 | | - if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){ |
| 111511 | | - zEndAff[nEq] = SQLITE_AFF_NONE; |
| 111512 | | - } |
| 111513 | | - } |
| 111514 | | - 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 | + } |
| 111515 | 111479 | nConstraint++; |
| 111516 | 111480 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
| 111517 | 111481 | } |
| 111518 | 111482 | sqlite3DbFree(db, zStartAff); |
| 111519 | | - sqlite3DbFree(db, zEndAff); |
| 111520 | 111483 | |
| 111521 | 111484 | /* Top of the loop body */ |
| 111522 | 111485 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 111523 | 111486 | |
| 111524 | 111487 | /* Check if the index cursor is past the end of the range. */ |
| | @@ -111539,10 +111502,11 @@ |
| 111539 | 111502 | testcase( pLoop->wsFlags & WHERE_BTM_LIMIT ); |
| 111540 | 111503 | testcase( pLoop->wsFlags & WHERE_TOP_LIMIT ); |
| 111541 | 111504 | if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 111542 | 111505 | && (j = pIdx->aiColumn[nEq])>=0 |
| 111543 | 111506 | && pIdx->pTable->aCol[j].notNull==0 |
| 111507 | + && (nEq || (pLoop->wsFlags & WHERE_BTM_LIMIT)==0) |
| 111544 | 111508 | ){ |
| 111545 | 111509 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 111546 | 111510 | VdbeComment((v, "%s", pIdx->pTable->aCol[j].zName)); |
| 111547 | 111511 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 111548 | 111512 | } |
| | @@ -111856,11 +111820,11 @@ |
| 111856 | 111820 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 111857 | 111821 | if( pAlt==0 ) continue; |
| 111858 | 111822 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
| 111859 | 111823 | testcase( pAlt->eOperator & WO_EQ ); |
| 111860 | 111824 | testcase( pAlt->eOperator & WO_IN ); |
| 111861 | | - VdbeNoopComment((v, "begin transitive constraint")); |
| 111825 | + VdbeModuleComment((v, "begin transitive constraint")); |
| 111862 | 111826 | pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 111863 | 111827 | if( pEAlt ){ |
| 111864 | 111828 | *pEAlt = *pAlt->pExpr; |
| 111865 | 111829 | pEAlt->pLeft = pE->pLeft; |
| 111866 | 111830 | sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); |
| | @@ -112313,14 +112277,19 @@ |
| 112313 | 112277 | saved_wsFlags = pNew->wsFlags; |
| 112314 | 112278 | saved_prereq = pNew->prereq; |
| 112315 | 112279 | saved_nOut = pNew->nOut; |
| 112316 | 112280 | pNew->rSetup = 0; |
| 112317 | 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 | + */ |
| 112318 | 112287 | if( pTerm==0 |
| 112319 | 112288 | && saved_nEq==saved_nSkip |
| 112320 | 112289 | && saved_nEq+1<pProbe->nKeyCol |
| 112321 | | - && pProbe->aiRowEst[saved_nEq+1]>50 |
| 112290 | + && pProbe->aiRowEst[saved_nEq+1]>50 /* TUNING: Minimum for skip-scan */ |
| 112322 | 112291 | ){ |
| 112323 | 112292 | LogEst nIter; |
| 112324 | 112293 | pNew->u.btree.nEq++; |
| 112325 | 112294 | pNew->u.btree.nSkip++; |
| 112326 | 112295 | pNew->aLTerm[pNew->nLTerm++] = 0; |
| | @@ -113812,11 +113781,10 @@ |
| 113812 | 113781 | /* Split the WHERE clause into separate subexpressions where each |
| 113813 | 113782 | ** subexpression is separated by an AND operator. |
| 113814 | 113783 | */ |
| 113815 | 113784 | initMaskSet(pMaskSet); |
| 113816 | 113785 | whereClauseInit(&pWInfo->sWC, pWInfo); |
| 113817 | | - sqlite3ExprCodeConstants(pParse, pWhere); |
| 113818 | 113786 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); |
| 113819 | 113787 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
| 113820 | 113788 | |
| 113821 | 113789 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 113822 | 113790 | ** expression and either jump over all of the code or fall thru. |
| | @@ -114127,11 +114095,11 @@ |
| 114127 | 114095 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
| 114128 | 114096 | pWInfo->iContinue = pLevel->addrCont; |
| 114129 | 114097 | } |
| 114130 | 114098 | |
| 114131 | 114099 | /* Done. */ |
| 114132 | | - VdbeNoopComment((v, "Begin WHERE-core")); |
| 114100 | + VdbeModuleComment((v, "Begin WHERE-core")); |
| 114133 | 114101 | return pWInfo; |
| 114134 | 114102 | |
| 114135 | 114103 | /* Jump here if malloc fails */ |
| 114136 | 114104 | whereBeginError: |
| 114137 | 114105 | if( pWInfo ){ |
| | @@ -114154,11 +114122,11 @@ |
| 114154 | 114122 | SrcList *pTabList = pWInfo->pTabList; |
| 114155 | 114123 | sqlite3 *db = pParse->db; |
| 114156 | 114124 | |
| 114157 | 114125 | /* Generate loop termination code. |
| 114158 | 114126 | */ |
| 114159 | | - VdbeNoopComment((v, "End WHERE-core")); |
| 114127 | + VdbeModuleComment((v, "End WHERE-core")); |
| 114160 | 114128 | sqlite3ExprCacheClear(pParse); |
| 114161 | 114129 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
| 114162 | 114130 | int addr; |
| 114163 | 114131 | pLevel = &pWInfo->a[i]; |
| 114164 | 114132 | pLoop = pLevel->pWLoop; |
| | @@ -114200,11 +114168,11 @@ |
| 114200 | 114168 | }else{ |
| 114201 | 114169 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 114202 | 114170 | } |
| 114203 | 114171 | sqlite3VdbeJumpHere(v, addr); |
| 114204 | 114172 | } |
| 114205 | | - VdbeNoopComment((v, "End WHERE-loop%d: %s", i, |
| 114173 | + VdbeModuleComment((v, "End WHERE-loop%d: %s", i, |
| 114206 | 114174 | pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); |
| 114207 | 114175 | } |
| 114208 | 114176 | |
| 114209 | 114177 | /* The "break" point is here, just past the end of the outer loop. |
| 114210 | 114178 | ** Set it. |
| | @@ -123900,10 +123868,13 @@ |
| 123900 | 123868 | } |
| 123901 | 123869 | |
| 123902 | 123870 | #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \ |
| 123903 | 123871 | v = (v & mask1) | ( (*ptr++) << shift ); \ |
| 123904 | 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; } |
| 123905 | 123876 | |
| 123906 | 123877 | /* |
| 123907 | 123878 | ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| 123908 | 123879 | ** Return the number of bytes read, or 0 on error. |
| 123909 | 123880 | ** The value is stored in *v. |
| | @@ -123912,11 +123883,11 @@ |
| 123912 | 123883 | const char *pStart = p; |
| 123913 | 123884 | u32 a; |
| 123914 | 123885 | u64 b; |
| 123915 | 123886 | int shift; |
| 123916 | 123887 | |
| 123917 | | - GETVARINT_STEP(a, p, 0, 0x00, 0x80, *v, 1); |
| 123888 | + GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1); |
| 123918 | 123889 | GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2); |
| 123919 | 123890 | GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3); |
| 123920 | 123891 | GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4); |
| 123921 | 123892 | b = (a & 0x0FFFFFFF ); |
| 123922 | 123893 | |
| | @@ -123924,11 +123895,11 @@ |
| 123924 | 123895 | u64 c = *p++; |
| 123925 | 123896 | b += (c&0x7F) << shift; |
| 123926 | 123897 | if( (c & 0x80)==0 ) break; |
| 123927 | 123898 | } |
| 123928 | 123899 | *v = b; |
| 123929 | | - return p - pStart; |
| 123900 | + return (int)(p - pStart); |
| 123930 | 123901 | } |
| 123931 | 123902 | |
| 123932 | 123903 | /* |
| 123933 | 123904 | ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a |
| 123934 | 123905 | ** 32-bit integer before it is returned. |
| | @@ -123935,11 +123906,11 @@ |
| 123935 | 123906 | */ |
| 123936 | 123907 | SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){ |
| 123937 | 123908 | u32 a; |
| 123938 | 123909 | |
| 123939 | 123910 | #ifndef fts3GetVarint32 |
| 123940 | | - GETVARINT_STEP(a, p, 0, 0x00, 0x80, *pi, 1); |
| 123911 | + GETVARINT_INIT(a, p, 0, 0x00, 0x80, *pi, 1); |
| 123941 | 123912 | #else |
| 123942 | 123913 | a = (*p++); |
| 123943 | 123914 | assert( a & 0x80 ); |
| 123944 | 123915 | #endif |
| 123945 | 123916 | |
| 123946 | 123917 | |