| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.38.0. By combining all the individual C code files into this |
| 3 | +** version 3.39.0. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -450,13 +450,13 @@ |
| 450 | 450 | ** |
| 451 | 451 | ** See also: [sqlite3_libversion()], |
| 452 | 452 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 453 | 453 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 454 | 454 | */ |
| 455 | | -#define SQLITE_VERSION "3.38.0" |
| 456 | | -#define SQLITE_VERSION_NUMBER 3038000 |
| 457 | | -#define SQLITE_SOURCE_ID "2022-01-25 00:03:25 a8db69411b0d1275909adeb21027784ada17af24efe3a59ae0ae2a897659ff17" |
| 455 | +#define SQLITE_VERSION "3.39.0" |
| 456 | +#define SQLITE_VERSION_NUMBER 3039000 |
| 457 | +#define SQLITE_SOURCE_ID "2022-04-01 17:23:17 a7d79560a0efd6221ba59ce84bcb4fa94024a901ac4a45e192ddecc6e1b5c78c" |
| 458 | 458 | |
| 459 | 459 | /* |
| 460 | 460 | ** CAPI3REF: Run-Time Library Version Numbers |
| 461 | 461 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 462 | 462 | ** |
| | @@ -870,11 +870,11 @@ |
| 870 | 870 | #define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8)) |
| 871 | 871 | #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8)) |
| 872 | 872 | #define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8)) |
| 873 | 873 | #define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8)) |
| 874 | 874 | #define SQLITE_OK_LOAD_PERMANENTLY (SQLITE_OK | (1<<8)) |
| 875 | | -#define SQLITE_OK_SYMLINK (SQLITE_OK | (2<<8)) |
| 875 | +#define SQLITE_OK_SYMLINK (SQLITE_OK | (2<<8)) /* internal use only */ |
| 876 | 876 | |
| 877 | 877 | /* |
| 878 | 878 | ** CAPI3REF: Flags For File Open Operations |
| 879 | 879 | ** |
| 880 | 880 | ** These bit values are intended for use in the |
| | @@ -4154,11 +4154,11 @@ |
| 4154 | 4154 | ** |
| 4155 | 4155 | ** ^If the most recent error references a specific token in the input |
| 4156 | 4156 | ** SQL, the sqlite3_error_offset() interface returns the byte offset |
| 4157 | 4157 | ** of the start of that token. ^The byte offset returned by |
| 4158 | 4158 | ** sqlite3_error_offset() assumes that the input SQL is UTF8. |
| 4159 | | -** ^If the most error does not reference a specific token in the input |
| 4159 | +** ^If the most recent error does not reference a specific token in the input |
| 4160 | 4160 | ** SQL, then the sqlite3_error_offset() function returns -1. |
| 4161 | 4161 | ** |
| 4162 | 4162 | ** When the serialized [threading mode] is in use, it might be the |
| 4163 | 4163 | ** case that a second error occurs on a separate thread in between |
| 4164 | 4164 | ** the time of the first error and the call to these interfaces. |
| | @@ -4588,10 +4588,14 @@ |
| 4588 | 4588 | ** ^For example, an UPDATE statement might have a WHERE clause that |
| 4589 | 4589 | ** makes it a no-op, but the sqlite3_stmt_readonly() result would still |
| 4590 | 4590 | ** be false. ^Similarly, a CREATE TABLE IF NOT EXISTS statement is a |
| 4591 | 4591 | ** read-only no-op if the table already exists, but |
| 4592 | 4592 | ** sqlite3_stmt_readonly() still returns false for such a statement. |
| 4593 | +** |
| 4594 | +** ^If prepared statement X is an [EXPLAIN] or [EXPLAIN QUERY PLAN] |
| 4595 | +** statement, then sqlite3_stmt_readonly(X) returns the same value as |
| 4596 | +** if the EXPLAIN or EXPLAIN QUERY PLAN prefix were omitted. |
| 4593 | 4597 | */ |
| 4594 | 4598 | SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt); |
| 4595 | 4599 | |
| 4596 | 4600 | /* |
| 4597 | 4601 | ** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement |
| | @@ -4656,10 +4660,12 @@ |
| 4656 | 4660 | ** still make the distinction between protected and unprotected |
| 4657 | 4661 | ** sqlite3_value objects even when not strictly required. |
| 4658 | 4662 | ** |
| 4659 | 4663 | ** ^The sqlite3_value objects that are passed as parameters into the |
| 4660 | 4664 | ** implementation of [application-defined SQL functions] are protected. |
| 4665 | +** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()] |
| 4666 | +** are protected. |
| 4661 | 4667 | ** ^The sqlite3_value object returned by |
| 4662 | 4668 | ** [sqlite3_column_value()] is unprotected. |
| 4663 | 4669 | ** Unprotected sqlite3_value objects may only be used as arguments |
| 4664 | 4670 | ** to [sqlite3_result_value()], [sqlite3_bind_value()], and |
| 4665 | 4671 | ** [sqlite3_value_dup()]. |
| | @@ -5276,10 +5282,14 @@ |
| 5276 | 5282 | ** bytes in the string, not the number of characters. |
| 5277 | 5283 | ** |
| 5278 | 5284 | ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), |
| 5279 | 5285 | ** even empty strings, are always zero-terminated. ^The return |
| 5280 | 5286 | ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. |
| 5287 | +** |
| 5288 | +** ^Strings returned by sqlite3_column_text16() always have the endianness |
| 5289 | +** which is native to the platform, regardless of the text encoding set |
| 5290 | +** for the database. |
| 5281 | 5291 | ** |
| 5282 | 5292 | ** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an |
| 5283 | 5293 | ** [unprotected sqlite3_value] object. In a multithreaded environment, |
| 5284 | 5294 | ** an unprotected sqlite3_value object may only be used safely with |
| 5285 | 5295 | ** [sqlite3_bind_value()] and [sqlite3_result_value()]. |
| | @@ -5290,11 +5300,11 @@ |
| 5290 | 5300 | ** Hence, the sqlite3_column_value() interface |
| 5291 | 5301 | ** is normally only useful within the implementation of |
| 5292 | 5302 | ** [application-defined SQL functions] or [virtual tables], not within |
| 5293 | 5303 | ** top-level application code. |
| 5294 | 5304 | ** |
| 5295 | | -** The these routines may attempt to convert the datatype of the result. |
| 5305 | +** These routines may attempt to convert the datatype of the result. |
| 5296 | 5306 | ** ^For example, if the internal representation is FLOAT and a text result |
| 5297 | 5307 | ** is requested, [sqlite3_snprintf()] is used internally to perform the |
| 5298 | 5308 | ** conversion automatically. ^(The following table details the conversions |
| 5299 | 5309 | ** that are applied: |
| 5300 | 5310 | ** |
| | @@ -5315,11 +5325,11 @@ |
| 5315 | 5325 | ** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER |
| 5316 | 5326 | ** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL |
| 5317 | 5327 | ** <tr><td> TEXT <td> BLOB <td> No change |
| 5318 | 5328 | ** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER |
| 5319 | 5329 | ** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL |
| 5320 | | -** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed |
| 5330 | +** <tr><td> BLOB <td> TEXT <td> [CAST] to TEXT, ensure zero terminator |
| 5321 | 5331 | ** </table> |
| 5322 | 5332 | ** </blockquote>)^ |
| 5323 | 5333 | ** |
| 5324 | 5334 | ** Note that when type conversions occur, pointers returned by prior |
| 5325 | 5335 | ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or |
| | @@ -5887,11 +5897,12 @@ |
| 5887 | 5897 | ** |
| 5888 | 5898 | ** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value] |
| 5889 | 5899 | ** object D and returns a pointer to that copy. ^The [sqlite3_value] returned |
| 5890 | 5900 | ** is a [protected sqlite3_value] object even if the input is not. |
| 5891 | 5901 | ** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a |
| 5892 | | -** memory allocation fails. |
| 5902 | +** memory allocation fails. ^If V is a [pointer value], then the result |
| 5903 | +** of sqlite3_value_dup(V) is a NULL value. |
| 5893 | 5904 | ** |
| 5894 | 5905 | ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object |
| 5895 | 5906 | ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer |
| 5896 | 5907 | ** then sqlite3_value_free(V) is a harmless no-op. |
| 5897 | 5908 | */ |
| | @@ -7435,28 +7446,60 @@ |
| 7435 | 7446 | /* |
| 7436 | 7447 | ** CAPI3REF: Virtual Table Constraint Operator Codes |
| 7437 | 7448 | ** |
| 7438 | 7449 | ** These macros define the allowed values for the |
| 7439 | 7450 | ** [sqlite3_index_info].aConstraint[].op field. Each value represents |
| 7440 | | -** an operator that is part of a constraint term in the wHERE clause of |
| 7451 | +** an operator that is part of a constraint term in the WHERE clause of |
| 7441 | 7452 | ** a query that uses a [virtual table]. |
| 7453 | +** |
| 7454 | +** ^The left-hand operand of the operator is given by the corresponding |
| 7455 | +** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand |
| 7456 | +** operand is the rowid. |
| 7457 | +** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET |
| 7458 | +** operators have no left-hand operand, and so for those operators the |
| 7459 | +** corresponding aConstraint[].iColumn is meaningless and should not be |
| 7460 | +** used. |
| 7461 | +** |
| 7462 | +** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through |
| 7463 | +** value 255 are reserved to represent functions that are overloaded |
| 7464 | +** by the [xFindFunction|xFindFunction method] of the virtual table |
| 7465 | +** implementation. |
| 7466 | +** |
| 7467 | +** The right-hand operands for each constraint might be accessible using |
| 7468 | +** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand |
| 7469 | +** operand is only available if it appears as a single constant literal |
| 7470 | +** in the input SQL. If the right-hand operand is another column or an |
| 7471 | +** expression (even a constant expression) or a parameter, then the |
| 7472 | +** sqlite3_vtab_rhs_value() probably will not be able to extract it. |
| 7473 | +** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and |
| 7474 | +** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand |
| 7475 | +** and hence calls to sqlite3_vtab_rhs_value() for those operators will |
| 7476 | +** always return SQLITE_NOTFOUND. |
| 7477 | +** |
| 7478 | +** The collating sequence to be used for comparison can be found using |
| 7479 | +** the [sqlite3_vtab_collation()] interface. For most real-world virtual |
| 7480 | +** tables, the collating sequence of constraints does not matter (for example |
| 7481 | +** because the constraints are numeric) and so the sqlite3_vtab_collation() |
| 7482 | +** interface is no commonly needed. |
| 7442 | 7483 | */ |
| 7443 | | -#define SQLITE_INDEX_CONSTRAINT_EQ 2 |
| 7444 | | -#define SQLITE_INDEX_CONSTRAINT_GT 4 |
| 7445 | | -#define SQLITE_INDEX_CONSTRAINT_LE 8 |
| 7446 | | -#define SQLITE_INDEX_CONSTRAINT_LT 16 |
| 7447 | | -#define SQLITE_INDEX_CONSTRAINT_GE 32 |
| 7448 | | -#define SQLITE_INDEX_CONSTRAINT_MATCH 64 |
| 7449 | | -#define SQLITE_INDEX_CONSTRAINT_LIKE 65 |
| 7450 | | -#define SQLITE_INDEX_CONSTRAINT_GLOB 66 |
| 7451 | | -#define SQLITE_INDEX_CONSTRAINT_REGEXP 67 |
| 7452 | | -#define SQLITE_INDEX_CONSTRAINT_NE 68 |
| 7453 | | -#define SQLITE_INDEX_CONSTRAINT_ISNOT 69 |
| 7454 | | -#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70 |
| 7455 | | -#define SQLITE_INDEX_CONSTRAINT_ISNULL 71 |
| 7456 | | -#define SQLITE_INDEX_CONSTRAINT_IS 72 |
| 7457 | | -#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150 |
| 7484 | +#define SQLITE_INDEX_CONSTRAINT_EQ 2 |
| 7485 | +#define SQLITE_INDEX_CONSTRAINT_GT 4 |
| 7486 | +#define SQLITE_INDEX_CONSTRAINT_LE 8 |
| 7487 | +#define SQLITE_INDEX_CONSTRAINT_LT 16 |
| 7488 | +#define SQLITE_INDEX_CONSTRAINT_GE 32 |
| 7489 | +#define SQLITE_INDEX_CONSTRAINT_MATCH 64 |
| 7490 | +#define SQLITE_INDEX_CONSTRAINT_LIKE 65 |
| 7491 | +#define SQLITE_INDEX_CONSTRAINT_GLOB 66 |
| 7492 | +#define SQLITE_INDEX_CONSTRAINT_REGEXP 67 |
| 7493 | +#define SQLITE_INDEX_CONSTRAINT_NE 68 |
| 7494 | +#define SQLITE_INDEX_CONSTRAINT_ISNOT 69 |
| 7495 | +#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70 |
| 7496 | +#define SQLITE_INDEX_CONSTRAINT_ISNULL 71 |
| 7497 | +#define SQLITE_INDEX_CONSTRAINT_IS 72 |
| 7498 | +#define SQLITE_INDEX_CONSTRAINT_LIMIT 73 |
| 7499 | +#define SQLITE_INDEX_CONSTRAINT_OFFSET 74 |
| 7500 | +#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150 |
| 7458 | 7501 | |
| 7459 | 7502 | /* |
| 7460 | 7503 | ** CAPI3REF: Register A Virtual Table Implementation |
| 7461 | 7504 | ** METHOD: sqlite3 |
| 7462 | 7505 | ** |
| | @@ -7481,11 +7524,11 @@ |
| 7481 | 7524 | ** ^The sqlite3_create_module() |
| 7482 | 7525 | ** interface is equivalent to sqlite3_create_module_v2() with a NULL |
| 7483 | 7526 | ** destructor. |
| 7484 | 7527 | ** |
| 7485 | 7528 | ** ^If the third parameter (the pointer to the sqlite3_module object) is |
| 7486 | | -** NULL then no new module is create and any existing modules with the |
| 7529 | +** NULL then no new module is created and any existing modules with the |
| 7487 | 7530 | ** same name are dropped. |
| 7488 | 7531 | ** |
| 7489 | 7532 | ** See also: [sqlite3_drop_modules()] |
| 7490 | 7533 | */ |
| 7491 | 7534 | SQLITE_API int sqlite3_create_module( |
| | @@ -9812,28 +9855,29 @@ |
| 9812 | 9855 | |
| 9813 | 9856 | /* |
| 9814 | 9857 | ** CAPI3REF: Determine if a virtual table query is DISTINCT |
| 9815 | 9858 | ** METHOD: sqlite3_index_info |
| 9816 | 9859 | ** |
| 9817 | | -** This API may only be used from within an xBestIndex() callback. The |
| 9818 | | -** results of calling it from outside of an xBestIndex() callback are |
| 9819 | | -** undefined and probably harmful. |
| 9860 | +** This API may only be used from within an [xBestIndex|xBestIndex method] |
| 9861 | +** of a [virtual table] implementation. The result of calling this |
| 9862 | +** interface from outside of xBestIndex() is undefined and probably harmful. |
| 9820 | 9863 | ** |
| 9821 | | -** ^The sqlite3_vtab_distinct() returns an integer that is either 0, 1, or |
| 9822 | | -** 2. The integer returned by sqlite3_vtab_distinct() gives the virtual table |
| 9823 | | -** additional information about how the query planner wants the output to be |
| 9824 | | -** ordered. As long as the virtual table can meet the ordering requirements |
| 9825 | | -** of the query planner, it may set the "orderByConsumed" flag. |
| 9864 | +** ^The sqlite3_vtab_distinct() interface returns an integer between 0 and |
| 9865 | +** 3. The integer returned by sqlite3_vtab_distinct() |
| 9866 | +** gives the virtual table additional information about how the query |
| 9867 | +** planner wants the output to be ordered. As long as the virtual table |
| 9868 | +** can meet the ordering requirements of the query planner, it may set |
| 9869 | +** the "orderByConsumed" flag. |
| 9826 | 9870 | ** |
| 9827 | 9871 | ** <ol><li value="0"><p> |
| 9828 | 9872 | ** ^If the sqlite3_vtab_distinct() interface returns 0, that means |
| 9829 | 9873 | ** that the query planner needs the virtual table to return all rows in the |
| 9830 | 9874 | ** sort order defined by the "nOrderBy" and "aOrderBy" fields of the |
| 9831 | 9875 | ** [sqlite3_index_info] object. This is the default expectation. If the |
| 9832 | 9876 | ** virtual table outputs all rows in sorted order, then it is always safe for |
| 9833 | 9877 | ** the xBestIndex method to set the "orderByConsumed" flag, regardless of |
| 9834 | | -** what the return value from sqlite3_vtab_distinct(). |
| 9878 | +** the return value from sqlite3_vtab_distinct(). |
| 9835 | 9879 | ** <li value="1"><p> |
| 9836 | 9880 | ** ^(If the sqlite3_vtab_distinct() interface returns 1, that means |
| 9837 | 9881 | ** that the query planner does not need the rows to be returned in sorted order |
| 9838 | 9882 | ** as long as all rows with the same values in all columns identified by the |
| 9839 | 9883 | ** "aOrderBy" field are adjacent.)^ This mode is used when the query planner |
| | @@ -9842,16 +9886,23 @@ |
| 9842 | 9886 | ** ^(If the sqlite3_vtab_distinct() interface returns 2, that means |
| 9843 | 9887 | ** that the query planner does not need the rows returned in any particular |
| 9844 | 9888 | ** order, as long as rows with the same values in all "aOrderBy" columns |
| 9845 | 9889 | ** are adjacent.)^ ^(Furthermore, only a single row for each particular |
| 9846 | 9890 | ** combination of values in the columns identified by the "aOrderBy" field |
| 9847 | | -** needs to be returned.)^ ^It is ok always for two or more rows with the same |
| 9891 | +** needs to be returned.)^ ^It is always ok for two or more rows with the same |
| 9848 | 9892 | ** values in all "aOrderBy" columns to be returned, as long as all such rows |
| 9849 | 9893 | ** are adjacent. ^The virtual table may, if it chooses, omit extra rows |
| 9850 | 9894 | ** that have the same value for all columns identified by "aOrderBy". |
| 9851 | 9895 | ** ^However omitting the extra rows is optional. |
| 9852 | 9896 | ** This mode is used for a DISTINCT query. |
| 9897 | +** <li value="3"><p> |
| 9898 | +** ^(If the sqlite3_vtab_distinct() interface returns 3, that means |
| 9899 | +** that the query planner needs only distinct rows but it does need the |
| 9900 | +** rows to be sorted.)^ ^The virtual table implementation is free to omit |
| 9901 | +** rows that are identical in all aOrderBy columns, if it wants to, but |
| 9902 | +** it is not required to omit any rows. This mode is used for queries |
| 9903 | +** that have both DISTINCT and ORDER BY clauses. |
| 9853 | 9904 | ** </ol> |
| 9854 | 9905 | ** |
| 9855 | 9906 | ** ^For the purposes of comparing virtual table output values to see if the |
| 9856 | 9907 | ** values are same value for sorting purposes, two NULL values are considered |
| 9857 | 9908 | ** to be the same. In other words, the comparison operator is "IS" |
| | @@ -9873,35 +9924,171 @@ |
| 9873 | 9924 | ** valid to do so, on the other hand, might cause SQLite to return incorrect |
| 9874 | 9925 | ** results. |
| 9875 | 9926 | */ |
| 9876 | 9927 | SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info*); |
| 9877 | 9928 | |
| 9929 | +/* |
| 9930 | +** CAPI3REF: Identify and handle IN constraints in xBestIndex |
| 9931 | +** |
| 9932 | +** This interface may only be used from within an |
| 9933 | +** [xBestIndex|xBestIndex() method] of a [virtual table] implementation. |
| 9934 | +** The result of invoking this interface from any other context is |
| 9935 | +** undefined and probably harmful. |
| 9936 | +** |
| 9937 | +** ^(A constraint on a virtual table of the form |
| 9938 | +** "[IN operator|column IN (...)]" is |
| 9939 | +** communicated to the xBestIndex method as a |
| 9940 | +** [SQLITE_INDEX_CONSTRAINT_EQ] constraint.)^ If xBestIndex wants to use |
| 9941 | +** this constraint, it must set the corresponding |
| 9942 | +** aConstraintUsage[].argvIndex to a postive integer. ^(Then, under |
| 9943 | +** the usual mode of handling IN operators, SQLite generates [bytecode] |
| 9944 | +** that invokes the [xFilter|xFilter() method] once for each value |
| 9945 | +** on the right-hand side of the IN operator.)^ Thus the virtual table |
| 9946 | +** only sees a single value from the right-hand side of the IN operator |
| 9947 | +** at a time. |
| 9948 | +** |
| 9949 | +** In some cases, however, it would be advantageous for the virtual |
| 9950 | +** table to see all values on the right-hand of the IN operator all at |
| 9951 | +** once. The sqlite3_vtab_in() interfaces facilitates this in two ways: |
| 9952 | +** |
| 9953 | +** <ol> |
| 9954 | +** <li><p> |
| 9955 | +** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero) |
| 9956 | +** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint |
| 9957 | +** is an [IN operator] that can be processed all at once. ^In other words, |
| 9958 | +** sqlite3_vtab_in() with -1 in the third argument is a mechanism |
| 9959 | +** by which the virtual table can ask SQLite if all-at-once processing |
| 9960 | +** of the IN operator is even possible. |
| 9961 | +** |
| 9962 | +** <li><p> |
| 9963 | +** ^A call to sqlite3_vtab_in(P,N,F) with F==1 or F==0 indicates |
| 9964 | +** to SQLite that the virtual table does or does not want to process |
| 9965 | +** the IN operator all-at-once, respectively. ^Thus when the third |
| 9966 | +** parameter (F) is non-negative, this interface is the mechanism by |
| 9967 | +** which the virtual table tells SQLite how it wants to process the |
| 9968 | +** IN operator. |
| 9969 | +** </ol> |
| 9970 | +** |
| 9971 | +** ^The sqlite3_vtab_in(P,N,F) interface can be invoked multiple times |
| 9972 | +** within the same xBestIndex method call. ^For any given P,N pair, |
| 9973 | +** the return value from sqlite3_vtab_in(P,N,F) will always be the same |
| 9974 | +** within the same xBestIndex call. ^If the interface returns true |
| 9975 | +** (non-zero), that means that the constraint is an IN operator |
| 9976 | +** that can be processed all-at-once. ^If the constraint is not an IN |
| 9977 | +** operator or cannot be processed all-at-once, then the interface returns |
| 9978 | +** false. |
| 9979 | +** |
| 9980 | +** ^(All-at-once processing of the IN operator is selected if both of the |
| 9981 | +** following conditions are met: |
| 9982 | +** |
| 9983 | +** <ol> |
| 9984 | +** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive |
| 9985 | +** integer. This is how the virtual table tells SQLite that it wants to |
| 9986 | +** use the N-th constraint. |
| 9987 | +** |
| 9988 | +** <li><p> The last call to sqlite3_vtab_in(P,N,F) for which F was |
| 9989 | +** non-negative had F>=1. |
| 9990 | +** </ol>)^ |
| 9991 | +** |
| 9992 | +** ^If either or both of the conditions above are false, then SQLite uses |
| 9993 | +** the traditional one-at-a-time processing strategy for the IN constraint. |
| 9994 | +** ^If both conditions are true, then the argvIndex-th parameter to the |
| 9995 | +** xFilter method will be an [sqlite3_value] that appears to be NULL, |
| 9996 | +** but which can be passed to [sqlite3_vtab_in_first()] and |
| 9997 | +** [sqlite3_vtab_in_next()] to find all values on the right-hand side |
| 9998 | +** of the IN constraint. |
| 9999 | +*/ |
| 10000 | +SQLITE_API int sqlite3_vtab_in(sqlite3_index_info*, int iCons, int bHandle); |
| 10001 | + |
| 10002 | +/* |
| 10003 | +** CAPI3REF: Find all elements on the right-hand side of an IN constraint. |
| 10004 | +** |
| 10005 | +** These interfaces are only useful from within the |
| 10006 | +** [xFilter|xFilter() method] of a [virtual table] implementation. |
| 10007 | +** The result of invoking these interfaces from any other context |
| 10008 | +** is undefined and probably harmful. |
| 10009 | +** |
| 10010 | +** The X parameter in a call to sqlite3_vtab_in_first(X,P) or |
| 10011 | +** sqlite3_vtab_in_next(X,P) must be one of the parameters to the |
| 10012 | +** xFilter method which invokes these routines, and specifically |
| 10013 | +** a parameter that was previously selected for all-at-once IN constraint |
| 10014 | +** processing use the [sqlite3_vtab_in()] interface in the |
| 10015 | +** [xBestIndex|xBestIndex method]. ^(If the X parameter is not |
| 10016 | +** an xFilter argument that was selected for all-at-once IN constraint |
| 10017 | +** processing, then these routines return [SQLITE_MISUSE])^ or perhaps |
| 10018 | +** exhibit some other undefined or harmful behavior. |
| 10019 | +** |
| 10020 | +** ^(Use these routines to access all values on the right-hand side |
| 10021 | +** of the IN constraint using code like the following: |
| 10022 | +** |
| 10023 | +** <blockquote><pre> |
| 10024 | +** for(rc=sqlite3_vtab_in_first(pList, &pVal); |
| 10025 | +** rc==SQLITE_OK && pVal |
| 10026 | +** rc=sqlite3_vtab_in_next(pList, &pVal) |
| 10027 | +** ){ |
| 10028 | +** // do something with pVal |
| 10029 | +** } |
| 10030 | +** if( rc!=SQLITE_OK ){ |
| 10031 | +** // an error has occurred |
| 10032 | +** } |
| 10033 | +** </pre></blockquote>)^ |
| 10034 | +** |
| 10035 | +** ^On success, the sqlite3_vtab_in_first(X,P) and sqlite3_vtab_in_next(X,P) |
| 10036 | +** routines return SQLITE_OK and set *P to point to the first or next value |
| 10037 | +** on the RHS of the IN constraint. ^If there are no more values on the |
| 10038 | +** right hand side of the IN constraint, then *P is set to NULL and these |
| 10039 | +** routines return [SQLITE_DONE]. ^The return value might be |
| 10040 | +** some other value, such as SQLITE_NOMEM, in the event of a malfunction. |
| 10041 | +** |
| 10042 | +** The *ppOut values returned by these routines are only valid until the |
| 10043 | +** next call to either of these routines or until the end of the xFilter |
| 10044 | +** method from which these routines were called. If the virtual table |
| 10045 | +** implementation needs to retain the *ppOut values for longer, it must make |
| 10046 | +** copies. The *ppOut values are [protected sqlite3_value|protected]. |
| 10047 | +*/ |
| 10048 | +SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut); |
| 10049 | +SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut); |
| 10050 | + |
| 9878 | 10051 | /* |
| 9879 | 10052 | ** CAPI3REF: Constraint values in xBestIndex() |
| 9880 | 10053 | ** METHOD: sqlite3_index_info |
| 9881 | 10054 | ** |
| 9882 | | -** This API may only be used from within an xBestIndex() callback. The |
| 9883 | | -** results of calling it from outside of an xBestIndex() callback are |
| 9884 | | -** undefined and probably harmful. |
| 10055 | +** This API may only be used from within the [xBestIndex|xBestIndex method] |
| 10056 | +** of a [virtual table] implementation. The result of calling this interface |
| 10057 | +** from outside of an xBestIndex method are undefined and probably harmful. |
| 9885 | 10058 | ** |
| 9886 | 10059 | ** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within |
| 9887 | 10060 | ** the [xBestIndex] method of a [virtual table] implementation, with P being |
| 9888 | 10061 | ** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and |
| 9889 | 10062 | ** J being a 0-based index into P->aConstraint[], then this routine |
| 9890 | | -** attempts to set *V to be the value on the right-hand side of |
| 9891 | | -** that constraint if the right-hand side is a known constant. ^If the |
| 9892 | | -** right-hand side of the constraint is not known, then *V is set to a NULL |
| 9893 | | -** pointer. ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if |
| 10063 | +** attempts to set *V to the value of the right-hand operand of |
| 10064 | +** that constraint if the right-hand operand is known. ^If the |
| 10065 | +** right-hand operand is not known, then *V is set to a NULL pointer. |
| 10066 | +** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if |
| 9894 | 10067 | ** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V) |
| 9895 | 10068 | ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th |
| 9896 | 10069 | ** constraint is not available. ^The sqlite3_vtab_rhs_value() interface |
| 9897 | 10070 | ** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if |
| 9898 | 10071 | ** something goes wrong. |
| 9899 | 10072 | ** |
| 9900 | | -** ^The sqlite3_value object returned in *V remains valid for the duration of |
| 9901 | | -** the xBestIndex method code. ^When xBestIndex returns, the sqlite3_value |
| 9902 | | -** object returned by sqlite3_vtab_rhs_value() is automatically deallocated. |
| 10073 | +** The sqlite3_vtab_rhs_value() interface is usually only successful if |
| 10074 | +** the right-hand operand of a constraint is a literal value in the original |
| 10075 | +** SQL statement. If the right-hand operand is an expression or a reference |
| 10076 | +** to some other column or a [host parameter], then sqlite3_vtab_rhs_value() |
| 10077 | +** will probably return [SQLITE_NOTFOUND]. |
| 10078 | +** |
| 10079 | +** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and |
| 10080 | +** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such |
| 10081 | +** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^ |
| 10082 | +** |
| 10083 | +** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value |
| 10084 | +** and remains valid for the duration of the xBestIndex method call. |
| 10085 | +** ^When xBestIndex returns, the sqlite3_value object returned by |
| 10086 | +** sqlite3_vtab_rhs_value() is automatically deallocated. |
| 10087 | +** |
| 10088 | +** The "_rhs_" in the name of this routine is an abbreviation for |
| 10089 | +** "Right-Hand Side". |
| 9903 | 10090 | */ |
| 9904 | 10091 | SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal); |
| 9905 | 10092 | |
| 9906 | 10093 | /* |
| 9907 | 10094 | ** CAPI3REF: Conflict resolution modes |
| | @@ -14340,11 +14527,11 @@ |
| 14340 | 14527 | ** one parameter that destructors normally want. So we have to introduce |
| 14341 | 14528 | ** this magic value that the code knows to handle differently. Any |
| 14342 | 14529 | ** pointer will work here as long as it is distinct from SQLITE_STATIC |
| 14343 | 14530 | ** and SQLITE_TRANSIENT. |
| 14344 | 14531 | */ |
| 14345 | | -#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3OomFault) |
| 14532 | +#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3OomClear) |
| 14346 | 14533 | |
| 14347 | 14534 | /* |
| 14348 | 14535 | ** When SQLITE_OMIT_WSD is defined, it means that the target platform does |
| 14349 | 14536 | ** not support Writable Static Data (WSD) such as global and static variables. |
| 14350 | 14537 | ** All variables must either be on the stack or dynamically allocated from |
| | @@ -14468,14 +14655,15 @@ |
| 14468 | 14655 | #define BMS ((int)(sizeof(Bitmask)*8)) |
| 14469 | 14656 | |
| 14470 | 14657 | /* |
| 14471 | 14658 | ** A bit in a Bitmask |
| 14472 | 14659 | */ |
| 14473 | | -#define MASKBIT(n) (((Bitmask)1)<<(n)) |
| 14474 | | -#define MASKBIT64(n) (((u64)1)<<(n)) |
| 14475 | | -#define MASKBIT32(n) (((unsigned int)1)<<(n)) |
| 14476 | | -#define ALLBITS ((Bitmask)-1) |
| 14660 | +#define MASKBIT(n) (((Bitmask)1)<<(n)) |
| 14661 | +#define MASKBIT64(n) (((u64)1)<<(n)) |
| 14662 | +#define MASKBIT32(n) (((unsigned int)1)<<(n)) |
| 14663 | +#define SMASKBIT32(n) ((n)<=31?((unsigned int)1)<<(n):0) |
| 14664 | +#define ALLBITS ((Bitmask)-1) |
| 14477 | 14665 | |
| 14478 | 14666 | /* A VList object records a mapping between parameters/variables/wildcards |
| 14479 | 14667 | ** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer |
| 14480 | 14668 | ** variable number associated with that parameter. See the format description |
| 14481 | 14669 | ** on the sqlite3VListAdd() routine for more information. A VList is really |
| | @@ -14533,18 +14721,19 @@ |
| 14533 | 14721 | ** Handle type for pages. |
| 14534 | 14722 | */ |
| 14535 | 14723 | typedef struct PgHdr DbPage; |
| 14536 | 14724 | |
| 14537 | 14725 | /* |
| 14538 | | -** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is |
| 14726 | +** Page number PAGER_SJ_PGNO is never used in an SQLite database (it is |
| 14539 | 14727 | ** reserved for working around a windows/posix incompatibility). It is |
| 14540 | 14728 | ** used in the journal to signify that the remainder of the journal file |
| 14541 | 14729 | ** is devoted to storing a super-journal name - there are no more pages to |
| 14542 | 14730 | ** roll back. See comments for function writeSuperJournal() in pager.c |
| 14543 | 14731 | ** for details. |
| 14544 | 14732 | */ |
| 14545 | | -#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1)) |
| 14733 | +#define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1)) |
| 14734 | +#define PAGER_SJ_PGNO(x) ((x)->lckPgno) |
| 14546 | 14735 | |
| 14547 | 14736 | /* |
| 14548 | 14737 | ** Allowed values for the flags parameter to sqlite3PagerOpen(). |
| 14549 | 14738 | ** |
| 14550 | 14739 | ** NOTE: These values must match the corresponding BTREE_ values in btree.h. |
| | @@ -15217,11 +15406,10 @@ |
| 15217 | 15406 | SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */ |
| 15218 | 15407 | Table *pTab; /* Used when p4type is P4_TABLE */ |
| 15219 | 15408 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
| 15220 | 15409 | Expr *pExpr; /* Used when p4type is P4_EXPR */ |
| 15221 | 15410 | #endif |
| 15222 | | - int (*xAdvance)(BtCursor *, int); |
| 15223 | 15411 | } p4; |
| 15224 | 15412 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 15225 | 15413 | char *zComment; /* Comment to improve readability */ |
| 15226 | 15414 | #endif |
| 15227 | 15415 | #ifdef VDBE_PROFILE |
| | @@ -15268,25 +15456,23 @@ |
| 15268 | 15456 | #define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */ |
| 15269 | 15457 | #define P4_STATIC (-1) /* Pointer to a static string */ |
| 15270 | 15458 | #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */ |
| 15271 | 15459 | #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */ |
| 15272 | 15460 | #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */ |
| 15273 | | -#define P4_ADVANCE (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */ |
| 15274 | | -#define P4_TABLE (-6) /* P4 is a pointer to a Table structure */ |
| 15461 | +#define P4_TABLE (-5) /* P4 is a pointer to a Table structure */ |
| 15275 | 15462 | /* Above do not own any resources. Must free those below */ |
| 15276 | | -#define P4_FREE_IF_LE (-7) |
| 15277 | | -#define P4_DYNAMIC (-7) /* Pointer to memory from sqliteMalloc() */ |
| 15278 | | -#define P4_FUNCDEF (-8) /* P4 is a pointer to a FuncDef structure */ |
| 15279 | | -#define P4_KEYINFO (-9) /* P4 is a pointer to a KeyInfo structure */ |
| 15280 | | -#define P4_EXPR (-10) /* P4 is a pointer to an Expr tree */ |
| 15281 | | -#define P4_MEM (-11) /* P4 is a pointer to a Mem* structure */ |
| 15282 | | -#define P4_VTAB (-12) /* P4 is a pointer to an sqlite3_vtab structure */ |
| 15283 | | -#define P4_REAL (-13) /* P4 is a 64-bit floating point value */ |
| 15284 | | -#define P4_INT64 (-14) /* P4 is a 64-bit signed integer */ |
| 15285 | | -#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ |
| 15286 | | -#define P4_FUNCCTX (-16) /* P4 is a pointer to an sqlite3_context object */ |
| 15287 | | -#define P4_DYNBLOB (-17) /* Pointer to memory from sqliteMalloc() */ |
| 15463 | +#define P4_FREE_IF_LE (-6) |
| 15464 | +#define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */ |
| 15465 | +#define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */ |
| 15466 | +#define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */ |
| 15467 | +#define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */ |
| 15468 | +#define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */ |
| 15469 | +#define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */ |
| 15470 | +#define P4_REAL (-12) /* P4 is a 64-bit floating point value */ |
| 15471 | +#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ |
| 15472 | +#define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */ |
| 15473 | +#define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */ |
| 15288 | 15474 | |
| 15289 | 15475 | /* Error message codes for OP_Halt */ |
| 15290 | 15476 | #define P5_ConstraintNotNull 1 |
| 15291 | 15477 | #define P5_ConstraintUnique 2 |
| 15292 | 15478 | #define P5_ConstraintCheck 3 |
| | @@ -15327,46 +15513,46 @@ |
| 15327 | 15513 | /* Automatically generated. Do not edit */ |
| 15328 | 15514 | /* See the tool/mkopcodeh.tcl script for details */ |
| 15329 | 15515 | #define OP_Savepoint 0 |
| 15330 | 15516 | #define OP_AutoCommit 1 |
| 15331 | 15517 | #define OP_Transaction 2 |
| 15332 | | -#define OP_SorterNext 3 /* jump */ |
| 15333 | | -#define OP_Prev 4 /* jump */ |
| 15334 | | -#define OP_Next 5 /* jump */ |
| 15335 | | -#define OP_Checkpoint 6 |
| 15336 | | -#define OP_JournalMode 7 |
| 15337 | | -#define OP_Vacuum 8 |
| 15338 | | -#define OP_VFilter 9 /* jump, synopsis: iplan=r[P3] zplan='P4' */ |
| 15339 | | -#define OP_VUpdate 10 /* synopsis: data=r[P3@P2] */ |
| 15340 | | -#define OP_Goto 11 /* jump */ |
| 15341 | | -#define OP_Gosub 12 /* jump */ |
| 15342 | | -#define OP_InitCoroutine 13 /* jump */ |
| 15343 | | -#define OP_Yield 14 /* jump */ |
| 15344 | | -#define OP_MustBeInt 15 /* jump */ |
| 15345 | | -#define OP_Jump 16 /* jump */ |
| 15346 | | -#define OP_Once 17 /* jump */ |
| 15347 | | -#define OP_If 18 /* jump */ |
| 15518 | +#define OP_Checkpoint 3 |
| 15519 | +#define OP_JournalMode 4 |
| 15520 | +#define OP_Vacuum 5 |
| 15521 | +#define OP_VFilter 6 /* jump, synopsis: iplan=r[P3] zplan='P4' */ |
| 15522 | +#define OP_VUpdate 7 /* synopsis: data=r[P3@P2] */ |
| 15523 | +#define OP_Goto 8 /* jump */ |
| 15524 | +#define OP_Gosub 9 /* jump */ |
| 15525 | +#define OP_InitCoroutine 10 /* jump */ |
| 15526 | +#define OP_Yield 11 /* jump */ |
| 15527 | +#define OP_MustBeInt 12 /* jump */ |
| 15528 | +#define OP_Jump 13 /* jump */ |
| 15529 | +#define OP_Once 14 /* jump */ |
| 15530 | +#define OP_If 15 /* jump */ |
| 15531 | +#define OP_IfNot 16 /* jump */ |
| 15532 | +#define OP_IsNullOrType 17 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */ |
| 15533 | +#define OP_IfNullRow 18 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */ |
| 15348 | 15534 | #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */ |
| 15349 | | -#define OP_IfNot 20 /* jump */ |
| 15350 | | -#define OP_IsNullOrType 21 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */ |
| 15351 | | -#define OP_IfNullRow 22 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */ |
| 15352 | | -#define OP_SeekLT 23 /* jump, synopsis: key=r[P3@P4] */ |
| 15353 | | -#define OP_SeekLE 24 /* jump, synopsis: key=r[P3@P4] */ |
| 15354 | | -#define OP_SeekGE 25 /* jump, synopsis: key=r[P3@P4] */ |
| 15355 | | -#define OP_SeekGT 26 /* jump, synopsis: key=r[P3@P4] */ |
| 15356 | | -#define OP_IfNotOpen 27 /* jump, synopsis: if( !csr[P1] ) goto P2 */ |
| 15357 | | -#define OP_IfNoHope 28 /* jump, synopsis: key=r[P3@P4] */ |
| 15358 | | -#define OP_NoConflict 29 /* jump, synopsis: key=r[P3@P4] */ |
| 15359 | | -#define OP_NotFound 30 /* jump, synopsis: key=r[P3@P4] */ |
| 15360 | | -#define OP_Found 31 /* jump, synopsis: key=r[P3@P4] */ |
| 15361 | | -#define OP_SeekRowid 32 /* jump, synopsis: intkey=r[P3] */ |
| 15362 | | -#define OP_NotExists 33 /* jump, synopsis: intkey=r[P3] */ |
| 15363 | | -#define OP_Last 34 /* jump */ |
| 15364 | | -#define OP_IfSmaller 35 /* jump */ |
| 15365 | | -#define OP_SorterSort 36 /* jump */ |
| 15366 | | -#define OP_Sort 37 /* jump */ |
| 15367 | | -#define OP_Rewind 38 /* jump */ |
| 15535 | +#define OP_SeekLT 20 /* jump, synopsis: key=r[P3@P4] */ |
| 15536 | +#define OP_SeekLE 21 /* jump, synopsis: key=r[P3@P4] */ |
| 15537 | +#define OP_SeekGE 22 /* jump, synopsis: key=r[P3@P4] */ |
| 15538 | +#define OP_SeekGT 23 /* jump, synopsis: key=r[P3@P4] */ |
| 15539 | +#define OP_IfNotOpen 24 /* jump, synopsis: if( !csr[P1] ) goto P2 */ |
| 15540 | +#define OP_IfNoHope 25 /* jump, synopsis: key=r[P3@P4] */ |
| 15541 | +#define OP_NoConflict 26 /* jump, synopsis: key=r[P3@P4] */ |
| 15542 | +#define OP_NotFound 27 /* jump, synopsis: key=r[P3@P4] */ |
| 15543 | +#define OP_Found 28 /* jump, synopsis: key=r[P3@P4] */ |
| 15544 | +#define OP_SeekRowid 29 /* jump, synopsis: intkey=r[P3] */ |
| 15545 | +#define OP_NotExists 30 /* jump, synopsis: intkey=r[P3] */ |
| 15546 | +#define OP_Last 31 /* jump */ |
| 15547 | +#define OP_IfSmaller 32 /* jump */ |
| 15548 | +#define OP_SorterSort 33 /* jump */ |
| 15549 | +#define OP_Sort 34 /* jump */ |
| 15550 | +#define OP_Rewind 35 /* jump */ |
| 15551 | +#define OP_SorterNext 36 /* jump */ |
| 15552 | +#define OP_Prev 37 /* jump */ |
| 15553 | +#define OP_Next 38 /* jump */ |
| 15368 | 15554 | #define OP_IdxLE 39 /* jump, synopsis: key=r[P3@P4] */ |
| 15369 | 15555 | #define OP_IdxGT 40 /* jump, synopsis: key=r[P3@P4] */ |
| 15370 | 15556 | #define OP_IdxLT 41 /* jump, synopsis: key=r[P3@P4] */ |
| 15371 | 15557 | #define OP_IdxGE 42 /* jump, synopsis: key=r[P3@P4] */ |
| 15372 | 15558 | #define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */ |
| | @@ -15395,41 +15581,41 @@ |
| 15395 | 15581 | #define OP_Function 66 /* synopsis: r[P3]=func(r[P2@NP]) */ |
| 15396 | 15582 | #define OP_Return 67 |
| 15397 | 15583 | #define OP_EndCoroutine 68 |
| 15398 | 15584 | #define OP_HaltIfNull 69 /* synopsis: if r[P3]=null halt */ |
| 15399 | 15585 | #define OP_Halt 70 |
| 15400 | | -#define OP_Integer 71 /* synopsis: r[P2]=P1 */ |
| 15401 | | -#define OP_Int64 72 /* synopsis: r[P2]=P4 */ |
| 15402 | | -#define OP_String 73 /* synopsis: r[P2]='P4' (len=P1) */ |
| 15403 | | -#define OP_Null 74 /* synopsis: r[P2..P3]=NULL */ |
| 15404 | | -#define OP_SoftNull 75 /* synopsis: r[P1]=NULL */ |
| 15405 | | -#define OP_Blob 76 /* synopsis: r[P2]=P4 (len=P1) */ |
| 15406 | | -#define OP_Variable 77 /* synopsis: r[P2]=parameter(P1,P4) */ |
| 15407 | | -#define OP_Move 78 /* synopsis: r[P2@P3]=r[P1@P3] */ |
| 15408 | | -#define OP_Copy 79 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */ |
| 15409 | | -#define OP_SCopy 80 /* synopsis: r[P2]=r[P1] */ |
| 15410 | | -#define OP_IntCopy 81 /* synopsis: r[P2]=r[P1] */ |
| 15411 | | -#define OP_FkCheck 82 |
| 15412 | | -#define OP_ResultRow 83 /* synopsis: output=r[P1@P2] */ |
| 15413 | | -#define OP_CollSeq 84 |
| 15414 | | -#define OP_AddImm 85 /* synopsis: r[P1]=r[P1]+P2 */ |
| 15415 | | -#define OP_RealAffinity 86 |
| 15416 | | -#define OP_Cast 87 /* synopsis: affinity(r[P1]) */ |
| 15417 | | -#define OP_Permutation 88 |
| 15418 | | -#define OP_Compare 89 /* synopsis: r[P1@P3] <-> r[P2@P3] */ |
| 15419 | | -#define OP_IsTrue 90 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */ |
| 15420 | | -#define OP_ZeroOrNull 91 /* synopsis: r[P2] = 0 OR NULL */ |
| 15421 | | -#define OP_Offset 92 /* synopsis: r[P3] = sqlite_offset(P1) */ |
| 15422 | | -#define OP_Column 93 /* synopsis: r[P3]=PX */ |
| 15423 | | -#define OP_TypeCheck 94 /* synopsis: typecheck(r[P1@P2]) */ |
| 15424 | | -#define OP_Affinity 95 /* synopsis: affinity(r[P1@P2]) */ |
| 15425 | | -#define OP_MakeRecord 96 /* synopsis: r[P3]=mkrec(r[P1@P2]) */ |
| 15426 | | -#define OP_Count 97 /* synopsis: r[P2]=count() */ |
| 15427 | | -#define OP_ReadCookie 98 |
| 15428 | | -#define OP_SetCookie 99 |
| 15429 | | -#define OP_ReopenIdx 100 /* synopsis: root=P2 iDb=P3 */ |
| 15430 | | -#define OP_OpenRead 101 /* synopsis: root=P2 iDb=P3 */ |
| 15586 | +#define OP_BeginSubrtn 71 /* synopsis: r[P2]=P1 */ |
| 15587 | +#define OP_Integer 72 /* synopsis: r[P2]=P1 */ |
| 15588 | +#define OP_Int64 73 /* synopsis: r[P2]=P4 */ |
| 15589 | +#define OP_String 74 /* synopsis: r[P2]='P4' (len=P1) */ |
| 15590 | +#define OP_Null 75 /* synopsis: r[P2..P3]=NULL */ |
| 15591 | +#define OP_SoftNull 76 /* synopsis: r[P1]=NULL */ |
| 15592 | +#define OP_Blob 77 /* synopsis: r[P2]=P4 (len=P1) */ |
| 15593 | +#define OP_Variable 78 /* synopsis: r[P2]=parameter(P1,P4) */ |
| 15594 | +#define OP_Move 79 /* synopsis: r[P2@P3]=r[P1@P3] */ |
| 15595 | +#define OP_Copy 80 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */ |
| 15596 | +#define OP_SCopy 81 /* synopsis: r[P2]=r[P1] */ |
| 15597 | +#define OP_IntCopy 82 /* synopsis: r[P2]=r[P1] */ |
| 15598 | +#define OP_FkCheck 83 |
| 15599 | +#define OP_ResultRow 84 /* synopsis: output=r[P1@P2] */ |
| 15600 | +#define OP_CollSeq 85 |
| 15601 | +#define OP_AddImm 86 /* synopsis: r[P1]=r[P1]+P2 */ |
| 15602 | +#define OP_RealAffinity 87 |
| 15603 | +#define OP_Cast 88 /* synopsis: affinity(r[P1]) */ |
| 15604 | +#define OP_Permutation 89 |
| 15605 | +#define OP_Compare 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */ |
| 15606 | +#define OP_IsTrue 91 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */ |
| 15607 | +#define OP_ZeroOrNull 92 /* synopsis: r[P2] = 0 OR NULL */ |
| 15608 | +#define OP_Offset 93 /* synopsis: r[P3] = sqlite_offset(P1) */ |
| 15609 | +#define OP_Column 94 /* synopsis: r[P3]=PX */ |
| 15610 | +#define OP_TypeCheck 95 /* synopsis: typecheck(r[P1@P2]) */ |
| 15611 | +#define OP_Affinity 96 /* synopsis: affinity(r[P1@P2]) */ |
| 15612 | +#define OP_MakeRecord 97 /* synopsis: r[P3]=mkrec(r[P1@P2]) */ |
| 15613 | +#define OP_Count 98 /* synopsis: r[P2]=count() */ |
| 15614 | +#define OP_ReadCookie 99 |
| 15615 | +#define OP_SetCookie 100 |
| 15616 | +#define OP_ReopenIdx 101 /* synopsis: root=P2 iDb=P3 */ |
| 15431 | 15617 | #define OP_BitAnd 102 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */ |
| 15432 | 15618 | #define OP_BitOr 103 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */ |
| 15433 | 15619 | #define OP_ShiftLeft 104 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */ |
| 15434 | 15620 | #define OP_ShiftRight 105 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */ |
| 15435 | 15621 | #define OP_Add 106 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */ |
| | @@ -15436,82 +15622,84 @@ |
| 15436 | 15622 | #define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */ |
| 15437 | 15623 | #define OP_Multiply 108 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */ |
| 15438 | 15624 | #define OP_Divide 109 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */ |
| 15439 | 15625 | #define OP_Remainder 110 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */ |
| 15440 | 15626 | #define OP_Concat 111 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */ |
| 15441 | | -#define OP_OpenWrite 112 /* synopsis: root=P2 iDb=P3 */ |
| 15442 | | -#define OP_OpenDup 113 |
| 15627 | +#define OP_OpenRead 112 /* synopsis: root=P2 iDb=P3 */ |
| 15628 | +#define OP_OpenWrite 113 /* synopsis: root=P2 iDb=P3 */ |
| 15443 | 15629 | #define OP_BitNot 114 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */ |
| 15444 | | -#define OP_OpenAutoindex 115 /* synopsis: nColumn=P2 */ |
| 15445 | | -#define OP_OpenEphemeral 116 /* synopsis: nColumn=P2 */ |
| 15630 | +#define OP_OpenDup 115 |
| 15631 | +#define OP_OpenAutoindex 116 /* synopsis: nColumn=P2 */ |
| 15446 | 15632 | #define OP_String8 117 /* same as TK_STRING, synopsis: r[P2]='P4' */ |
| 15447 | | -#define OP_SorterOpen 118 |
| 15448 | | -#define OP_SequenceTest 119 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */ |
| 15449 | | -#define OP_OpenPseudo 120 /* synopsis: P3 columns in r[P2] */ |
| 15450 | | -#define OP_Close 121 |
| 15451 | | -#define OP_ColumnsUsed 122 |
| 15452 | | -#define OP_SeekScan 123 /* synopsis: Scan-ahead up to P1 rows */ |
| 15453 | | -#define OP_SeekHit 124 /* synopsis: set P2<=seekHit<=P3 */ |
| 15454 | | -#define OP_Sequence 125 /* synopsis: r[P2]=cursor[P1].ctr++ */ |
| 15455 | | -#define OP_NewRowid 126 /* synopsis: r[P2]=rowid */ |
| 15456 | | -#define OP_Insert 127 /* synopsis: intkey=r[P3] data=r[P2] */ |
| 15457 | | -#define OP_RowCell 128 |
| 15458 | | -#define OP_Delete 129 |
| 15459 | | -#define OP_ResetCount 130 |
| 15460 | | -#define OP_SorterCompare 131 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */ |
| 15461 | | -#define OP_SorterData 132 /* synopsis: r[P2]=data */ |
| 15462 | | -#define OP_RowData 133 /* synopsis: r[P2]=data */ |
| 15463 | | -#define OP_Rowid 134 /* synopsis: r[P2]=rowid */ |
| 15464 | | -#define OP_NullRow 135 |
| 15465 | | -#define OP_SeekEnd 136 |
| 15466 | | -#define OP_IdxInsert 137 /* synopsis: key=r[P2] */ |
| 15467 | | -#define OP_SorterInsert 138 /* synopsis: key=r[P2] */ |
| 15468 | | -#define OP_IdxDelete 139 /* synopsis: key=r[P2@P3] */ |
| 15469 | | -#define OP_DeferredSeek 140 /* synopsis: Move P3 to P1.rowid if needed */ |
| 15470 | | -#define OP_IdxRowid 141 /* synopsis: r[P2]=rowid */ |
| 15471 | | -#define OP_FinishSeek 142 |
| 15472 | | -#define OP_Destroy 143 |
| 15473 | | -#define OP_Clear 144 |
| 15474 | | -#define OP_ResetSorter 145 |
| 15475 | | -#define OP_CreateBtree 146 /* synopsis: r[P2]=root iDb=P1 flags=P3 */ |
| 15476 | | -#define OP_SqlExec 147 |
| 15477 | | -#define OP_ParseSchema 148 |
| 15478 | | -#define OP_LoadAnalysis 149 |
| 15479 | | -#define OP_DropTable 150 |
| 15480 | | -#define OP_DropIndex 151 |
| 15481 | | -#define OP_DropTrigger 152 |
| 15633 | +#define OP_OpenEphemeral 118 /* synopsis: nColumn=P2 */ |
| 15634 | +#define OP_SorterOpen 119 |
| 15635 | +#define OP_SequenceTest 120 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */ |
| 15636 | +#define OP_OpenPseudo 121 /* synopsis: P3 columns in r[P2] */ |
| 15637 | +#define OP_Close 122 |
| 15638 | +#define OP_ColumnsUsed 123 |
| 15639 | +#define OP_SeekScan 124 /* synopsis: Scan-ahead up to P1 rows */ |
| 15640 | +#define OP_SeekHit 125 /* synopsis: set P2<=seekHit<=P3 */ |
| 15641 | +#define OP_Sequence 126 /* synopsis: r[P2]=cursor[P1].ctr++ */ |
| 15642 | +#define OP_NewRowid 127 /* synopsis: r[P2]=rowid */ |
| 15643 | +#define OP_Insert 128 /* synopsis: intkey=r[P3] data=r[P2] */ |
| 15644 | +#define OP_RowCell 129 |
| 15645 | +#define OP_Delete 130 |
| 15646 | +#define OP_ResetCount 131 |
| 15647 | +#define OP_SorterCompare 132 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */ |
| 15648 | +#define OP_SorterData 133 /* synopsis: r[P2]=data */ |
| 15649 | +#define OP_RowData 134 /* synopsis: r[P2]=data */ |
| 15650 | +#define OP_Rowid 135 /* synopsis: r[P2]=rowid */ |
| 15651 | +#define OP_NullRow 136 |
| 15652 | +#define OP_SeekEnd 137 |
| 15653 | +#define OP_IdxInsert 138 /* synopsis: key=r[P2] */ |
| 15654 | +#define OP_SorterInsert 139 /* synopsis: key=r[P2] */ |
| 15655 | +#define OP_IdxDelete 140 /* synopsis: key=r[P2@P3] */ |
| 15656 | +#define OP_DeferredSeek 141 /* synopsis: Move P3 to P1.rowid if needed */ |
| 15657 | +#define OP_IdxRowid 142 /* synopsis: r[P2]=rowid */ |
| 15658 | +#define OP_FinishSeek 143 |
| 15659 | +#define OP_Destroy 144 |
| 15660 | +#define OP_Clear 145 |
| 15661 | +#define OP_ResetSorter 146 |
| 15662 | +#define OP_CreateBtree 147 /* synopsis: r[P2]=root iDb=P1 flags=P3 */ |
| 15663 | +#define OP_SqlExec 148 |
| 15664 | +#define OP_ParseSchema 149 |
| 15665 | +#define OP_LoadAnalysis 150 |
| 15666 | +#define OP_DropTable 151 |
| 15667 | +#define OP_DropIndex 152 |
| 15482 | 15668 | #define OP_Real 153 /* same as TK_FLOAT, synopsis: r[P2]=P4 */ |
| 15483 | | -#define OP_IntegrityCk 154 |
| 15484 | | -#define OP_RowSetAdd 155 /* synopsis: rowset(P1)=r[P2] */ |
| 15485 | | -#define OP_Param 156 |
| 15486 | | -#define OP_FkCounter 157 /* synopsis: fkctr[P1]+=P2 */ |
| 15487 | | -#define OP_MemMax 158 /* synopsis: r[P1]=max(r[P1],r[P2]) */ |
| 15488 | | -#define OP_OffsetLimit 159 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */ |
| 15489 | | -#define OP_AggInverse 160 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */ |
| 15490 | | -#define OP_AggStep 161 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 15491 | | -#define OP_AggStep1 162 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 15492 | | -#define OP_AggValue 163 /* synopsis: r[P3]=value N=P2 */ |
| 15493 | | -#define OP_AggFinal 164 /* synopsis: accum=r[P1] N=P2 */ |
| 15494 | | -#define OP_Expire 165 |
| 15495 | | -#define OP_CursorLock 166 |
| 15496 | | -#define OP_CursorUnlock 167 |
| 15497 | | -#define OP_TableLock 168 /* synopsis: iDb=P1 root=P2 write=P3 */ |
| 15498 | | -#define OP_VBegin 169 |
| 15499 | | -#define OP_VCreate 170 |
| 15500 | | -#define OP_VDestroy 171 |
| 15501 | | -#define OP_VOpen 172 |
| 15502 | | -#define OP_VColumn 173 /* synopsis: r[P3]=vcolumn(P2) */ |
| 15503 | | -#define OP_VRename 174 |
| 15504 | | -#define OP_Pagecount 175 |
| 15505 | | -#define OP_MaxPgcnt 176 |
| 15506 | | -#define OP_FilterAdd 177 /* synopsis: filter(P1) += key(P3@P4) */ |
| 15507 | | -#define OP_Trace 178 |
| 15508 | | -#define OP_CursorHint 179 |
| 15509 | | -#define OP_ReleaseReg 180 /* synopsis: release r[P1@P2] mask P3 */ |
| 15510 | | -#define OP_Noop 181 |
| 15511 | | -#define OP_Explain 182 |
| 15512 | | -#define OP_Abortable 183 |
| 15669 | +#define OP_DropTrigger 154 |
| 15670 | +#define OP_IntegrityCk 155 |
| 15671 | +#define OP_RowSetAdd 156 /* synopsis: rowset(P1)=r[P2] */ |
| 15672 | +#define OP_Param 157 |
| 15673 | +#define OP_FkCounter 158 /* synopsis: fkctr[P1]+=P2 */ |
| 15674 | +#define OP_MemMax 159 /* synopsis: r[P1]=max(r[P1],r[P2]) */ |
| 15675 | +#define OP_OffsetLimit 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */ |
| 15676 | +#define OP_AggInverse 161 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */ |
| 15677 | +#define OP_AggStep 162 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 15678 | +#define OP_AggStep1 163 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 15679 | +#define OP_AggValue 164 /* synopsis: r[P3]=value N=P2 */ |
| 15680 | +#define OP_AggFinal 165 /* synopsis: accum=r[P1] N=P2 */ |
| 15681 | +#define OP_Expire 166 |
| 15682 | +#define OP_CursorLock 167 |
| 15683 | +#define OP_CursorUnlock 168 |
| 15684 | +#define OP_TableLock 169 /* synopsis: iDb=P1 root=P2 write=P3 */ |
| 15685 | +#define OP_VBegin 170 |
| 15686 | +#define OP_VCreate 171 |
| 15687 | +#define OP_VDestroy 172 |
| 15688 | +#define OP_VOpen 173 |
| 15689 | +#define OP_VInitIn 174 /* synopsis: r[P2]=ValueList(P1,P3) */ |
| 15690 | +#define OP_VColumn 175 /* synopsis: r[P3]=vcolumn(P2) */ |
| 15691 | +#define OP_VRename 176 |
| 15692 | +#define OP_Pagecount 177 |
| 15693 | +#define OP_MaxPgcnt 178 |
| 15694 | +#define OP_FilterAdd 179 /* synopsis: filter(P1) += key(P3@P4) */ |
| 15695 | +#define OP_Trace 180 |
| 15696 | +#define OP_CursorHint 181 |
| 15697 | +#define OP_ReleaseReg 182 /* synopsis: release r[P1@P2] mask P3 */ |
| 15698 | +#define OP_Noop 183 |
| 15699 | +#define OP_Explain 184 |
| 15700 | +#define OP_Abortable 185 |
| 15513 | 15701 | |
| 15514 | 15702 | /* Properties such as "out2" or "jump" that are specified in |
| 15515 | 15703 | ** comments following the "case" for each opcode in the vdbe.c |
| 15516 | 15704 | ** are encoded into bitvectors as follows: |
| 15517 | 15705 | */ |
| | @@ -15520,34 +15708,34 @@ |
| 15520 | 15708 | #define OPFLG_IN2 0x04 /* in2: P2 is an input */ |
| 15521 | 15709 | #define OPFLG_IN3 0x08 /* in3: P3 is an input */ |
| 15522 | 15710 | #define OPFLG_OUT2 0x10 /* out2: P2 is an output */ |
| 15523 | 15711 | #define OPFLG_OUT3 0x20 /* out3: P3 is an output */ |
| 15524 | 15712 | #define OPFLG_INITIALIZER {\ |
| 15525 | | -/* 0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10,\ |
| 15526 | | -/* 8 */ 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03,\ |
| 15527 | | -/* 16 */ 0x01, 0x01, 0x03, 0x12, 0x03, 0x03, 0x01, 0x09,\ |
| 15528 | | -/* 24 */ 0x09, 0x09, 0x09, 0x01, 0x09, 0x09, 0x09, 0x09,\ |
| 15529 | | -/* 32 */ 0x09, 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\ |
| 15713 | +/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\ |
| 15714 | +/* 8 */ 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01, 0x03,\ |
| 15715 | +/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x09, 0x09, 0x09, 0x09,\ |
| 15716 | +/* 24 */ 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x01,\ |
| 15717 | +/* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\ |
| 15530 | 15718 | /* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x23, 0x0b, 0x01,\ |
| 15531 | 15719 | /* 48 */ 0x01, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\ |
| 15532 | 15720 | /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01, 0x01, 0x01,\ |
| 15533 | | -/* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\ |
| 15534 | | -/* 72 */ 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00,\ |
| 15535 | | -/* 80 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02,\ |
| 15536 | | -/* 88 */ 0x00, 0x00, 0x12, 0x1e, 0x20, 0x00, 0x00, 0x00,\ |
| 15537 | | -/* 96 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x26, 0x26,\ |
| 15721 | +/* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x00,\ |
| 15722 | +/* 72 */ 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00,\ |
| 15723 | +/* 80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x02, 0x02,\ |
| 15724 | +/* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x00, 0x00,\ |
| 15725 | +/* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x26, 0x26,\ |
| 15538 | 15726 | /* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\ |
| 15539 | 15727 | /* 112 */ 0x00, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00, 0x00,\ |
| 15540 | | -/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\ |
| 15541 | | -/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,\ |
| 15542 | | -/* 136 */ 0x00, 0x04, 0x04, 0x00, 0x00, 0x10, 0x00, 0x10,\ |
| 15543 | | -/* 144 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 15544 | | -/* 152 */ 0x00, 0x10, 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a,\ |
| 15545 | | -/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 15546 | | -/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\ |
| 15547 | | -/* 176 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 15548 | | -} |
| 15728 | +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\ |
| 15729 | +/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\ |
| 15730 | +/* 136 */ 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10, 0x00,\ |
| 15731 | +/* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\ |
| 15732 | +/* 152 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\ |
| 15733 | +/* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 15734 | +/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,\ |
| 15735 | +/* 176 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 15736 | +/* 184 */ 0x00, 0x00,} |
| 15549 | 15737 | |
| 15550 | 15738 | /* The resolve3P2Values() routine is able to run faster if it knows |
| 15551 | 15739 | ** the value of the largest JUMP opcode. The smaller the maximum |
| 15552 | 15740 | ** JUMP opcode the better, so the mkopcodeh.tcl script that |
| 15553 | 15741 | ** generated this include file strives to group all JUMP opcodes |
| | @@ -17601,10 +17789,15 @@ |
| 17601 | 17789 | ** b-tree. |
| 17602 | 17790 | */ |
| 17603 | 17791 | struct UnpackedRecord { |
| 17604 | 17792 | KeyInfo *pKeyInfo; /* Collation and sort-order information */ |
| 17605 | 17793 | Mem *aMem; /* Values */ |
| 17794 | + union { |
| 17795 | + char *z; /* Cache of aMem[0].z for vdbeRecordCompareString() */ |
| 17796 | + i64 i; /* Cache of aMem[0].u.i for vdbeRecordCompareInt() */ |
| 17797 | + } u; |
| 17798 | + int n; /* Cache of aMem[0].n used by vdbeRecordCompareString() */ |
| 17606 | 17799 | u16 nField; /* Number of entries in apMem[] */ |
| 17607 | 17800 | i8 default_rc; /* Comparison result if keys are equal */ |
| 17608 | 17801 | u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */ |
| 17609 | 17802 | i8 r1; /* Value to return if (lhs < rhs) */ |
| 17610 | 17803 | i8 r2; /* Value to return if (lhs > rhs) */ |
| | @@ -17908,11 +18101,14 @@ |
| 17908 | 18101 | ** TK_SELECT: 1st register of result vector */ |
| 17909 | 18102 | ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid. |
| 17910 | 18103 | ** TK_VARIABLE: variable number (always >= 1). |
| 17911 | 18104 | ** TK_SELECT_COLUMN: column of the result vector */ |
| 17912 | 18105 | i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */ |
| 17913 | | - int iRightJoinTable; /* If EP_FromJoin, the right table of the join */ |
| 18106 | + union { |
| 18107 | + int iRightJoinTable; /* If EP_FromJoin, the right table of the join */ |
| 18108 | + int iOfst; /* else: start of token from start of statement */ |
| 18109 | + } w; |
| 17914 | 18110 | AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */ |
| 17915 | 18111 | union { |
| 17916 | 18112 | Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL |
| 17917 | 18113 | ** for a column of an index on an expression */ |
| 17918 | 18114 | Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */ |
| | @@ -19017,10 +19213,11 @@ |
| 19017 | 19213 | #endif |
| 19018 | 19214 | #ifndef SQLITE_UNTESTABLE |
| 19019 | 19215 | int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */ |
| 19020 | 19216 | #endif |
| 19021 | 19217 | int bLocaltimeFault; /* True to fail localtime() calls */ |
| 19218 | + int (*xAltLocaltime)(const void*,void*); /* Alternative localtime() routine */ |
| 19022 | 19219 | int iOnceResetThreshold; /* When to reset OP_Once counters */ |
| 19023 | 19220 | u32 szSorterRef; /* Min size in bytes to use sorter-refs */ |
| 19024 | 19221 | unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */ |
| 19025 | 19222 | /* vvvv--- must be last ---vvv */ |
| 19026 | 19223 | #ifdef SQLITE_DEBUG |
| | @@ -19221,11 +19418,11 @@ |
| 19221 | 19418 | Expr *pStart; /* Expression for "<expr> PRECEDING" */ |
| 19222 | 19419 | Expr *pEnd; /* Expression for "<expr> FOLLOWING" */ |
| 19223 | 19420 | Window **ppThis; /* Pointer to this object in Select.pWin list */ |
| 19224 | 19421 | Window *pNextWin; /* Next window function belonging to this SELECT */ |
| 19225 | 19422 | Expr *pFilter; /* The FILTER expression */ |
| 19226 | | - FuncDef *pFunc; /* The function */ |
| 19423 | + FuncDef *pWFunc; /* The function */ |
| 19227 | 19424 | int iEphCsr; /* Partition buffer or Peer buffer */ |
| 19228 | 19425 | int regAccum; /* Accumulator */ |
| 19229 | 19426 | int regResult; /* Interim result */ |
| 19230 | 19427 | int csrApp; /* Function cursor (used by min/max) */ |
| 19231 | 19428 | int regApp; /* Function register (also used by min/max) */ |
| | @@ -19662,11 +19859,12 @@ |
| 19662 | 19859 | #endif |
| 19663 | 19860 | SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe*,int,const char*); |
| 19664 | 19861 | SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*); |
| 19665 | 19862 | SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*, |
| 19666 | 19863 | Upsert*); |
| 19667 | | -SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int); |
| 19864 | +SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*, |
| 19865 | + ExprList*,Select*,u16,int); |
| 19668 | 19866 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*); |
| 19669 | 19867 | SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*); |
| 19670 | 19868 | SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*); |
| 19671 | 19869 | SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*); |
| 19672 | 19870 | SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*); |
| | @@ -20075,10 +20273,11 @@ |
| 20075 | 20273 | SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum*, u8); |
| 20076 | 20274 | SQLITE_PRIVATE void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*); |
| 20077 | 20275 | SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int); |
| 20078 | 20276 | SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int); |
| 20079 | 20277 | SQLITE_PRIVATE void sqlite3RecordErrorByteOffset(sqlite3*,const char*); |
| 20278 | +SQLITE_PRIVATE void sqlite3RecordErrorOffsetOfExpr(sqlite3*,const Expr*); |
| 20080 | 20279 | |
| 20081 | 20280 | SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *); |
| 20082 | 20281 | SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *); |
| 20083 | 20282 | |
| 20084 | 20283 | #ifndef SQLITE_OMIT_SUBQUERY |
| | @@ -20179,11 +20378,16 @@ |
| 20179 | 20378 | SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*); |
| 20180 | 20379 | SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **); |
| 20181 | 20380 | SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*); |
| 20182 | 20381 | SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *); |
| 20183 | 20382 | SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *); |
| 20383 | + |
| 20184 | 20384 | SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); |
| 20385 | +#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \ |
| 20386 | + && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 20387 | +SQLITE_PRIVATE void sqlite3VtabWriteAll(sqlite3_index_info*); |
| 20388 | +#endif |
| 20185 | 20389 | SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); |
| 20186 | 20390 | SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); |
| 20187 | 20391 | SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); |
| 20188 | 20392 | SQLITE_PRIVATE void sqlite3ParseObjectInit(Parse*,sqlite3*); |
| 20189 | 20393 | SQLITE_PRIVATE void sqlite3ParseObjectReset(Parse*); |
| | @@ -21695,13 +21899,17 @@ |
| 21695 | 21899 | #endif |
| 21696 | 21900 | #ifndef SQLITE_UNTESTABLE |
| 21697 | 21901 | 0, /* xTestCallback */ |
| 21698 | 21902 | #endif |
| 21699 | 21903 | 0, /* bLocaltimeFault */ |
| 21904 | + 0, /* xAltLocaltime */ |
| 21700 | 21905 | 0x7ffffffe, /* iOnceResetThreshold */ |
| 21701 | 21906 | SQLITE_DEFAULT_SORTERREF_SIZE, /* szSorterRef */ |
| 21702 | 21907 | 0, /* iPrngSeed */ |
| 21908 | +#ifdef SQLITE_DEBUG |
| 21909 | + {0,0,0,0,0,0} /* aTune */ |
| 21910 | +#endif |
| 21703 | 21911 | }; |
| 21704 | 21912 | |
| 21705 | 21913 | /* |
| 21706 | 21914 | ** Hash table for global functions - functions common to all |
| 21707 | 21915 | ** database connections. After initialization, this table is |
| | @@ -22041,20 +22249,20 @@ |
| 22041 | 22249 | i64 i; /* Integer value used when MEM_Int is set in flags */ |
| 22042 | 22250 | int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */ |
| 22043 | 22251 | const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */ |
| 22044 | 22252 | FuncDef *pDef; /* Used only when flags==MEM_Agg */ |
| 22045 | 22253 | } u; |
| 22254 | + char *z; /* String or BLOB value */ |
| 22255 | + int n; /* Number of characters in string value, excluding '\0' */ |
| 22046 | 22256 | u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ |
| 22047 | 22257 | u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ |
| 22048 | 22258 | u8 eSubtype; /* Subtype for this value */ |
| 22049 | | - int n; /* Number of characters in string value, excluding '\0' */ |
| 22050 | | - char *z; /* String or BLOB value */ |
| 22051 | 22259 | /* ShallowCopy only needs to copy the information above */ |
| 22052 | | - char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */ |
| 22260 | + sqlite3 *db; /* The associated database connection */ |
| 22053 | 22261 | int szMalloc; /* Size of the zMalloc allocation */ |
| 22054 | 22262 | u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */ |
| 22055 | | - sqlite3 *db; /* The associated database connection */ |
| 22263 | + char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */ |
| 22056 | 22264 | void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */ |
| 22057 | 22265 | #ifdef SQLITE_DEBUG |
| 22058 | 22266 | Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ |
| 22059 | 22267 | u16 mScopyFlags; /* flags value immediately after the shallow copy */ |
| 22060 | 22268 | #endif |
| | @@ -22062,15 +22270,47 @@ |
| 22062 | 22270 | |
| 22063 | 22271 | /* |
| 22064 | 22272 | ** Size of struct Mem not including the Mem.zMalloc member or anything that |
| 22065 | 22273 | ** follows. |
| 22066 | 22274 | */ |
| 22067 | | -#define MEMCELLSIZE offsetof(Mem,zMalloc) |
| 22275 | +#define MEMCELLSIZE offsetof(Mem,db) |
| 22068 | 22276 | |
| 22069 | | -/* One or more of the following flags are set to indicate the validOK |
| 22277 | +/* One or more of the following flags are set to indicate the |
| 22070 | 22278 | ** representations of the value stored in the Mem struct. |
| 22071 | 22279 | ** |
| 22280 | +** * MEM_Null An SQL NULL value |
| 22281 | +** |
| 22282 | +** * MEM_Null|MEM_Zero An SQL NULL with the virtual table |
| 22283 | +** UPDATE no-change flag set |
| 22284 | +** |
| 22285 | +** * MEM_Null|MEM_Term| An SQL NULL, but also contains a |
| 22286 | +** MEM_Subtype pointer accessible using |
| 22287 | +** sqlite3_value_pointer(). |
| 22288 | +** |
| 22289 | +** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal |
| 22290 | +** to other NULLs even using the IS operator. |
| 22291 | +** |
| 22292 | +** * MEM_Str A string, stored in Mem.z with |
| 22293 | +** length Mem.n. Zero-terminated if |
| 22294 | +** MEM_Term is set. This flag is |
| 22295 | +** incompatible with MEM_Blob and |
| 22296 | +** MEM_Null, but can appear with MEM_Int, |
| 22297 | +** MEM_Real, and MEM_IntReal. |
| 22298 | +** |
| 22299 | +** * MEM_Blob A blob, stored in Mem.z length Mem.n. |
| 22300 | +** Incompatible with MEM_Str, MEM_Null, |
| 22301 | +** MEM_Int, MEM_Real, and MEM_IntReal. |
| 22302 | +** |
| 22303 | +** * MEM_Blob|MEM_Zero A blob in Mem.z of length Mem.n plus |
| 22304 | +** MEM.u.i extra 0x00 bytes at the end. |
| 22305 | +** |
| 22306 | +** * MEM_Int Integer stored in Mem.u.i. |
| 22307 | +** |
| 22308 | +** * MEM_Real Real stored in Mem.u.r. |
| 22309 | +** |
| 22310 | +** * MEM_IntReal Real stored as an integer in Mem.u.i. |
| 22311 | +** |
| 22072 | 22312 | ** If the MEM_Null flag is set, then the value is an SQL NULL value. |
| 22073 | 22313 | ** For a pointer type created using sqlite3_bind_pointer() or |
| 22074 | 22314 | ** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set. |
| 22075 | 22315 | ** |
| 22076 | 22316 | ** If the MEM_Str flag is set then Mem.z points at a string representation. |
| | @@ -22077,39 +22317,36 @@ |
| 22077 | 22317 | ** Usually this is encoded in the same unicode encoding as the main |
| 22078 | 22318 | ** database (see below for exceptions). If the MEM_Term flag is also |
| 22079 | 22319 | ** set, then the string is nul terminated. The MEM_Int and MEM_Real |
| 22080 | 22320 | ** flags may coexist with the MEM_Str flag. |
| 22081 | 22321 | */ |
| 22322 | +#define MEM_Undefined 0x0000 /* Value is undefined */ |
| 22082 | 22323 | #define MEM_Null 0x0001 /* Value is NULL (or a pointer) */ |
| 22083 | 22324 | #define MEM_Str 0x0002 /* Value is a string */ |
| 22084 | 22325 | #define MEM_Int 0x0004 /* Value is an integer */ |
| 22085 | 22326 | #define MEM_Real 0x0008 /* Value is a real number */ |
| 22086 | 22327 | #define MEM_Blob 0x0010 /* Value is a BLOB */ |
| 22087 | 22328 | #define MEM_IntReal 0x0020 /* MEM_Int that stringifies like MEM_Real */ |
| 22088 | 22329 | #define MEM_AffMask 0x003f /* Mask of affinity bits */ |
| 22330 | + |
| 22331 | +/* Extra bits that modify the meanings of the core datatypes above |
| 22332 | +*/ |
| 22089 | 22333 | #define MEM_FromBind 0x0040 /* Value originates from sqlite3_bind() */ |
| 22090 | | -#define MEM_Undefined 0x0080 /* Value is undefined */ |
| 22334 | + /* 0x0080 // Available */ |
| 22091 | 22335 | #define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */ |
| 22092 | | -#define MEM_TypeMask 0xc1bf /* Mask of type bits */ |
| 22093 | | - |
| 22094 | | - |
| 22095 | | -/* Whenever Mem contains a valid string or blob representation, one of |
| 22096 | | -** the following flags must be set to determine the memory management |
| 22097 | | -** policy for Mem.z. The MEM_Term flag tells us whether or not the |
| 22098 | | -** string is \000 or \u0000 terminated |
| 22099 | | -*/ |
| 22100 | 22336 | #define MEM_Term 0x0200 /* String in Mem.z is zero terminated */ |
| 22101 | | -#define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */ |
| 22102 | | -#define MEM_Static 0x0800 /* Mem.z points to a static string */ |
| 22103 | | -#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ |
| 22104 | | -#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ |
| 22105 | | -#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ |
| 22106 | | -#define MEM_Subtype 0x8000 /* Mem.eSubtype is valid */ |
| 22107 | | -#ifdef SQLITE_OMIT_INCRBLOB |
| 22108 | | - #undef MEM_Zero |
| 22109 | | - #define MEM_Zero 0x0000 |
| 22110 | | -#endif |
| 22337 | +#define MEM_Zero 0x0400 /* Mem.i contains count of 0s appended to blob */ |
| 22338 | +#define MEM_Subtype 0x0800 /* Mem.eSubtype is valid */ |
| 22339 | +#define MEM_TypeMask 0x0dbf /* Mask of type bits */ |
| 22340 | + |
| 22341 | +/* Bits that determine the storage for Mem.z for a string or blob or |
| 22342 | +** aggregate accumulator. |
| 22343 | +*/ |
| 22344 | +#define MEM_Dyn 0x1000 /* Need to call Mem.xDel() on Mem.z */ |
| 22345 | +#define MEM_Static 0x2000 /* Mem.z points to a static string */ |
| 22346 | +#define MEM_Ephem 0x4000 /* Mem.z points to an ephemeral string */ |
| 22347 | +#define MEM_Agg 0x8000 /* Mem.z points to an agg function context */ |
| 22111 | 22348 | |
| 22112 | 22349 | /* Return TRUE if Mem X contains dynamically allocated content - anything |
| 22113 | 22350 | ** that needs to be deallocated to avoid a leak. |
| 22114 | 22351 | */ |
| 22115 | 22352 | #define VdbeMemDynamic(X) \ |
| | @@ -22127,15 +22364,19 @@ |
| 22127 | 22364 | #define MemNullNochng(X) \ |
| 22128 | 22365 | (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \ |
| 22129 | 22366 | && (X)->n==0 && (X)->u.nZero==0) |
| 22130 | 22367 | |
| 22131 | 22368 | /* |
| 22132 | | -** Return true if a memory cell is not marked as invalid. This macro |
| 22369 | +** Return true if a memory cell has been initialized and is valid. |
| 22133 | 22370 | ** is for use inside assert() statements only. |
| 22371 | +** |
| 22372 | +** A Memory cell is initialized if at least one of the |
| 22373 | +** MEM_Null, MEM_Str, MEM_Int, MEM_Real, MEM_Blob, or MEM_IntReal bits |
| 22374 | +** is set. It is "undefined" if all those bits are zero. |
| 22134 | 22375 | */ |
| 22135 | 22376 | #ifdef SQLITE_DEBUG |
| 22136 | | -#define memIsValid(M) ((M)->flags & MEM_Undefined)==0 |
| 22377 | +#define memIsValid(M) ((M)->flags & MEM_AffMask)!=0 |
| 22137 | 22378 | #endif |
| 22138 | 22379 | |
| 22139 | 22380 | /* |
| 22140 | 22381 | ** Each auxiliary data pointer stored by a user defined function |
| 22141 | 22382 | ** implementation calling sqlite3_set_auxdata() is stored in an instance |
| | @@ -22169,10 +22410,11 @@ |
| 22169 | 22410 | FuncDef *pFunc; /* Pointer to function information */ |
| 22170 | 22411 | Mem *pMem; /* Memory cell used to store aggregate context */ |
| 22171 | 22412 | Vdbe *pVdbe; /* The VM that owns this context */ |
| 22172 | 22413 | int iOp; /* Instruction number of OP_Function */ |
| 22173 | 22414 | int isError; /* Error code returned by the function. */ |
| 22415 | + u8 enc; /* Encoding to use for results */ |
| 22174 | 22416 | u8 skipFlag; /* Skip accumulator loading if true */ |
| 22175 | 22417 | u8 argc; /* Number of arguments */ |
| 22176 | 22418 | sqlite3_value *argv[1]; /* Argument set */ |
| 22177 | 22419 | }; |
| 22178 | 22420 | |
| | @@ -22217,11 +22459,10 @@ |
| 22217 | 22459 | struct Vdbe { |
| 22218 | 22460 | sqlite3 *db; /* The database connection that owns this statement */ |
| 22219 | 22461 | Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ |
| 22220 | 22462 | Parse *pParse; /* Parsing context used to create this Vdbe */ |
| 22221 | 22463 | ynVar nVar; /* Number of entries in aVar[] */ |
| 22222 | | - u32 iVdbeMagic; /* Magic number defining state of the SQL statement */ |
| 22223 | 22464 | int nMem; /* Number of memory locations currently allocated */ |
| 22224 | 22465 | int nCursor; /* Number of slots in apCsr[] */ |
| 22225 | 22466 | u32 cacheCtr; /* VdbeCursor row cache generation counter */ |
| 22226 | 22467 | int pc; /* The program counter */ |
| 22227 | 22468 | int rc; /* Value to return */ |
| | @@ -22256,10 +22497,11 @@ |
| 22256 | 22497 | u16 nResColumn; /* Number of columns in one row of the result set */ |
| 22257 | 22498 | u8 errorAction; /* Recovery action to do in case of an error */ |
| 22258 | 22499 | u8 minWriteFileFormat; /* Minimum file format for writable database files */ |
| 22259 | 22500 | u8 prepFlags; /* SQLITE_PREPARE_* flags */ |
| 22260 | 22501 | u8 doingRerun; /* True if rerunning after an auto-reprepare */ |
| 22502 | + u8 eVdbeState; /* On of the VDBE_*_STATE values */ |
| 22261 | 22503 | bft expired:2; /* 1: recompile VM immediately 2: when convenient */ |
| 22262 | 22504 | bft explain:2; /* True if EXPLAIN present on SQL command */ |
| 22263 | 22505 | bft changeCntOn:1; /* True to update the change-counter */ |
| 22264 | 22506 | bft runOnlyOnce:1; /* Automatically expire on reset */ |
| 22265 | 22507 | bft usesStmtJournal:1; /* True if uses a statement journal */ |
| | @@ -22286,17 +22528,16 @@ |
| 22286 | 22528 | ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */ |
| 22287 | 22529 | #endif |
| 22288 | 22530 | }; |
| 22289 | 22531 | |
| 22290 | 22532 | /* |
| 22291 | | -** The following are allowed values for Vdbe.magic |
| 22533 | +** The following are allowed values for Vdbe.eVdbeState |
| 22292 | 22534 | */ |
| 22293 | | -#define VDBE_MAGIC_INIT 0x16bceaa5 /* Building a VDBE program */ |
| 22294 | | -#define VDBE_MAGIC_RUN 0x2df20da3 /* VDBE is ready to execute */ |
| 22295 | | -#define VDBE_MAGIC_HALT 0x319c2973 /* VDBE has completed execution */ |
| 22296 | | -#define VDBE_MAGIC_RESET 0x48fa9f76 /* Reset and ready to run again */ |
| 22297 | | -#define VDBE_MAGIC_DEAD 0x5606c3c8 /* The VDBE has been deallocated */ |
| 22535 | +#define VDBE_INIT_STATE 0 /* Prepared statement under construction */ |
| 22536 | +#define VDBE_READY_STATE 1 /* Ready to run but not yet started */ |
| 22537 | +#define VDBE_RUN_STATE 2 /* Run in progress */ |
| 22538 | +#define VDBE_HALT_STATE 3 /* Finished. Need reset() or finalize() */ |
| 22298 | 22539 | |
| 22299 | 22540 | /* |
| 22300 | 22541 | ** Structure used to store the context required by the |
| 22301 | 22542 | ** sqlite3_preupdate_*() API functions. |
| 22302 | 22543 | */ |
| | @@ -22315,18 +22556,36 @@ |
| 22315 | 22556 | Mem *aNew; /* Array of new.* values */ |
| 22316 | 22557 | Table *pTab; /* Schema object being upated */ |
| 22317 | 22558 | Index *pPk; /* PK index if pTab is WITHOUT ROWID */ |
| 22318 | 22559 | }; |
| 22319 | 22560 | |
| 22561 | +/* |
| 22562 | +** An instance of this object is used to pass an vector of values into |
| 22563 | +** OP_VFilter, the xFilter method of a virtual table. The vector is the |
| 22564 | +** set of values on the right-hand side of an IN constraint. |
| 22565 | +** |
| 22566 | +** The value as passed into xFilter is an sqlite3_value with a "pointer" |
| 22567 | +** type, such as is generated by sqlite3_result_pointer() and read by |
| 22568 | +** sqlite3_value_pointer. Such values have MEM_Term|MEM_Subtype|MEM_Null |
| 22569 | +** and a subtype of 'p'. The sqlite3_vtab_in_first() and _next() interfaces |
| 22570 | +** know how to use this object to step through all the values in the |
| 22571 | +** right operand of the IN constraint. |
| 22572 | +*/ |
| 22573 | +typedef struct ValueList ValueList; |
| 22574 | +struct ValueList { |
| 22575 | + BtCursor *pCsr; /* An ephemeral table holding all values */ |
| 22576 | + sqlite3_value *pOut; /* Register to hold each decoded output value */ |
| 22577 | +}; |
| 22578 | + |
| 22320 | 22579 | /* |
| 22321 | 22580 | ** Function prototypes |
| 22322 | 22581 | */ |
| 22323 | 22582 | SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...); |
| 22324 | 22583 | SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*); |
| 22325 | 22584 | void sqliteVdbePopStack(Vdbe*,int); |
| 22585 | +SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeHandleMovedCursor(VdbeCursor *p); |
| 22326 | 22586 | SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor*); |
| 22327 | | -SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, u32*); |
| 22328 | 22587 | SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*); |
| 22329 | 22588 | SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32); |
| 22330 | 22589 | SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8); |
| 22331 | 22590 | SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32); |
| 22332 | 22591 | SQLITE_PRIVATE void sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); |
| | @@ -22384,10 +22643,11 @@ |
| 22384 | 22643 | SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*); |
| 22385 | 22644 | SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8); |
| 22386 | 22645 | SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*); |
| 22387 | 22646 | SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*); |
| 22388 | 22647 | SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); |
| 22648 | +SQLITE_PRIVATE void sqlite3VdbeMemReleaseMalloc(Mem*p); |
| 22389 | 22649 | SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); |
| 22390 | 22650 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 22391 | 22651 | SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*); |
| 22392 | 22652 | #endif |
| 22393 | 22653 | #if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB) |
| | @@ -23351,12 +23611,14 @@ |
| 23351 | 23611 | ** The following routine implements the rough equivalent of localtime_r() |
| 23352 | 23612 | ** using whatever operating-system specific localtime facility that |
| 23353 | 23613 | ** is available. This routine returns 0 on success and |
| 23354 | 23614 | ** non-zero on any kind of error. |
| 23355 | 23615 | ** |
| 23356 | | -** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this |
| 23357 | | -** routine will always fail. |
| 23616 | +** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this |
| 23617 | +** routine will always fail. If bLocaltimeFault is nonzero and |
| 23618 | +** sqlite3GlobalConfig.xAltLocaltime is not NULL, then xAltLocaltime() is |
| 23619 | +** invoked in place of the OS-defined localtime() function. |
| 23358 | 23620 | ** |
| 23359 | 23621 | ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C |
| 23360 | 23622 | ** library function localtime_r() is used to assist in the calculation of |
| 23361 | 23623 | ** local time. |
| 23362 | 23624 | */ |
| | @@ -23368,20 +23630,34 @@ |
| 23368 | 23630 | sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); |
| 23369 | 23631 | #endif |
| 23370 | 23632 | sqlite3_mutex_enter(mutex); |
| 23371 | 23633 | pX = localtime(t); |
| 23372 | 23634 | #ifndef SQLITE_UNTESTABLE |
| 23373 | | - if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0; |
| 23635 | + if( sqlite3GlobalConfig.bLocaltimeFault ){ |
| 23636 | + if( sqlite3GlobalConfig.xAltLocaltime!=0 |
| 23637 | + && 0==sqlite3GlobalConfig.xAltLocaltime((const void*)t,(void*)pTm) |
| 23638 | + ){ |
| 23639 | + pX = pTm; |
| 23640 | + }else{ |
| 23641 | + pX = 0; |
| 23642 | + } |
| 23643 | + } |
| 23374 | 23644 | #endif |
| 23375 | 23645 | if( pX ) *pTm = *pX; |
| 23376 | 23646 | #if SQLITE_THREADSAFE>0 |
| 23377 | 23647 | sqlite3_mutex_leave(mutex); |
| 23378 | 23648 | #endif |
| 23379 | 23649 | rc = pX==0; |
| 23380 | 23650 | #else |
| 23381 | 23651 | #ifndef SQLITE_UNTESTABLE |
| 23382 | | - if( sqlite3GlobalConfig.bLocaltimeFault ) return 1; |
| 23652 | + if( sqlite3GlobalConfig.bLocaltimeFault ){ |
| 23653 | + if( sqlite3GlobalConfig.xAltLocaltime!=0 ){ |
| 23654 | + return sqlite3GlobalConfig.xAltLocaltime((const void*)t,(void*)pTm); |
| 23655 | + }else{ |
| 23656 | + return 1; |
| 23657 | + } |
| 23658 | + } |
| 23383 | 23659 | #endif |
| 23384 | 23660 | #if HAVE_LOCALTIME_R |
| 23385 | 23661 | rc = localtime_r(t, pTm)==0; |
| 23386 | 23662 | #else |
| 23387 | 23663 | rc = localtime_s(pTm, t); |
| | @@ -23392,71 +23668,60 @@ |
| 23392 | 23668 | #endif /* SQLITE_OMIT_LOCALTIME */ |
| 23393 | 23669 | |
| 23394 | 23670 | |
| 23395 | 23671 | #ifndef SQLITE_OMIT_LOCALTIME |
| 23396 | 23672 | /* |
| 23397 | | -** Compute the difference (in milliseconds) between localtime and UTC |
| 23398 | | -** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs, |
| 23399 | | -** return this value and set *pRc to SQLITE_OK. |
| 23400 | | -** |
| 23401 | | -** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value |
| 23402 | | -** is undefined in this case. |
| 23673 | +** Assuming the input DateTime is UTC, move it to its localtime equivalent. |
| 23403 | 23674 | */ |
| 23404 | | -static sqlite3_int64 localtimeOffset( |
| 23405 | | - DateTime *p, /* Date at which to calculate offset */ |
| 23406 | | - sqlite3_context *pCtx, /* Write error here if one occurs */ |
| 23407 | | - int *pRc /* OUT: Error code. SQLITE_OK or ERROR */ |
| 23675 | +static int toLocaltime( |
| 23676 | + DateTime *p, /* Date at which to calculate offset */ |
| 23677 | + sqlite3_context *pCtx /* Write error here if one occurs */ |
| 23408 | 23678 | ){ |
| 23409 | | - DateTime x, y; |
| 23410 | 23679 | time_t t; |
| 23411 | 23680 | struct tm sLocal; |
| 23681 | + int iYearDiff; |
| 23412 | 23682 | |
| 23413 | 23683 | /* Initialize the contents of sLocal to avoid a compiler warning. */ |
| 23414 | 23684 | memset(&sLocal, 0, sizeof(sLocal)); |
| 23415 | 23685 | |
| 23416 | | - x = *p; |
| 23417 | | - computeYMD_HMS(&x); |
| 23418 | | - if( x.Y<1971 || x.Y>=2038 ){ |
| 23686 | + computeJD(p); |
| 23687 | + if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */ |
| 23688 | + || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */ |
| 23689 | + ){ |
| 23419 | 23690 | /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only |
| 23420 | 23691 | ** works for years between 1970 and 2037. For dates outside this range, |
| 23421 | 23692 | ** SQLite attempts to map the year into an equivalent year within this |
| 23422 | 23693 | ** range, do the calculation, then map the year back. |
| 23423 | 23694 | */ |
| 23424 | | - x.Y = 2000; |
| 23425 | | - x.M = 1; |
| 23426 | | - x.D = 1; |
| 23427 | | - x.h = 0; |
| 23428 | | - x.m = 0; |
| 23429 | | - x.s = 0.0; |
| 23430 | | - } else { |
| 23431 | | - int s = (int)(x.s + 0.5); |
| 23432 | | - x.s = s; |
| 23433 | | - } |
| 23434 | | - x.tz = 0; |
| 23435 | | - x.validJD = 0; |
| 23436 | | - computeJD(&x); |
| 23437 | | - t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); |
| 23695 | + DateTime x = *p; |
| 23696 | + computeYMD_HMS(&x); |
| 23697 | + iYearDiff = (2000 + x.Y%4) - x.Y; |
| 23698 | + x.Y += iYearDiff; |
| 23699 | + x.validJD = 0; |
| 23700 | + computeJD(&x); |
| 23701 | + t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); |
| 23702 | + }else{ |
| 23703 | + iYearDiff = 0; |
| 23704 | + t = (time_t)(p->iJD/1000 - 21086676*(i64)10000); |
| 23705 | + } |
| 23438 | 23706 | if( osLocaltime(&t, &sLocal) ){ |
| 23439 | 23707 | sqlite3_result_error(pCtx, "local time unavailable", -1); |
| 23440 | | - *pRc = SQLITE_ERROR; |
| 23441 | | - return 0; |
| 23442 | | - } |
| 23443 | | - y.Y = sLocal.tm_year + 1900; |
| 23444 | | - y.M = sLocal.tm_mon + 1; |
| 23445 | | - y.D = sLocal.tm_mday; |
| 23446 | | - y.h = sLocal.tm_hour; |
| 23447 | | - y.m = sLocal.tm_min; |
| 23448 | | - y.s = sLocal.tm_sec; |
| 23449 | | - y.validYMD = 1; |
| 23450 | | - y.validHMS = 1; |
| 23451 | | - y.validJD = 0; |
| 23452 | | - y.rawS = 0; |
| 23453 | | - y.validTZ = 0; |
| 23454 | | - y.isError = 0; |
| 23455 | | - computeJD(&y); |
| 23456 | | - *pRc = SQLITE_OK; |
| 23457 | | - return y.iJD - x.iJD; |
| 23708 | + return SQLITE_ERROR; |
| 23709 | + } |
| 23710 | + p->Y = sLocal.tm_year + 1900 - iYearDiff; |
| 23711 | + p->M = sLocal.tm_mon + 1; |
| 23712 | + p->D = sLocal.tm_mday; |
| 23713 | + p->h = sLocal.tm_hour; |
| 23714 | + p->m = sLocal.tm_min; |
| 23715 | + p->s = sLocal.tm_sec + (p->iJD%1000)*0.001; |
| 23716 | + p->validYMD = 1; |
| 23717 | + p->validHMS = 1; |
| 23718 | + p->validJD = 0; |
| 23719 | + p->rawS = 0; |
| 23720 | + p->validTZ = 0; |
| 23721 | + p->isError = 0; |
| 23722 | + return SQLITE_OK; |
| 23458 | 23723 | } |
| 23459 | 23724 | #endif /* SQLITE_OMIT_LOCALTIME */ |
| 23460 | 23725 | |
| 23461 | 23726 | /* |
| 23462 | 23727 | ** The following table defines various date transformations of the form |
| | @@ -23524,11 +23789,13 @@ |
| 23524 | 23789 | if( sqlite3_stricmp(z, "auto")==0 ){ |
| 23525 | 23790 | if( idx>1 ) return 1; /* IMP: R-33611-57934 */ |
| 23526 | 23791 | if( !p->rawS || p->validJD ){ |
| 23527 | 23792 | rc = 0; |
| 23528 | 23793 | p->rawS = 0; |
| 23529 | | - }else if( p->s>=-210866760000 && p->s<=253402300799 ){ |
| 23794 | + }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */ |
| 23795 | + && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */ |
| 23796 | + ){ |
| 23530 | 23797 | r = p->s*1000.0 + 210866760000000.0; |
| 23531 | 23798 | clearYMD_HMS_TZ(p); |
| 23532 | 23799 | p->iJD = (sqlite3_int64)(r + 0.5); |
| 23533 | 23800 | p->validJD = 1; |
| 23534 | 23801 | p->rawS = 0; |
| | @@ -23545,11 +23812,11 @@ |
| 23545 | 23812 | ** is not the first modifier, or if the prior argument is not a numeric |
| 23546 | 23813 | ** value in the allowed range of julian day numbers understood by |
| 23547 | 23814 | ** SQLite (0..5373484.5) then the result will be NULL. |
| 23548 | 23815 | */ |
| 23549 | 23816 | if( sqlite3_stricmp(z, "julianday")==0 ){ |
| 23550 | | - if( idx>1 ) return 1; |
| 23817 | + if( idx>1 ) return 1; /* IMP: R-31176-64601 */ |
| 23551 | 23818 | if( p->validJD && p->rawS ){ |
| 23552 | 23819 | rc = 0; |
| 23553 | 23820 | p->rawS = 0; |
| 23554 | 23821 | } |
| 23555 | 23822 | } |
| | @@ -23561,13 +23828,11 @@ |
| 23561 | 23828 | ** |
| 23562 | 23829 | ** Assuming the current time value is UTC (a.k.a. GMT), shift it to |
| 23563 | 23830 | ** show local time. |
| 23564 | 23831 | */ |
| 23565 | 23832 | if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){ |
| 23566 | | - computeJD(p); |
| 23567 | | - p->iJD += localtimeOffset(p, pCtx, &rc); |
| 23568 | | - clearYMD_HMS_TZ(p); |
| 23833 | + rc = toLocaltime(p, pCtx); |
| 23569 | 23834 | } |
| 23570 | 23835 | break; |
| 23571 | 23836 | } |
| 23572 | 23837 | #endif |
| 23573 | 23838 | case 'u': { |
| | @@ -23576,10 +23841,11 @@ |
| 23576 | 23841 | ** |
| 23577 | 23842 | ** Treat the current value of p->s as the number of |
| 23578 | 23843 | ** seconds since 1970. Convert to a real julian day number. |
| 23579 | 23844 | */ |
| 23580 | 23845 | if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ |
| 23846 | + if( idx>1 ) return 1; /* IMP: R-49255-55373 */ |
| 23581 | 23847 | r = p->s*1000.0 + 210866760000000.0; |
| 23582 | 23848 | if( r>=0.0 && r<464269060800000.0 ){ |
| 23583 | 23849 | clearYMD_HMS_TZ(p); |
| 23584 | 23850 | p->iJD = (sqlite3_int64)(r + 0.5); |
| 23585 | 23851 | p->validJD = 1; |
| | @@ -23588,22 +23854,35 @@ |
| 23588 | 23854 | } |
| 23589 | 23855 | } |
| 23590 | 23856 | #ifndef SQLITE_OMIT_LOCALTIME |
| 23591 | 23857 | else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){ |
| 23592 | 23858 | if( p->tzSet==0 ){ |
| 23593 | | - sqlite3_int64 c1; |
| 23859 | + i64 iOrigJD; /* Original localtime */ |
| 23860 | + i64 iGuess; /* Guess at the corresponding utc time */ |
| 23861 | + int cnt = 0; /* Safety to prevent infinite loop */ |
| 23862 | + int iErr; /* Guess is off by this much */ |
| 23863 | + |
| 23594 | 23864 | computeJD(p); |
| 23595 | | - c1 = localtimeOffset(p, pCtx, &rc); |
| 23596 | | - if( rc==SQLITE_OK ){ |
| 23597 | | - p->iJD -= c1; |
| 23598 | | - clearYMD_HMS_TZ(p); |
| 23599 | | - p->iJD += c1 - localtimeOffset(p, pCtx, &rc); |
| 23600 | | - } |
| 23865 | + iGuess = iOrigJD = p->iJD; |
| 23866 | + iErr = 0; |
| 23867 | + do{ |
| 23868 | + DateTime new; |
| 23869 | + memset(&new, 0, sizeof(new)); |
| 23870 | + iGuess -= iErr; |
| 23871 | + new.iJD = iGuess; |
| 23872 | + new.validJD = 1; |
| 23873 | + rc = toLocaltime(&new, pCtx); |
| 23874 | + if( rc ) return rc; |
| 23875 | + computeJD(&new); |
| 23876 | + iErr = new.iJD - iOrigJD; |
| 23877 | + }while( iErr && cnt++<3 ); |
| 23878 | + memset(p, 0, sizeof(*p)); |
| 23879 | + p->iJD = iGuess; |
| 23880 | + p->validJD = 1; |
| 23601 | 23881 | p->tzSet = 1; |
| 23602 | | - }else{ |
| 23603 | | - rc = SQLITE_OK; |
| 23604 | 23882 | } |
| 23883 | + rc = SQLITE_OK; |
| 23605 | 23884 | } |
| 23606 | 23885 | #endif |
| 23607 | 23886 | break; |
| 23608 | 23887 | } |
| 23609 | 23888 | case 'w': { |
| | @@ -23849,15 +24128,42 @@ |
| 23849 | 24128 | int argc, |
| 23850 | 24129 | sqlite3_value **argv |
| 23851 | 24130 | ){ |
| 23852 | 24131 | DateTime x; |
| 23853 | 24132 | if( isDate(context, argc, argv, &x)==0 ){ |
| 23854 | | - char zBuf[100]; |
| 24133 | + int Y, s; |
| 24134 | + char zBuf[24]; |
| 23855 | 24135 | computeYMD_HMS(&x); |
| 23856 | | - sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d", |
| 23857 | | - x.Y, x.M, x.D, x.h, x.m, (int)(x.s)); |
| 23858 | | - sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 24136 | + Y = x.Y; |
| 24137 | + if( Y<0 ) Y = -Y; |
| 24138 | + zBuf[1] = '0' + (Y/1000)%10; |
| 24139 | + zBuf[2] = '0' + (Y/100)%10; |
| 24140 | + zBuf[3] = '0' + (Y/10)%10; |
| 24141 | + zBuf[4] = '0' + (Y)%10; |
| 24142 | + zBuf[5] = '-'; |
| 24143 | + zBuf[6] = '0' + (x.M/10)%10; |
| 24144 | + zBuf[7] = '0' + (x.M)%10; |
| 24145 | + zBuf[8] = '-'; |
| 24146 | + zBuf[9] = '0' + (x.D/10)%10; |
| 24147 | + zBuf[10] = '0' + (x.D)%10; |
| 24148 | + zBuf[11] = ' '; |
| 24149 | + zBuf[12] = '0' + (x.h/10)%10; |
| 24150 | + zBuf[13] = '0' + (x.h)%10; |
| 24151 | + zBuf[14] = ':'; |
| 24152 | + zBuf[15] = '0' + (x.m/10)%10; |
| 24153 | + zBuf[16] = '0' + (x.m)%10; |
| 24154 | + zBuf[17] = ':'; |
| 24155 | + s = (int)x.s; |
| 24156 | + zBuf[18] = '0' + (s/10)%10; |
| 24157 | + zBuf[19] = '0' + (s)%10; |
| 24158 | + zBuf[20] = 0; |
| 24159 | + if( x.Y<0 ){ |
| 24160 | + zBuf[0] = '-'; |
| 24161 | + sqlite3_result_text(context, zBuf, 20, SQLITE_TRANSIENT); |
| 24162 | + }else{ |
| 24163 | + sqlite3_result_text(context, &zBuf[1], 19, SQLITE_TRANSIENT); |
| 24164 | + } |
| 23859 | 24165 | } |
| 23860 | 24166 | } |
| 23861 | 24167 | |
| 23862 | 24168 | /* |
| 23863 | 24169 | ** time( TIMESTRING, MOD, MOD, ...) |
| | @@ -23869,14 +24175,24 @@ |
| 23869 | 24175 | int argc, |
| 23870 | 24176 | sqlite3_value **argv |
| 23871 | 24177 | ){ |
| 23872 | 24178 | DateTime x; |
| 23873 | 24179 | if( isDate(context, argc, argv, &x)==0 ){ |
| 23874 | | - char zBuf[100]; |
| 24180 | + int s; |
| 24181 | + char zBuf[16]; |
| 23875 | 24182 | computeHMS(&x); |
| 23876 | | - sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s); |
| 23877 | | - sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 24183 | + zBuf[0] = '0' + (x.h/10)%10; |
| 24184 | + zBuf[1] = '0' + (x.h)%10; |
| 24185 | + zBuf[2] = ':'; |
| 24186 | + zBuf[3] = '0' + (x.m/10)%10; |
| 24187 | + zBuf[4] = '0' + (x.m)%10; |
| 24188 | + zBuf[5] = ':'; |
| 24189 | + s = (int)x.s; |
| 24190 | + zBuf[6] = '0' + (s/10)%10; |
| 24191 | + zBuf[7] = '0' + (s)%10; |
| 24192 | + zBuf[8] = 0; |
| 24193 | + sqlite3_result_text(context, zBuf, 8, SQLITE_TRANSIENT); |
| 23878 | 24194 | } |
| 23879 | 24195 | } |
| 23880 | 24196 | |
| 23881 | 24197 | /* |
| 23882 | 24198 | ** date( TIMESTRING, MOD, MOD, ...) |
| | @@ -23888,14 +24204,32 @@ |
| 23888 | 24204 | int argc, |
| 23889 | 24205 | sqlite3_value **argv |
| 23890 | 24206 | ){ |
| 23891 | 24207 | DateTime x; |
| 23892 | 24208 | if( isDate(context, argc, argv, &x)==0 ){ |
| 23893 | | - char zBuf[100]; |
| 24209 | + int Y; |
| 24210 | + char zBuf[16]; |
| 23894 | 24211 | computeYMD(&x); |
| 23895 | | - sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D); |
| 23896 | | - sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 24212 | + Y = x.Y; |
| 24213 | + if( Y<0 ) Y = -Y; |
| 24214 | + zBuf[1] = '0' + (Y/1000)%10; |
| 24215 | + zBuf[2] = '0' + (Y/100)%10; |
| 24216 | + zBuf[3] = '0' + (Y/10)%10; |
| 24217 | + zBuf[4] = '0' + (Y)%10; |
| 24218 | + zBuf[5] = '-'; |
| 24219 | + zBuf[6] = '0' + (x.M/10)%10; |
| 24220 | + zBuf[7] = '0' + (x.M)%10; |
| 24221 | + zBuf[8] = '-'; |
| 24222 | + zBuf[9] = '0' + (x.D/10)%10; |
| 24223 | + zBuf[10] = '0' + (x.D)%10; |
| 24224 | + zBuf[11] = 0; |
| 24225 | + if( x.Y<0 ){ |
| 24226 | + zBuf[0] = '-'; |
| 24227 | + sqlite3_result_text(context, zBuf, 11, SQLITE_TRANSIENT); |
| 24228 | + }else{ |
| 24229 | + sqlite3_result_text(context, &zBuf[1], 10, SQLITE_TRANSIENT); |
| 24230 | + } |
| 23897 | 24231 | } |
| 23898 | 24232 | } |
| 23899 | 24233 | |
| 23900 | 24234 | /* |
| 23901 | 24235 | ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...) |
| | @@ -26668,12 +27002,17 @@ |
| 26668 | 27002 | ** 32-bit signed integer. Hence the largest allocation is 0x40000000 |
| 26669 | 27003 | ** or 1073741824 bytes. |
| 26670 | 27004 | */ |
| 26671 | 27005 | static int memsys5Roundup(int n){ |
| 26672 | 27006 | int iFullSz; |
| 26673 | | - if( n > 0x40000000 ) return 0; |
| 26674 | | - for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2); |
| 27007 | + if( n<=mem5.szAtom*2 ){ |
| 27008 | + if( n<=mem5.szAtom ) return mem5.szAtom; |
| 27009 | + return mem5.szAtom*2; |
| 27010 | + } |
| 27011 | + if( n>0x40000000 ) return 0; |
| 27012 | + for(iFullSz=mem5.szAtom*8; iFullSz<n; iFullSz *= 4); |
| 27013 | + if( (iFullSz/2)>=n ) return iFullSz/2; |
| 26675 | 27014 | return iFullSz; |
| 26676 | 27015 | } |
| 26677 | 27016 | |
| 26678 | 27017 | /* |
| 26679 | 27018 | ** Return the ceiling of the logarithm base 2 of iValue. |
| | @@ -29988,17 +30327,26 @@ |
| 29988 | 30327 | bufpt[j] = 0; |
| 29989 | 30328 | length = j; |
| 29990 | 30329 | goto adjust_width_for_utf8; |
| 29991 | 30330 | } |
| 29992 | 30331 | case etTOKEN: { |
| 29993 | | - Token *pToken; |
| 29994 | 30332 | if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
| 29995 | | - pToken = va_arg(ap, Token*); |
| 29996 | | - assert( bArgList==0 ); |
| 29997 | | - if( pToken && pToken->n ){ |
| 29998 | | - sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); |
| 29999 | | - sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); |
| 30333 | + if( flag_alternateform ){ |
| 30334 | + /* %#T means an Expr pointer that uses Expr.u.zToken */ |
| 30335 | + Expr *pExpr = va_arg(ap,Expr*); |
| 30336 | + if( ALWAYS(pExpr) && ALWAYS(!ExprHasProperty(pExpr,EP_IntValue)) ){ |
| 30337 | + sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken); |
| 30338 | + sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr); |
| 30339 | + } |
| 30340 | + }else{ |
| 30341 | + /* %T means a Token pointer */ |
| 30342 | + Token *pToken = va_arg(ap, Token*); |
| 30343 | + assert( bArgList==0 ); |
| 30344 | + if( pToken && pToken->n ){ |
| 30345 | + sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); |
| 30346 | + sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); |
| 30347 | + } |
| 30000 | 30348 | } |
| 30001 | 30349 | length = width = 0; |
| 30002 | 30350 | break; |
| 30003 | 30351 | } |
| 30004 | 30352 | case etSRCITEM: { |
| | @@ -30072,10 +30420,22 @@ |
| 30072 | 30420 | zEnd = &zText[strlen(zText)]; |
| 30073 | 30421 | if( SQLITE_WITHIN(z,zText,zEnd) ){ |
| 30074 | 30422 | db->errByteOffset = (int)(z-zText); |
| 30075 | 30423 | } |
| 30076 | 30424 | } |
| 30425 | + |
| 30426 | +/* |
| 30427 | +** If pExpr has a byte offset for the start of a token, record that as |
| 30428 | +** as the error offset. |
| 30429 | +*/ |
| 30430 | +SQLITE_PRIVATE void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){ |
| 30431 | + while( pExpr && (ExprHasProperty(pExpr,EP_FromJoin) || pExpr->w.iOfst<=0) ){ |
| 30432 | + pExpr = pExpr->pLeft; |
| 30433 | + } |
| 30434 | + if( pExpr==0 ) return; |
| 30435 | + db->errByteOffset = pExpr->w.iOfst; |
| 30436 | +} |
| 30077 | 30437 | |
| 30078 | 30438 | /* |
| 30079 | 30439 | ** Enlarge the memory allocation on a StrAccum object so that it is |
| 30080 | 30440 | ** able to accept at least N more bytes of text. |
| 30081 | 30441 | ** |
| | @@ -30535,11 +30895,11 @@ |
| 30535 | 30895 | memset(p, 0, sizeof(*p)); |
| 30536 | 30896 | }else{ |
| 30537 | 30897 | p->iLevel++; |
| 30538 | 30898 | } |
| 30539 | 30899 | assert( moreToFollow==0 || moreToFollow==1 ); |
| 30540 | | - if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; |
| 30900 | + if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; |
| 30541 | 30901 | return p; |
| 30542 | 30902 | } |
| 30543 | 30903 | |
| 30544 | 30904 | /* |
| 30545 | 30905 | ** Finished with one layer of the tree |
| | @@ -30559,11 +30919,11 @@ |
| 30559 | 30919 | int i; |
| 30560 | 30920 | StrAccum acc; |
| 30561 | 30921 | char zBuf[500]; |
| 30562 | 30922 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 30563 | 30923 | if( p ){ |
| 30564 | | - for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){ |
| 30924 | + for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){ |
| 30565 | 30925 | sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4); |
| 30566 | 30926 | } |
| 30567 | 30927 | sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); |
| 30568 | 30928 | } |
| 30569 | 30929 | if( zFormat!=0 ){ |
| | @@ -30889,11 +31249,11 @@ |
| 30889 | 31249 | ** Generate a human-readable explanation for a Window Function object |
| 30890 | 31250 | */ |
| 30891 | 31251 | SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){ |
| 30892 | 31252 | pView = sqlite3TreeViewPush(pView, more); |
| 30893 | 31253 | sqlite3TreeViewLine(pView, "WINFUNC %s(%d)", |
| 30894 | | - pWin->pFunc->zName, pWin->pFunc->nArg); |
| 31254 | + pWin->pWFunc->zName, pWin->pWFunc->nArg); |
| 30895 | 31255 | sqlite3TreeViewWindow(pView, pWin, 0); |
| 30896 | 31256 | sqlite3TreeViewPop(pView); |
| 30897 | 31257 | } |
| 30898 | 31258 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 30899 | 31259 | |
| | @@ -30914,11 +31274,11 @@ |
| 30914 | 31274 | StrAccum x; |
| 30915 | 31275 | sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0); |
| 30916 | 31276 | sqlite3_str_appendf(&x, " fg.af=%x.%c", |
| 30917 | 31277 | pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n'); |
| 30918 | 31278 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 30919 | | - sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable); |
| 31279 | + sqlite3_str_appendf(&x, " iRJT=%d", pExpr->w.iRightJoinTable); |
| 30920 | 31280 | } |
| 30921 | 31281 | if( ExprHasProperty(pExpr, EP_FromDDL) ){ |
| 30922 | 31282 | sqlite3_str_appendf(&x, " DDL"); |
| 30923 | 31283 | } |
| 30924 | 31284 | if( ExprHasVVAProperty(pExpr, EP_Immutable) ){ |
| | @@ -34282,46 +34642,46 @@ |
| 34282 | 34642 | SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ |
| 34283 | 34643 | static const char *const azName[] = { |
| 34284 | 34644 | /* 0 */ "Savepoint" OpHelp(""), |
| 34285 | 34645 | /* 1 */ "AutoCommit" OpHelp(""), |
| 34286 | 34646 | /* 2 */ "Transaction" OpHelp(""), |
| 34287 | | - /* 3 */ "SorterNext" OpHelp(""), |
| 34288 | | - /* 4 */ "Prev" OpHelp(""), |
| 34289 | | - /* 5 */ "Next" OpHelp(""), |
| 34290 | | - /* 6 */ "Checkpoint" OpHelp(""), |
| 34291 | | - /* 7 */ "JournalMode" OpHelp(""), |
| 34292 | | - /* 8 */ "Vacuum" OpHelp(""), |
| 34293 | | - /* 9 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"), |
| 34294 | | - /* 10 */ "VUpdate" OpHelp("data=r[P3@P2]"), |
| 34295 | | - /* 11 */ "Goto" OpHelp(""), |
| 34296 | | - /* 12 */ "Gosub" OpHelp(""), |
| 34297 | | - /* 13 */ "InitCoroutine" OpHelp(""), |
| 34298 | | - /* 14 */ "Yield" OpHelp(""), |
| 34299 | | - /* 15 */ "MustBeInt" OpHelp(""), |
| 34300 | | - /* 16 */ "Jump" OpHelp(""), |
| 34301 | | - /* 17 */ "Once" OpHelp(""), |
| 34302 | | - /* 18 */ "If" OpHelp(""), |
| 34647 | + /* 3 */ "Checkpoint" OpHelp(""), |
| 34648 | + /* 4 */ "JournalMode" OpHelp(""), |
| 34649 | + /* 5 */ "Vacuum" OpHelp(""), |
| 34650 | + /* 6 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"), |
| 34651 | + /* 7 */ "VUpdate" OpHelp("data=r[P3@P2]"), |
| 34652 | + /* 8 */ "Goto" OpHelp(""), |
| 34653 | + /* 9 */ "Gosub" OpHelp(""), |
| 34654 | + /* 10 */ "InitCoroutine" OpHelp(""), |
| 34655 | + /* 11 */ "Yield" OpHelp(""), |
| 34656 | + /* 12 */ "MustBeInt" OpHelp(""), |
| 34657 | + /* 13 */ "Jump" OpHelp(""), |
| 34658 | + /* 14 */ "Once" OpHelp(""), |
| 34659 | + /* 15 */ "If" OpHelp(""), |
| 34660 | + /* 16 */ "IfNot" OpHelp(""), |
| 34661 | + /* 17 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"), |
| 34662 | + /* 18 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"), |
| 34303 | 34663 | /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"), |
| 34304 | | - /* 20 */ "IfNot" OpHelp(""), |
| 34305 | | - /* 21 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"), |
| 34306 | | - /* 22 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"), |
| 34307 | | - /* 23 */ "SeekLT" OpHelp("key=r[P3@P4]"), |
| 34308 | | - /* 24 */ "SeekLE" OpHelp("key=r[P3@P4]"), |
| 34309 | | - /* 25 */ "SeekGE" OpHelp("key=r[P3@P4]"), |
| 34310 | | - /* 26 */ "SeekGT" OpHelp("key=r[P3@P4]"), |
| 34311 | | - /* 27 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"), |
| 34312 | | - /* 28 */ "IfNoHope" OpHelp("key=r[P3@P4]"), |
| 34313 | | - /* 29 */ "NoConflict" OpHelp("key=r[P3@P4]"), |
| 34314 | | - /* 30 */ "NotFound" OpHelp("key=r[P3@P4]"), |
| 34315 | | - /* 31 */ "Found" OpHelp("key=r[P3@P4]"), |
| 34316 | | - /* 32 */ "SeekRowid" OpHelp("intkey=r[P3]"), |
| 34317 | | - /* 33 */ "NotExists" OpHelp("intkey=r[P3]"), |
| 34318 | | - /* 34 */ "Last" OpHelp(""), |
| 34319 | | - /* 35 */ "IfSmaller" OpHelp(""), |
| 34320 | | - /* 36 */ "SorterSort" OpHelp(""), |
| 34321 | | - /* 37 */ "Sort" OpHelp(""), |
| 34322 | | - /* 38 */ "Rewind" OpHelp(""), |
| 34664 | + /* 20 */ "SeekLT" OpHelp("key=r[P3@P4]"), |
| 34665 | + /* 21 */ "SeekLE" OpHelp("key=r[P3@P4]"), |
| 34666 | + /* 22 */ "SeekGE" OpHelp("key=r[P3@P4]"), |
| 34667 | + /* 23 */ "SeekGT" OpHelp("key=r[P3@P4]"), |
| 34668 | + /* 24 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"), |
| 34669 | + /* 25 */ "IfNoHope" OpHelp("key=r[P3@P4]"), |
| 34670 | + /* 26 */ "NoConflict" OpHelp("key=r[P3@P4]"), |
| 34671 | + /* 27 */ "NotFound" OpHelp("key=r[P3@P4]"), |
| 34672 | + /* 28 */ "Found" OpHelp("key=r[P3@P4]"), |
| 34673 | + /* 29 */ "SeekRowid" OpHelp("intkey=r[P3]"), |
| 34674 | + /* 30 */ "NotExists" OpHelp("intkey=r[P3]"), |
| 34675 | + /* 31 */ "Last" OpHelp(""), |
| 34676 | + /* 32 */ "IfSmaller" OpHelp(""), |
| 34677 | + /* 33 */ "SorterSort" OpHelp(""), |
| 34678 | + /* 34 */ "Sort" OpHelp(""), |
| 34679 | + /* 35 */ "Rewind" OpHelp(""), |
| 34680 | + /* 36 */ "SorterNext" OpHelp(""), |
| 34681 | + /* 37 */ "Prev" OpHelp(""), |
| 34682 | + /* 38 */ "Next" OpHelp(""), |
| 34323 | 34683 | /* 39 */ "IdxLE" OpHelp("key=r[P3@P4]"), |
| 34324 | 34684 | /* 40 */ "IdxGT" OpHelp("key=r[P3@P4]"), |
| 34325 | 34685 | /* 41 */ "IdxLT" OpHelp("key=r[P3@P4]"), |
| 34326 | 34686 | /* 42 */ "IdxGE" OpHelp("key=r[P3@P4]"), |
| 34327 | 34687 | /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"), |
| | @@ -34350,41 +34710,41 @@ |
| 34350 | 34710 | /* 66 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"), |
| 34351 | 34711 | /* 67 */ "Return" OpHelp(""), |
| 34352 | 34712 | /* 68 */ "EndCoroutine" OpHelp(""), |
| 34353 | 34713 | /* 69 */ "HaltIfNull" OpHelp("if r[P3]=null halt"), |
| 34354 | 34714 | /* 70 */ "Halt" OpHelp(""), |
| 34355 | | - /* 71 */ "Integer" OpHelp("r[P2]=P1"), |
| 34356 | | - /* 72 */ "Int64" OpHelp("r[P2]=P4"), |
| 34357 | | - /* 73 */ "String" OpHelp("r[P2]='P4' (len=P1)"), |
| 34358 | | - /* 74 */ "Null" OpHelp("r[P2..P3]=NULL"), |
| 34359 | | - /* 75 */ "SoftNull" OpHelp("r[P1]=NULL"), |
| 34360 | | - /* 76 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"), |
| 34361 | | - /* 77 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"), |
| 34362 | | - /* 78 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"), |
| 34363 | | - /* 79 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"), |
| 34364 | | - /* 80 */ "SCopy" OpHelp("r[P2]=r[P1]"), |
| 34365 | | - /* 81 */ "IntCopy" OpHelp("r[P2]=r[P1]"), |
| 34366 | | - /* 82 */ "FkCheck" OpHelp(""), |
| 34367 | | - /* 83 */ "ResultRow" OpHelp("output=r[P1@P2]"), |
| 34368 | | - /* 84 */ "CollSeq" OpHelp(""), |
| 34369 | | - /* 85 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"), |
| 34370 | | - /* 86 */ "RealAffinity" OpHelp(""), |
| 34371 | | - /* 87 */ "Cast" OpHelp("affinity(r[P1])"), |
| 34372 | | - /* 88 */ "Permutation" OpHelp(""), |
| 34373 | | - /* 89 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"), |
| 34374 | | - /* 90 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"), |
| 34375 | | - /* 91 */ "ZeroOrNull" OpHelp("r[P2] = 0 OR NULL"), |
| 34376 | | - /* 92 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"), |
| 34377 | | - /* 93 */ "Column" OpHelp("r[P3]=PX"), |
| 34378 | | - /* 94 */ "TypeCheck" OpHelp("typecheck(r[P1@P2])"), |
| 34379 | | - /* 95 */ "Affinity" OpHelp("affinity(r[P1@P2])"), |
| 34380 | | - /* 96 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"), |
| 34381 | | - /* 97 */ "Count" OpHelp("r[P2]=count()"), |
| 34382 | | - /* 98 */ "ReadCookie" OpHelp(""), |
| 34383 | | - /* 99 */ "SetCookie" OpHelp(""), |
| 34384 | | - /* 100 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"), |
| 34385 | | - /* 101 */ "OpenRead" OpHelp("root=P2 iDb=P3"), |
| 34715 | + /* 71 */ "BeginSubrtn" OpHelp("r[P2]=P1"), |
| 34716 | + /* 72 */ "Integer" OpHelp("r[P2]=P1"), |
| 34717 | + /* 73 */ "Int64" OpHelp("r[P2]=P4"), |
| 34718 | + /* 74 */ "String" OpHelp("r[P2]='P4' (len=P1)"), |
| 34719 | + /* 75 */ "Null" OpHelp("r[P2..P3]=NULL"), |
| 34720 | + /* 76 */ "SoftNull" OpHelp("r[P1]=NULL"), |
| 34721 | + /* 77 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"), |
| 34722 | + /* 78 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"), |
| 34723 | + /* 79 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"), |
| 34724 | + /* 80 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"), |
| 34725 | + /* 81 */ "SCopy" OpHelp("r[P2]=r[P1]"), |
| 34726 | + /* 82 */ "IntCopy" OpHelp("r[P2]=r[P1]"), |
| 34727 | + /* 83 */ "FkCheck" OpHelp(""), |
| 34728 | + /* 84 */ "ResultRow" OpHelp("output=r[P1@P2]"), |
| 34729 | + /* 85 */ "CollSeq" OpHelp(""), |
| 34730 | + /* 86 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"), |
| 34731 | + /* 87 */ "RealAffinity" OpHelp(""), |
| 34732 | + /* 88 */ "Cast" OpHelp("affinity(r[P1])"), |
| 34733 | + /* 89 */ "Permutation" OpHelp(""), |
| 34734 | + /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"), |
| 34735 | + /* 91 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"), |
| 34736 | + /* 92 */ "ZeroOrNull" OpHelp("r[P2] = 0 OR NULL"), |
| 34737 | + /* 93 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"), |
| 34738 | + /* 94 */ "Column" OpHelp("r[P3]=PX"), |
| 34739 | + /* 95 */ "TypeCheck" OpHelp("typecheck(r[P1@P2])"), |
| 34740 | + /* 96 */ "Affinity" OpHelp("affinity(r[P1@P2])"), |
| 34741 | + /* 97 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"), |
| 34742 | + /* 98 */ "Count" OpHelp("r[P2]=count()"), |
| 34743 | + /* 99 */ "ReadCookie" OpHelp(""), |
| 34744 | + /* 100 */ "SetCookie" OpHelp(""), |
| 34745 | + /* 101 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"), |
| 34386 | 34746 | /* 102 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"), |
| 34387 | 34747 | /* 103 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"), |
| 34388 | 34748 | /* 104 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"), |
| 34389 | 34749 | /* 105 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"), |
| 34390 | 34750 | /* 106 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"), |
| | @@ -34391,82 +34751,84 @@ |
| 34391 | 34751 | /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"), |
| 34392 | 34752 | /* 108 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"), |
| 34393 | 34753 | /* 109 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"), |
| 34394 | 34754 | /* 110 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"), |
| 34395 | 34755 | /* 111 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"), |
| 34396 | | - /* 112 */ "OpenWrite" OpHelp("root=P2 iDb=P3"), |
| 34397 | | - /* 113 */ "OpenDup" OpHelp(""), |
| 34756 | + /* 112 */ "OpenRead" OpHelp("root=P2 iDb=P3"), |
| 34757 | + /* 113 */ "OpenWrite" OpHelp("root=P2 iDb=P3"), |
| 34398 | 34758 | /* 114 */ "BitNot" OpHelp("r[P2]= ~r[P1]"), |
| 34399 | | - /* 115 */ "OpenAutoindex" OpHelp("nColumn=P2"), |
| 34400 | | - /* 116 */ "OpenEphemeral" OpHelp("nColumn=P2"), |
| 34759 | + /* 115 */ "OpenDup" OpHelp(""), |
| 34760 | + /* 116 */ "OpenAutoindex" OpHelp("nColumn=P2"), |
| 34401 | 34761 | /* 117 */ "String8" OpHelp("r[P2]='P4'"), |
| 34402 | | - /* 118 */ "SorterOpen" OpHelp(""), |
| 34403 | | - /* 119 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"), |
| 34404 | | - /* 120 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"), |
| 34405 | | - /* 121 */ "Close" OpHelp(""), |
| 34406 | | - /* 122 */ "ColumnsUsed" OpHelp(""), |
| 34407 | | - /* 123 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"), |
| 34408 | | - /* 124 */ "SeekHit" OpHelp("set P2<=seekHit<=P3"), |
| 34409 | | - /* 125 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"), |
| 34410 | | - /* 126 */ "NewRowid" OpHelp("r[P2]=rowid"), |
| 34411 | | - /* 127 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"), |
| 34412 | | - /* 128 */ "RowCell" OpHelp(""), |
| 34413 | | - /* 129 */ "Delete" OpHelp(""), |
| 34414 | | - /* 130 */ "ResetCount" OpHelp(""), |
| 34415 | | - /* 131 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"), |
| 34416 | | - /* 132 */ "SorterData" OpHelp("r[P2]=data"), |
| 34417 | | - /* 133 */ "RowData" OpHelp("r[P2]=data"), |
| 34418 | | - /* 134 */ "Rowid" OpHelp("r[P2]=rowid"), |
| 34419 | | - /* 135 */ "NullRow" OpHelp(""), |
| 34420 | | - /* 136 */ "SeekEnd" OpHelp(""), |
| 34421 | | - /* 137 */ "IdxInsert" OpHelp("key=r[P2]"), |
| 34422 | | - /* 138 */ "SorterInsert" OpHelp("key=r[P2]"), |
| 34423 | | - /* 139 */ "IdxDelete" OpHelp("key=r[P2@P3]"), |
| 34424 | | - /* 140 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"), |
| 34425 | | - /* 141 */ "IdxRowid" OpHelp("r[P2]=rowid"), |
| 34426 | | - /* 142 */ "FinishSeek" OpHelp(""), |
| 34427 | | - /* 143 */ "Destroy" OpHelp(""), |
| 34428 | | - /* 144 */ "Clear" OpHelp(""), |
| 34429 | | - /* 145 */ "ResetSorter" OpHelp(""), |
| 34430 | | - /* 146 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"), |
| 34431 | | - /* 147 */ "SqlExec" OpHelp(""), |
| 34432 | | - /* 148 */ "ParseSchema" OpHelp(""), |
| 34433 | | - /* 149 */ "LoadAnalysis" OpHelp(""), |
| 34434 | | - /* 150 */ "DropTable" OpHelp(""), |
| 34435 | | - /* 151 */ "DropIndex" OpHelp(""), |
| 34436 | | - /* 152 */ "DropTrigger" OpHelp(""), |
| 34762 | + /* 118 */ "OpenEphemeral" OpHelp("nColumn=P2"), |
| 34763 | + /* 119 */ "SorterOpen" OpHelp(""), |
| 34764 | + /* 120 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"), |
| 34765 | + /* 121 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"), |
| 34766 | + /* 122 */ "Close" OpHelp(""), |
| 34767 | + /* 123 */ "ColumnsUsed" OpHelp(""), |
| 34768 | + /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"), |
| 34769 | + /* 125 */ "SeekHit" OpHelp("set P2<=seekHit<=P3"), |
| 34770 | + /* 126 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"), |
| 34771 | + /* 127 */ "NewRowid" OpHelp("r[P2]=rowid"), |
| 34772 | + /* 128 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"), |
| 34773 | + /* 129 */ "RowCell" OpHelp(""), |
| 34774 | + /* 130 */ "Delete" OpHelp(""), |
| 34775 | + /* 131 */ "ResetCount" OpHelp(""), |
| 34776 | + /* 132 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"), |
| 34777 | + /* 133 */ "SorterData" OpHelp("r[P2]=data"), |
| 34778 | + /* 134 */ "RowData" OpHelp("r[P2]=data"), |
| 34779 | + /* 135 */ "Rowid" OpHelp("r[P2]=rowid"), |
| 34780 | + /* 136 */ "NullRow" OpHelp(""), |
| 34781 | + /* 137 */ "SeekEnd" OpHelp(""), |
| 34782 | + /* 138 */ "IdxInsert" OpHelp("key=r[P2]"), |
| 34783 | + /* 139 */ "SorterInsert" OpHelp("key=r[P2]"), |
| 34784 | + /* 140 */ "IdxDelete" OpHelp("key=r[P2@P3]"), |
| 34785 | + /* 141 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"), |
| 34786 | + /* 142 */ "IdxRowid" OpHelp("r[P2]=rowid"), |
| 34787 | + /* 143 */ "FinishSeek" OpHelp(""), |
| 34788 | + /* 144 */ "Destroy" OpHelp(""), |
| 34789 | + /* 145 */ "Clear" OpHelp(""), |
| 34790 | + /* 146 */ "ResetSorter" OpHelp(""), |
| 34791 | + /* 147 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"), |
| 34792 | + /* 148 */ "SqlExec" OpHelp(""), |
| 34793 | + /* 149 */ "ParseSchema" OpHelp(""), |
| 34794 | + /* 150 */ "LoadAnalysis" OpHelp(""), |
| 34795 | + /* 151 */ "DropTable" OpHelp(""), |
| 34796 | + /* 152 */ "DropIndex" OpHelp(""), |
| 34437 | 34797 | /* 153 */ "Real" OpHelp("r[P2]=P4"), |
| 34438 | | - /* 154 */ "IntegrityCk" OpHelp(""), |
| 34439 | | - /* 155 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"), |
| 34440 | | - /* 156 */ "Param" OpHelp(""), |
| 34441 | | - /* 157 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), |
| 34442 | | - /* 158 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), |
| 34443 | | - /* 159 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"), |
| 34444 | | - /* 160 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"), |
| 34445 | | - /* 161 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 34446 | | - /* 162 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 34447 | | - /* 163 */ "AggValue" OpHelp("r[P3]=value N=P2"), |
| 34448 | | - /* 164 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), |
| 34449 | | - /* 165 */ "Expire" OpHelp(""), |
| 34450 | | - /* 166 */ "CursorLock" OpHelp(""), |
| 34451 | | - /* 167 */ "CursorUnlock" OpHelp(""), |
| 34452 | | - /* 168 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), |
| 34453 | | - /* 169 */ "VBegin" OpHelp(""), |
| 34454 | | - /* 170 */ "VCreate" OpHelp(""), |
| 34455 | | - /* 171 */ "VDestroy" OpHelp(""), |
| 34456 | | - /* 172 */ "VOpen" OpHelp(""), |
| 34457 | | - /* 173 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 34458 | | - /* 174 */ "VRename" OpHelp(""), |
| 34459 | | - /* 175 */ "Pagecount" OpHelp(""), |
| 34460 | | - /* 176 */ "MaxPgcnt" OpHelp(""), |
| 34461 | | - /* 177 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"), |
| 34462 | | - /* 178 */ "Trace" OpHelp(""), |
| 34463 | | - /* 179 */ "CursorHint" OpHelp(""), |
| 34464 | | - /* 180 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"), |
| 34465 | | - /* 181 */ "Noop" OpHelp(""), |
| 34466 | | - /* 182 */ "Explain" OpHelp(""), |
| 34467 | | - /* 183 */ "Abortable" OpHelp(""), |
| 34798 | + /* 154 */ "DropTrigger" OpHelp(""), |
| 34799 | + /* 155 */ "IntegrityCk" OpHelp(""), |
| 34800 | + /* 156 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"), |
| 34801 | + /* 157 */ "Param" OpHelp(""), |
| 34802 | + /* 158 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), |
| 34803 | + /* 159 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), |
| 34804 | + /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"), |
| 34805 | + /* 161 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"), |
| 34806 | + /* 162 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 34807 | + /* 163 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 34808 | + /* 164 */ "AggValue" OpHelp("r[P3]=value N=P2"), |
| 34809 | + /* 165 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), |
| 34810 | + /* 166 */ "Expire" OpHelp(""), |
| 34811 | + /* 167 */ "CursorLock" OpHelp(""), |
| 34812 | + /* 168 */ "CursorUnlock" OpHelp(""), |
| 34813 | + /* 169 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), |
| 34814 | + /* 170 */ "VBegin" OpHelp(""), |
| 34815 | + /* 171 */ "VCreate" OpHelp(""), |
| 34816 | + /* 172 */ "VDestroy" OpHelp(""), |
| 34817 | + /* 173 */ "VOpen" OpHelp(""), |
| 34818 | + /* 174 */ "VInitIn" OpHelp("r[P2]=ValueList(P1,P3)"), |
| 34819 | + /* 175 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 34820 | + /* 176 */ "VRename" OpHelp(""), |
| 34821 | + /* 177 */ "Pagecount" OpHelp(""), |
| 34822 | + /* 178 */ "MaxPgcnt" OpHelp(""), |
| 34823 | + /* 179 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"), |
| 34824 | + /* 180 */ "Trace" OpHelp(""), |
| 34825 | + /* 181 */ "CursorHint" OpHelp(""), |
| 34826 | + /* 182 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"), |
| 34827 | + /* 183 */ "Noop" OpHelp(""), |
| 34828 | + /* 184 */ "Explain" OpHelp(""), |
| 34829 | + /* 185 */ "Abortable" OpHelp(""), |
| 34468 | 34830 | }; |
| 34469 | 34831 | return azName[i]; |
| 34470 | 34832 | } |
| 34471 | 34833 | #endif |
| 34472 | 34834 | |
| | @@ -39382,15 +39744,21 @@ |
| 39382 | 39744 | int ofst, /* First lock to acquire or release */ |
| 39383 | 39745 | int n, /* Number of locks to acquire or release */ |
| 39384 | 39746 | int flags /* What to do with the lock */ |
| 39385 | 39747 | ){ |
| 39386 | 39748 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 39387 | | - unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 39388 | | - unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 39749 | + unixShm *p; /* The shared memory being locked */ |
| 39750 | + unixShmNode *pShmNode; /* The underlying file iNode */ |
| 39389 | 39751 | int rc = SQLITE_OK; /* Result code */ |
| 39390 | 39752 | u16 mask; /* Mask of locks to take or release */ |
| 39391 | | - int *aLock = pShmNode->aLock; |
| 39753 | + int *aLock; |
| 39754 | + |
| 39755 | + p = pDbFd->pShm; |
| 39756 | + if( p==0 ) return SQLITE_IOERR_SHMLOCK; |
| 39757 | + pShmNode = p->pShmNode; |
| 39758 | + if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK; |
| 39759 | + aLock = pShmNode->aLock; |
| 39392 | 39760 | |
| 39393 | 39761 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 39394 | 39762 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 39395 | 39763 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
| 39396 | 39764 | assert( n>=1 ); |
| | @@ -40483,24 +40851,27 @@ |
| 40483 | 40851 | ** "<path to db>-journalNN" |
| 40484 | 40852 | ** "<path to db>-walNN" |
| 40485 | 40853 | ** |
| 40486 | 40854 | ** where NN is a decimal number. The NN naming schemes are |
| 40487 | 40855 | ** used by the test_multiplex.c module. |
| 40856 | + ** |
| 40857 | + ** In normal operation, the journal file name will always contain |
| 40858 | + ** a '-' character. However in 8+3 filename mode, or if a corrupt |
| 40859 | + ** rollback journal specifies a super-journal with a goofy name, then |
| 40860 | + ** the '-' might be missing or the '-' might be the first character in |
| 40861 | + ** the filename. In that case, just return SQLITE_OK with *pMode==0. |
| 40488 | 40862 | */ |
| 40489 | 40863 | nDb = sqlite3Strlen30(zPath) - 1; |
| 40490 | | - while( zPath[nDb]!='-' ){ |
| 40491 | | - /* In normal operation, the journal file name will always contain |
| 40492 | | - ** a '-' character. However in 8+3 filename mode, or if a corrupt |
| 40493 | | - ** rollback journal specifies a super-journal with a goofy name, then |
| 40494 | | - ** the '-' might be missing. */ |
| 40495 | | - if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK; |
| 40864 | + while( nDb>0 && zPath[nDb]!='.' ){ |
| 40865 | + if( zPath[nDb]=='-' ){ |
| 40866 | + memcpy(zDb, zPath, nDb); |
| 40867 | + zDb[nDb] = '\0'; |
| 40868 | + rc = getFileMode(zDb, pMode, pUid, pGid); |
| 40869 | + break; |
| 40870 | + } |
| 40496 | 40871 | nDb--; |
| 40497 | 40872 | } |
| 40498 | | - memcpy(zDb, zPath, nDb); |
| 40499 | | - zDb[nDb] = '\0'; |
| 40500 | | - |
| 40501 | | - rc = getFileMode(zDb, pMode, pUid, pGid); |
| 40502 | 40873 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 40503 | 40874 | *pMode = 0600; |
| 40504 | 40875 | }else if( flags & SQLITE_OPEN_URI ){ |
| 40505 | 40876 | /* If this is a main database file and the file was opened using a URI |
| 40506 | 40877 | ** filename, check for the "modeof" parameter. If present, interpret |
| | @@ -46674,14 +47045,18 @@ |
| 46674 | 47045 | int flags /* What to do with the lock */ |
| 46675 | 47046 | ){ |
| 46676 | 47047 | winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */ |
| 46677 | 47048 | winShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 46678 | 47049 | winShm *pX; /* For looping over all siblings */ |
| 46679 | | - winShmNode *pShmNode = p->pShmNode; |
| 47050 | + winShmNode *pShmNode; |
| 46680 | 47051 | int rc = SQLITE_OK; /* Result code */ |
| 46681 | 47052 | u16 mask; /* Mask of locks to take or release */ |
| 46682 | 47053 | |
| 47054 | + if( p==0 ) return SQLITE_IOERR_SHMLOCK; |
| 47055 | + pShmNode = p->pShmNode; |
| 47056 | + if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK; |
| 47057 | + |
| 46683 | 47058 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
| 46684 | 47059 | assert( n>=1 ); |
| 46685 | 47060 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 46686 | 47061 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 46687 | 47062 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| | @@ -53576,10 +53951,11 @@ |
| 53576 | 53951 | u16 nExtra; /* Add this many bytes to each in-memory page */ |
| 53577 | 53952 | i16 nReserve; /* Number of unused bytes at end of each page */ |
| 53578 | 53953 | u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ |
| 53579 | 53954 | u32 sectorSize; /* Assumed sector size during rollback */ |
| 53580 | 53955 | Pgno mxPgno; /* Maximum allowed size of the database */ |
| 53956 | + Pgno lckPgno; /* Page number for the locking page */ |
| 53581 | 53957 | i64 pageSize; /* Number of bytes in a page */ |
| 53582 | 53958 | i64 journalSizeLimit; /* Size limit for persistent journal files */ |
| 53583 | 53959 | char *zFilename; /* Name of the database file */ |
| 53584 | 53960 | char *zJournal; /* Name of the journal file */ |
| 53585 | 53961 | int (*xBusyHandler)(void*); /* Function to call when busy */ |
| | @@ -54562,11 +54938,11 @@ |
| 54562 | 54938 | ** pPager at the current location. The super-journal name must be the last |
| 54563 | 54939 | ** thing written to a journal file. If the pager is in full-sync mode, the |
| 54564 | 54940 | ** journal file descriptor is advanced to the next sector boundary before |
| 54565 | 54941 | ** anything is written. The format is: |
| 54566 | 54942 | ** |
| 54567 | | -** + 4 bytes: PAGER_MJ_PGNO. |
| 54943 | +** + 4 bytes: PAGER_SJ_PGNO. |
| 54568 | 54944 | ** + N bytes: super-journal filename in utf-8. |
| 54569 | 54945 | ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator). |
| 54570 | 54946 | ** + 4 bytes: super-journal name checksum. |
| 54571 | 54947 | ** + 8 bytes: aJournalMagic[]. |
| 54572 | 54948 | ** |
| | @@ -54610,11 +54986,11 @@ |
| 54610 | 54986 | iHdrOff = pPager->journalOff; |
| 54611 | 54987 | |
| 54612 | 54988 | /* Write the super-journal data to the end of the journal file. If |
| 54613 | 54989 | ** an error occurs, return the error code to the caller. |
| 54614 | 54990 | */ |
| 54615 | | - if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager)))) |
| 54991 | + if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager)))) |
| 54616 | 54992 | || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4))) |
| 54617 | 54993 | || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper))) |
| 54618 | 54994 | || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum))) |
| 54619 | 54995 | || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, |
| 54620 | 54996 | iHdrOff+4+nSuper+8))) |
| | @@ -55120,11 +55496,11 @@ |
| 55120 | 55496 | ** to the database file, then the IO error code is returned. If data |
| 55121 | 55497 | ** is successfully read from the (sub-)journal file but appears to be |
| 55122 | 55498 | ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in |
| 55123 | 55499 | ** two circumstances: |
| 55124 | 55500 | ** |
| 55125 | | -** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or |
| 55501 | +** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or |
| 55126 | 55502 | ** * If the record is being rolled back from the main journal file |
| 55127 | 55503 | ** and the checksum field does not match the record content. |
| 55128 | 55504 | ** |
| 55129 | 55505 | ** Neither of these two scenarios are possible during a savepoint rollback. |
| 55130 | 55506 | ** |
| | @@ -55180,11 +55556,11 @@ |
| 55180 | 55556 | /* Sanity checking on the page. This is more important that I originally |
| 55181 | 55557 | ** thought. If a power failure occurs while the journal is being written, |
| 55182 | 55558 | ** it could cause invalid data to be written into the journal. We need to |
| 55183 | 55559 | ** detect this invalid data (with high probability) and ignore it. |
| 55184 | 55560 | */ |
| 55185 | | - if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ |
| 55561 | + if( pgno==0 || pgno==PAGER_SJ_PGNO(pPager) ){ |
| 55186 | 55562 | assert( !isSavepnt ); |
| 55187 | 55563 | return SQLITE_DONE; |
| 55188 | 55564 | } |
| 55189 | 55565 | if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){ |
| 55190 | 55566 | return SQLITE_OK; |
| | @@ -55739,10 +56115,13 @@ |
| 55739 | 56115 | rc = pager_truncate(pPager, mxPg); |
| 55740 | 56116 | if( rc!=SQLITE_OK ){ |
| 55741 | 56117 | goto end_playback; |
| 55742 | 56118 | } |
| 55743 | 56119 | pPager->dbSize = mxPg; |
| 56120 | + if( pPager->mxPgno<mxPg ){ |
| 56121 | + pPager->mxPgno = mxPg; |
| 56122 | + } |
| 55744 | 56123 | } |
| 55745 | 56124 | |
| 55746 | 56125 | /* Copy original pages out of the journal and back into the |
| 55747 | 56126 | ** database file and/or page cache. |
| 55748 | 56127 | */ |
| | @@ -56635,10 +57014,11 @@ |
| 56635 | 57014 | if( rc==SQLITE_OK ){ |
| 56636 | 57015 | sqlite3PageFree(pPager->pTmpSpace); |
| 56637 | 57016 | pPager->pTmpSpace = pNew; |
| 56638 | 57017 | pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize); |
| 56639 | 57018 | pPager->pageSize = pageSize; |
| 57019 | + pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1; |
| 56640 | 57020 | }else{ |
| 56641 | 57021 | sqlite3PageFree(pNew); |
| 56642 | 57022 | } |
| 56643 | 57023 | } |
| 56644 | 57024 | |
| | @@ -56840,11 +57220,11 @@ |
| 56840 | 57220 | ** Once this function has been called, the transaction must either be |
| 56841 | 57221 | ** rolled back or committed. It is not safe to call this function and |
| 56842 | 57222 | ** then continue writing to the database. |
| 56843 | 57223 | */ |
| 56844 | 57224 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ |
| 56845 | | - assert( pPager->dbSize>=nPage ); |
| 57225 | + assert( pPager->dbSize>=nPage || CORRUPT_DB ); |
| 56846 | 57226 | assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); |
| 56847 | 57227 | pPager->dbSize = nPage; |
| 56848 | 57228 | |
| 56849 | 57229 | /* At one point the code here called assertTruncateConstraint() to |
| 56850 | 57230 | ** ensure that all pages being truncated away by this operation are, |
| | @@ -58404,11 +58784,11 @@ |
| 58404 | 58784 | |
| 58405 | 58785 | noContent = (flags & PAGER_GET_NOCONTENT)!=0; |
| 58406 | 58786 | if( pPg->pPager && !noContent ){ |
| 58407 | 58787 | /* In this case the pcache already contains an initialized copy of |
| 58408 | 58788 | ** the page. Return without further ado. */ |
| 58409 | | - assert( pgno!=PAGER_MJ_PGNO(pPager) ); |
| 58789 | + assert( pgno!=PAGER_SJ_PGNO(pPager) ); |
| 58410 | 58790 | pPager->aStat[PAGER_STAT_HIT]++; |
| 58411 | 58791 | return SQLITE_OK; |
| 58412 | 58792 | |
| 58413 | 58793 | }else{ |
| 58414 | 58794 | /* The pager cache has created a new page. Its content needs to |
| | @@ -58415,11 +58795,11 @@ |
| 58415 | 58795 | ** be initialized. But first some error checks: |
| 58416 | 58796 | ** |
| 58417 | 58797 | ** (*) obsolete. Was: maximum page number is 2^31 |
| 58418 | 58798 | ** (2) Never try to fetch the locking page |
| 58419 | 58799 | */ |
| 58420 | | - if( pgno==PAGER_MJ_PGNO(pPager) ){ |
| 58800 | + if( pgno==PAGER_SJ_PGNO(pPager) ){ |
| 58421 | 58801 | rc = SQLITE_CORRUPT_BKPT; |
| 58422 | 58802 | goto pager_acquire_err; |
| 58423 | 58803 | } |
| 58424 | 58804 | |
| 58425 | 58805 | pPg->pPager = pPager; |
| | @@ -58814,11 +59194,11 @@ |
| 58814 | 59194 | i64 iOff = pPager->journalOff; |
| 58815 | 59195 | |
| 58816 | 59196 | /* We should never write to the journal file the page that |
| 58817 | 59197 | ** contains the database locks. The following assert verifies |
| 58818 | 59198 | ** that we do not. */ |
| 58819 | | - assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
| 59199 | + assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) ); |
| 58820 | 59200 | |
| 58821 | 59201 | assert( pPager->journalHdr<=pPager->journalOff ); |
| 58822 | 59202 | pData2 = pPg->pData; |
| 58823 | 59203 | cksum = pager_cksum(pPager, (u8*)pData2); |
| 58824 | 59204 | |
| | @@ -58993,11 +59373,11 @@ |
| 58993 | 59373 | |
| 58994 | 59374 | for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ |
| 58995 | 59375 | Pgno pg = pg1+ii; |
| 58996 | 59376 | PgHdr *pPage; |
| 58997 | 59377 | if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ |
| 58998 | | - if( pg!=PAGER_MJ_PGNO(pPager) ){ |
| 59378 | + if( pg!=PAGER_SJ_PGNO(pPager) ){ |
| 58999 | 59379 | rc = sqlite3PagerGet(pPager, pg, &pPage, 0); |
| 59000 | 59380 | if( rc==SQLITE_OK ){ |
| 59001 | 59381 | rc = pager_write(pPage); |
| 59002 | 59382 | if( pPage->flags&PGHDR_NEED_SYNC ){ |
| 59003 | 59383 | needSync = 1; |
| | @@ -59471,11 +59851,11 @@ |
| 59471 | 59851 | ** image was extended as part of the current transaction and then the |
| 59472 | 59852 | ** last page in the db image moved to the free-list. In this case the |
| 59473 | 59853 | ** last page is never written out to disk, leaving the database file |
| 59474 | 59854 | ** undersized. Fix this now if it is the case. */ |
| 59475 | 59855 | if( pPager->dbSize>pPager->dbFileSize ){ |
| 59476 | | - Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager)); |
| 59856 | + Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager)); |
| 59477 | 59857 | assert( pPager->eState==PAGER_WRITER_DBMOD ); |
| 59478 | 59858 | rc = pager_truncate(pPager, nNew); |
| 59479 | 59859 | if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 59480 | 59860 | } |
| 59481 | 59861 | |
| | @@ -60320,10 +60700,22 @@ |
| 60320 | 60700 | int eMode, /* Type of checkpoint */ |
| 60321 | 60701 | int *pnLog, /* OUT: Final number of frames in log */ |
| 60322 | 60702 | int *pnCkpt /* OUT: Final number of checkpointed frames */ |
| 60323 | 60703 | ){ |
| 60324 | 60704 | int rc = SQLITE_OK; |
| 60705 | + if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){ |
| 60706 | + /* This only happens when a database file is zero bytes in size opened and |
| 60707 | + ** then "PRAGMA journal_mode=WAL" is run and then sqlite3_wal_checkpoint() |
| 60708 | + ** is invoked without any intervening transactions. We need to start |
| 60709 | + ** a transaction to initialize pWal. The PRAGMA table_list statement is |
| 60710 | + ** used for this since it starts transactions on every database file, |
| 60711 | + ** including all ATTACHed databases. This seems expensive for a single |
| 60712 | + ** sqlite3_wal_checkpoint() call, but it happens very rarely. |
| 60713 | + ** https://sqlite.org/forum/forumpost/fd0f19d229156939 |
| 60714 | + */ |
| 60715 | + sqlite3_exec(db, "PRAGMA table_list",0,0,0); |
| 60716 | + } |
| 60325 | 60717 | if( pPager->pWal ){ |
| 60326 | 60718 | rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode, |
| 60327 | 60719 | (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), |
| 60328 | 60720 | pPager->pBusyHandlerArg, |
| 60329 | 60721 | pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, |
| | @@ -65086,11 +65478,13 @@ |
| 65086 | 65478 | u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th |
| 65087 | 65479 | ** non-overflow cell */ |
| 65088 | 65480 | u8 *apOvfl[4]; /* Pointers to the body of overflow cells */ |
| 65089 | 65481 | BtShared *pBt; /* Pointer to BtShared that this page is part of */ |
| 65090 | 65482 | u8 *aData; /* Pointer to disk image of the page data */ |
| 65091 | | - u8 *aDataEnd; /* One byte past the end of usable data */ |
| 65483 | + u8 *aDataEnd; /* One byte past the end of the entire page - not just |
| 65484 | + ** the usable space, the entire page. Used to prevent |
| 65485 | + ** corruption-induced buffer overflow. */ |
| 65092 | 65486 | u8 *aCellIdx; /* The cell index area */ |
| 65093 | 65487 | u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */ |
| 65094 | 65488 | DbPage *pDbPage; /* Pager page handle */ |
| 65095 | 65489 | u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */ |
| 65096 | 65490 | void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */ |
| | @@ -65391,11 +65785,11 @@ |
| 65391 | 65785 | #define CURSOR_FAULT 4 |
| 65392 | 65786 | |
| 65393 | 65787 | /* |
| 65394 | 65788 | ** The database page the PENDING_BYTE occupies. This page is never used. |
| 65395 | 65789 | */ |
| 65396 | | -# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt) |
| 65790 | +#define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1)) |
| 65397 | 65791 | |
| 65398 | 65792 | /* |
| 65399 | 65793 | ** These macros define the location of the pointer-map entry for a |
| 65400 | 65794 | ** database page. The first argument to each is the number of usable |
| 65401 | 65795 | ** bytes on each page of the database (often 1024). The second is the |
| | @@ -66032,11 +66426,11 @@ |
| 66032 | 66426 | if( isIndex ){ |
| 66033 | 66427 | HashElem *p; |
| 66034 | 66428 | int bSeen = 0; |
| 66035 | 66429 | for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){ |
| 66036 | 66430 | Index *pIdx = (Index *)sqliteHashData(p); |
| 66037 | | - if( pIdx->tnum==(int)iRoot ){ |
| 66431 | + if( pIdx->tnum==iRoot ){ |
| 66038 | 66432 | if( bSeen ){ |
| 66039 | 66433 | /* Two or more indexes share the same root page. There must |
| 66040 | 66434 | ** be imposter tables. So just return true. The assert is not |
| 66041 | 66435 | ** useful in that case. */ |
| 66042 | 66436 | return 1; |
| | @@ -66625,11 +67019,11 @@ |
| 66625 | 67019 | } |
| 66626 | 67020 | |
| 66627 | 67021 | /* |
| 66628 | 67022 | ** In this version of BtreeMoveto, pKey is a packed index record |
| 66629 | 67023 | ** such as is generated by the OP_MakeRecord opcode. Unpack the |
| 66630 | | -** record and then call BtreeMovetoUnpacked() to do the work. |
| 67024 | +** record and then call sqlite3BtreeIndexMoveto() to do the work. |
| 66631 | 67025 | */ |
| 66632 | 67026 | static int btreeMoveto( |
| 66633 | 67027 | BtCursor *pCur, /* Cursor open on the btree to be searched */ |
| 66634 | 67028 | const void *pKey, /* Packed key if the btree is an index */ |
| 66635 | 67029 | i64 nKey, /* Integer key for tables. Size of pKey for indices */ |
| | @@ -67145,10 +67539,11 @@ |
| 67145 | 67539 | ** data area of the btree-page. The return number includes the cell |
| 67146 | 67540 | ** data header and the local payload, but not any overflow page or |
| 67147 | 67541 | ** the space used by the cell pointer. |
| 67148 | 67542 | ** |
| 67149 | 67543 | ** cellSizePtrNoPayload() => table internal nodes |
| 67544 | +** cellSizePtrTableLeaf() => table leaf nodes |
| 67150 | 67545 | ** cellSizePtr() => all index nodes & table leaf nodes |
| 67151 | 67546 | */ |
| 67152 | 67547 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 67153 | 67548 | u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ |
| 67154 | 67549 | u8 *pEnd; /* End mark for a varint */ |
| | @@ -67170,17 +67565,10 @@ |
| 67170 | 67565 | do{ |
| 67171 | 67566 | nSize = (nSize<<7) | (*++pIter & 0x7f); |
| 67172 | 67567 | }while( *(pIter)>=0x80 && pIter<pEnd ); |
| 67173 | 67568 | } |
| 67174 | 67569 | pIter++; |
| 67175 | | - if( pPage->intKey ){ |
| 67176 | | - /* pIter now points at the 64-bit integer key value, a variable length |
| 67177 | | - ** integer. The following block moves pIter to point at the first byte |
| 67178 | | - ** past the end of the key value. */ |
| 67179 | | - pEnd = &pIter[9]; |
| 67180 | | - while( (*pIter++)&0x80 && pIter<pEnd ); |
| 67181 | | - } |
| 67182 | 67570 | testcase( nSize==pPage->maxLocal ); |
| 67183 | 67571 | testcase( nSize==(u32)pPage->maxLocal+1 ); |
| 67184 | 67572 | if( nSize<=pPage->maxLocal ){ |
| 67185 | 67573 | nSize += (u32)(pIter - pCell); |
| 67186 | 67574 | if( nSize<4 ) nSize = 4; |
| | @@ -67216,10 +67604,62 @@ |
| 67216 | 67604 | pEnd = pIter + 9; |
| 67217 | 67605 | while( (*pIter++)&0x80 && pIter<pEnd ); |
| 67218 | 67606 | assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB ); |
| 67219 | 67607 | return (u16)(pIter - pCell); |
| 67220 | 67608 | } |
| 67609 | +static u16 cellSizePtrTableLeaf(MemPage *pPage, u8 *pCell){ |
| 67610 | + u8 *pIter = pCell; /* For looping over bytes of pCell */ |
| 67611 | + u8 *pEnd; /* End mark for a varint */ |
| 67612 | + u32 nSize; /* Size value to return */ |
| 67613 | + |
| 67614 | +#ifdef SQLITE_DEBUG |
| 67615 | + /* The value returned by this function should always be the same as |
| 67616 | + ** the (CellInfo.nSize) value found by doing a full parse of the |
| 67617 | + ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
| 67618 | + ** this function verifies that this invariant is not violated. */ |
| 67619 | + CellInfo debuginfo; |
| 67620 | + pPage->xParseCell(pPage, pCell, &debuginfo); |
| 67621 | +#endif |
| 67622 | + |
| 67623 | + nSize = *pIter; |
| 67624 | + if( nSize>=0x80 ){ |
| 67625 | + pEnd = &pIter[8]; |
| 67626 | + nSize &= 0x7f; |
| 67627 | + do{ |
| 67628 | + nSize = (nSize<<7) | (*++pIter & 0x7f); |
| 67629 | + }while( *(pIter)>=0x80 && pIter<pEnd ); |
| 67630 | + } |
| 67631 | + pIter++; |
| 67632 | + /* pIter now points at the 64-bit integer key value, a variable length |
| 67633 | + ** integer. The following block moves pIter to point at the first byte |
| 67634 | + ** past the end of the key value. */ |
| 67635 | + if( (*pIter++)&0x80 |
| 67636 | + && (*pIter++)&0x80 |
| 67637 | + && (*pIter++)&0x80 |
| 67638 | + && (*pIter++)&0x80 |
| 67639 | + && (*pIter++)&0x80 |
| 67640 | + && (*pIter++)&0x80 |
| 67641 | + && (*pIter++)&0x80 |
| 67642 | + && (*pIter++)&0x80 ){ pIter++; } |
| 67643 | + testcase( nSize==pPage->maxLocal ); |
| 67644 | + testcase( nSize==(u32)pPage->maxLocal+1 ); |
| 67645 | + if( nSize<=pPage->maxLocal ){ |
| 67646 | + nSize += (u32)(pIter - pCell); |
| 67647 | + if( nSize<4 ) nSize = 4; |
| 67648 | + }else{ |
| 67649 | + int minLocal = pPage->minLocal; |
| 67650 | + nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); |
| 67651 | + testcase( nSize==pPage->maxLocal ); |
| 67652 | + testcase( nSize==(u32)pPage->maxLocal+1 ); |
| 67653 | + if( nSize>pPage->maxLocal ){ |
| 67654 | + nSize = minLocal; |
| 67655 | + } |
| 67656 | + nSize += 4 + (u16)(pIter - pCell); |
| 67657 | + } |
| 67658 | + assert( nSize==debuginfo.nSize || CORRUPT_DB ); |
| 67659 | + return (u16)nSize; |
| 67660 | +} |
| 67221 | 67661 | |
| 67222 | 67662 | |
| 67223 | 67663 | #ifdef SQLITE_DEBUG |
| 67224 | 67664 | /* This variation on cellSizePtr() is used inside of assert() statements |
| 67225 | 67665 | ** only. */ |
| | @@ -67229,11 +67669,11 @@ |
| 67229 | 67669 | #endif |
| 67230 | 67670 | |
| 67231 | 67671 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 67232 | 67672 | /* |
| 67233 | 67673 | ** The cell pCell is currently part of page pSrc but will ultimately be part |
| 67234 | | -** of pPage. (pSrc and pPager are often the same.) If pCell contains a |
| 67674 | +** of pPage. (pSrc and pPage are often the same.) If pCell contains a |
| 67235 | 67675 | ** pointer to an overflow page, insert an entry into the pointer-map for |
| 67236 | 67676 | ** the overflow page that will be valid after pCell has been moved to pPage. |
| 67237 | 67677 | */ |
| 67238 | 67678 | static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){ |
| 67239 | 67679 | CellInfo info; |
| | @@ -67404,21 +67844,23 @@ |
| 67404 | 67844 | */ |
| 67405 | 67845 | static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){ |
| 67406 | 67846 | const int hdr = pPg->hdrOffset; /* Offset to page header */ |
| 67407 | 67847 | u8 * const aData = pPg->aData; /* Page data */ |
| 67408 | 67848 | int iAddr = hdr + 1; /* Address of ptr to pc */ |
| 67409 | | - int pc = get2byte(&aData[iAddr]); /* Address of a free slot */ |
| 67849 | + u8 *pTmp = &aData[iAddr]; /* Temporary ptr into aData[] */ |
| 67850 | + int pc = get2byte(pTmp); /* Address of a free slot */ |
| 67410 | 67851 | int x; /* Excess size of the slot */ |
| 67411 | 67852 | int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */ |
| 67412 | 67853 | int size; /* Size of the free slot */ |
| 67413 | 67854 | |
| 67414 | 67855 | assert( pc>0 ); |
| 67415 | 67856 | while( pc<=maxPC ){ |
| 67416 | 67857 | /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each |
| 67417 | 67858 | ** freeblock form a big-endian integer which is the size of the freeblock |
| 67418 | 67859 | ** in bytes, including the 4-byte header. */ |
| 67419 | | - size = get2byte(&aData[pc+2]); |
| 67860 | + pTmp = &aData[pc+2]; |
| 67861 | + size = get2byte(pTmp); |
| 67420 | 67862 | if( (x = size - nByte)>=0 ){ |
| 67421 | 67863 | testcase( x==4 ); |
| 67422 | 67864 | testcase( x==3 ); |
| 67423 | 67865 | if( x<4 ){ |
| 67424 | 67866 | /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total |
| | @@ -67439,11 +67881,12 @@ |
| 67439 | 67881 | put2byte(&aData[pc+2], x); |
| 67440 | 67882 | } |
| 67441 | 67883 | return &aData[pc + x]; |
| 67442 | 67884 | } |
| 67443 | 67885 | iAddr = pc; |
| 67444 | | - pc = get2byte(&aData[pc]); |
| 67886 | + pTmp = &aData[pc]; |
| 67887 | + pc = get2byte(pTmp); |
| 67445 | 67888 | if( pc<=iAddr+size ){ |
| 67446 | 67889 | if( pc ){ |
| 67447 | 67890 | /* The next slot in the chain is not past the end of the current slot */ |
| 67448 | 67891 | *pRc = SQLITE_CORRUPT_PAGE(pPg); |
| 67449 | 67892 | } |
| | @@ -67473,10 +67916,11 @@ |
| 67473 | 67916 | static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
| 67474 | 67917 | const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
| 67475 | 67918 | u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
| 67476 | 67919 | int top; /* First byte of cell content area */ |
| 67477 | 67920 | int rc = SQLITE_OK; /* Integer return code */ |
| 67921 | + u8 *pTmp; /* Temp ptr into data[] */ |
| 67478 | 67922 | int gap; /* First byte of gap between cell pointers and cell content */ |
| 67479 | 67923 | |
| 67480 | 67924 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 67481 | 67925 | assert( pPage->pBt ); |
| 67482 | 67926 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| | @@ -67491,11 +67935,12 @@ |
| 67491 | 67935 | /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size |
| 67492 | 67936 | ** and the reserved space is zero (the usual value for reserved space) |
| 67493 | 67937 | ** then the cell content offset of an empty page wants to be 65536. |
| 67494 | 67938 | ** However, that integer is too large to be stored in a 2-byte unsigned |
| 67495 | 67939 | ** integer, so a value of 0 is used in its place. */ |
| 67496 | | - top = get2byte(&data[hdr+5]); |
| 67940 | + pTmp = &data[hdr+5]; |
| 67941 | + top = get2byte(pTmp); |
| 67497 | 67942 | assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */ |
| 67498 | 67943 | if( gap>top ){ |
| 67499 | 67944 | if( top==0 && pPage->pBt->usableSize==65536 ){ |
| 67500 | 67945 | top = 65536; |
| 67501 | 67946 | }else{ |
| | @@ -67573,10 +68018,11 @@ |
| 67573 | 68018 | u8 nFrag = 0; /* Reduction in fragmentation */ |
| 67574 | 68019 | u16 iOrigSize = iSize; /* Original value of iSize */ |
| 67575 | 68020 | u16 x; /* Offset to cell content area */ |
| 67576 | 68021 | u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */ |
| 67577 | 68022 | unsigned char *data = pPage->aData; /* Page content */ |
| 68023 | + u8 *pTmp; /* Temporary ptr into data[] */ |
| 67578 | 68024 | |
| 67579 | 68025 | assert( pPage->pBt!=0 ); |
| 67580 | 68026 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 67581 | 68027 | assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
| 67582 | 68028 | assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); |
| | @@ -67600,11 +68046,11 @@ |
| 67600 | 68046 | iPtr = iFreeBlk; |
| 67601 | 68047 | } |
| 67602 | 68048 | if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */ |
| 67603 | 68049 | return SQLITE_CORRUPT_PAGE(pPage); |
| 67604 | 68050 | } |
| 67605 | | - assert( iFreeBlk>iPtr || iFreeBlk==0 ); |
| 68051 | + assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB ); |
| 67606 | 68052 | |
| 67607 | 68053 | /* At this point: |
| 67608 | 68054 | ** iFreeBlk: First freeblock after iStart, or zero if none |
| 67609 | 68055 | ** iPtr: The address of a pointer to iFreeBlk |
| 67610 | 68056 | ** |
| | @@ -67635,11 +68081,12 @@ |
| 67635 | 68081 | } |
| 67636 | 68082 | } |
| 67637 | 68083 | if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage); |
| 67638 | 68084 | data[hdr+7] -= nFrag; |
| 67639 | 68085 | } |
| 67640 | | - x = get2byte(&data[hdr+5]); |
| 68086 | + pTmp = &data[hdr+5]; |
| 68087 | + x = get2byte(pTmp); |
| 67641 | 68088 | if( iStart<=x ){ |
| 67642 | 68089 | /* The new freeblock is at the beginning of the cell content area, |
| 67643 | 68090 | ** so just extend the cell content area rather than create another |
| 67644 | 68091 | ** freelist entry */ |
| 67645 | 68092 | if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage); |
| | @@ -67679,11 +68126,10 @@ |
| 67679 | 68126 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 67680 | 68127 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 67681 | 68128 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
| 67682 | 68129 | flagByte &= ~PTF_LEAF; |
| 67683 | 68130 | pPage->childPtrSize = 4-4*pPage->leaf; |
| 67684 | | - pPage->xCellSize = cellSizePtr; |
| 67685 | 68131 | pBt = pPage->pBt; |
| 67686 | 68132 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 67687 | 68133 | /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an |
| 67688 | 68134 | ** interior table b-tree page. */ |
| 67689 | 68135 | assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); |
| | @@ -67691,10 +68137,11 @@ |
| 67691 | 68137 | ** leaf table b-tree page. */ |
| 67692 | 68138 | assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); |
| 67693 | 68139 | pPage->intKey = 1; |
| 67694 | 68140 | if( pPage->leaf ){ |
| 67695 | 68141 | pPage->intKeyLeaf = 1; |
| 68142 | + pPage->xCellSize = cellSizePtrTableLeaf; |
| 67696 | 68143 | pPage->xParseCell = btreeParseCellPtr; |
| 67697 | 68144 | }else{ |
| 67698 | 68145 | pPage->intKeyLeaf = 0; |
| 67699 | 68146 | pPage->xCellSize = cellSizePtrNoPayload; |
| 67700 | 68147 | pPage->xParseCell = btreeParseCellPtrNoPayload; |
| | @@ -67708,16 +68155,21 @@ |
| 67708 | 68155 | /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a |
| 67709 | 68156 | ** leaf index b-tree page. */ |
| 67710 | 68157 | assert( (PTF_ZERODATA|PTF_LEAF)==10 ); |
| 67711 | 68158 | pPage->intKey = 0; |
| 67712 | 68159 | pPage->intKeyLeaf = 0; |
| 68160 | + pPage->xCellSize = cellSizePtr; |
| 67713 | 68161 | pPage->xParseCell = btreeParseCellPtrIndex; |
| 67714 | 68162 | pPage->maxLocal = pBt->maxLocal; |
| 67715 | 68163 | pPage->minLocal = pBt->minLocal; |
| 67716 | 68164 | }else{ |
| 67717 | 68165 | /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is |
| 67718 | 68166 | ** an error. */ |
| 68167 | + pPage->intKey = 0; |
| 68168 | + pPage->intKeyLeaf = 0; |
| 68169 | + pPage->xCellSize = cellSizePtr; |
| 68170 | + pPage->xParseCell = btreeParseCellPtrIndex; |
| 67719 | 68171 | return SQLITE_CORRUPT_PAGE(pPage); |
| 67720 | 68172 | } |
| 67721 | 68173 | pPage->max1bytePayload = pBt->max1bytePayload; |
| 67722 | 68174 | return SQLITE_OK; |
| 67723 | 68175 | } |
| | @@ -67871,11 +68323,11 @@ |
| 67871 | 68323 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 67872 | 68324 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
| 67873 | 68325 | pPage->nOverflow = 0; |
| 67874 | 68326 | pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize; |
| 67875 | 68327 | pPage->aCellIdx = data + pPage->childPtrSize + 8; |
| 67876 | | - pPage->aDataEnd = pPage->aData + pBt->usableSize; |
| 68328 | + pPage->aDataEnd = pPage->aData + pBt->pageSize; |
| 67877 | 68329 | pPage->aDataOfst = pPage->aData + pPage->childPtrSize; |
| 67878 | 68330 | /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 67879 | 68331 | ** number of cells on the page. */ |
| 67880 | 68332 | pPage->nCell = get2byte(&data[3]); |
| 67881 | 68333 | if( pPage->nCell>MX_CELL(pBt) ){ |
| | @@ -67906,11 +68358,11 @@ |
| 67906 | 68358 | unsigned char *data = pPage->aData; |
| 67907 | 68359 | BtShared *pBt = pPage->pBt; |
| 67908 | 68360 | u8 hdr = pPage->hdrOffset; |
| 67909 | 68361 | u16 first; |
| 67910 | 68362 | |
| 67911 | | - assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
| 68363 | + assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB ); |
| 67912 | 68364 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 67913 | 68365 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
| 67914 | 68366 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 67915 | 68367 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 67916 | 68368 | if( pBt->btsFlags & BTS_FAST_SECURE ){ |
| | @@ -67922,11 +68374,11 @@ |
| 67922 | 68374 | data[hdr+7] = 0; |
| 67923 | 68375 | put2byte(&data[hdr+5], pBt->usableSize); |
| 67924 | 68376 | pPage->nFree = (u16)(pBt->usableSize - first); |
| 67925 | 68377 | decodeFlags(pPage, flags); |
| 67926 | 68378 | pPage->cellOffset = first; |
| 67927 | | - pPage->aDataEnd = &data[pBt->usableSize]; |
| 68379 | + pPage->aDataEnd = &data[pBt->pageSize]; |
| 67928 | 68380 | pPage->aCellIdx = &data[first]; |
| 67929 | 68381 | pPage->aDataOfst = &data[pPage->childPtrSize]; |
| 67930 | 68382 | pPage->nOverflow = 0; |
| 67931 | 68383 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 67932 | 68384 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
| | @@ -68048,11 +68500,11 @@ |
| 68048 | 68500 | rc = btreeInitPage(*ppPage); |
| 68049 | 68501 | if( rc!=SQLITE_OK ){ |
| 68050 | 68502 | goto getAndInitPage_error2; |
| 68051 | 68503 | } |
| 68052 | 68504 | } |
| 68053 | | - assert( (*ppPage)->pgno==pgno ); |
| 68505 | + assert( (*ppPage)->pgno==pgno || CORRUPT_DB ); |
| 68054 | 68506 | assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) ); |
| 68055 | 68507 | |
| 68056 | 68508 | /* If obtaining a child page for a cursor, we must verify that the page is |
| 68057 | 68509 | ** compatible with the root page. */ |
| 68058 | 68510 | if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){ |
| | @@ -68067,11 +68519,13 @@ |
| 68067 | 68519 | if( pCur ){ |
| 68068 | 68520 | pCur->iPage--; |
| 68069 | 68521 | pCur->pPage = pCur->apPage[pCur->iPage]; |
| 68070 | 68522 | } |
| 68071 | 68523 | testcase( pgno==0 ); |
| 68072 | | - assert( pgno!=0 || rc==SQLITE_CORRUPT ); |
| 68524 | + assert( pgno!=0 || rc==SQLITE_CORRUPT |
| 68525 | + || rc==SQLITE_IOERR_NOMEM |
| 68526 | + || rc==SQLITE_NOMEM ); |
| 68073 | 68527 | return rc; |
| 68074 | 68528 | } |
| 68075 | 68529 | |
| 68076 | 68530 | /* |
| 68077 | 68531 | ** Release a MemPage. This should be called once for each prior |
| | @@ -71135,11 +71589,11 @@ |
| 71135 | 71589 | } |
| 71136 | 71590 | pCur->iPage = 0; |
| 71137 | 71591 | pCur->curIntKey = pCur->pPage->intKey; |
| 71138 | 71592 | } |
| 71139 | 71593 | pRoot = pCur->pPage; |
| 71140 | | - assert( pRoot->pgno==pCur->pgnoRoot ); |
| 71594 | + assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB ); |
| 71141 | 71595 | |
| 71142 | 71596 | /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor |
| 71143 | 71597 | ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is |
| 71144 | 71598 | ** NULL, the caller expects a table b-tree. If this is not the case, |
| 71145 | 71599 | ** return an SQLITE_CORRUPT error. |
| | @@ -71455,10 +71909,73 @@ |
| 71455 | 71909 | moveto_table_finish: |
| 71456 | 71910 | pCur->info.nSize = 0; |
| 71457 | 71911 | assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); |
| 71458 | 71912 | return rc; |
| 71459 | 71913 | } |
| 71914 | + |
| 71915 | +/* |
| 71916 | +** Compare the "idx"-th cell on the page the cursor pCur is currently |
| 71917 | +** pointing to to pIdxKey using xRecordCompare. Return negative or |
| 71918 | +** zero if the cell is less than or equal pIdxKey. Return positive |
| 71919 | +** if unknown. |
| 71920 | +** |
| 71921 | +** Return value negative: Cell at pCur[idx] less than pIdxKey |
| 71922 | +** |
| 71923 | +** Return value is zero: Cell at pCur[idx] equals pIdxKey |
| 71924 | +** |
| 71925 | +** Return value positive: Nothing is known about the relationship |
| 71926 | +** of the cell at pCur[idx] and pIdxKey. |
| 71927 | +** |
| 71928 | +** This routine is part of an optimization. It is always safe to return |
| 71929 | +** a positive value as that will cause the optimization to be skipped. |
| 71930 | +*/ |
| 71931 | +static int indexCellCompare( |
| 71932 | + BtCursor *pCur, |
| 71933 | + int idx, |
| 71934 | + UnpackedRecord *pIdxKey, |
| 71935 | + RecordCompare xRecordCompare |
| 71936 | +){ |
| 71937 | + MemPage *pPage = pCur->pPage; |
| 71938 | + int c; |
| 71939 | + int nCell; /* Size of the pCell cell in bytes */ |
| 71940 | + u8 *pCell = findCellPastPtr(pPage, idx); |
| 71941 | + |
| 71942 | + nCell = pCell[0]; |
| 71943 | + if( nCell<=pPage->max1bytePayload ){ |
| 71944 | + /* This branch runs if the record-size field of the cell is a |
| 71945 | + ** single byte varint and the record fits entirely on the main |
| 71946 | + ** b-tree page. */ |
| 71947 | + testcase( pCell+nCell+1==pPage->aDataEnd ); |
| 71948 | + c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey); |
| 71949 | + }else if( !(pCell[1] & 0x80) |
| 71950 | + && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal |
| 71951 | + ){ |
| 71952 | + /* The record-size field is a 2 byte varint and the record |
| 71953 | + ** fits entirely on the main b-tree page. */ |
| 71954 | + testcase( pCell+nCell+2==pPage->aDataEnd ); |
| 71955 | + c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey); |
| 71956 | + }else{ |
| 71957 | + /* If the record extends into overflow pages, do not attempt |
| 71958 | + ** the optimization. */ |
| 71959 | + c = 99; |
| 71960 | + } |
| 71961 | + return c; |
| 71962 | +} |
| 71963 | + |
| 71964 | +/* |
| 71965 | +** Return true (non-zero) if pCur is current pointing to the last |
| 71966 | +** page of a table. |
| 71967 | +*/ |
| 71968 | +static int cursorOnLastPage(BtCursor *pCur){ |
| 71969 | + int i; |
| 71970 | + assert( pCur->eState==CURSOR_VALID ); |
| 71971 | + for(i=0; i<pCur->iPage; i++){ |
| 71972 | + MemPage *pPage = pCur->apPage[i]; |
| 71973 | + if( pCur->aiIdx[i]<pPage->nCell ) return 0; |
| 71974 | + } |
| 71975 | + return 1; |
| 71976 | +} |
| 71460 | 71977 | |
| 71461 | 71978 | /* Move the cursor so that it points to an entry in an index table |
| 71462 | 71979 | ** near the key pIdxKey. Return a success code. |
| 71463 | 71980 | ** |
| 71464 | 71981 | ** If an exact match is not found, then the cursor is always |
| | @@ -71506,25 +72023,61 @@ |
| 71506 | 72023 | assert( pIdxKey->default_rc==1 |
| 71507 | 72024 | || pIdxKey->default_rc==0 |
| 71508 | 72025 | || pIdxKey->default_rc==-1 |
| 71509 | 72026 | ); |
| 71510 | 72027 | |
| 72028 | + |
| 72029 | + /* Check to see if we can skip a lot of work. Two cases: |
| 72030 | + ** |
| 72031 | + ** (1) If the cursor is already pointing to the very last cell |
| 72032 | + ** in the table and the pIdxKey search key is greater than or |
| 72033 | + ** equal to that last cell, then no movement is required. |
| 72034 | + ** |
| 72035 | + ** (2) If the cursor is on the last page of the table and the first |
| 72036 | + ** cell on that last page is less than or equal to the pIdxKey |
| 72037 | + ** search key, then we can start the search on the current page |
| 72038 | + ** without needing to go back to root. |
| 72039 | + */ |
| 72040 | + if( pCur->eState==CURSOR_VALID |
| 72041 | + && pCur->pPage->leaf |
| 72042 | + && cursorOnLastPage(pCur) |
| 72043 | + ){ |
| 72044 | + int c; |
| 72045 | + if( pCur->ix==pCur->pPage->nCell-1 |
| 72046 | + && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0 |
| 72047 | + && pIdxKey->errCode==SQLITE_OK |
| 72048 | + ){ |
| 72049 | + *pRes = c; |
| 72050 | + return SQLITE_OK; /* Cursor already pointing at the correct spot */ |
| 72051 | + } |
| 72052 | + if( pCur->iPage>0 |
| 72053 | + && indexCellCompare(pCur, 0, pIdxKey, xRecordCompare)<=0 |
| 72054 | + && pIdxKey->errCode==SQLITE_OK |
| 72055 | + ){ |
| 72056 | + pCur->curFlags &= ~BTCF_ValidOvfl; |
| 72057 | + goto bypass_moveto_root; /* Start search on the current page */ |
| 72058 | + } |
| 72059 | + pIdxKey->errCode = SQLITE_OK; |
| 72060 | + } |
| 72061 | + |
| 71511 | 72062 | rc = moveToRoot(pCur); |
| 71512 | 72063 | if( rc ){ |
| 71513 | 72064 | if( rc==SQLITE_EMPTY ){ |
| 71514 | 72065 | assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); |
| 71515 | 72066 | *pRes = -1; |
| 71516 | 72067 | return SQLITE_OK; |
| 71517 | 72068 | } |
| 71518 | 72069 | return rc; |
| 71519 | 72070 | } |
| 72071 | + |
| 72072 | +bypass_moveto_root: |
| 71520 | 72073 | assert( pCur->pPage ); |
| 71521 | 72074 | assert( pCur->pPage->isInit ); |
| 71522 | 72075 | assert( pCur->eState==CURSOR_VALID ); |
| 71523 | 72076 | assert( pCur->pPage->nCell > 0 ); |
| 71524 | | - assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey ); |
| 71525 | | - assert( pCur->curIntKey || pIdxKey ); |
| 72077 | + assert( pCur->curIntKey==0 ); |
| 72078 | + assert( pIdxKey!=0 ); |
| 71526 | 72079 | for(;;){ |
| 71527 | 72080 | int lwr, upr, idx, c; |
| 71528 | 72081 | Pgno chldPg; |
| 71529 | 72082 | MemPage *pPage = pCur->pPage; |
| 71530 | 72083 | u8 *pCell; /* Pointer to current cell in pPage */ |
| | @@ -71534,11 +72087,11 @@ |
| 71534 | 72087 | ** not run. If this is not the root-page, then the moveToChild() routine |
| 71535 | 72088 | ** would have already detected db corruption. Similarly, pPage must |
| 71536 | 72089 | ** be the right kind (index or table) of b-tree page. Otherwise |
| 71537 | 72090 | ** a moveToChild() or moveToRoot() call would have detected corruption. */ |
| 71538 | 72091 | assert( pPage->nCell>0 ); |
| 71539 | | - assert( pPage->intKey==(pIdxKey==0) ); |
| 72092 | + assert( pPage->intKey==0 ); |
| 71540 | 72093 | lwr = 0; |
| 71541 | 72094 | upr = pPage->nCell-1; |
| 71542 | 72095 | idx = upr>>1; /* idx = (lwr+upr)/2; */ |
| 71543 | 72096 | for(;;){ |
| 71544 | 72097 | int nCell; /* Size of the pCell cell in bytes */ |
| | @@ -72229,11 +72782,11 @@ |
| 72229 | 72782 | |
| 72230 | 72783 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 72231 | 72784 | assert( CORRUPT_DB || iPage>1 ); |
| 72232 | 72785 | assert( !pMemPage || pMemPage->pgno==iPage ); |
| 72233 | 72786 | |
| 72234 | | - if( NEVER(iPage<2) || iPage>pBt->nPage ){ |
| 72787 | + if( iPage<2 || iPage>pBt->nPage ){ |
| 72235 | 72788 | return SQLITE_CORRUPT_BKPT; |
| 72236 | 72789 | } |
| 72237 | 72790 | if( pMemPage ){ |
| 72238 | 72791 | pPage = pMemPage; |
| 72239 | 72792 | sqlite3PagerRef(pPage->pDbPage); |
| | @@ -72664,10 +73217,16 @@ |
| 72664 | 73217 | data = pPage->aData; |
| 72665 | 73218 | ptr = &pPage->aCellIdx[2*idx]; |
| 72666 | 73219 | assert( pPage->pBt->usableSize > (u32)(ptr-data) ); |
| 72667 | 73220 | pc = get2byte(ptr); |
| 72668 | 73221 | hdr = pPage->hdrOffset; |
| 73222 | +#if 0 /* Not required. Omit for efficiency */ |
| 73223 | + if( pc<hdr+pPage->nCell*2 ){ |
| 73224 | + *pRC = SQLITE_CORRUPT_BKPT; |
| 73225 | + return; |
| 73226 | + } |
| 73227 | +#endif |
| 72669 | 73228 | testcase( pc==(u32)get2byte(&data[hdr+5]) ); |
| 72670 | 73229 | testcase( pc+sz==pPage->pBt->usableSize ); |
| 72671 | 73230 | if( pc+sz > pPage->pBt->usableSize ){ |
| 72672 | 73231 | *pRC = SQLITE_CORRUPT_BKPT; |
| 72673 | 73232 | return; |
| | @@ -74395,11 +74954,10 @@ |
| 74395 | 74954 | ** balance_deeper() |
| 74396 | 74955 | ** balance_nonroot() |
| 74397 | 74956 | */ |
| 74398 | 74957 | static int balance(BtCursor *pCur){ |
| 74399 | 74958 | int rc = SQLITE_OK; |
| 74400 | | - const int nMin = pCur->pBt->usableSize * 2 / 3; |
| 74401 | 74959 | u8 aBalanceQuickSpace[13]; |
| 74402 | 74960 | u8 *pFree = 0; |
| 74403 | 74961 | |
| 74404 | 74962 | VVA_ONLY( int balance_quick_called = 0 ); |
| 74405 | 74963 | VVA_ONLY( int balance_deeper_called = 0 ); |
| | @@ -74407,11 +74965,15 @@ |
| 74407 | 74965 | do { |
| 74408 | 74966 | int iPage; |
| 74409 | 74967 | MemPage *pPage = pCur->pPage; |
| 74410 | 74968 | |
| 74411 | 74969 | if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break; |
| 74412 | | - if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ |
| 74970 | + if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){ |
| 74971 | + /* No rebalance required as long as: |
| 74972 | + ** (1) There are no overflow cells |
| 74973 | + ** (2) The amount of free space on the page is less than 2/3rds of |
| 74974 | + ** the total usable space on the page. */ |
| 74413 | 74975 | break; |
| 74414 | 74976 | }else if( (iPage = pCur->iPage)==0 ){ |
| 74415 | 74977 | if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){ |
| 74416 | 74978 | /* The root page of the b-tree is overfull. In this case call the |
| 74417 | 74979 | ** balance_deeper() function to create a new child for the root-page |
| | @@ -74627,11 +75189,11 @@ |
| 74627 | 75189 | ** For an index btree (used for indexes and WITHOUT ROWID tables), the |
| 74628 | 75190 | ** key is an arbitrary byte sequence stored in pX.pKey,nKey. The |
| 74629 | 75191 | ** pX.pData,nData,nZero fields must be zero. |
| 74630 | 75192 | ** |
| 74631 | 75193 | ** If the seekResult parameter is non-zero, then a successful call to |
| 74632 | | -** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already |
| 75194 | +** sqlite3BtreeIndexMoveto() to seek cursor pCur to (pKey,nKey) has already |
| 74633 | 75195 | ** been performed. In other words, if seekResult!=0 then the cursor |
| 74634 | 75196 | ** is currently pointing to a cell that will be adjacent to the cell |
| 74635 | 75197 | ** to be inserted. If seekResult<0 then pCur points to a cell that is |
| 74636 | 75198 | ** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell |
| 74637 | 75199 | ** that is larger than (pKey,nKey). |
| | @@ -74645,11 +75207,11 @@ |
| 74645 | 75207 | */ |
| 74646 | 75208 | SQLITE_PRIVATE int sqlite3BtreeInsert( |
| 74647 | 75209 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
| 74648 | 75210 | const BtreePayload *pX, /* Content of the row to be inserted */ |
| 74649 | 75211 | int flags, /* True if this is likely an append */ |
| 74650 | | - int seekResult /* Result of prior MovetoUnpacked() call */ |
| 75212 | + int seekResult /* Result of prior IndexMoveto() call */ |
| 74651 | 75213 | ){ |
| 74652 | 75214 | int rc; |
| 74653 | 75215 | int loc = seekResult; /* -1: before desired location +1: after */ |
| 74654 | 75216 | int szNew = 0; |
| 74655 | 75217 | int idx; |
| | @@ -74660,28 +75222,10 @@ |
| 74660 | 75222 | unsigned char *newCell = 0; |
| 74661 | 75223 | |
| 74662 | 75224 | assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags ); |
| 74663 | 75225 | assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 ); |
| 74664 | 75226 | |
| 74665 | | - if( pCur->eState==CURSOR_FAULT ){ |
| 74666 | | - assert( pCur->skipNext!=SQLITE_OK ); |
| 74667 | | - return pCur->skipNext; |
| 74668 | | - } |
| 74669 | | - |
| 74670 | | - assert( cursorOwnsBtShared(pCur) ); |
| 74671 | | - assert( (pCur->curFlags & BTCF_WriteFlag)!=0 |
| 74672 | | - && pBt->inTransaction==TRANS_WRITE |
| 74673 | | - && (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
| 74674 | | - assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 74675 | | - |
| 74676 | | - /* Assert that the caller has been consistent. If this cursor was opened |
| 74677 | | - ** expecting an index b-tree, then the caller should be inserting blob |
| 74678 | | - ** keys with no associated data. If the cursor was opened expecting an |
| 74679 | | - ** intkey table, the caller should be inserting integer keys with a |
| 74680 | | - ** blob of associated data. */ |
| 74681 | | - assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) ); |
| 74682 | | - |
| 74683 | 75227 | /* Save the positions of any other cursors open on this table. |
| 74684 | 75228 | ** |
| 74685 | 75229 | ** In some cases, the call to btreeMoveto() below is a no-op. For |
| 74686 | 75230 | ** example, when inserting data into a table with auto-generated integer |
| 74687 | 75231 | ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the |
| | @@ -74702,10 +75246,33 @@ |
| 74702 | 75246 | ** set the flag, load the schema, and then unset the flag. */ |
| 74703 | 75247 | return SQLITE_CORRUPT_BKPT; |
| 74704 | 75248 | } |
| 74705 | 75249 | } |
| 74706 | 75250 | |
| 75251 | + /* Ensure that the cursor is not in the CURSOR_FAULT state and that it |
| 75252 | + ** points to a valid cell. |
| 75253 | + */ |
| 75254 | + if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 75255 | + testcase( pCur->eState==CURSOR_REQUIRESEEK ); |
| 75256 | + testcase( pCur->eState==CURSOR_FAULT ); |
| 75257 | + rc = moveToRoot(pCur); |
| 75258 | + if( rc && rc!=SQLITE_EMPTY ) return rc; |
| 75259 | + } |
| 75260 | + |
| 75261 | + assert( cursorOwnsBtShared(pCur) ); |
| 75262 | + assert( (pCur->curFlags & BTCF_WriteFlag)!=0 |
| 75263 | + && pBt->inTransaction==TRANS_WRITE |
| 75264 | + && (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
| 75265 | + assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 75266 | + |
| 75267 | + /* Assert that the caller has been consistent. If this cursor was opened |
| 75268 | + ** expecting an index b-tree, then the caller should be inserting blob |
| 75269 | + ** keys with no associated data. If the cursor was opened expecting an |
| 75270 | + ** intkey table, the caller should be inserting integer keys with a |
| 75271 | + ** blob of associated data. */ |
| 75272 | + assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) ); |
| 75273 | + |
| 74707 | 75274 | if( pCur->pKeyInfo==0 ){ |
| 74708 | 75275 | assert( pX->pKey==0 ); |
| 74709 | 75276 | /* If this is an insert into a table b-tree, invalidate any incrblob |
| 74710 | 75277 | ** cursors open on the row being replaced */ |
| 74711 | 75278 | if( p->hasIncrblobCur ){ |
| | @@ -74790,18 +75357,18 @@ |
| 74790 | 75357 | return btreeOverwriteCell(pCur, &x2); |
| 74791 | 75358 | } |
| 74792 | 75359 | } |
| 74793 | 75360 | } |
| 74794 | 75361 | assert( pCur->eState==CURSOR_VALID |
| 74795 | | - || (pCur->eState==CURSOR_INVALID && loc) |
| 74796 | | - || CORRUPT_DB ); |
| 75362 | + || (pCur->eState==CURSOR_INVALID && loc) ); |
| 74797 | 75363 | |
| 74798 | 75364 | pPage = pCur->pPage; |
| 74799 | 75365 | assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) ); |
| 74800 | 75366 | assert( pPage->leaf || !pPage->intKey ); |
| 74801 | 75367 | if( pPage->nFree<0 ){ |
| 74802 | 75368 | if( NEVER(pCur->eState>CURSOR_INVALID) ){ |
| 75369 | + /* ^^^^^--- due to the moveToRoot() call above */ |
| 74803 | 75370 | rc = SQLITE_CORRUPT_BKPT; |
| 74804 | 75371 | }else{ |
| 74805 | 75372 | rc = btreeComputeFreeSpace(pPage); |
| 74806 | 75373 | } |
| 74807 | 75374 | if( rc ) return rc; |
| | @@ -75078,16 +75645,20 @@ |
| 75078 | 75645 | assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
| 75079 | 75646 | assert( pCur->curFlags & BTCF_WriteFlag ); |
| 75080 | 75647 | assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 75081 | 75648 | assert( !hasReadConflicts(p, pCur->pgnoRoot) ); |
| 75082 | 75649 | assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 ); |
| 75083 | | - if( pCur->eState==CURSOR_REQUIRESEEK ){ |
| 75084 | | - rc = btreeRestoreCursorPosition(pCur); |
| 75085 | | - assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID ); |
| 75086 | | - if( rc || pCur->eState!=CURSOR_VALID ) return rc; |
| 75650 | + if( pCur->eState!=CURSOR_VALID ){ |
| 75651 | + if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 75652 | + rc = btreeRestoreCursorPosition(pCur); |
| 75653 | + assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID ); |
| 75654 | + if( rc || pCur->eState!=CURSOR_VALID ) return rc; |
| 75655 | + }else{ |
| 75656 | + return SQLITE_CORRUPT_BKPT; |
| 75657 | + } |
| 75087 | 75658 | } |
| 75088 | | - assert( CORRUPT_DB || pCur->eState==CURSOR_VALID ); |
| 75659 | + assert( pCur->eState==CURSOR_VALID ); |
| 75089 | 75660 | |
| 75090 | 75661 | iCellDepth = pCur->iPage; |
| 75091 | 75662 | iCellIdx = pCur->ix; |
| 75092 | 75663 | pPage = pCur->pPage; |
| 75093 | 75664 | if( pPage->nCell<=iCellIdx ){ |
| | @@ -75115,11 +75686,12 @@ |
| 75115 | 75686 | ** bPreserve==2 Cursor won't move. Set CURSOR_SKIPNEXT. |
| 75116 | 75687 | */ |
| 75117 | 75688 | bPreserve = (flags & BTREE_SAVEPOSITION)!=0; |
| 75118 | 75689 | if( bPreserve ){ |
| 75119 | 75690 | if( !pPage->leaf |
| 75120 | | - || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3) |
| 75691 | + || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) > |
| 75692 | + (int)(pBt->usableSize*2/3) |
| 75121 | 75693 | || pPage->nCell==1 /* See dbfuzz001.test for a test case */ |
| 75122 | 75694 | ){ |
| 75123 | 75695 | /* A b-tree rebalance will be required after deleting this entry. |
| 75124 | 75696 | ** Save the cursor key. */ |
| 75125 | 75697 | rc = saveCursorKey(pCur); |
| | @@ -75211,11 +75783,19 @@ |
| 75211 | 75783 | ** on the leaf node first. If the balance proceeds far enough up the |
| 75212 | 75784 | ** tree that we can be sure that any problem in the internal node has |
| 75213 | 75785 | ** been corrected, so be it. Otherwise, after balancing the leaf node, |
| 75214 | 75786 | ** walk the cursor up the tree to the internal node and balance it as |
| 75215 | 75787 | ** well. */ |
| 75216 | | - rc = balance(pCur); |
| 75788 | + assert( pCur->pPage->nOverflow==0 ); |
| 75789 | + assert( pCur->pPage->nFree>=0 ); |
| 75790 | + if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){ |
| 75791 | + /* Optimization: If the free space is less than 2/3rds of the page, |
| 75792 | + ** then balance() will always be a no-op. No need to invoke it. */ |
| 75793 | + rc = SQLITE_OK; |
| 75794 | + }else{ |
| 75795 | + rc = balance(pCur); |
| 75796 | + } |
| 75217 | 75797 | if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){ |
| 75218 | 75798 | releasePageNotNull(pCur->pPage); |
| 75219 | 75799 | pCur->iPage--; |
| 75220 | 75800 | while( pCur->iPage>iCellDepth ){ |
| 75221 | 75801 | releasePage(pCur->apPage[pCur->iPage--]); |
| | @@ -75434,11 +76014,11 @@ |
| 75434 | 76014 | return SQLITE_CORRUPT_BKPT; |
| 75435 | 76015 | } |
| 75436 | 76016 | rc = getAndInitPage(pBt, pgno, &pPage, 0, 0); |
| 75437 | 76017 | if( rc ) return rc; |
| 75438 | 76018 | if( (pBt->openFlags & BTREE_SINGLE)==0 |
| 75439 | | - && sqlite3PagerPageRefcount(pPage->pDbPage)!=1 |
| 76019 | + && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1)) |
| 75440 | 76020 | ){ |
| 75441 | 76021 | rc = SQLITE_CORRUPT_BKPT; |
| 75442 | 76022 | goto cleardatabasepage_out; |
| 75443 | 76023 | } |
| 75444 | 76024 | hdr = pPage->hdrOffset; |
| | @@ -76814,11 +77394,11 @@ |
| 76814 | 77394 | int i = sqlite3FindDbName(pDb, zDb); |
| 76815 | 77395 | |
| 76816 | 77396 | if( i==1 ){ |
| 76817 | 77397 | Parse sParse; |
| 76818 | 77398 | int rc = 0; |
| 76819 | | - sqlite3ParseObjectInit(&sParse,pErrorDb); |
| 77399 | + sqlite3ParseObjectInit(&sParse,pDb); |
| 76820 | 77400 | if( sqlite3OpenTempDatabase(&sParse) ){ |
| 76821 | 77401 | sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg); |
| 76822 | 77402 | rc = SQLITE_ERROR; |
| 76823 | 77403 | } |
| 76824 | 77404 | sqlite3DbFree(pErrorDb, sParse.zErrMsg); |
| | @@ -77706,11 +78286,15 @@ |
| 77706 | 78286 | #endif |
| 77707 | 78287 | assert( pMem!=0 ); |
| 77708 | 78288 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); |
| 77709 | 78289 | assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE |
| 77710 | 78290 | || desiredEnc==SQLITE_UTF16BE ); |
| 77711 | | - if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){ |
| 78291 | + if( !(pMem->flags&MEM_Str) ){ |
| 78292 | + pMem->enc = desiredEnc; |
| 78293 | + return SQLITE_OK; |
| 78294 | + } |
| 78295 | + if( pMem->enc==desiredEnc ){ |
| 77712 | 78296 | return SQLITE_OK; |
| 77713 | 78297 | } |
| 77714 | 78298 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 77715 | 78299 | #ifdef SQLITE_OMIT_UTF16 |
| 77716 | 78300 | return SQLITE_ERROR; |
| | @@ -77965,10 +78549,11 @@ |
| 77965 | 78549 | t.flags = MEM_Null; |
| 77966 | 78550 | t.db = pMem->db; |
| 77967 | 78551 | ctx.pOut = &t; |
| 77968 | 78552 | ctx.pMem = pMem; |
| 77969 | 78553 | ctx.pFunc = pFunc; |
| 78554 | + ctx.enc = ENC(t.db); |
| 77970 | 78555 | pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */ |
| 77971 | 78556 | assert( (pMem->flags & MEM_Dyn)==0 ); |
| 77972 | 78557 | if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc); |
| 77973 | 78558 | memcpy(pMem, &t, sizeof(t)); |
| 77974 | 78559 | return ctx.isError; |
| | @@ -77992,10 +78577,11 @@ |
| 77992 | 78577 | memset(&ctx, 0, sizeof(ctx)); |
| 77993 | 78578 | sqlite3VdbeMemSetNull(pOut); |
| 77994 | 78579 | ctx.pOut = pOut; |
| 77995 | 78580 | ctx.pMem = pAccum; |
| 77996 | 78581 | ctx.pFunc = pFunc; |
| 78582 | + ctx.enc = ENC(pAccum->db); |
| 77997 | 78583 | pFunc->xValue(&ctx); |
| 77998 | 78584 | return ctx.isError; |
| 77999 | 78585 | } |
| 78000 | 78586 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 78001 | 78587 | |
| | @@ -78056,10 +78642,18 @@ |
| 78056 | 78642 | assert( sqlite3VdbeCheckMemInvariants(p) ); |
| 78057 | 78643 | if( VdbeMemDynamic(p) || p->szMalloc ){ |
| 78058 | 78644 | vdbeMemClear(p); |
| 78059 | 78645 | } |
| 78060 | 78646 | } |
| 78647 | + |
| 78648 | +/* Like sqlite3VdbeMemRelease() but faster for cases where we |
| 78649 | +** know in advance that the Mem is not MEM_Dyn or MEM_Agg. |
| 78650 | +*/ |
| 78651 | +SQLITE_PRIVATE void sqlite3VdbeMemReleaseMalloc(Mem *p){ |
| 78652 | + assert( !VdbeMemDynamic(p) ); |
| 78653 | + if( p->szMalloc ) vdbeMemClear(p); |
| 78654 | +} |
| 78061 | 78655 | |
| 78062 | 78656 | /* |
| 78063 | 78657 | ** Convert a 64-bit IEEE double into a 64-bit signed integer. |
| 78064 | 78658 | ** If the double is out of range of a 64-bit signed integer then |
| 78065 | 78659 | ** return the closest available 64-bit signed integer. |
| | @@ -78418,10 +79012,11 @@ |
| 78418 | 79012 | void *pPtr, |
| 78419 | 79013 | const char *zPType, |
| 78420 | 79014 | void (*xDestructor)(void*) |
| 78421 | 79015 | ){ |
| 78422 | 79016 | assert( pMem->flags==MEM_Null ); |
| 79017 | + vdbeMemClear(pMem); |
| 78423 | 79018 | pMem->u.zPType = zPType ? zPType : ""; |
| 78424 | 79019 | pMem->z = pPtr; |
| 78425 | 79020 | pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term; |
| 78426 | 79021 | pMem->eSubtype = 'p'; |
| 78427 | 79022 | pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor; |
| | @@ -78600,10 +79195,17 @@ |
| 78600 | 79195 | ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH |
| 78601 | 79196 | ** size limit) then no memory allocation occurs. If the string can be |
| 78602 | 79197 | ** stored without allocating memory, then it is. If a memory allocation |
| 78603 | 79198 | ** is required to store the string, then value of pMem is unchanged. In |
| 78604 | 79199 | ** either case, SQLITE_TOOBIG is returned. |
| 79200 | +** |
| 79201 | +** The "enc" parameter is the text encoding for the string, or zero |
| 79202 | +** to store a blob. |
| 79203 | +** |
| 79204 | +** If n is negative, then the string consists of all bytes up to but |
| 79205 | +** excluding the first zero character. The n parameter must be |
| 79206 | +** non-negative for blobs. |
| 78605 | 79207 | */ |
| 78606 | 79208 | SQLITE_PRIVATE int sqlite3VdbeMemSetStr( |
| 78607 | 79209 | Mem *pMem, /* Memory cell to set to string value */ |
| 78608 | 79210 | const char *z, /* String pointer */ |
| 78609 | 79211 | i64 n, /* Bytes in string, or negative */ |
| | @@ -78610,15 +79212,16 @@ |
| 78610 | 79212 | u8 enc, /* Encoding of z. 0 for BLOBs */ |
| 78611 | 79213 | void (*xDel)(void*) /* Destructor function */ |
| 78612 | 79214 | ){ |
| 78613 | 79215 | i64 nByte = n; /* New value for pMem->n */ |
| 78614 | 79216 | int iLimit; /* Maximum allowed string or blob size */ |
| 78615 | | - u16 flags = 0; /* New value for pMem->flags */ |
| 79217 | + u16 flags; /* New value for pMem->flags */ |
| 78616 | 79218 | |
| 78617 | 79219 | assert( pMem!=0 ); |
| 78618 | 79220 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 78619 | 79221 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); |
| 79222 | + assert( enc!=0 || n>=0 ); |
| 78620 | 79223 | |
| 78621 | 79224 | /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ |
| 78622 | 79225 | if( !z ){ |
| 78623 | 79226 | sqlite3VdbeMemSetNull(pMem); |
| 78624 | 79227 | return SQLITE_OK; |
| | @@ -78627,19 +79230,34 @@ |
| 78627 | 79230 | if( pMem->db ){ |
| 78628 | 79231 | iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 78629 | 79232 | }else{ |
| 78630 | 79233 | iLimit = SQLITE_MAX_LENGTH; |
| 78631 | 79234 | } |
| 78632 | | - flags = (enc==0?MEM_Blob:MEM_Str); |
| 78633 | 79235 | if( nByte<0 ){ |
| 78634 | 79236 | assert( enc!=0 ); |
| 78635 | 79237 | if( enc==SQLITE_UTF8 ){ |
| 78636 | 79238 | nByte = strlen(z); |
| 78637 | 79239 | }else{ |
| 78638 | 79240 | for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){} |
| 78639 | 79241 | } |
| 78640 | | - flags |= MEM_Term; |
| 79242 | + flags= MEM_Str|MEM_Term; |
| 79243 | + }else if( enc==0 ){ |
| 79244 | + flags = MEM_Blob; |
| 79245 | + enc = SQLITE_UTF8; |
| 79246 | + }else{ |
| 79247 | + flags = MEM_Str; |
| 79248 | + } |
| 79249 | + if( nByte>iLimit ){ |
| 79250 | + if( xDel && xDel!=SQLITE_TRANSIENT ){ |
| 79251 | + if( xDel==SQLITE_DYNAMIC ){ |
| 79252 | + sqlite3DbFree(pMem->db, (void*)z); |
| 79253 | + }else{ |
| 79254 | + xDel((void*)z); |
| 79255 | + } |
| 79256 | + } |
| 79257 | + sqlite3VdbeMemSetNull(pMem); |
| 79258 | + return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG); |
| 78641 | 79259 | } |
| 78642 | 79260 | |
| 78643 | 79261 | /* The following block sets the new values of Mem.z and Mem.xDel. It |
| 78644 | 79262 | ** also sets a flag in local variable "flags" to indicate the memory |
| 78645 | 79263 | ** management (one of MEM_Dyn or MEM_Static). |
| | @@ -78647,13 +79265,10 @@ |
| 78647 | 79265 | if( xDel==SQLITE_TRANSIENT ){ |
| 78648 | 79266 | i64 nAlloc = nByte; |
| 78649 | 79267 | if( flags&MEM_Term ){ |
| 78650 | 79268 | nAlloc += (enc==SQLITE_UTF8?1:2); |
| 78651 | 79269 | } |
| 78652 | | - if( nByte>iLimit ){ |
| 78653 | | - return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG); |
| 78654 | | - } |
| 78655 | 79270 | testcase( nAlloc==0 ); |
| 78656 | 79271 | testcase( nAlloc==31 ); |
| 78657 | 79272 | testcase( nAlloc==32 ); |
| 78658 | 79273 | if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){ |
| 78659 | 79274 | return SQLITE_NOMEM_BKPT; |
| | @@ -78671,30 +79286,18 @@ |
| 78671 | 79286 | } |
| 78672 | 79287 | } |
| 78673 | 79288 | |
| 78674 | 79289 | pMem->n = (int)(nByte & 0x7fffffff); |
| 78675 | 79290 | pMem->flags = flags; |
| 78676 | | - if( enc ){ |
| 78677 | | - pMem->enc = enc; |
| 78678 | | -#ifdef SQLITE_ENABLE_SESSION |
| 78679 | | - }else if( pMem->db==0 ){ |
| 78680 | | - pMem->enc = SQLITE_UTF8; |
| 78681 | | -#endif |
| 78682 | | - }else{ |
| 78683 | | - assert( pMem->db!=0 ); |
| 78684 | | - pMem->enc = ENC(pMem->db); |
| 78685 | | - } |
| 79291 | + pMem->enc = enc; |
| 78686 | 79292 | |
| 78687 | 79293 | #ifndef SQLITE_OMIT_UTF16 |
| 78688 | 79294 | if( enc>SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ |
| 78689 | 79295 | return SQLITE_NOMEM_BKPT; |
| 78690 | 79296 | } |
| 78691 | 79297 | #endif |
| 78692 | 79298 | |
| 78693 | | - if( nByte>iLimit ){ |
| 78694 | | - return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG); |
| 78695 | | - } |
| 78696 | 79299 | |
| 78697 | 79300 | return SQLITE_OK; |
| 78698 | 79301 | } |
| 78699 | 79302 | |
| 78700 | 79303 | /* |
| | @@ -78972,10 +79575,11 @@ |
| 78972 | 79575 | |
| 78973 | 79576 | assert( pCtx->pParse->rc==SQLITE_OK ); |
| 78974 | 79577 | memset(&ctx, 0, sizeof(ctx)); |
| 78975 | 79578 | ctx.pOut = pVal; |
| 78976 | 79579 | ctx.pFunc = pFunc; |
| 79580 | + ctx.enc = ENC(db); |
| 78977 | 79581 | pFunc->xSFunc(&ctx, nVal, apVal); |
| 78978 | 79582 | if( ctx.isError ){ |
| 78979 | 79583 | rc = ctx.isError; |
| 78980 | 79584 | sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal)); |
| 78981 | 79585 | }else{ |
| | @@ -79032,15 +79636,11 @@ |
| 79032 | 79636 | const char *zNeg = ""; |
| 79033 | 79637 | int rc = SQLITE_OK; |
| 79034 | 79638 | |
| 79035 | 79639 | assert( pExpr!=0 ); |
| 79036 | 79640 | while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft; |
| 79037 | | -#if defined(SQLITE_ENABLE_STAT4) |
| 79038 | 79641 | if( op==TK_REGISTER ) op = pExpr->op2; |
| 79039 | | -#else |
| 79040 | | - if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; |
| 79041 | | -#endif |
| 79042 | 79642 | |
| 79043 | 79643 | /* Compressed expressions only appear when parsing the DEFAULT clause |
| 79044 | 79644 | ** on a table column definition, and hence only when pCtx==0. This |
| 79045 | 79645 | ** check ensures that an EP_TokenOnly expression is never passed down |
| 79046 | 79646 | ** into valueFromFunction(). */ |
| | @@ -79151,11 +79751,11 @@ |
| 79151 | 79751 | *ppVal = pVal; |
| 79152 | 79752 | return rc; |
| 79153 | 79753 | |
| 79154 | 79754 | no_mem: |
| 79155 | 79755 | #ifdef SQLITE_ENABLE_STAT4 |
| 79156 | | - if( pCtx==0 || pCtx->pParse->nErr==0 ) |
| 79756 | + if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) ) |
| 79157 | 79757 | #endif |
| 79158 | 79758 | sqlite3OomFault(db); |
| 79159 | 79759 | sqlite3DbFree(db, zVal); |
| 79160 | 79760 | assert( *ppVal==0 ); |
| 79161 | 79761 | #ifdef SQLITE_ENABLE_STAT4 |
| | @@ -79486,11 +80086,11 @@ |
| 79486 | 80086 | db->pVdbe->pPrev = p; |
| 79487 | 80087 | } |
| 79488 | 80088 | p->pNext = db->pVdbe; |
| 79489 | 80089 | p->pPrev = 0; |
| 79490 | 80090 | db->pVdbe = p; |
| 79491 | | - p->iVdbeMagic = VDBE_MAGIC_INIT; |
| 80091 | + assert( p->eVdbeState==VDBE_INIT_STATE ); |
| 79492 | 80092 | p->pParse = pParse; |
| 79493 | 80093 | pParse->pVdbe = p; |
| 79494 | 80094 | assert( pParse->aLabel==0 ); |
| 79495 | 80095 | assert( pParse->nLabel==0 ); |
| 79496 | 80096 | assert( p->nOpAlloc==0 ); |
| | @@ -79631,11 +80231,11 @@ |
| 79631 | 80231 | if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){ |
| 79632 | 80232 | sqlite3OomFault(p->db); |
| 79633 | 80233 | return SQLITE_NOMEM; |
| 79634 | 80234 | } |
| 79635 | 80235 | |
| 79636 | | - assert( nOp<=(1024/sizeof(Op)) ); |
| 80236 | + assert( nOp<=(int)(1024/sizeof(Op)) ); |
| 79637 | 80237 | assert( nNew>=(v->nOpAlloc+nOp) ); |
| 79638 | 80238 | pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); |
| 79639 | 80239 | if( pNew ){ |
| 79640 | 80240 | p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew); |
| 79641 | 80241 | v->nOpAlloc = p->szOpAlloc/sizeof(Op); |
| | @@ -79687,11 +80287,11 @@ |
| 79687 | 80287 | SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ |
| 79688 | 80288 | int i; |
| 79689 | 80289 | VdbeOp *pOp; |
| 79690 | 80290 | |
| 79691 | 80291 | i = p->nOp; |
| 79692 | | - assert( p->iVdbeMagic==VDBE_MAGIC_INIT ); |
| 80292 | + assert( p->eVdbeState==VDBE_INIT_STATE ); |
| 79693 | 80293 | assert( op>=0 && op<0xff ); |
| 79694 | 80294 | if( p->nOpAlloc<=i ){ |
| 79695 | 80295 | return growOp3(p, op, p1, p2, p3); |
| 79696 | 80296 | } |
| 79697 | 80297 | assert( p->aOp!=0 ); |
| | @@ -80019,11 +80619,11 @@ |
| 80019 | 80619 | } |
| 80020 | 80620 | } |
| 80021 | 80621 | SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){ |
| 80022 | 80622 | Parse *p = v->pParse; |
| 80023 | 80623 | int j = ADDR(x); |
| 80024 | | - assert( v->iVdbeMagic==VDBE_MAGIC_INIT ); |
| 80624 | + assert( v->eVdbeState==VDBE_INIT_STATE ); |
| 80025 | 80625 | assert( j<-p->nLabel ); |
| 80026 | 80626 | assert( j>=0 ); |
| 80027 | 80627 | #ifdef SQLITE_DEBUG |
| 80028 | 80628 | if( p->db->flags & SQLITE_VdbeAddopTrace ){ |
| 80029 | 80629 | printf("RESOLVE LABEL %d to %d\n", x, v->nOp); |
| | @@ -80150,10 +80750,12 @@ |
| 80150 | 80750 | int hasCreateTable = 0; |
| 80151 | 80751 | int hasCreateIndex = 0; |
| 80152 | 80752 | int hasInitCoroutine = 0; |
| 80153 | 80753 | Op *pOp; |
| 80154 | 80754 | VdbeOpIter sIter; |
| 80755 | + |
| 80756 | + if( v==0 ) return 0; |
| 80155 | 80757 | memset(&sIter, 0, sizeof(sIter)); |
| 80156 | 80758 | sIter.v = v; |
| 80157 | 80759 | |
| 80158 | 80760 | while( (pOp = opIterNext(&sIter))!=0 ){ |
| 80159 | 80761 | int opcode = pOp->opcode; |
| | @@ -80233,11 +80835,11 @@ |
| 80233 | 80835 | ** and store that value in *pMaxFuncArgs. |
| 80234 | 80836 | ** |
| 80235 | 80837 | ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately |
| 80236 | 80838 | ** indicate what the prepared statement actually does. |
| 80237 | 80839 | ** |
| 80238 | | -** (4) Initialize the p4.xAdvance pointer on opcodes that use it. |
| 80840 | +** (4) (discontinued) |
| 80239 | 80841 | ** |
| 80240 | 80842 | ** (5) Reclaim the memory allocated for storing labels. |
| 80241 | 80843 | ** |
| 80242 | 80844 | ** This routine will only function correctly if the mkopcodeh.tcl generator |
| 80243 | 80845 | ** script numbers the opcodes correctly. Changes to this routine must be |
| | @@ -80279,29 +80881,10 @@ |
| 80279 | 80881 | case OP_JournalMode: { |
| 80280 | 80882 | p->readOnly = 0; |
| 80281 | 80883 | p->bIsReader = 1; |
| 80282 | 80884 | break; |
| 80283 | 80885 | } |
| 80284 | | - case OP_Next: |
| 80285 | | - case OP_SorterNext: { |
| 80286 | | - pOp->p4.xAdvance = sqlite3BtreeNext; |
| 80287 | | - pOp->p4type = P4_ADVANCE; |
| 80288 | | - /* The code generator never codes any of these opcodes as a jump |
| 80289 | | - ** to a label. They are always coded as a jump backwards to a |
| 80290 | | - ** known address */ |
| 80291 | | - assert( pOp->p2>=0 ); |
| 80292 | | - break; |
| 80293 | | - } |
| 80294 | | - case OP_Prev: { |
| 80295 | | - pOp->p4.xAdvance = sqlite3BtreePrevious; |
| 80296 | | - pOp->p4type = P4_ADVANCE; |
| 80297 | | - /* The code generator never codes any of these opcodes as a jump |
| 80298 | | - ** to a label. They are always coded as a jump backwards to a |
| 80299 | | - ** known address */ |
| 80300 | | - assert( pOp->p2>=0 ); |
| 80301 | | - break; |
| 80302 | | - } |
| 80303 | 80886 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 80304 | 80887 | case OP_VUpdate: { |
| 80305 | 80888 | if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; |
| 80306 | 80889 | break; |
| 80307 | 80890 | } |
| | @@ -80333,22 +80916,24 @@ |
| 80333 | 80916 | assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0); |
| 80334 | 80917 | } |
| 80335 | 80918 | if( pOp==p->aOp ) break; |
| 80336 | 80919 | pOp--; |
| 80337 | 80920 | } |
| 80338 | | - sqlite3DbFree(p->db, pParse->aLabel); |
| 80339 | | - pParse->aLabel = 0; |
| 80921 | + if( aLabel ){ |
| 80922 | + sqlite3DbFreeNN(p->db, pParse->aLabel); |
| 80923 | + pParse->aLabel = 0; |
| 80924 | + } |
| 80340 | 80925 | pParse->nLabel = 0; |
| 80341 | 80926 | *pMaxFuncArgs = nMaxArgs; |
| 80342 | 80927 | assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); |
| 80343 | 80928 | } |
| 80344 | 80929 | |
| 80345 | 80930 | /* |
| 80346 | 80931 | ** Return the address of the next instruction to be inserted. |
| 80347 | 80932 | */ |
| 80348 | 80933 | SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){ |
| 80349 | | - assert( p->iVdbeMagic==VDBE_MAGIC_INIT ); |
| 80934 | + assert( p->eVdbeState==VDBE_INIT_STATE ); |
| 80350 | 80935 | return p->nOp; |
| 80351 | 80936 | } |
| 80352 | 80937 | |
| 80353 | 80938 | /* |
| 80354 | 80939 | ** Verify that at least N opcode slots are available in p without |
| | @@ -80429,11 +81014,11 @@ |
| 80429 | 81014 | int iLineno /* Source-file line number of first opcode */ |
| 80430 | 81015 | ){ |
| 80431 | 81016 | int i; |
| 80432 | 81017 | VdbeOp *pOut, *pFirst; |
| 80433 | 81018 | assert( nOp>0 ); |
| 80434 | | - assert( p->iVdbeMagic==VDBE_MAGIC_INIT ); |
| 81019 | + assert( p->eVdbeState==VDBE_INIT_STATE ); |
| 80435 | 81020 | if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){ |
| 80436 | 81021 | return 0; |
| 80437 | 81022 | } |
| 80438 | 81023 | pFirst = pOut = &p->aOp[p->nOp]; |
| 80439 | 81024 | for(i=0; i<nOp; i++, aOp++, pOut++){ |
| | @@ -80581,11 +81166,10 @@ |
| 80581 | 81166 | break; |
| 80582 | 81167 | } |
| 80583 | 81168 | case P4_REAL: |
| 80584 | 81169 | case P4_INT64: |
| 80585 | 81170 | case P4_DYNAMIC: |
| 80586 | | - case P4_DYNBLOB: |
| 80587 | 81171 | case P4_INTARRAY: { |
| 80588 | 81172 | sqlite3DbFree(db, p4); |
| 80589 | 81173 | break; |
| 80590 | 81174 | } |
| 80591 | 81175 | case P4_KEYINFO: { |
| | @@ -80621,17 +81205,20 @@ |
| 80621 | 81205 | ** Free the space allocated for aOp and any p4 values allocated for the |
| 80622 | 81206 | ** opcodes contained within. If aOp is not NULL it is assumed to contain |
| 80623 | 81207 | ** nOp entries. |
| 80624 | 81208 | */ |
| 80625 | 81209 | static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ |
| 81210 | + assert( nOp>=0 ); |
| 80626 | 81211 | if( aOp ){ |
| 80627 | | - Op *pOp; |
| 80628 | | - for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){ |
| 81212 | + Op *pOp = &aOp[nOp-1]; |
| 81213 | + while(1){ /* Exit via break */ |
| 80629 | 81214 | if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p); |
| 80630 | 81215 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 80631 | 81216 | sqlite3DbFree(db, pOp->zComment); |
| 80632 | 81217 | #endif |
| 81218 | + if( pOp==aOp ) break; |
| 81219 | + pOp--; |
| 80633 | 81220 | } |
| 80634 | 81221 | sqlite3DbFreeNN(db, aOp); |
| 80635 | 81222 | } |
| 80636 | 81223 | } |
| 80637 | 81224 | |
| | @@ -80753,11 +81340,11 @@ |
| 80753 | 81340 | SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ |
| 80754 | 81341 | Op *pOp; |
| 80755 | 81342 | sqlite3 *db; |
| 80756 | 81343 | assert( p!=0 ); |
| 80757 | 81344 | db = p->db; |
| 80758 | | - assert( p->iVdbeMagic==VDBE_MAGIC_INIT ); |
| 81345 | + assert( p->eVdbeState==VDBE_INIT_STATE ); |
| 80759 | 81346 | assert( p->aOp!=0 || db->mallocFailed ); |
| 80760 | 81347 | if( db->mallocFailed ){ |
| 80761 | 81348 | if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4); |
| 80762 | 81349 | return; |
| 80763 | 81350 | } |
| | @@ -80881,11 +81468,11 @@ |
| 80881 | 81468 | */ |
| 80882 | 81469 | SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ |
| 80883 | 81470 | /* C89 specifies that the constant "dummy" will be initialized to all |
| 80884 | 81471 | ** zeros, which is correct. MSVC generates a warning, nevertheless. */ |
| 80885 | 81472 | static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ |
| 80886 | | - assert( p->iVdbeMagic==VDBE_MAGIC_INIT ); |
| 81473 | + assert( p->eVdbeState==VDBE_INIT_STATE ); |
| 80887 | 81474 | if( addr<0 ){ |
| 80888 | 81475 | addr = p->nOp - 1; |
| 80889 | 81476 | } |
| 80890 | 81477 | assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); |
| 80891 | 81478 | if( p->db->mallocFailed ){ |
| | @@ -81178,14 +81765,10 @@ |
| 81178 | 81765 | } |
| 81179 | 81766 | case P4_SUBPROGRAM: { |
| 81180 | 81767 | zP4 = "program"; |
| 81181 | 81768 | break; |
| 81182 | 81769 | } |
| 81183 | | - case P4_DYNBLOB: |
| 81184 | | - case P4_ADVANCE: { |
| 81185 | | - break; |
| 81186 | | - } |
| 81187 | 81770 | case P4_TABLE: { |
| 81188 | 81771 | zP4 = pOp->p4.pTab->zName; |
| 81189 | 81772 | break; |
| 81190 | 81773 | } |
| 81191 | 81774 | default: { |
| | @@ -81313,25 +81896,44 @@ |
| 81313 | 81896 | } |
| 81314 | 81897 | #endif |
| 81315 | 81898 | |
| 81316 | 81899 | /* |
| 81317 | 81900 | ** Initialize an array of N Mem element. |
| 81901 | +** |
| 81902 | +** This is a high-runner, so only those fields that really do need to |
| 81903 | +** be initialized are set. The Mem structure is organized so that |
| 81904 | +** the fields that get initialized are nearby and hopefully on the same |
| 81905 | +** cache line. |
| 81906 | +** |
| 81907 | +** Mem.flags = flags |
| 81908 | +** Mem.db = db |
| 81909 | +** Mem.szMalloc = 0 |
| 81910 | +** |
| 81911 | +** All other fields of Mem can safely remain uninitialized for now. They |
| 81912 | +** will be initialized before use. |
| 81318 | 81913 | */ |
| 81319 | 81914 | static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){ |
| 81320 | | - while( (N--)>0 ){ |
| 81321 | | - p->db = db; |
| 81322 | | - p->flags = flags; |
| 81323 | | - p->szMalloc = 0; |
| 81915 | + if( N>0 ){ |
| 81916 | + do{ |
| 81917 | + p->flags = flags; |
| 81918 | + p->db = db; |
| 81919 | + p->szMalloc = 0; |
| 81324 | 81920 | #ifdef SQLITE_DEBUG |
| 81325 | | - p->pScopyFrom = 0; |
| 81921 | + p->pScopyFrom = 0; |
| 81326 | 81922 | #endif |
| 81327 | | - p++; |
| 81923 | + p++; |
| 81924 | + }while( (--N)>0 ); |
| 81328 | 81925 | } |
| 81329 | 81926 | } |
| 81330 | 81927 | |
| 81331 | 81928 | /* |
| 81332 | | -** Release an array of N Mem elements |
| 81929 | +** Release auxiliary memory held in an array of N Mem elements. |
| 81930 | +** |
| 81931 | +** After this routine returns, all Mem elements in the array will still |
| 81932 | +** be valid. Those Mem elements that were not holding auxiliary resources |
| 81933 | +** will be unchanged. Mem elements which had something freed will be |
| 81934 | +** set to MEM_Undefined. |
| 81333 | 81935 | */ |
| 81334 | 81936 | static void releaseMemArray(Mem *p, int N){ |
| 81335 | 81937 | if( p && N ){ |
| 81336 | 81938 | Mem *pEnd = &p[N]; |
| 81337 | 81939 | sqlite3 *db = p->db; |
| | @@ -81360,16 +81962,21 @@ |
| 81360 | 81962 | testcase( p->flags & MEM_Agg ); |
| 81361 | 81963 | testcase( p->flags & MEM_Dyn ); |
| 81362 | 81964 | if( p->flags&(MEM_Agg|MEM_Dyn) ){ |
| 81363 | 81965 | testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel ); |
| 81364 | 81966 | sqlite3VdbeMemRelease(p); |
| 81967 | + p->flags = MEM_Undefined; |
| 81365 | 81968 | }else if( p->szMalloc ){ |
| 81366 | 81969 | sqlite3DbFreeNN(db, p->zMalloc); |
| 81367 | 81970 | p->szMalloc = 0; |
| 81971 | + p->flags = MEM_Undefined; |
| 81368 | 81972 | } |
| 81369 | | - |
| 81370 | | - p->flags = MEM_Undefined; |
| 81973 | +#ifdef SQLITE_DEBUG |
| 81974 | + else{ |
| 81975 | + p->flags = MEM_Undefined; |
| 81976 | + } |
| 81977 | +#endif |
| 81371 | 81978 | }while( (++p)<pEnd ); |
| 81372 | 81979 | } |
| 81373 | 81980 | } |
| 81374 | 81981 | |
| 81375 | 81982 | #ifdef SQLITE_DEBUG |
| | @@ -81563,11 +82170,11 @@ |
| 81563 | 82170 | int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0); |
| 81564 | 82171 | Op *aOp; /* Array of opcodes */ |
| 81565 | 82172 | Op *pOp; /* Current opcode */ |
| 81566 | 82173 | |
| 81567 | 82174 | assert( p->explain ); |
| 81568 | | - assert( p->iVdbeMagic==VDBE_MAGIC_RUN ); |
| 82175 | + assert( p->eVdbeState==VDBE_RUN_STATE ); |
| 81569 | 82176 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); |
| 81570 | 82177 | |
| 81571 | 82178 | /* Even though this opcode does not use dynamic strings for |
| 81572 | 82179 | ** the result, result columns may become dynamic if the user calls |
| 81573 | 82180 | ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. |
| | @@ -81743,18 +82350,19 @@ |
| 81743 | 82350 | SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){ |
| 81744 | 82351 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 81745 | 82352 | int i; |
| 81746 | 82353 | #endif |
| 81747 | 82354 | assert( p!=0 ); |
| 81748 | | - assert( p->iVdbeMagic==VDBE_MAGIC_INIT || p->iVdbeMagic==VDBE_MAGIC_RESET ); |
| 82355 | + assert( p->eVdbeState==VDBE_INIT_STATE |
| 82356 | + || p->eVdbeState==VDBE_READY_STATE |
| 82357 | + || p->eVdbeState==VDBE_HALT_STATE ); |
| 81749 | 82358 | |
| 81750 | 82359 | /* There should be at least one opcode. |
| 81751 | 82360 | */ |
| 81752 | 82361 | assert( p->nOp>0 ); |
| 81753 | 82362 | |
| 81754 | | - /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ |
| 81755 | | - p->iVdbeMagic = VDBE_MAGIC_RUN; |
| 82363 | + p->eVdbeState = VDBE_READY_STATE; |
| 81756 | 82364 | |
| 81757 | 82365 | #ifdef SQLITE_DEBUG |
| 81758 | 82366 | for(i=0; i<p->nMem; i++){ |
| 81759 | 82367 | assert( p->aMem[i].db==p->db ); |
| 81760 | 82368 | } |
| | @@ -81806,11 +82414,11 @@ |
| 81806 | 82414 | struct ReusableSpace x; /* Reusable bulk memory */ |
| 81807 | 82415 | |
| 81808 | 82416 | assert( p!=0 ); |
| 81809 | 82417 | assert( p->nOp>0 ); |
| 81810 | 82418 | assert( pParse!=0 ); |
| 81811 | | - assert( p->iVdbeMagic==VDBE_MAGIC_INIT ); |
| 82419 | + assert( p->eVdbeState==VDBE_INIT_STATE ); |
| 81812 | 82420 | assert( pParse==p->pParse ); |
| 81813 | 82421 | p->pVList = pParse->pVList; |
| 81814 | 82422 | pParse->pVList = 0; |
| 81815 | 82423 | db = p->db; |
| 81816 | 82424 | assert( db->mallocFailed==0 ); |
| | @@ -81947,18 +82555,16 @@ |
| 81947 | 82555 | |
| 81948 | 82556 | /* |
| 81949 | 82557 | ** Close all cursors in the current frame. |
| 81950 | 82558 | */ |
| 81951 | 82559 | static void closeCursorsInFrame(Vdbe *p){ |
| 81952 | | - if( p->apCsr ){ |
| 81953 | | - int i; |
| 81954 | | - for(i=0; i<p->nCursor; i++){ |
| 81955 | | - VdbeCursor *pC = p->apCsr[i]; |
| 81956 | | - if( pC ){ |
| 81957 | | - sqlite3VdbeFreeCursor(p, pC); |
| 81958 | | - p->apCsr[i] = 0; |
| 81959 | | - } |
| 82560 | + int i; |
| 82561 | + for(i=0; i<p->nCursor; i++){ |
| 82562 | + VdbeCursor *pC = p->apCsr[i]; |
| 82563 | + if( pC ){ |
| 82564 | + sqlite3VdbeFreeCursor(p, pC); |
| 82565 | + p->apCsr[i] = 0; |
| 81960 | 82566 | } |
| 81961 | 82567 | } |
| 81962 | 82568 | } |
| 81963 | 82569 | |
| 81964 | 82570 | /* |
| | @@ -82003,13 +82609,11 @@ |
| 82003 | 82609 | p->pFrame = 0; |
| 82004 | 82610 | p->nFrame = 0; |
| 82005 | 82611 | } |
| 82006 | 82612 | assert( p->nFrame==0 ); |
| 82007 | 82613 | closeCursorsInFrame(p); |
| 82008 | | - if( p->aMem ){ |
| 82009 | | - releaseMemArray(p->aMem, p->nMem); |
| 82010 | | - } |
| 82614 | + releaseMemArray(p->aMem, p->nMem); |
| 82011 | 82615 | while( p->pDelFrame ){ |
| 82012 | 82616 | VdbeFrame *pDel = p->pDelFrame; |
| 82013 | 82617 | p->pDelFrame = pDel->pParent; |
| 82014 | 82618 | sqlite3VdbeFrameDelete(pDel); |
| 82015 | 82619 | } |
| | @@ -82445,11 +83049,12 @@ |
| 82445 | 83049 | || (!deferred && p->nFkConstraint>0) |
| 82446 | 83050 | ){ |
| 82447 | 83051 | p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; |
| 82448 | 83052 | p->errorAction = OE_Abort; |
| 82449 | 83053 | sqlite3VdbeError(p, "FOREIGN KEY constraint failed"); |
| 82450 | | - return SQLITE_ERROR; |
| 83054 | + if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR; |
| 83055 | + return SQLITE_CONSTRAINT_FOREIGNKEY; |
| 82451 | 83056 | } |
| 82452 | 83057 | return SQLITE_OK; |
| 82453 | 83058 | } |
| 82454 | 83059 | #endif |
| 82455 | 83060 | |
| | @@ -82484,11 +83089,11 @@ |
| 82484 | 83089 | ** Then the internal cache might have been left in an inconsistent |
| 82485 | 83090 | ** state. We need to rollback the statement transaction, if there is |
| 82486 | 83091 | ** one, or the complete transaction if there is no statement transaction. |
| 82487 | 83092 | */ |
| 82488 | 83093 | |
| 82489 | | - if( p->iVdbeMagic!=VDBE_MAGIC_RUN ){ |
| 83094 | + if( p->eVdbeState!=VDBE_RUN_STATE ){ |
| 82490 | 83095 | return SQLITE_OK; |
| 82491 | 83096 | } |
| 82492 | 83097 | if( db->mallocFailed ){ |
| 82493 | 83098 | p->rc = SQLITE_NOMEM_BKPT; |
| 82494 | 83099 | } |
| | @@ -82495,11 +83100,11 @@ |
| 82495 | 83100 | closeAllCursors(p); |
| 82496 | 83101 | checkActiveVdbeCnt(db); |
| 82497 | 83102 | |
| 82498 | 83103 | /* No commit or rollback needed if the program never started or if the |
| 82499 | 83104 | ** SQL statement does not read or write a database file. */ |
| 82500 | | - if( p->pc>=0 && p->bIsReader ){ |
| 83105 | + if( p->bIsReader ){ |
| 82501 | 83106 | int mrc; /* Primary error code from p->rc */ |
| 82502 | 83107 | int eStatementOp = 0; |
| 82503 | 83108 | int isSpecialError; /* Set to true if a 'special' error */ |
| 82504 | 83109 | |
| 82505 | 83110 | /* Lock all btrees used by the statement */ |
| | @@ -82643,19 +83248,17 @@ |
| 82643 | 83248 | /* Release the locks */ |
| 82644 | 83249 | sqlite3VdbeLeave(p); |
| 82645 | 83250 | } |
| 82646 | 83251 | |
| 82647 | 83252 | /* We have successfully halted and closed the VM. Record this fact. */ |
| 82648 | | - if( p->pc>=0 ){ |
| 82649 | | - db->nVdbeActive--; |
| 82650 | | - if( !p->readOnly ) db->nVdbeWrite--; |
| 82651 | | - if( p->bIsReader ) db->nVdbeRead--; |
| 82652 | | - assert( db->nVdbeActive>=db->nVdbeRead ); |
| 82653 | | - assert( db->nVdbeRead>=db->nVdbeWrite ); |
| 82654 | | - assert( db->nVdbeWrite>=0 ); |
| 82655 | | - } |
| 82656 | | - p->iVdbeMagic = VDBE_MAGIC_HALT; |
| 83253 | + db->nVdbeActive--; |
| 83254 | + if( !p->readOnly ) db->nVdbeWrite--; |
| 83255 | + if( p->bIsReader ) db->nVdbeRead--; |
| 83256 | + assert( db->nVdbeActive>=db->nVdbeRead ); |
| 83257 | + assert( db->nVdbeRead>=db->nVdbeWrite ); |
| 83258 | + assert( db->nVdbeWrite>=0 ); |
| 83259 | + p->eVdbeState = VDBE_HALT_STATE; |
| 82657 | 83260 | checkActiveVdbeCnt(db); |
| 82658 | 83261 | if( db->mallocFailed ){ |
| 82659 | 83262 | p->rc = SQLITE_NOMEM_BKPT; |
| 82660 | 83263 | } |
| 82661 | 83264 | |
| | @@ -82700,10 +83303,11 @@ |
| 82700 | 83303 | db->bBenignMalloc--; |
| 82701 | 83304 | }else if( db->pErr ){ |
| 82702 | 83305 | sqlite3ValueSetNull(db->pErr); |
| 82703 | 83306 | } |
| 82704 | 83307 | db->errCode = rc; |
| 83308 | + db->errByteOffset = -1; |
| 82705 | 83309 | return rc; |
| 82706 | 83310 | } |
| 82707 | 83311 | |
| 82708 | 83312 | #ifdef SQLITE_ENABLE_SQLLOG |
| 82709 | 83313 | /* |
| | @@ -82732,12 +83336,12 @@ |
| 82732 | 83336 | ** |
| 82733 | 83337 | ** After this routine is run, the VDBE should be ready to be executed |
| 82734 | 83338 | ** again. |
| 82735 | 83339 | ** |
| 82736 | 83340 | ** To look at it another way, this routine resets the state of the |
| 82737 | | -** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to |
| 82738 | | -** VDBE_MAGIC_INIT. |
| 83341 | +** virtual machine from VDBE_RUN_STATE or VDBE_HALT_STATE back to |
| 83342 | +** VDBE_READY_STATE. |
| 82739 | 83343 | */ |
| 82740 | 83344 | SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){ |
| 82741 | 83345 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 82742 | 83346 | int i; |
| 82743 | 83347 | #endif |
| | @@ -82762,16 +83366,10 @@ |
| 82762 | 83366 | sqlite3VdbeTransferError(p); |
| 82763 | 83367 | }else{ |
| 82764 | 83368 | db->errCode = p->rc; |
| 82765 | 83369 | } |
| 82766 | 83370 | if( p->runOnlyOnce ) p->expired = 1; |
| 82767 | | - }else if( p->rc && p->expired ){ |
| 82768 | | - /* The expired flag was set on the VDBE before the first call |
| 82769 | | - ** to sqlite3_step(). For consistency (since sqlite3_step() was |
| 82770 | | - ** called), set the database error in this case as well. |
| 82771 | | - */ |
| 82772 | | - sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); |
| 82773 | 83371 | } |
| 82774 | 83372 | |
| 82775 | 83373 | /* Reset register contents and reclaim error message memory. |
| 82776 | 83374 | */ |
| 82777 | 83375 | #ifdef SQLITE_DEBUG |
| | @@ -82824,21 +83422,23 @@ |
| 82824 | 83422 | } |
| 82825 | 83423 | fclose(out); |
| 82826 | 83424 | } |
| 82827 | 83425 | } |
| 82828 | 83426 | #endif |
| 82829 | | - p->iVdbeMagic = VDBE_MAGIC_RESET; |
| 82830 | 83427 | return p->rc & db->errMask; |
| 82831 | 83428 | } |
| 82832 | 83429 | |
| 82833 | 83430 | /* |
| 82834 | 83431 | ** Clean up and delete a VDBE after execution. Return an integer which is |
| 82835 | 83432 | ** the result code. Write any error message text into *pzErrMsg. |
| 82836 | 83433 | */ |
| 82837 | 83434 | SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){ |
| 82838 | 83435 | int rc = SQLITE_OK; |
| 82839 | | - if( p->iVdbeMagic==VDBE_MAGIC_RUN || p->iVdbeMagic==VDBE_MAGIC_HALT ){ |
| 83436 | + assert( VDBE_RUN_STATE>VDBE_READY_STATE ); |
| 83437 | + assert( VDBE_HALT_STATE>VDBE_READY_STATE ); |
| 83438 | + assert( VDBE_INIT_STATE<VDBE_READY_STATE ); |
| 83439 | + if( p->eVdbeState>=VDBE_READY_STATE ){ |
| 82840 | 83440 | rc = sqlite3VdbeReset(p); |
| 82841 | 83441 | assert( (rc & p->db->errMask)==rc ); |
| 82842 | 83442 | } |
| 82843 | 83443 | sqlite3VdbeDelete(p); |
| 82844 | 83444 | return rc; |
| | @@ -82889,23 +83489,25 @@ |
| 82889 | 83489 | ** the database connection and frees the object itself. |
| 82890 | 83490 | */ |
| 82891 | 83491 | SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ |
| 82892 | 83492 | SubProgram *pSub, *pNext; |
| 82893 | 83493 | assert( p->db==0 || p->db==db ); |
| 82894 | | - releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
| 83494 | + if( p->aColName ){ |
| 83495 | + releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
| 83496 | + sqlite3DbFreeNN(db, p->aColName); |
| 83497 | + } |
| 82895 | 83498 | for(pSub=p->pProgram; pSub; pSub=pNext){ |
| 82896 | 83499 | pNext = pSub->pNext; |
| 82897 | 83500 | vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); |
| 82898 | 83501 | sqlite3DbFree(db, pSub); |
| 82899 | 83502 | } |
| 82900 | | - if( p->iVdbeMagic!=VDBE_MAGIC_INIT ){ |
| 83503 | + if( p->eVdbeState!=VDBE_INIT_STATE ){ |
| 82901 | 83504 | releaseMemArray(p->aVar, p->nVar); |
| 82902 | | - sqlite3DbFree(db, p->pVList); |
| 82903 | | - sqlite3DbFree(db, p->pFree); |
| 83505 | + if( p->pVList ) sqlite3DbFreeNN(db, p->pVList); |
| 83506 | + if( p->pFree ) sqlite3DbFreeNN(db, p->pFree); |
| 82904 | 83507 | } |
| 82905 | 83508 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 82906 | | - sqlite3DbFree(db, p->aColName); |
| 82907 | 83509 | sqlite3DbFree(db, p->zSql); |
| 82908 | 83510 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 82909 | 83511 | sqlite3DbFree(db, p->zNormSql); |
| 82910 | 83512 | { |
| 82911 | 83513 | DblquoteStr *pThis, *pNext; |
| | @@ -82943,12 +83545,10 @@ |
| 82943 | 83545 | db->pVdbe = p->pNext; |
| 82944 | 83546 | } |
| 82945 | 83547 | if( p->pNext ){ |
| 82946 | 83548 | p->pNext->pPrev = p->pPrev; |
| 82947 | 83549 | } |
| 82948 | | - p->iVdbeMagic = VDBE_MAGIC_DEAD; |
| 82949 | | - p->db = 0; |
| 82950 | 83550 | sqlite3DbFreeNN(db, p); |
| 82951 | 83551 | } |
| 82952 | 83552 | |
| 82953 | 83553 | /* |
| 82954 | 83554 | ** The cursor "p" has a pending seek operation that has not yet been |
| | @@ -82979,11 +83579,11 @@ |
| 82979 | 83579 | ** pointed to was deleted out from under it. Or maybe the btree was |
| 82980 | 83580 | ** rebalanced. Whatever the cause, try to restore "p" to the place it |
| 82981 | 83581 | ** is supposed to be pointing. If the row was deleted out from under the |
| 82982 | 83582 | ** cursor, set the cursor to point to a NULL row. |
| 82983 | 83583 | */ |
| 82984 | | -static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){ |
| 83584 | +SQLITE_PRIVATE int SQLITE_NOINLINE sqlite3VdbeHandleMovedCursor(VdbeCursor *p){ |
| 82985 | 83585 | int isDifferentRow, rc; |
| 82986 | 83586 | assert( p->eCurType==CURTYPE_BTREE ); |
| 82987 | 83587 | assert( p->uc.pCursor!=0 ); |
| 82988 | 83588 | assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ); |
| 82989 | 83589 | rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow); |
| | @@ -82997,43 +83597,11 @@ |
| 82997 | 83597 | ** if need be. Return any I/O error from the restore operation. |
| 82998 | 83598 | */ |
| 82999 | 83599 | SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){ |
| 83000 | 83600 | assert( p->eCurType==CURTYPE_BTREE ); |
| 83001 | 83601 | if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ |
| 83002 | | - return handleMovedCursor(p); |
| 83003 | | - } |
| 83004 | | - return SQLITE_OK; |
| 83005 | | -} |
| 83006 | | - |
| 83007 | | -/* |
| 83008 | | -** Make sure the cursor p is ready to read or write the row to which it |
| 83009 | | -** was last positioned. Return an error code if an OOM fault or I/O error |
| 83010 | | -** prevents us from positioning the cursor to its correct position. |
| 83011 | | -** |
| 83012 | | -** If a MoveTo operation is pending on the given cursor, then do that |
| 83013 | | -** MoveTo now. If no move is pending, check to see if the row has been |
| 83014 | | -** deleted out from under the cursor and if it has, mark the row as |
| 83015 | | -** a NULL row. |
| 83016 | | -** |
| 83017 | | -** If the cursor is already pointing to the correct row and that row has |
| 83018 | | -** not been deleted out from under the cursor, then this routine is a no-op. |
| 83019 | | -*/ |
| 83020 | | -SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, u32 *piCol){ |
| 83021 | | - VdbeCursor *p = *pp; |
| 83022 | | - assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO ); |
| 83023 | | - if( p->deferredMoveto ){ |
| 83024 | | - u32 iMap; |
| 83025 | | - assert( !p->isEphemeral ); |
| 83026 | | - if( p->ub.aAltMap && (iMap = p->ub.aAltMap[1+*piCol])>0 && !p->nullRow ){ |
| 83027 | | - *pp = p->pAltCursor; |
| 83028 | | - *piCol = iMap - 1; |
| 83029 | | - return SQLITE_OK; |
| 83030 | | - } |
| 83031 | | - return sqlite3VdbeFinishMoveto(p); |
| 83032 | | - } |
| 83033 | | - if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ |
| 83034 | | - return handleMovedCursor(p); |
| 83602 | + return sqlite3VdbeHandleMovedCursor(p); |
| 83035 | 83603 | } |
| 83036 | 83604 | return SQLITE_OK; |
| 83037 | 83605 | } |
| 83038 | 83606 | |
| 83039 | 83607 | /* |
| | @@ -83691,12 +84259,12 @@ |
| 83691 | 84259 | if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT; |
| 83692 | 84260 | rc = 0; |
| 83693 | 84261 | }else{ |
| 83694 | 84262 | rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2); |
| 83695 | 84263 | } |
| 83696 | | - sqlite3VdbeMemRelease(&c1); |
| 83697 | | - sqlite3VdbeMemRelease(&c2); |
| 84264 | + sqlite3VdbeMemReleaseMalloc(&c1); |
| 84265 | + sqlite3VdbeMemReleaseMalloc(&c2); |
| 83698 | 84266 | return rc; |
| 83699 | 84267 | } |
| 83700 | 84268 | } |
| 83701 | 84269 | |
| 83702 | 84270 | /* |
| | @@ -84216,11 +84784,12 @@ |
| 84216 | 84784 | |
| 84217 | 84785 | default: |
| 84218 | 84786 | return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); |
| 84219 | 84787 | } |
| 84220 | 84788 | |
| 84221 | | - v = pPKey2->aMem[0].u.i; |
| 84789 | + assert( pPKey2->u.i == pPKey2->aMem[0].u.i ); |
| 84790 | + v = pPKey2->u.i; |
| 84222 | 84791 | if( v>lhs ){ |
| 84223 | 84792 | res = pPKey2->r1; |
| 84224 | 84793 | }else if( v<lhs ){ |
| 84225 | 84794 | res = pPKey2->r2; |
| 84226 | 84795 | }else if( pPKey2->nField>1 ){ |
| | @@ -84251,16 +84820,22 @@ |
| 84251 | 84820 | const u8 *aKey1 = (const u8*)pKey1; |
| 84252 | 84821 | int serial_type; |
| 84253 | 84822 | int res; |
| 84254 | 84823 | |
| 84255 | 84824 | assert( pPKey2->aMem[0].flags & MEM_Str ); |
| 84825 | + assert( pPKey2->aMem[0].n == pPKey2->n ); |
| 84826 | + assert( pPKey2->aMem[0].z == pPKey2->u.z ); |
| 84256 | 84827 | vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); |
| 84257 | | - serial_type = (u8)(aKey1[1]); |
| 84258 | | - if( serial_type >= 0x80 ){ |
| 84259 | | - sqlite3GetVarint32(&aKey1[1], (u32*)&serial_type); |
| 84260 | | - } |
| 84828 | + serial_type = (signed char)(aKey1[1]); |
| 84829 | + |
| 84830 | +vrcs_restart: |
| 84261 | 84831 | if( serial_type<12 ){ |
| 84832 | + if( serial_type<0 ){ |
| 84833 | + sqlite3GetVarint32(&aKey1[1], (u32*)&serial_type); |
| 84834 | + if( serial_type>=12 ) goto vrcs_restart; |
| 84835 | + assert( CORRUPT_DB ); |
| 84836 | + } |
| 84262 | 84837 | res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ |
| 84263 | 84838 | }else if( !(serial_type & 0x01) ){ |
| 84264 | 84839 | res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ |
| 84265 | 84840 | }else{ |
| 84266 | 84841 | int nCmp; |
| | @@ -84270,19 +84845,19 @@ |
| 84270 | 84845 | nStr = (serial_type-12) / 2; |
| 84271 | 84846 | if( (szHdr + nStr) > nKey1 ){ |
| 84272 | 84847 | pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
| 84273 | 84848 | return 0; /* Corruption */ |
| 84274 | 84849 | } |
| 84275 | | - nCmp = MIN( pPKey2->aMem[0].n, nStr ); |
| 84276 | | - res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); |
| 84850 | + nCmp = MIN( pPKey2->n, nStr ); |
| 84851 | + res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp); |
| 84277 | 84852 | |
| 84278 | 84853 | if( res>0 ){ |
| 84279 | 84854 | res = pPKey2->r2; |
| 84280 | 84855 | }else if( res<0 ){ |
| 84281 | 84856 | res = pPKey2->r1; |
| 84282 | 84857 | }else{ |
| 84283 | | - res = nStr - pPKey2->aMem[0].n; |
| 84858 | + res = nStr - pPKey2->n; |
| 84284 | 84859 | if( res==0 ){ |
| 84285 | 84860 | if( pPKey2->nField>1 ){ |
| 84286 | 84861 | res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
| 84287 | 84862 | }else{ |
| 84288 | 84863 | res = pPKey2->default_rc; |
| | @@ -84333,19 +84908,22 @@ |
| 84333 | 84908 | }else{ |
| 84334 | 84909 | p->r1 = -1; |
| 84335 | 84910 | p->r2 = 1; |
| 84336 | 84911 | } |
| 84337 | 84912 | if( (flags & MEM_Int) ){ |
| 84913 | + p->u.i = p->aMem[0].u.i; |
| 84338 | 84914 | return vdbeRecordCompareInt; |
| 84339 | 84915 | } |
| 84340 | 84916 | testcase( flags & MEM_Real ); |
| 84341 | 84917 | testcase( flags & MEM_Null ); |
| 84342 | 84918 | testcase( flags & MEM_Blob ); |
| 84343 | 84919 | if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0 |
| 84344 | 84920 | && p->pKeyInfo->aColl[0]==0 |
| 84345 | 84921 | ){ |
| 84346 | 84922 | assert( flags & MEM_Str ); |
| 84923 | + p->u.z = p->aMem[0].z; |
| 84924 | + p->n = p->aMem[0].n; |
| 84347 | 84925 | return vdbeRecordCompareString; |
| 84348 | 84926 | } |
| 84349 | 84927 | } |
| 84350 | 84928 | |
| 84351 | 84929 | return sqlite3VdbeRecordCompare; |
| | @@ -84414,18 +84992,18 @@ |
| 84414 | 84992 | } |
| 84415 | 84993 | |
| 84416 | 84994 | /* Fetch the integer off the end of the index record */ |
| 84417 | 84995 | sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); |
| 84418 | 84996 | *rowid = v.u.i; |
| 84419 | | - sqlite3VdbeMemRelease(&m); |
| 84997 | + sqlite3VdbeMemReleaseMalloc(&m); |
| 84420 | 84998 | return SQLITE_OK; |
| 84421 | 84999 | |
| 84422 | 85000 | /* Jump here if database corruption is detected after m has been |
| 84423 | 85001 | ** allocated. Free the m object and return SQLITE_CORRUPT. */ |
| 84424 | 85002 | idx_rowid_corruption: |
| 84425 | 85003 | testcase( m.szMalloc!=0 ); |
| 84426 | | - sqlite3VdbeMemRelease(&m); |
| 85004 | + sqlite3VdbeMemReleaseMalloc(&m); |
| 84427 | 85005 | return SQLITE_CORRUPT_BKPT; |
| 84428 | 85006 | } |
| 84429 | 85007 | |
| 84430 | 85008 | /* |
| 84431 | 85009 | ** Compare the key of the index entry that cursor pC is pointing to against |
| | @@ -84463,11 +85041,11 @@ |
| 84463 | 85041 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m); |
| 84464 | 85042 | if( rc ){ |
| 84465 | 85043 | return rc; |
| 84466 | 85044 | } |
| 84467 | 85045 | *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0); |
| 84468 | | - sqlite3VdbeMemRelease(&m); |
| 85046 | + sqlite3VdbeMemReleaseMalloc(&m); |
| 84469 | 85047 | return SQLITE_OK; |
| 84470 | 85048 | } |
| 84471 | 85049 | |
| 84472 | 85050 | /* |
| 84473 | 85051 | ** This routine sets the value to be returned by subsequent calls to |
| | @@ -84630,11 +85208,11 @@ |
| 84630 | 85208 | static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){ |
| 84631 | 85209 | if( p ){ |
| 84632 | 85210 | int i; |
| 84633 | 85211 | for(i=0; i<nField; i++){ |
| 84634 | 85212 | Mem *pMem = &p->aMem[i]; |
| 84635 | | - if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem); |
| 85213 | + if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem); |
| 84636 | 85214 | } |
| 84637 | 85215 | sqlite3DbFreeNN(db, p); |
| 84638 | 85216 | } |
| 84639 | 85217 | } |
| 84640 | 85218 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
| | @@ -85057,10 +85635,13 @@ |
| 85057 | 85635 | pNew->flags |= MEM_Ephem; |
| 85058 | 85636 | if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ |
| 85059 | 85637 | sqlite3ValueFree(pNew); |
| 85060 | 85638 | pNew = 0; |
| 85061 | 85639 | } |
| 85640 | + }else if( pNew->flags & MEM_Null ){ |
| 85641 | + /* Do not duplicate pointer values */ |
| 85642 | + pNew->flags &= ~(MEM_Term|MEM_Subtype); |
| 85062 | 85643 | } |
| 85063 | 85644 | return pNew; |
| 85064 | 85645 | } |
| 85065 | 85646 | |
| 85066 | 85647 | /* Destroy an sqlite3_value object previously obtained from |
| | @@ -85087,20 +85668,26 @@ |
| 85087 | 85668 | const char *z, /* String pointer */ |
| 85088 | 85669 | int n, /* Bytes in string, or negative */ |
| 85089 | 85670 | u8 enc, /* Encoding of z. 0 for BLOBs */ |
| 85090 | 85671 | void (*xDel)(void*) /* Destructor function */ |
| 85091 | 85672 | ){ |
| 85092 | | - int rc = sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel); |
| 85673 | + Mem *pOut = pCtx->pOut; |
| 85674 | + int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel); |
| 85093 | 85675 | if( rc ){ |
| 85094 | 85676 | if( rc==SQLITE_TOOBIG ){ |
| 85095 | 85677 | sqlite3_result_error_toobig(pCtx); |
| 85096 | 85678 | }else{ |
| 85097 | 85679 | /* The only errors possible from sqlite3VdbeMemSetStr are |
| 85098 | 85680 | ** SQLITE_TOOBIG and SQLITE_NOMEM */ |
| 85099 | 85681 | assert( rc==SQLITE_NOMEM ); |
| 85100 | 85682 | sqlite3_result_error_nomem(pCtx); |
| 85101 | 85683 | } |
| 85684 | + return; |
| 85685 | + } |
| 85686 | + sqlite3VdbeChangeEncoding(pOut, pCtx->enc); |
| 85687 | + if( sqlite3VdbeMemTooBig(pOut) ){ |
| 85688 | + sqlite3_result_error_toobig(pCtx); |
| 85102 | 85689 | } |
| 85103 | 85690 | } |
| 85104 | 85691 | static int invokeValueDestructor( |
| 85105 | 85692 | const void *p, /* Value to destroy */ |
| 85106 | 85693 | void (*xDel)(void*), /* The destructor */ |
| | @@ -85240,21 +85827,26 @@ |
| 85240 | 85827 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 85241 | 85828 | setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); |
| 85242 | 85829 | } |
| 85243 | 85830 | #endif /* SQLITE_OMIT_UTF16 */ |
| 85244 | 85831 | SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ |
| 85832 | + Mem *pOut = pCtx->pOut; |
| 85245 | 85833 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 85246 | | - sqlite3VdbeMemCopy(pCtx->pOut, pValue); |
| 85834 | + sqlite3VdbeMemCopy(pOut, pValue); |
| 85835 | + sqlite3VdbeChangeEncoding(pOut, pCtx->enc); |
| 85836 | + if( sqlite3VdbeMemTooBig(pOut) ){ |
| 85837 | + sqlite3_result_error_toobig(pCtx); |
| 85838 | + } |
| 85247 | 85839 | } |
| 85248 | 85840 | SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ |
| 85249 | | - assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 85250 | | - sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n); |
| 85841 | + sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0); |
| 85251 | 85842 | } |
| 85252 | 85843 | SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ |
| 85253 | 85844 | Mem *pOut = pCtx->pOut; |
| 85254 | 85845 | assert( sqlite3_mutex_held(pOut->db->mutex) ); |
| 85255 | 85846 | if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 85847 | + sqlite3_result_error_toobig(pCtx); |
| 85256 | 85848 | return SQLITE_TOOBIG; |
| 85257 | 85849 | } |
| 85258 | 85850 | #ifndef SQLITE_OMIT_INCRBLOB |
| 85259 | 85851 | sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); |
| 85260 | 85852 | return SQLITE_OK; |
| | @@ -85266,12 +85858,12 @@ |
| 85266 | 85858 | pCtx->isError = errCode ? errCode : -1; |
| 85267 | 85859 | #ifdef SQLITE_DEBUG |
| 85268 | 85860 | if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; |
| 85269 | 85861 | #endif |
| 85270 | 85862 | if( pCtx->pOut->flags & MEM_Null ){ |
| 85271 | | - sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, |
| 85272 | | - SQLITE_UTF8, SQLITE_STATIC); |
| 85863 | + setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8, |
| 85864 | + SQLITE_STATIC); |
| 85273 | 85865 | } |
| 85274 | 85866 | } |
| 85275 | 85867 | |
| 85276 | 85868 | /* Force an SQLITE_TOOBIG error. */ |
| 85277 | 85869 | SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){ |
| | @@ -85340,85 +85932,94 @@ |
| 85340 | 85932 | */ |
| 85341 | 85933 | static int sqlite3Step(Vdbe *p){ |
| 85342 | 85934 | sqlite3 *db; |
| 85343 | 85935 | int rc; |
| 85344 | 85936 | |
| 85345 | | - assert(p); |
| 85346 | | - if( p->iVdbeMagic!=VDBE_MAGIC_RUN ){ |
| 85347 | | - /* We used to require that sqlite3_reset() be called before retrying |
| 85348 | | - ** sqlite3_step() after any error or after SQLITE_DONE. But beginning |
| 85349 | | - ** with version 3.7.0, we changed this so that sqlite3_reset() would |
| 85350 | | - ** be called automatically instead of throwing the SQLITE_MISUSE error. |
| 85351 | | - ** This "automatic-reset" change is not technically an incompatibility, |
| 85352 | | - ** since any application that receives an SQLITE_MISUSE is broken by |
| 85353 | | - ** definition. |
| 85354 | | - ** |
| 85355 | | - ** Nevertheless, some published applications that were originally written |
| 85356 | | - ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE |
| 85357 | | - ** returns, and those were broken by the automatic-reset change. As a |
| 85358 | | - ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the |
| 85359 | | - ** legacy behavior of returning SQLITE_MISUSE for cases where the |
| 85360 | | - ** previous sqlite3_step() returned something other than a SQLITE_LOCKED |
| 85361 | | - ** or SQLITE_BUSY error. |
| 85362 | | - */ |
| 85363 | | -#ifdef SQLITE_OMIT_AUTORESET |
| 85364 | | - if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
| 85365 | | - sqlite3_reset((sqlite3_stmt*)p); |
| 85366 | | - }else{ |
| 85367 | | - return SQLITE_MISUSE_BKPT; |
| 85368 | | - } |
| 85369 | | -#else |
| 85370 | | - sqlite3_reset((sqlite3_stmt*)p); |
| 85371 | | -#endif |
| 85372 | | - } |
| 85373 | | - |
| 85374 | 85937 | /* Check that malloc() has not failed. If it has, return early. */ |
| 85375 | 85938 | db = p->db; |
| 85376 | 85939 | if( db->mallocFailed ){ |
| 85377 | 85940 | p->rc = SQLITE_NOMEM; |
| 85378 | 85941 | return SQLITE_NOMEM_BKPT; |
| 85379 | 85942 | } |
| 85380 | 85943 | |
| 85381 | | - if( p->pc<0 && p->expired ){ |
| 85382 | | - p->rc = SQLITE_SCHEMA; |
| 85383 | | - rc = SQLITE_ERROR; |
| 85384 | | - if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ |
| 85385 | | - /* If this statement was prepared using saved SQL and an |
| 85386 | | - ** error has occurred, then return the error code in p->rc to the |
| 85387 | | - ** caller. Set the error code in the database handle to the same value. |
| 85388 | | - */ |
| 85389 | | - rc = sqlite3VdbeTransferError(p); |
| 85390 | | - } |
| 85391 | | - goto end_of_step; |
| 85392 | | - } |
| 85393 | | - if( p->pc<0 ){ |
| 85394 | | - /* If there are no other statements currently running, then |
| 85395 | | - ** reset the interrupt flag. This prevents a call to sqlite3_interrupt |
| 85396 | | - ** from interrupting a statement that has not yet started. |
| 85397 | | - */ |
| 85398 | | - if( db->nVdbeActive==0 ){ |
| 85399 | | - AtomicStore(&db->u1.isInterrupted, 0); |
| 85400 | | - } |
| 85401 | | - |
| 85402 | | - assert( db->nVdbeWrite>0 || db->autoCommit==0 |
| 85403 | | - || (db->nDeferredCons==0 && db->nDeferredImmCons==0) |
| 85404 | | - ); |
| 85944 | + assert(p); |
| 85945 | + if( p->eVdbeState!=VDBE_RUN_STATE ){ |
| 85946 | + restart_step: |
| 85947 | + if( p->eVdbeState==VDBE_READY_STATE ){ |
| 85948 | + if( p->expired ){ |
| 85949 | + p->rc = SQLITE_SCHEMA; |
| 85950 | + rc = SQLITE_ERROR; |
| 85951 | + if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ |
| 85952 | + /* If this statement was prepared using saved SQL and an |
| 85953 | + ** error has occurred, then return the error code in p->rc to the |
| 85954 | + ** caller. Set the error code in the database handle to the same |
| 85955 | + ** value. |
| 85956 | + */ |
| 85957 | + rc = sqlite3VdbeTransferError(p); |
| 85958 | + } |
| 85959 | + goto end_of_step; |
| 85960 | + } |
| 85961 | + |
| 85962 | + /* If there are no other statements currently running, then |
| 85963 | + ** reset the interrupt flag. This prevents a call to sqlite3_interrupt |
| 85964 | + ** from interrupting a statement that has not yet started. |
| 85965 | + */ |
| 85966 | + if( db->nVdbeActive==0 ){ |
| 85967 | + AtomicStore(&db->u1.isInterrupted, 0); |
| 85968 | + } |
| 85969 | + |
| 85970 | + assert( db->nVdbeWrite>0 || db->autoCommit==0 |
| 85971 | + || (db->nDeferredCons==0 && db->nDeferredImmCons==0) |
| 85972 | + ); |
| 85405 | 85973 | |
| 85406 | 85974 | #ifndef SQLITE_OMIT_TRACE |
| 85407 | | - if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 |
| 85408 | | - && !db->init.busy && p->zSql ){ |
| 85409 | | - sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); |
| 85410 | | - }else{ |
| 85411 | | - assert( p->startTime==0 ); |
| 85412 | | - } |
| 85975 | + if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 |
| 85976 | + && !db->init.busy && p->zSql ){ |
| 85977 | + sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); |
| 85978 | + }else{ |
| 85979 | + assert( p->startTime==0 ); |
| 85980 | + } |
| 85413 | 85981 | #endif |
| 85414 | 85982 | |
| 85415 | | - db->nVdbeActive++; |
| 85416 | | - if( p->readOnly==0 ) db->nVdbeWrite++; |
| 85417 | | - if( p->bIsReader ) db->nVdbeRead++; |
| 85418 | | - p->pc = 0; |
| 85983 | + db->nVdbeActive++; |
| 85984 | + if( p->readOnly==0 ) db->nVdbeWrite++; |
| 85985 | + if( p->bIsReader ) db->nVdbeRead++; |
| 85986 | + p->pc = 0; |
| 85987 | + p->eVdbeState = VDBE_RUN_STATE; |
| 85988 | + }else |
| 85989 | + |
| 85990 | + if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){ |
| 85991 | + /* We used to require that sqlite3_reset() be called before retrying |
| 85992 | + ** sqlite3_step() after any error or after SQLITE_DONE. But beginning |
| 85993 | + ** with version 3.7.0, we changed this so that sqlite3_reset() would |
| 85994 | + ** be called automatically instead of throwing the SQLITE_MISUSE error. |
| 85995 | + ** This "automatic-reset" change is not technically an incompatibility, |
| 85996 | + ** since any application that receives an SQLITE_MISUSE is broken by |
| 85997 | + ** definition. |
| 85998 | + ** |
| 85999 | + ** Nevertheless, some published applications that were originally written |
| 86000 | + ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE |
| 86001 | + ** returns, and those were broken by the automatic-reset change. As a |
| 86002 | + ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the |
| 86003 | + ** legacy behavior of returning SQLITE_MISUSE for cases where the |
| 86004 | + ** previous sqlite3_step() returned something other than a SQLITE_LOCKED |
| 86005 | + ** or SQLITE_BUSY error. |
| 86006 | + */ |
| 86007 | +#ifdef SQLITE_OMIT_AUTORESET |
| 86008 | + if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
| 86009 | + sqlite3_reset((sqlite3_stmt*)p); |
| 86010 | + }else{ |
| 86011 | + return SQLITE_MISUSE_BKPT; |
| 86012 | + } |
| 86013 | +#else |
| 86014 | + sqlite3_reset((sqlite3_stmt*)p); |
| 86015 | +#endif |
| 86016 | + assert( p->eVdbeState==VDBE_READY_STATE ); |
| 86017 | + goto restart_step; |
| 86018 | + } |
| 85419 | 86019 | } |
| 86020 | + |
| 85420 | 86021 | #ifdef SQLITE_DEBUG |
| 85421 | 86022 | p->rcApp = SQLITE_OK; |
| 85422 | 86023 | #endif |
| 85423 | 86024 | #ifndef SQLITE_OMIT_EXPLAIN |
| 85424 | 86025 | if( p->explain ){ |
| | @@ -85429,11 +86030,16 @@ |
| 85429 | 86030 | db->nVdbeExec++; |
| 85430 | 86031 | rc = sqlite3VdbeExec(p); |
| 85431 | 86032 | db->nVdbeExec--; |
| 85432 | 86033 | } |
| 85433 | 86034 | |
| 85434 | | - if( rc!=SQLITE_ROW ){ |
| 86035 | + if( rc==SQLITE_ROW ){ |
| 86036 | + assert( p->rc==SQLITE_OK ); |
| 86037 | + assert( db->mallocFailed==0 ); |
| 86038 | + db->errCode = SQLITE_ROW; |
| 86039 | + return SQLITE_ROW; |
| 86040 | + }else{ |
| 85435 | 86041 | #ifndef SQLITE_OMIT_TRACE |
| 85436 | 86042 | /* If the statement completed successfully, invoke the profile callback */ |
| 85437 | 86043 | checkProfileCallback(db, p); |
| 85438 | 86044 | #endif |
| 85439 | 86045 | |
| | @@ -85557,10 +86163,74 @@ |
| 85557 | 86163 | */ |
| 85558 | 86164 | SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){ |
| 85559 | 86165 | assert( p ); |
| 85560 | 86166 | return sqlite3_value_nochange(p->pOut); |
| 85561 | 86167 | } |
| 86168 | + |
| 86169 | +/* |
| 86170 | +** Implementation of sqlite3_vtab_in_first() (if bNext==0) and |
| 86171 | +** sqlite3_vtab_in_next() (if bNext!=0). |
| 86172 | +*/ |
| 86173 | +static int valueFromValueList( |
| 86174 | + sqlite3_value *pVal, /* Pointer to the ValueList object */ |
| 86175 | + sqlite3_value **ppOut, /* Store the next value from the list here */ |
| 86176 | + int bNext /* 1 for _next(). 0 for _first() */ |
| 86177 | +){ |
| 86178 | + int rc; |
| 86179 | + ValueList *pRhs; |
| 86180 | + |
| 86181 | + *ppOut = 0; |
| 86182 | + if( pVal==0 ) return SQLITE_MISUSE; |
| 86183 | + pRhs = (ValueList*)sqlite3_value_pointer(pVal, "ValueList"); |
| 86184 | + if( pRhs==0 ) return SQLITE_MISUSE; |
| 86185 | + if( bNext ){ |
| 86186 | + rc = sqlite3BtreeNext(pRhs->pCsr, 0); |
| 86187 | + }else{ |
| 86188 | + int dummy = 0; |
| 86189 | + rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy); |
| 86190 | + assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) ); |
| 86191 | + if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE; |
| 86192 | + } |
| 86193 | + if( rc==SQLITE_OK ){ |
| 86194 | + u32 sz; /* Size of current row in bytes */ |
| 86195 | + Mem sMem; /* Raw content of current row */ |
| 86196 | + memset(&sMem, 0, sizeof(sMem)); |
| 86197 | + sz = sqlite3BtreePayloadSize(pRhs->pCsr); |
| 86198 | + rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem); |
| 86199 | + if( rc==SQLITE_OK ){ |
| 86200 | + u8 *zBuf = (u8*)sMem.z; |
| 86201 | + u32 iSerial; |
| 86202 | + sqlite3_value *pOut = pRhs->pOut; |
| 86203 | + int iOff = 1 + getVarint32(&zBuf[1], iSerial); |
| 86204 | + sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut); |
| 86205 | + pOut->enc = ENC(pOut->db); |
| 86206 | + if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){ |
| 86207 | + rc = SQLITE_NOMEM; |
| 86208 | + }else{ |
| 86209 | + *ppOut = pOut; |
| 86210 | + } |
| 86211 | + } |
| 86212 | + sqlite3VdbeMemRelease(&sMem); |
| 86213 | + } |
| 86214 | + return rc; |
| 86215 | +} |
| 86216 | + |
| 86217 | +/* |
| 86218 | +** Set the iterator value pVal to point to the first value in the set. |
| 86219 | +** Set (*ppOut) to point to this value before returning. |
| 86220 | +*/ |
| 86221 | +SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){ |
| 86222 | + return valueFromValueList(pVal, ppOut, 0); |
| 86223 | +} |
| 86224 | + |
| 86225 | +/* |
| 86226 | +** Set the iterator value pVal to point to the next value in the set. |
| 86227 | +** Set (*ppOut) to point to this value before returning. |
| 86228 | +*/ |
| 86229 | +SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){ |
| 86230 | + return valueFromValueList(pVal, ppOut, 1); |
| 86231 | +} |
| 85562 | 86232 | |
| 85563 | 86233 | /* |
| 85564 | 86234 | ** Return the current time for a statement. If the current time |
| 85565 | 86235 | ** is requested more than once within the same run of a single prepared |
| 85566 | 86236 | ** statement, the exact same time is returned for each invocation regardless |
| | @@ -85752,19 +86422,19 @@ |
| 85752 | 86422 | #if defined(SQLITE_DEBUG) && defined(__GNUC__) |
| 85753 | 86423 | __attribute__((aligned(8))) |
| 85754 | 86424 | #endif |
| 85755 | 86425 | = { |
| 85756 | 86426 | /* .u = */ {0}, |
| 86427 | + /* .z = */ (char*)0, |
| 86428 | + /* .n = */ (int)0, |
| 85757 | 86429 | /* .flags = */ (u16)MEM_Null, |
| 85758 | 86430 | /* .enc = */ (u8)0, |
| 85759 | 86431 | /* .eSubtype = */ (u8)0, |
| 85760 | | - /* .n = */ (int)0, |
| 85761 | | - /* .z = */ (char*)0, |
| 85762 | | - /* .zMalloc = */ (char*)0, |
| 86432 | + /* .db = */ (sqlite3*)0, |
| 85763 | 86433 | /* .szMalloc = */ (int)0, |
| 85764 | 86434 | /* .uTemp = */ (u32)0, |
| 85765 | | - /* .db = */ (sqlite3*)0, |
| 86435 | + /* .zMalloc = */ (char*)0, |
| 85766 | 86436 | /* .xDel = */ (void(*)(void*))0, |
| 85767 | 86437 | #ifdef SQLITE_DEBUG |
| 85768 | 86438 | /* .pScopyFrom = */ (Mem*)0, |
| 85769 | 86439 | /* .mScopyFlags= */ 0, |
| 85770 | 86440 | #endif |
| | @@ -86057,11 +86727,11 @@ |
| 86057 | 86727 | Mem *pVar; |
| 86058 | 86728 | if( vdbeSafetyNotNull(p) ){ |
| 86059 | 86729 | return SQLITE_MISUSE_BKPT; |
| 86060 | 86730 | } |
| 86061 | 86731 | sqlite3_mutex_enter(p->db->mutex); |
| 86062 | | - if( p->iVdbeMagic!=VDBE_MAGIC_RUN || p->pc>=0 ){ |
| 86732 | + if( p->eVdbeState!=VDBE_READY_STATE ){ |
| 86063 | 86733 | sqlite3Error(p->db, SQLITE_MISUSE); |
| 86064 | 86734 | sqlite3_mutex_leave(p->db->mutex); |
| 86065 | 86735 | sqlite3_log(SQLITE_MISUSE, |
| 86066 | 86736 | "bind on a busy prepared statement: [%s]", p->zSql); |
| 86067 | 86737 | return SQLITE_MISUSE_BKPT; |
| | @@ -86242,11 +86912,14 @@ |
| 86242 | 86912 | case SQLITE_INTEGER: { |
| 86243 | 86913 | rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); |
| 86244 | 86914 | break; |
| 86245 | 86915 | } |
| 86246 | 86916 | case SQLITE_FLOAT: { |
| 86247 | | - rc = sqlite3_bind_double(pStmt, i, pValue->u.r); |
| 86917 | + assert( pValue->flags & (MEM_Real|MEM_IntReal) ); |
| 86918 | + rc = sqlite3_bind_double(pStmt, i, |
| 86919 | + (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i |
| 86920 | + ); |
| 86248 | 86921 | break; |
| 86249 | 86922 | } |
| 86250 | 86923 | case SQLITE_BLOB: { |
| 86251 | 86924 | if( pValue->flags & MEM_Zero ){ |
| 86252 | 86925 | rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); |
| | @@ -86407,11 +87080,11 @@ |
| 86407 | 87080 | /* |
| 86408 | 87081 | ** Return true if the prepared statement is in need of being reset. |
| 86409 | 87082 | */ |
| 86410 | 87083 | SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ |
| 86411 | 87084 | Vdbe *v = (Vdbe*)pStmt; |
| 86412 | | - return v!=0 && v->iVdbeMagic==VDBE_MAGIC_RUN && v->pc>=0; |
| 87085 | + return v!=0 && v->eVdbeState==VDBE_RUN_STATE; |
| 86413 | 87086 | } |
| 86414 | 87087 | |
| 86415 | 87088 | /* |
| 86416 | 87089 | ** Return a pointer to the next prepared statement after pStmt associated |
| 86417 | 87090 | ** with database connection pDb. If pStmt is NULL, return the first |
| | @@ -87721,11 +88394,11 @@ |
| 87721 | 88394 | #ifdef VDBE_PROFILE |
| 87722 | 88395 | u64 start; /* CPU clock count at start of opcode */ |
| 87723 | 88396 | #endif |
| 87724 | 88397 | /*** INSERT STACK UNION HERE ***/ |
| 87725 | 88398 | |
| 87726 | | - assert( p->iVdbeMagic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
| 88399 | + assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */ |
| 87727 | 88400 | sqlite3VdbeEnter(p); |
| 87728 | 88401 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 87729 | 88402 | if( db->xProgress ){ |
| 87730 | 88403 | u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; |
| 87731 | 88404 | assert( 0 < db->nProgressOps ); |
| | @@ -87972,14 +88645,18 @@ |
| 87972 | 88645 | jump_to_p2: |
| 87973 | 88646 | pOp = &aOp[pOp->p2 - 1]; |
| 87974 | 88647 | break; |
| 87975 | 88648 | } |
| 87976 | 88649 | |
| 87977 | | -/* Opcode: Return P1 * * * * |
| 88650 | +/* Opcode: Return P1 * P3 * * |
| 87978 | 88651 | ** |
| 87979 | 88652 | ** Jump to the next instruction after the address in register P1. After |
| 87980 | 88653 | ** the jump, register P1 becomes undefined. |
| 88654 | +** |
| 88655 | +** P3 is not used by the byte-code engine. However, the code generator |
| 88656 | +** sets P3 to address of the associated OP_BeginSubrtn opcode, if there is |
| 88657 | +** one. |
| 87981 | 88658 | */ |
| 87982 | 88659 | case OP_Return: { /* in1 */ |
| 87983 | 88660 | pIn1 = &aMem[pOp->p1]; |
| 87984 | 88661 | assert( pIn1->flags==MEM_Int ); |
| 87985 | 88662 | pOp = &aOp[pIn1->u.i]; |
| | @@ -88163,15 +88840,26 @@ |
| 88163 | 88840 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
| 88164 | 88841 | } |
| 88165 | 88842 | goto vdbe_return; |
| 88166 | 88843 | } |
| 88167 | 88844 | |
| 88845 | +/* Opcode: BeginSubrtn P1 P2 * * * |
| 88846 | +** Synopsis: r[P2]=P1 |
| 88847 | +** |
| 88848 | +** Mark the beginning of a subroutine by loading the integer value P1 |
| 88849 | +** into register r[P2]. The P2 register is used to store the return |
| 88850 | +** address of the subroutine call. |
| 88851 | +** |
| 88852 | +** This opcode is identical to OP_Integer. It has a different name |
| 88853 | +** only to make the byte code easier to read and verify. |
| 88854 | +*/ |
| 88168 | 88855 | /* Opcode: Integer P1 P2 * * * |
| 88169 | 88856 | ** Synopsis: r[P2]=P1 |
| 88170 | 88857 | ** |
| 88171 | 88858 | ** The 32-bit integer value P1 is written into register P2. |
| 88172 | 88859 | */ |
| 88860 | +case OP_BeginSubrtn: |
| 88173 | 88861 | case OP_Integer: { /* out2 */ |
| 88174 | 88862 | pOut = out2Prerelease(p, pOp); |
| 88175 | 88863 | pOut->u.i = pOp->p1; |
| 88176 | 88864 | break; |
| 88177 | 88865 | } |
| | @@ -89608,14 +90296,22 @@ |
| 89608 | 90296 | case OP_Offset: { /* out3 */ |
| 89609 | 90297 | VdbeCursor *pC; /* The VDBE cursor */ |
| 89610 | 90298 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 89611 | 90299 | pC = p->apCsr[pOp->p1]; |
| 89612 | 90300 | pOut = &p->aMem[pOp->p3]; |
| 89613 | | - if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){ |
| 90301 | + if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){ |
| 89614 | 90302 | sqlite3VdbeMemSetNull(pOut); |
| 89615 | 90303 | }else{ |
| 89616 | | - sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); |
| 90304 | + if( pC->deferredMoveto ){ |
| 90305 | + rc = sqlite3VdbeFinishMoveto(pC); |
| 90306 | + if( rc ) goto abort_due_to_error; |
| 90307 | + } |
| 90308 | + if( sqlite3BtreeEof(pC->uc.pCursor) ){ |
| 90309 | + sqlite3VdbeMemSetNull(pOut); |
| 90310 | + }else{ |
| 90311 | + sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); |
| 90312 | + } |
| 89617 | 90313 | } |
| 89618 | 90314 | break; |
| 89619 | 90315 | } |
| 89620 | 90316 | #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ |
| 89621 | 90317 | |
| | @@ -89640,11 +90336,11 @@ |
| 89640 | 90336 | ** skipped for length() and all content loading can be skipped for typeof(). |
| 89641 | 90337 | */ |
| 89642 | 90338 | case OP_Column: { |
| 89643 | 90339 | u32 p2; /* column number to retrieve */ |
| 89644 | 90340 | VdbeCursor *pC; /* The VDBE cursor */ |
| 89645 | | - BtCursor *pCrsr; /* The BTree cursor */ |
| 90341 | + BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */ |
| 89646 | 90342 | u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ |
| 89647 | 90343 | int len; /* The length of the serialized data for the column */ |
| 89648 | 90344 | int i; /* Loop counter */ |
| 89649 | 90345 | Mem *pDest; /* Where to write the extracted value */ |
| 89650 | 90346 | Mem sMem; /* For storing the record being decoded */ |
| | @@ -89654,23 +90350,15 @@ |
| 89654 | 90350 | u64 offset64; /* 64-bit offset */ |
| 89655 | 90351 | u32 t; /* A type code from the record header */ |
| 89656 | 90352 | Mem *pReg; /* PseudoTable input register */ |
| 89657 | 90353 | |
| 89658 | 90354 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 90355 | + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 89659 | 90356 | pC = p->apCsr[pOp->p1]; |
| 89660 | | - assert( pC!=0 ); |
| 89661 | 90357 | p2 = (u32)pOp->p2; |
| 89662 | 90358 | |
| 89663 | | - /* If the cursor cache is stale (meaning it is not currently point at |
| 89664 | | - ** the correct row) then bring it up-to-date by doing the necessary |
| 89665 | | - ** B-Tree seek. */ |
| 89666 | | - rc = sqlite3VdbeCursorMoveto(&pC, &p2); |
| 89667 | | - if( rc ) goto abort_due_to_error; |
| 89668 | | - |
| 89669 | | - assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 89670 | | - pDest = &aMem[pOp->p3]; |
| 89671 | | - memAboutToChange(p, pDest); |
| 90359 | +op_column_restart: |
| 89672 | 90360 | assert( pC!=0 ); |
| 89673 | 90361 | assert( p2<(u32)pC->nField ); |
| 89674 | 90362 | aOffset = pC->aOffset; |
| 89675 | 90363 | assert( aOffset==pC->aType+pC->nField ); |
| 89676 | 90364 | assert( pC->eCurType!=CURTYPE_VTAB ); |
| | @@ -89687,30 +90375,43 @@ |
| 89687 | 90375 | assert( pReg->flags & MEM_Blob ); |
| 89688 | 90376 | assert( memIsValid(pReg) ); |
| 89689 | 90377 | pC->payloadSize = pC->szRow = pReg->n; |
| 89690 | 90378 | pC->aRow = (u8*)pReg->z; |
| 89691 | 90379 | }else{ |
| 90380 | + pDest = &aMem[pOp->p3]; |
| 90381 | + memAboutToChange(p, pDest); |
| 89692 | 90382 | sqlite3VdbeMemSetNull(pDest); |
| 89693 | 90383 | goto op_column_out; |
| 89694 | 90384 | } |
| 89695 | 90385 | }else{ |
| 89696 | 90386 | pCrsr = pC->uc.pCursor; |
| 90387 | + if( pC->deferredMoveto ){ |
| 90388 | + u32 iMap; |
| 90389 | + assert( !pC->isEphemeral ); |
| 90390 | + if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){ |
| 90391 | + pC = pC->pAltCursor; |
| 90392 | + p2 = iMap - 1; |
| 90393 | + goto op_column_restart; |
| 90394 | + } |
| 90395 | + rc = sqlite3VdbeFinishMoveto(pC); |
| 90396 | + if( rc ) goto abort_due_to_error; |
| 90397 | + }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){ |
| 90398 | + rc = sqlite3VdbeHandleMovedCursor(pC); |
| 90399 | + if( rc ) goto abort_due_to_error; |
| 90400 | + goto op_column_restart; |
| 90401 | + } |
| 89697 | 90402 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 89698 | 90403 | assert( pCrsr ); |
| 89699 | 90404 | assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 89700 | 90405 | pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); |
| 89701 | 90406 | pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow); |
| 89702 | 90407 | assert( pC->szRow<=pC->payloadSize ); |
| 89703 | 90408 | assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */ |
| 89704 | | - if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 89705 | | - goto too_big; |
| 89706 | | - } |
| 89707 | 90409 | } |
| 89708 | 90410 | pC->cacheStatus = p->cacheCtr; |
| 89709 | 90411 | pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]); |
| 89710 | 90412 | pC->nHdrParsed = 0; |
| 89711 | | - |
| 89712 | 90413 | |
| 89713 | 90414 | if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/ |
| 89714 | 90415 | /* pC->aRow does not have to hold the entire row, but it does at least |
| 89715 | 90416 | ** need to cover the header of the record. If pC->aRow does not contain |
| 89716 | 90417 | ** the complete header, then set it to zero, forcing the header to be |
| | @@ -89747,10 +90448,14 @@ |
| 89747 | 90448 | zData = pC->aRow; |
| 89748 | 90449 | assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ |
| 89749 | 90450 | testcase( aOffset[0]==0 ); |
| 89750 | 90451 | goto op_column_read_header; |
| 89751 | 90452 | } |
| 90453 | + }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){ |
| 90454 | + rc = sqlite3VdbeHandleMovedCursor(pC); |
| 90455 | + if( rc ) goto abort_due_to_error; |
| 90456 | + goto op_column_restart; |
| 89752 | 90457 | } |
| 89753 | 90458 | |
| 89754 | 90459 | /* Make sure at least the first p2+1 entries of the header have been |
| 89755 | 90460 | ** parsed and valid information is in aOffset[] and pC->aType[]. |
| 89756 | 90461 | */ |
| | @@ -89815,10 +90520,12 @@ |
| 89815 | 90520 | /* If after trying to extract new entries from the header, nHdrParsed is |
| 89816 | 90521 | ** still not up to p2, that means that the record has fewer than p2 |
| 89817 | 90522 | ** columns. So the result will be either the default value or a NULL. |
| 89818 | 90523 | */ |
| 89819 | 90524 | if( pC->nHdrParsed<=p2 ){ |
| 90525 | + pDest = &aMem[pOp->p3]; |
| 90526 | + memAboutToChange(p, pDest); |
| 89820 | 90527 | if( pOp->p4type==P4_MEM ){ |
| 89821 | 90528 | sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); |
| 89822 | 90529 | }else{ |
| 89823 | 90530 | sqlite3VdbeMemSetNull(pDest); |
| 89824 | 90531 | } |
| | @@ -89832,10 +90539,12 @@ |
| 89832 | 90539 | ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are |
| 89833 | 90540 | ** all valid. |
| 89834 | 90541 | */ |
| 89835 | 90542 | assert( p2<pC->nHdrParsed ); |
| 89836 | 90543 | assert( rc==SQLITE_OK ); |
| 90544 | + pDest = &aMem[pOp->p3]; |
| 90545 | + memAboutToChange(p, pDest); |
| 89837 | 90546 | assert( sqlite3VdbeCheckMemInvariants(pDest) ); |
| 89838 | 90547 | if( VdbeMemDynamic(pDest) ){ |
| 89839 | 90548 | sqlite3VdbeMemSetNull(pDest); |
| 89840 | 90549 | } |
| 89841 | 90550 | assert( t==pC->aType[p2] ); |
| | @@ -89852,10 +90561,11 @@ |
| 89852 | 90561 | */ |
| 89853 | 90562 | static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; |
| 89854 | 90563 | pDest->n = len = (t-12)/2; |
| 89855 | 90564 | pDest->enc = encoding; |
| 89856 | 90565 | if( pDest->szMalloc < len+2 ){ |
| 90566 | + if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big; |
| 89857 | 90567 | pDest->flags = MEM_Null; |
| 89858 | 90568 | if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; |
| 89859 | 90569 | }else{ |
| 89860 | 90570 | pDest->z = pDest->zMalloc; |
| 89861 | 90571 | } |
| | @@ -89884,10 +90594,11 @@ |
| 89884 | 90594 | ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint()) |
| 89885 | 90595 | ** and it begins with a bunch of zeros. |
| 89886 | 90596 | */ |
| 89887 | 90597 | sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest); |
| 89888 | 90598 | }else{ |
| 90599 | + if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big; |
| 89889 | 90600 | rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest); |
| 89890 | 90601 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 89891 | 90602 | sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); |
| 89892 | 90603 | pDest->flags &= ~MEM_Ephem; |
| 89893 | 90604 | } |
| | @@ -90096,11 +90807,10 @@ |
| 90096 | 90807 | u32 serial_type; /* Type field */ |
| 90097 | 90808 | Mem *pData0; /* First field to be combined into the record */ |
| 90098 | 90809 | Mem *pLast; /* Last field of the record */ |
| 90099 | 90810 | int nField; /* Number of fields in the record */ |
| 90100 | 90811 | char *zAffinity; /* The affinity string for the record */ |
| 90101 | | - int file_format; /* File format to use for encoding */ |
| 90102 | 90812 | u32 len; /* Length of a field */ |
| 90103 | 90813 | u8 *zHdr; /* Where to write next byte of the header */ |
| 90104 | 90814 | u8 *zPayload; /* Where to write next byte of the payload */ |
| 90105 | 90815 | |
| 90106 | 90816 | /* Assuming the record contains N fields, the record format looks |
| | @@ -90125,11 +90835,10 @@ |
| 90125 | 90835 | zAffinity = pOp->p4.z; |
| 90126 | 90836 | assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 ); |
| 90127 | 90837 | pData0 = &aMem[nField]; |
| 90128 | 90838 | nField = pOp->p2; |
| 90129 | 90839 | pLast = &pData0[nField-1]; |
| 90130 | | - file_format = p->minWriteFileFormat; |
| 90131 | 90840 | |
| 90132 | 90841 | /* Identify the output register */ |
| 90133 | 90842 | assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); |
| 90134 | 90843 | pOut = &aMem[pOp->p3]; |
| 90135 | 90844 | memAboutToChange(p, pOut); |
| | @@ -90224,14 +90933,14 @@ |
| 90224 | 90933 | } |
| 90225 | 90934 | nHdr++; |
| 90226 | 90935 | testcase( uu==127 ); testcase( uu==128 ); |
| 90227 | 90936 | testcase( uu==32767 ); testcase( uu==32768 ); |
| 90228 | 90937 | testcase( uu==8388607 ); testcase( uu==8388608 ); |
| 90229 | | - testcase( uu==2147483647 ); testcase( uu==2147483648 ); |
| 90938 | + testcase( uu==2147483647 ); testcase( uu==2147483648LL ); |
| 90230 | 90939 | testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL ); |
| 90231 | 90940 | if( uu<=127 ){ |
| 90232 | | - if( (i&1)==i && file_format>=4 ){ |
| 90941 | + if( (i&1)==i && p->minWriteFileFormat>=4 ){ |
| 90233 | 90942 | pRec->uTemp = 8+(u32)uu; |
| 90234 | 90943 | }else{ |
| 90235 | 90944 | nData++; |
| 90236 | 90945 | pRec->uTemp = 1; |
| 90237 | 90946 | } |
| | @@ -92690,10 +93399,14 @@ |
| 92690 | 93399 | /* Opcode: NullRow P1 * * * * |
| 92691 | 93400 | ** |
| 92692 | 93401 | ** Move the cursor P1 to a null row. Any OP_Column operations |
| 92693 | 93402 | ** that occur while the cursor is on the null row will always |
| 92694 | 93403 | ** write a NULL. |
| 93404 | +** |
| 93405 | +** Or, if P1 is a Pseudo-Cursor (a cursor opened using OP_OpenPseudo) |
| 93406 | +** just reset the cache for that cursor. This causes the row of |
| 93407 | +** content held by the pseudo-cursor to be reparsed. |
| 92695 | 93408 | */ |
| 92696 | 93409 | case OP_NullRow: { |
| 92697 | 93410 | VdbeCursor *pC; |
| 92698 | 93411 | |
| 92699 | 93412 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| | @@ -92869,11 +93582,11 @@ |
| 92869 | 93582 | VdbeBranchTaken(res!=0,2); |
| 92870 | 93583 | if( res ) goto jump_to_p2; |
| 92871 | 93584 | break; |
| 92872 | 93585 | } |
| 92873 | 93586 | |
| 92874 | | -/* Opcode: Next P1 P2 P3 P4 P5 |
| 93587 | +/* Opcode: Next P1 P2 P3 * P5 |
| 92875 | 93588 | ** |
| 92876 | 93589 | ** Advance cursor P1 so that it points to the next key/data pair in its |
| 92877 | 93590 | ** table or index. If there are no more key/value pairs then fall through |
| 92878 | 93591 | ** to the following instruction. But if the cursor advance was successful, |
| 92879 | 93592 | ** jump immediately to P2. |
| | @@ -92888,19 +93601,16 @@ |
| 92888 | 93601 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 92889 | 93602 | ** means P1 is an SQL index and that this instruction could have been |
| 92890 | 93603 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 92891 | 93604 | ** always either 0 or 1. |
| 92892 | 93605 | ** |
| 92893 | | -** P4 is always of type P4_ADVANCE. The function pointer points to |
| 92894 | | -** sqlite3BtreeNext(). |
| 92895 | | -** |
| 92896 | 93606 | ** If P5 is positive and the jump is taken, then event counter |
| 92897 | 93607 | ** number P5-1 in the prepared statement is incremented. |
| 92898 | 93608 | ** |
| 92899 | 93609 | ** See also: Prev |
| 92900 | 93610 | */ |
| 92901 | | -/* Opcode: Prev P1 P2 P3 P4 P5 |
| 93611 | +/* Opcode: Prev P1 P2 P3 * P5 |
| 92902 | 93612 | ** |
| 92903 | 93613 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 92904 | 93614 | ** table or index. If there is no previous key/value pairs then fall through |
| 92905 | 93615 | ** to the following instruction. But if the cursor backup was successful, |
| 92906 | 93616 | ** jump immediately to P2. |
| | @@ -92916,13 +93626,10 @@ |
| 92916 | 93626 | ** The P3 value is a hint to the btree implementation. If P3==1, that |
| 92917 | 93627 | ** means P1 is an SQL index and that this instruction could have been |
| 92918 | 93628 | ** omitted if that index had been unique. P3 is usually 0. P3 is |
| 92919 | 93629 | ** always either 0 or 1. |
| 92920 | 93630 | ** |
| 92921 | | -** P4 is always of type P4_ADVANCE. The function pointer points to |
| 92922 | | -** sqlite3BtreePrevious(). |
| 92923 | | -** |
| 92924 | 93631 | ** If P5 is positive and the jump is taken, then event counter |
| 92925 | 93632 | ** number P5-1 in the prepared statement is incremented. |
| 92926 | 93633 | */ |
| 92927 | 93634 | /* Opcode: SorterNext P1 P2 * * P5 |
| 92928 | 93635 | ** |
| | @@ -92936,34 +93643,37 @@ |
| 92936 | 93643 | |
| 92937 | 93644 | pC = p->apCsr[pOp->p1]; |
| 92938 | 93645 | assert( isSorter(pC) ); |
| 92939 | 93646 | rc = sqlite3VdbeSorterNext(db, pC); |
| 92940 | 93647 | goto next_tail; |
| 93648 | + |
| 92941 | 93649 | case OP_Prev: /* jump */ |
| 93650 | + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 93651 | + assert( pOp->p5<ArraySize(p->aCounter) ); |
| 93652 | + pC = p->apCsr[pOp->p1]; |
| 93653 | + assert( pC!=0 ); |
| 93654 | + assert( pC->deferredMoveto==0 ); |
| 93655 | + assert( pC->eCurType==CURTYPE_BTREE ); |
| 93656 | + assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE |
| 93657 | + || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope |
| 93658 | + || pC->seekOp==OP_NullRow); |
| 93659 | + rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3); |
| 93660 | + goto next_tail; |
| 93661 | + |
| 92942 | 93662 | case OP_Next: /* jump */ |
| 92943 | 93663 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 92944 | 93664 | assert( pOp->p5<ArraySize(p->aCounter) ); |
| 92945 | 93665 | pC = p->apCsr[pOp->p1]; |
| 92946 | 93666 | assert( pC!=0 ); |
| 92947 | 93667 | assert( pC->deferredMoveto==0 ); |
| 92948 | 93668 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 92949 | | - assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); |
| 92950 | | - assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); |
| 92951 | | - |
| 92952 | | - /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found. |
| 92953 | | - ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ |
| 92954 | | - assert( pOp->opcode!=OP_Next |
| 92955 | | - || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE |
| 93669 | + assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE |
| 92956 | 93670 | || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found |
| 92957 | 93671 | || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid |
| 92958 | 93672 | || pC->seekOp==OP_IfNoHope); |
| 92959 | | - assert( pOp->opcode!=OP_Prev |
| 92960 | | - || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE |
| 92961 | | - || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope |
| 92962 | | - || pC->seekOp==OP_NullRow); |
| 93673 | + rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3); |
| 92963 | 93674 | |
| 92964 | | - rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3); |
| 92965 | 93675 | next_tail: |
| 92966 | 93676 | pC->cacheStatus = CACHE_STALE; |
| 92967 | 93677 | VdbeBranchTaken(rc==SQLITE_OK,2); |
| 92968 | 93678 | if( rc==SQLITE_OK ){ |
| 92969 | 93679 | pC->nullRow = 0; |
| | @@ -93177,10 +93887,11 @@ |
| 93177 | 93887 | assert( pTabCur->uc.pCursor!=0 ); |
| 93178 | 93888 | assert( pTabCur->isTable ); |
| 93179 | 93889 | pTabCur->nullRow = 0; |
| 93180 | 93890 | pTabCur->movetoTarget = rowid; |
| 93181 | 93891 | pTabCur->deferredMoveto = 1; |
| 93892 | + pTabCur->cacheStatus = CACHE_STALE; |
| 93182 | 93893 | assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); |
| 93183 | 93894 | assert( !pTabCur->isEphemeral ); |
| 93184 | 93895 | pTabCur->ub.aAltMap = pOp->p4.ai; |
| 93185 | 93896 | assert( !pC->isEphemeral ); |
| 93186 | 93897 | pTabCur->pAltCursor = pC; |
| | @@ -93311,11 +94022,11 @@ |
| 93311 | 94022 | } |
| 93312 | 94023 | sqlite3VdbeMemInit(&m, db, 0); |
| 93313 | 94024 | rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m); |
| 93314 | 94025 | if( rc ) goto abort_due_to_error; |
| 93315 | 94026 | res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0); |
| 93316 | | - sqlite3VdbeMemRelease(&m); |
| 94027 | + sqlite3VdbeMemReleaseMalloc(&m); |
| 93317 | 94028 | } |
| 93318 | 94029 | /* End of inlined sqlite3VdbeIdxKeyCompare() */ |
| 93319 | 94030 | |
| 93320 | 94031 | assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); |
| 93321 | 94032 | if( (pOp->opcode&1)==(OP_IdxLT&1) ){ |
| | @@ -94192,10 +94903,11 @@ |
| 94192 | 94903 | pCtx->pFunc = pOp->p4.pFunc; |
| 94193 | 94904 | pCtx->iOp = (int)(pOp - aOp); |
| 94194 | 94905 | pCtx->pVdbe = p; |
| 94195 | 94906 | pCtx->skipFlag = 0; |
| 94196 | 94907 | pCtx->isError = 0; |
| 94908 | + pCtx->enc = encoding; |
| 94197 | 94909 | pCtx->argc = n; |
| 94198 | 94910 | pOp->p4type = P4_FUNCCTX; |
| 94199 | 94911 | pOp->p4.pCtx = pCtx; |
| 94200 | 94912 | |
| 94201 | 94913 | /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */ |
| | @@ -94321,13 +95033,10 @@ |
| 94321 | 95033 | sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); |
| 94322 | 95034 | goto abort_due_to_error; |
| 94323 | 95035 | } |
| 94324 | 95036 | sqlite3VdbeChangeEncoding(pMem, encoding); |
| 94325 | 95037 | UPDATE_MAX_BLOBSIZE(pMem); |
| 94326 | | - if( sqlite3VdbeMemTooBig(pMem) ){ |
| 94327 | | - goto too_big; |
| 94328 | | - } |
| 94329 | 95038 | break; |
| 94330 | 95039 | } |
| 94331 | 95040 | |
| 94332 | 95041 | #ifndef SQLITE_OMIT_WAL |
| 94333 | 95042 | /* Opcode: Checkpoint P1 P2 P3 * * |
| | @@ -94714,10 +95423,38 @@ |
| 94714 | 95423 | goto no_mem; |
| 94715 | 95424 | } |
| 94716 | 95425 | break; |
| 94717 | 95426 | } |
| 94718 | 95427 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 95428 | + |
| 95429 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 95430 | +/* Opcode: VInitIn P1 P2 P3 * * |
| 95431 | +** Synopsis: r[P2]=ValueList(P1,P3) |
| 95432 | +** |
| 95433 | +** Set register P2 to be a pointer to a ValueList object for cursor P1 |
| 95434 | +** with cache register P3 and output register P3+1. This ValueList object |
| 95435 | +** can be used as the first argument to sqlite3_vtab_in_first() and |
| 95436 | +** sqlite3_vtab_in_next() to extract all of the values stored in the P1 |
| 95437 | +** cursor. Register P3 is used to hold the values returned by |
| 95438 | +** sqlite3_vtab_in_first() and sqlite3_vtab_in_next(). |
| 95439 | +*/ |
| 95440 | +case OP_VInitIn: { /* out2 */ |
| 95441 | + VdbeCursor *pC; /* The cursor containing the RHS values */ |
| 95442 | + ValueList *pRhs; /* New ValueList object to put in reg[P2] */ |
| 95443 | + |
| 95444 | + pC = p->apCsr[pOp->p1]; |
| 95445 | + pRhs = sqlite3_malloc64( sizeof(*pRhs) ); |
| 95446 | + if( pRhs==0 ) goto no_mem; |
| 95447 | + pRhs->pCsr = pC->uc.pCursor; |
| 95448 | + pRhs->pOut = &aMem[pOp->p3]; |
| 95449 | + pOut = out2Prerelease(p, pOp); |
| 95450 | + pOut->flags = MEM_Null; |
| 95451 | + sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3_free); |
| 95452 | + break; |
| 95453 | +} |
| 95454 | +#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 95455 | + |
| 94719 | 95456 | |
| 94720 | 95457 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 94721 | 95458 | /* Opcode: VFilter P1 P2 P3 P4 * |
| 94722 | 95459 | ** Synopsis: iplan=r[P3] zplan='P4' |
| 94723 | 95460 | ** |
| | @@ -94816,10 +95553,11 @@ |
| 94816 | 95553 | pVtab = pCur->uc.pVCur->pVtab; |
| 94817 | 95554 | pModule = pVtab->pModule; |
| 94818 | 95555 | assert( pModule->xColumn ); |
| 94819 | 95556 | memset(&sContext, 0, sizeof(sContext)); |
| 94820 | 95557 | sContext.pOut = pDest; |
| 95558 | + sContext.enc = encoding; |
| 94821 | 95559 | assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 ); |
| 94822 | 95560 | if( pOp->p5 & OPFLAG_NOCHNG ){ |
| 94823 | 95561 | sqlite3VdbeMemSetNull(pDest); |
| 94824 | 95562 | pDest->flags = MEM_Null|MEM_Zero; |
| 94825 | 95563 | pDest->u.nZero = 0; |
| | @@ -94834,13 +95572,10 @@ |
| 94834 | 95572 | } |
| 94835 | 95573 | sqlite3VdbeChangeEncoding(pDest, encoding); |
| 94836 | 95574 | REGISTER_TRACE(pOp->p3, pDest); |
| 94837 | 95575 | UPDATE_MAX_BLOBSIZE(pDest); |
| 94838 | 95576 | |
| 94839 | | - if( sqlite3VdbeMemTooBig(pDest) ){ |
| 94840 | | - goto too_big; |
| 94841 | | - } |
| 94842 | 95577 | if( rc ) goto abort_due_to_error; |
| 94843 | 95578 | break; |
| 94844 | 95579 | } |
| 94845 | 95580 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 94846 | 95581 | |
| | @@ -95103,10 +95838,11 @@ |
| 95103 | 95838 | ** reinitializes the relavant parts of the sqlite3_context object */ |
| 95104 | 95839 | pOut = &aMem[pOp->p3]; |
| 95105 | 95840 | if( pCtx->pOut != pOut ){ |
| 95106 | 95841 | pCtx->pVdbe = p; |
| 95107 | 95842 | pCtx->pOut = pOut; |
| 95843 | + pCtx->enc = encoding; |
| 95108 | 95844 | for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 95109 | 95845 | } |
| 95110 | 95846 | assert( pCtx->pVdbe==p ); |
| 95111 | 95847 | |
| 95112 | 95848 | memAboutToChange(p, pOut); |
| | @@ -95129,15 +95865,14 @@ |
| 95129 | 95865 | sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); |
| 95130 | 95866 | pCtx->isError = 0; |
| 95131 | 95867 | if( rc ) goto abort_due_to_error; |
| 95132 | 95868 | } |
| 95133 | 95869 | |
| 95134 | | - /* Copy the result of the function into register P3 */ |
| 95135 | | - if( pOut->flags & (MEM_Str|MEM_Blob) ){ |
| 95136 | | - sqlite3VdbeChangeEncoding(pOut, encoding); |
| 95137 | | - if( sqlite3VdbeMemTooBig(pOut) ) goto too_big; |
| 95138 | | - } |
| 95870 | + assert( (pOut->flags&MEM_Str)==0 |
| 95871 | + || pOut->enc==encoding |
| 95872 | + || db->mallocFailed ); |
| 95873 | + assert( !sqlite3VdbeMemTooBig(pOut) ); |
| 95139 | 95874 | |
| 95140 | 95875 | REGISTER_TRACE(pOp->p3, pOut); |
| 95141 | 95876 | UPDATE_MAX_BLOBSIZE(pOut); |
| 95142 | 95877 | break; |
| 95143 | 95878 | } |
| | @@ -99442,10 +100177,13 @@ |
| 99442 | 100177 | } |
| 99443 | 100178 | return rc; |
| 99444 | 100179 | } |
| 99445 | 100180 | |
| 99446 | 100181 | |
| 100182 | +/* Forward reference */ |
| 100183 | +static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size); |
| 100184 | + |
| 99447 | 100185 | /* |
| 99448 | 100186 | ** Write data to the file. |
| 99449 | 100187 | */ |
| 99450 | 100188 | static int memjrnlWrite( |
| 99451 | 100189 | sqlite3_file *pJfd, /* The journal file into which to write */ |
| | @@ -99472,26 +100210,24 @@ |
| 99472 | 100210 | /* An in-memory journal file should only ever be appended to. Random |
| 99473 | 100211 | ** access writes are not required. The only exception to this is when |
| 99474 | 100212 | ** the in-memory journal is being used by a connection using the |
| 99475 | 100213 | ** atomic-write optimization. In this case the first 28 bytes of the |
| 99476 | 100214 | ** journal file may be written as part of committing the transaction. */ |
| 99477 | | - assert( iOfst==p->endpoint.iOffset || iOfst==0 ); |
| 99478 | | -#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \ |
| 99479 | | - || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
| 100215 | + assert( iOfst<=p->endpoint.iOffset ); |
| 100216 | + if( iOfst>0 && iOfst!=p->endpoint.iOffset ){ |
| 100217 | + memjrnlTruncate(pJfd, iOfst); |
| 100218 | + } |
| 99480 | 100219 | if( iOfst==0 && p->pFirst ){ |
| 99481 | 100220 | assert( p->nChunkSize>iAmt ); |
| 99482 | 100221 | memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt); |
| 99483 | | - }else |
| 99484 | | -#else |
| 99485 | | - assert( iOfst>0 || p->pFirst==0 ); |
| 99486 | | -#endif |
| 99487 | | - { |
| 100222 | + }else{ |
| 99488 | 100223 | while( nWrite>0 ){ |
| 99489 | 100224 | FileChunk *pChunk = p->endpoint.pChunk; |
| 99490 | 100225 | int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize); |
| 99491 | 100226 | int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset); |
| 99492 | 100227 | |
| 100228 | + assert( pChunk!=0 || iChunkOffset==0 ); |
| 99493 | 100229 | if( iChunkOffset==0 ){ |
| 99494 | 100230 | /* New chunk is required to extend the file. */ |
| 99495 | 100231 | FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)); |
| 99496 | 100232 | if( !pNew ){ |
| 99497 | 100233 | return SQLITE_IOERR_NOMEM_BKPT; |
| | @@ -99502,14 +100238,15 @@ |
| 99502 | 100238 | pChunk->pNext = pNew; |
| 99503 | 100239 | }else{ |
| 99504 | 100240 | assert( !p->pFirst ); |
| 99505 | 100241 | p->pFirst = pNew; |
| 99506 | 100242 | } |
| 99507 | | - p->endpoint.pChunk = pNew; |
| 100243 | + pChunk = p->endpoint.pChunk = pNew; |
| 99508 | 100244 | } |
| 99509 | 100245 | |
| 99510 | | - memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace); |
| 100246 | + assert( pChunk!=0 ); |
| 100247 | + memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace); |
| 99511 | 100248 | zWrite += iSpace; |
| 99512 | 100249 | nWrite -= iSpace; |
| 99513 | 100250 | p->endpoint.iOffset += iSpace; |
| 99514 | 100251 | } |
| 99515 | 100252 | } |
| | @@ -100583,10 +101320,11 @@ |
| 100583 | 101320 | }else if( zTab ){ |
| 100584 | 101321 | sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); |
| 100585 | 101322 | }else{ |
| 100586 | 101323 | sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); |
| 100587 | 101324 | } |
| 101325 | + sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); |
| 100588 | 101326 | pParse->checkSchema = 1; |
| 100589 | 101327 | pTopNC->nNcErr++; |
| 100590 | 101328 | } |
| 100591 | 101329 | |
| 100592 | 101330 | /* If a column from a table in pSrcList is referenced, then record |
| | @@ -100691,11 +101429,12 @@ |
| 100691 | 101429 | */ |
| 100692 | 101430 | static void notValidImpl( |
| 100693 | 101431 | Parse *pParse, /* Leave error message here */ |
| 100694 | 101432 | NameContext *pNC, /* The name context */ |
| 100695 | 101433 | const char *zMsg, /* Type of error */ |
| 100696 | | - Expr *pExpr /* Invalidate this expression on error */ |
| 101434 | + Expr *pExpr, /* Invalidate this expression on error */ |
| 101435 | + Expr *pError /* Associate error with this expression */ |
| 100697 | 101436 | ){ |
| 100698 | 101437 | const char *zIn = "partial index WHERE clauses"; |
| 100699 | 101438 | if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions"; |
| 100700 | 101439 | #ifndef SQLITE_OMIT_CHECK |
| 100701 | 101440 | else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints"; |
| | @@ -100703,14 +101442,15 @@ |
| 100703 | 101442 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 100704 | 101443 | else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns"; |
| 100705 | 101444 | #endif |
| 100706 | 101445 | sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn); |
| 100707 | 101446 | if( pExpr ) pExpr->op = TK_NULL; |
| 101447 | + sqlite3RecordErrorOffsetOfExpr(pParse->db, pError); |
| 100708 | 101448 | } |
| 100709 | | -#define sqlite3ResolveNotValid(P,N,M,X,E) \ |
| 101449 | +#define sqlite3ResolveNotValid(P,N,M,X,E,R) \ |
| 100710 | 101450 | assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 ); \ |
| 100711 | | - if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E); |
| 101451 | + if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R); |
| 100712 | 101452 | |
| 100713 | 101453 | /* |
| 100714 | 101454 | ** Expression p should encode a floating point value between 1.0 and 0.0. |
| 100715 | 101455 | ** Return 1024 times this value. Or return -1 if p is not a floating point |
| 100716 | 101456 | ** value between 1.0 and 0.0. |
| | @@ -100841,11 +101581,11 @@ |
| 100841 | 101581 | }else{ |
| 100842 | 101582 | Expr *pLeft = pExpr->pLeft; |
| 100843 | 101583 | testcase( pNC->ncFlags & NC_IdxExpr ); |
| 100844 | 101584 | testcase( pNC->ncFlags & NC_GenCol ); |
| 100845 | 101585 | sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator", |
| 100846 | | - NC_IdxExpr|NC_GenCol, 0); |
| 101586 | + NC_IdxExpr|NC_GenCol, 0, pExpr); |
| 100847 | 101587 | pRight = pExpr->pRight; |
| 100848 | 101588 | if( pRight->op==TK_ID ){ |
| 100849 | 101589 | zDb = 0; |
| 100850 | 101590 | }else{ |
| 100851 | 101591 | assert( pRight->op==TK_DOT ); |
| | @@ -100872,21 +101612,19 @@ |
| 100872 | 101612 | ExprList *pList = pExpr->x.pList; /* The argument list */ |
| 100873 | 101613 | int n = pList ? pList->nExpr : 0; /* Number of arguments */ |
| 100874 | 101614 | int no_such_func = 0; /* True if no such function exists */ |
| 100875 | 101615 | int wrong_num_args = 0; /* True if wrong number of arguments */ |
| 100876 | 101616 | int is_agg = 0; /* True if is an aggregate function */ |
| 100877 | | - int nId; /* Number of characters in function name */ |
| 100878 | 101617 | const char *zId; /* The function name. */ |
| 100879 | 101618 | FuncDef *pDef; /* Information about the function */ |
| 100880 | 101619 | u8 enc = ENC(pParse->db); /* The database encoding */ |
| 100881 | 101620 | int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin)); |
| 100882 | 101621 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 100883 | 101622 | Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0); |
| 100884 | 101623 | #endif |
| 100885 | 101624 | assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) ); |
| 100886 | 101625 | zId = pExpr->u.zToken; |
| 100887 | | - nId = sqlite3Strlen30(zId); |
| 100888 | 101626 | pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0); |
| 100889 | 101627 | if( pDef==0 ){ |
| 100890 | 101628 | pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0); |
| 100891 | 101629 | if( pDef==0 ){ |
| 100892 | 101630 | no_such_func = 1; |
| | @@ -100899,12 +101637,12 @@ |
| 100899 | 101637 | ExprSetProperty(pExpr, EP_Unlikely); |
| 100900 | 101638 | if( n==2 ){ |
| 100901 | 101639 | pExpr->iTable = exprProbability(pList->a[1].pExpr); |
| 100902 | 101640 | if( pExpr->iTable<0 ){ |
| 100903 | 101641 | sqlite3ErrorMsg(pParse, |
| 100904 | | - "second argument to likelihood() must be a " |
| 100905 | | - "constant between 0.0 and 1.0"); |
| 101642 | + "second argument to %#T() must be a " |
| 101643 | + "constant between 0.0 and 1.0", pExpr); |
| 100906 | 101644 | pNC->nNcErr++; |
| 100907 | 101645 | } |
| 100908 | 101646 | }else{ |
| 100909 | 101647 | /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is |
| 100910 | 101648 | ** equivalent to likelihood(X, 0.0625). |
| | @@ -100921,12 +101659,12 @@ |
| 100921 | 101659 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 100922 | 101660 | { |
| 100923 | 101661 | int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0); |
| 100924 | 101662 | if( auth!=SQLITE_OK ){ |
| 100925 | 101663 | if( auth==SQLITE_DENY ){ |
| 100926 | | - sqlite3ErrorMsg(pParse, "not authorized to use function: %s", |
| 100927 | | - pDef->zName); |
| 101664 | + sqlite3ErrorMsg(pParse, "not authorized to use function: %#T", |
| 101665 | + pExpr); |
| 100928 | 101666 | pNC->nNcErr++; |
| 100929 | 101667 | } |
| 100930 | 101668 | pExpr->op = TK_NULL; |
| 100931 | 101669 | return WRC_Prune; |
| 100932 | 101670 | } |
| | @@ -100945,11 +101683,11 @@ |
| 100945 | 101683 | ** sqlite_version() that might change over time cannot be used |
| 100946 | 101684 | ** in an index or generated column. Curiously, they can be used |
| 100947 | 101685 | ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all |
| 100948 | 101686 | ** all this. */ |
| 100949 | 101687 | sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions", |
| 100950 | | - NC_IdxExpr|NC_PartIdx|NC_GenCol, 0); |
| 101688 | + NC_IdxExpr|NC_PartIdx|NC_GenCol, 0, pExpr); |
| 100951 | 101689 | }else{ |
| 100952 | 101690 | assert( (NC_SelfRef & 0xff)==NC_SelfRef ); /* Must fit in 8 bits */ |
| 100953 | 101691 | pExpr->op2 = pNC->ncFlags & NC_SelfRef; |
| 100954 | 101692 | if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL); |
| 100955 | 101693 | } |
| | @@ -100958,11 +101696,11 @@ |
| 100958 | 101696 | && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0 |
| 100959 | 101697 | ){ |
| 100960 | 101698 | /* Internal-use-only functions are disallowed unless the |
| 100961 | 101699 | ** SQL is being compiled using sqlite3NestedParse() or |
| 100962 | 101700 | ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be |
| 100963 | | - ** used to activate internal functionsn for testing purposes */ |
| 101701 | + ** used to activate internal functions for testing purposes */ |
| 100964 | 101702 | no_such_func = 1; |
| 100965 | 101703 | pDef = 0; |
| 100966 | 101704 | }else |
| 100967 | 101705 | if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 |
| 100968 | 101706 | && !IN_RENAME_OBJECT |
| | @@ -100977,11 +101715,11 @@ |
| 100977 | 101715 | || (pDef->xValue==0 && pDef->xInverse==0) |
| 100978 | 101716 | || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize) |
| 100979 | 101717 | ); |
| 100980 | 101718 | if( pDef && pDef->xValue==0 && pWin ){ |
| 100981 | 101719 | sqlite3ErrorMsg(pParse, |
| 100982 | | - "%.*s() may not be used as a window function", nId, zId |
| 101720 | + "%#T() may not be used as a window function", pExpr |
| 100983 | 101721 | ); |
| 100984 | 101722 | pNC->nNcErr++; |
| 100985 | 101723 | }else if( |
| 100986 | 101724 | (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) |
| 100987 | 101725 | || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin) |
| | @@ -100991,38 +101729,38 @@ |
| 100991 | 101729 | if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){ |
| 100992 | 101730 | zType = "window"; |
| 100993 | 101731 | }else{ |
| 100994 | 101732 | zType = "aggregate"; |
| 100995 | 101733 | } |
| 100996 | | - sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId); |
| 101734 | + sqlite3ErrorMsg(pParse, "misuse of %s function %#T()",zType,pExpr); |
| 100997 | 101735 | pNC->nNcErr++; |
| 100998 | 101736 | is_agg = 0; |
| 100999 | 101737 | } |
| 101000 | 101738 | #else |
| 101001 | 101739 | if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){ |
| 101002 | | - sqlite3ErrorMsg(pParse,"misuse of aggregate function %.*s()",nId,zId); |
| 101740 | + sqlite3ErrorMsg(pParse,"misuse of aggregate function %#T()",pExpr); |
| 101003 | 101741 | pNC->nNcErr++; |
| 101004 | 101742 | is_agg = 0; |
| 101005 | 101743 | } |
| 101006 | 101744 | #endif |
| 101007 | 101745 | else if( no_such_func && pParse->db->init.busy==0 |
| 101008 | 101746 | #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
| 101009 | 101747 | && pParse->explain==0 |
| 101010 | 101748 | #endif |
| 101011 | 101749 | ){ |
| 101012 | | - sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); |
| 101750 | + sqlite3ErrorMsg(pParse, "no such function: %#T", pExpr); |
| 101013 | 101751 | pNC->nNcErr++; |
| 101014 | 101752 | }else if( wrong_num_args ){ |
| 101015 | | - sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", |
| 101016 | | - nId, zId); |
| 101753 | + sqlite3ErrorMsg(pParse,"wrong number of arguments to function %#T()", |
| 101754 | + pExpr); |
| 101017 | 101755 | pNC->nNcErr++; |
| 101018 | 101756 | } |
| 101019 | 101757 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 101020 | 101758 | else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 101021 | 101759 | sqlite3ErrorMsg(pParse, |
| 101022 | | - "FILTER may not be used with non-aggregate %.*s()", |
| 101023 | | - nId, zId |
| 101760 | + "FILTER may not be used with non-aggregate %#T()", |
| 101761 | + pExpr |
| 101024 | 101762 | ); |
| 101025 | 101763 | pNC->nNcErr++; |
| 101026 | 101764 | } |
| 101027 | 101765 | #endif |
| 101028 | 101766 | if( is_agg ){ |
| | @@ -101103,11 +101841,11 @@ |
| 101103 | 101841 | testcase( pNC->ncFlags & NC_IsCheck ); |
| 101104 | 101842 | testcase( pNC->ncFlags & NC_PartIdx ); |
| 101105 | 101843 | testcase( pNC->ncFlags & NC_IdxExpr ); |
| 101106 | 101844 | testcase( pNC->ncFlags & NC_GenCol ); |
| 101107 | 101845 | if( pNC->ncFlags & NC_SelfRef ){ |
| 101108 | | - notValidImpl(pParse, pNC, "subqueries", pExpr); |
| 101846 | + notValidImpl(pParse, pNC, "subqueries", pExpr, pExpr); |
| 101109 | 101847 | }else{ |
| 101110 | 101848 | sqlite3WalkSelect(pWalker, pExpr->x.pSelect); |
| 101111 | 101849 | } |
| 101112 | 101850 | assert( pNC->nRef>=nRef ); |
| 101113 | 101851 | if( nRef!=pNC->nRef ){ |
| | @@ -101121,11 +101859,11 @@ |
| 101121 | 101859 | testcase( pNC->ncFlags & NC_IsCheck ); |
| 101122 | 101860 | testcase( pNC->ncFlags & NC_PartIdx ); |
| 101123 | 101861 | testcase( pNC->ncFlags & NC_IdxExpr ); |
| 101124 | 101862 | testcase( pNC->ncFlags & NC_GenCol ); |
| 101125 | 101863 | sqlite3ResolveNotValid(pParse, pNC, "parameters", |
| 101126 | | - NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr); |
| 101864 | + NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr, pExpr); |
| 101127 | 101865 | break; |
| 101128 | 101866 | } |
| 101129 | 101867 | case TK_IS: |
| 101130 | 101868 | case TK_ISNOT: { |
| 101131 | 101869 | Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight); |
| | @@ -101173,10 +101911,11 @@ |
| 101173 | 101911 | testcase( pExpr->op==TK_GE ); |
| 101174 | 101912 | testcase( pExpr->op==TK_IS ); |
| 101175 | 101913 | testcase( pExpr->op==TK_ISNOT ); |
| 101176 | 101914 | testcase( pExpr->op==TK_BETWEEN ); |
| 101177 | 101915 | sqlite3ErrorMsg(pParse, "row value misused"); |
| 101916 | + sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); |
| 101178 | 101917 | } |
| 101179 | 101918 | break; |
| 101180 | 101919 | } |
| 101181 | 101920 | } |
| 101182 | 101921 | assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 ); |
| | @@ -101286,15 +102025,17 @@ |
| 101286 | 102025 | */ |
| 101287 | 102026 | static void resolveOutOfRangeError( |
| 101288 | 102027 | Parse *pParse, /* The error context into which to write the error */ |
| 101289 | 102028 | const char *zType, /* "ORDER" or "GROUP" */ |
| 101290 | 102029 | int i, /* The index (1-based) of the term out of range */ |
| 101291 | | - int mx /* Largest permissible value of i */ |
| 102030 | + int mx, /* Largest permissible value of i */ |
| 102031 | + Expr *pError /* Associate the error with the expression */ |
| 101292 | 102032 | ){ |
| 101293 | 102033 | sqlite3ErrorMsg(pParse, |
| 101294 | 102034 | "%r %s BY term out of range - should be " |
| 101295 | 102035 | "between 1 and %d", i, zType, mx); |
| 102036 | + sqlite3RecordErrorOffsetOfExpr(pParse->db, pError); |
| 101296 | 102037 | } |
| 101297 | 102038 | |
| 101298 | 102039 | /* |
| 101299 | 102040 | ** Analyze the ORDER BY clause in a compound SELECT statement. Modify |
| 101300 | 102041 | ** each term of the ORDER BY clause is a constant integer between 1 |
| | @@ -101346,11 +102087,11 @@ |
| 101346 | 102087 | if( pItem->done ) continue; |
| 101347 | 102088 | pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr); |
| 101348 | 102089 | if( NEVER(pE==0) ) continue; |
| 101349 | 102090 | if( sqlite3ExprIsInteger(pE, &iCol) ){ |
| 101350 | 102091 | if( iCol<=0 || iCol>pEList->nExpr ){ |
| 101351 | | - resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr); |
| 102092 | + resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE); |
| 101352 | 102093 | return 1; |
| 101353 | 102094 | } |
| 101354 | 102095 | }else{ |
| 101355 | 102096 | iCol = resolveAsName(pParse, pEList, pE); |
| 101356 | 102097 | if( iCol==0 ){ |
| | @@ -101442,11 +102183,11 @@ |
| 101442 | 102183 | pEList = pSelect->pEList; |
| 101443 | 102184 | assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ |
| 101444 | 102185 | for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
| 101445 | 102186 | if( pItem->u.x.iOrderByCol ){ |
| 101446 | 102187 | if( pItem->u.x.iOrderByCol>pEList->nExpr ){ |
| 101447 | | - resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr); |
| 102188 | + resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0); |
| 101448 | 102189 | return 1; |
| 101449 | 102190 | } |
| 101450 | 102191 | resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0); |
| 101451 | 102192 | } |
| 101452 | 102193 | } |
| | @@ -101534,11 +102275,11 @@ |
| 101534 | 102275 | if( sqlite3ExprIsInteger(pE2, &iCol) ){ |
| 101535 | 102276 | /* The ORDER BY term is an integer constant. Again, set the column |
| 101536 | 102277 | ** number so that sqlite3ResolveOrderGroupBy() will convert the |
| 101537 | 102278 | ** order-by term to a copy of the result-set expression */ |
| 101538 | 102279 | if( iCol<1 || iCol>0xffff ){ |
| 101539 | | - resolveOutOfRangeError(pParse, zType, i+1, nResult); |
| 102280 | + resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2); |
| 101540 | 102281 | return 1; |
| 101541 | 102282 | } |
| 101542 | 102283 | pItem->u.x.iOrderByCol = (u16)iCol; |
| 101543 | 102284 | continue; |
| 101544 | 102285 | } |
| | @@ -102787,13 +103528,12 @@ |
| 102787 | 103528 | ** |
| 102788 | 103529 | ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, |
| 102789 | 103530 | ** if appropriate. |
| 102790 | 103531 | */ |
| 102791 | 103532 | static void exprSetHeight(Expr *p){ |
| 102792 | | - int nHeight = 0; |
| 102793 | | - heightOfExpr(p->pLeft, &nHeight); |
| 102794 | | - heightOfExpr(p->pRight, &nHeight); |
| 103533 | + int nHeight = p->pLeft ? p->pLeft->nHeight : 0; |
| 103534 | + if( p->pRight && p->pRight->nHeight>nHeight ) nHeight = p->pRight->nHeight; |
| 102795 | 103535 | if( ExprUseXSelect(p) ){ |
| 102796 | 103536 | heightOfSelect(p->x.pSelect, &nHeight); |
| 102797 | 103537 | }else if( p->x.pList ){ |
| 102798 | 103538 | heightOfExprList(p->x.pList, &nHeight); |
| 102799 | 103539 | p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); |
| | @@ -103088,10 +103828,11 @@ |
| 103088 | 103828 | pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); |
| 103089 | 103829 | if( pNew==0 ){ |
| 103090 | 103830 | sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ |
| 103091 | 103831 | return 0; |
| 103092 | 103832 | } |
| 103833 | + pNew->w.iOfst = (int)(pToken->z - pParse->zTail); |
| 103093 | 103834 | if( pList |
| 103094 | 103835 | && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] |
| 103095 | 103836 | && !pParse->nested |
| 103096 | 103837 | ){ |
| 103097 | 103838 | sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken); |
| | @@ -103131,11 +103872,11 @@ |
| 103131 | 103872 | ** (2) not tagged with SQLITE_INNOCUOUS (which means it |
| 103132 | 103873 | ** is tagged with SQLITE_FUNC_UNSAFE) and |
| 103133 | 103874 | ** SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning |
| 103134 | 103875 | ** that the schema is possibly tainted). |
| 103135 | 103876 | */ |
| 103136 | | - sqlite3ErrorMsg(pParse, "unsafe use of %s()", pDef->zName); |
| 103877 | + sqlite3ErrorMsg(pParse, "unsafe use of %#T()", pExpr); |
| 103137 | 103878 | } |
| 103138 | 103879 | } |
| 103139 | 103880 | } |
| 103140 | 103881 | |
| 103141 | 103882 | /* |
| | @@ -103187,10 +103928,11 @@ |
| 103187 | 103928 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); |
| 103188 | 103929 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
| 103189 | 103930 | if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
| 103190 | 103931 | sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
| 103191 | 103932 | db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); |
| 103933 | + sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); |
| 103192 | 103934 | return; |
| 103193 | 103935 | } |
| 103194 | 103936 | x = (ynVar)i; |
| 103195 | 103937 | if( x>pParse->nVar ){ |
| 103196 | 103938 | pParse->nVar = (int)x; |
| | @@ -103214,10 +103956,11 @@ |
| 103214 | 103956 | } |
| 103215 | 103957 | } |
| 103216 | 103958 | pExpr->iColumn = x; |
| 103217 | 103959 | if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
| 103218 | 103960 | sqlite3ErrorMsg(pParse, "too many SQL variables"); |
| 103961 | + sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); |
| 103219 | 103962 | } |
| 103220 | 103963 | } |
| 103221 | 103964 | |
| 103222 | 103965 | /* |
| 103223 | 103966 | ** Recursively delete an expression tree. |
| | @@ -105056,12 +105799,11 @@ |
| 105056 | 105799 | assert( !ExprUseYWin(pExpr) ); |
| 105057 | 105800 | ExprSetProperty(pExpr, EP_Subrtn); |
| 105058 | 105801 | assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
| 105059 | 105802 | pExpr->y.sub.regReturn = ++pParse->nMem; |
| 105060 | 105803 | pExpr->y.sub.iAddr = |
| 105061 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1; |
| 105062 | | - VdbeComment((v, "return address")); |
| 105804 | + sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1; |
| 105063 | 105805 | |
| 105064 | 105806 | addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
| 105065 | 105807 | } |
| 105066 | 105808 | |
| 105067 | 105809 | /* Check to see if this is a vector IN operator */ |
| | @@ -105159,10 +105901,11 @@ |
| 105159 | 105901 | ** disable the test that was generated above that makes sure |
| 105160 | 105902 | ** this code only executes once. Because for a non-constant |
| 105161 | 105903 | ** expression we need to rerun this code each time. |
| 105162 | 105904 | */ |
| 105163 | 105905 | if( addrOnce && !sqlite3ExprIsConstant(pE2) ){ |
| 105906 | + sqlite3VdbeChangeToNoop(v, addrOnce-1); |
| 105164 | 105907 | sqlite3VdbeChangeToNoop(v, addrOnce); |
| 105165 | 105908 | ExprClearProperty(pExpr, EP_Subrtn); |
| 105166 | 105909 | addrOnce = 0; |
| 105167 | 105910 | } |
| 105168 | 105911 | |
| | @@ -105179,11 +105922,14 @@ |
| 105179 | 105922 | } |
| 105180 | 105923 | if( addrOnce ){ |
| 105181 | 105924 | sqlite3VdbeJumpHere(v, addrOnce); |
| 105182 | 105925 | /* Subroutine return */ |
| 105183 | 105926 | assert( ExprUseYSub(pExpr) ); |
| 105184 | | - sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn); |
| 105927 | + assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn |
| 105928 | + || pParse->nErr ); |
| 105929 | + sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn, 0, |
| 105930 | + pExpr->y.sub.iAddr-1); |
| 105185 | 105931 | sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1); |
| 105186 | 105932 | sqlite3ClearTempRegCache(pParse); |
| 105187 | 105933 | } |
| 105188 | 105934 | } |
| 105189 | 105935 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| | @@ -105234,13 +105980,11 @@ |
| 105234 | 105980 | assert( !ExprUseYWin(pExpr) ); |
| 105235 | 105981 | assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) ); |
| 105236 | 105982 | ExprSetProperty(pExpr, EP_Subrtn); |
| 105237 | 105983 | pExpr->y.sub.regReturn = ++pParse->nMem; |
| 105238 | 105984 | pExpr->y.sub.iAddr = |
| 105239 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1; |
| 105240 | | - VdbeComment((v, "return address")); |
| 105241 | | - |
| 105985 | + sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1; |
| 105242 | 105986 | |
| 105243 | 105987 | /* The evaluation of the EXISTS/SELECT must be repeated every time it |
| 105244 | 105988 | ** is encountered if any of the following is true: |
| 105245 | 105989 | ** |
| 105246 | 105990 | ** * The right-hand side is a correlated subquery |
| | @@ -105309,11 +106053,14 @@ |
| 105309 | 106053 | sqlite3VdbeJumpHere(v, addrOnce); |
| 105310 | 106054 | } |
| 105311 | 106055 | |
| 105312 | 106056 | /* Subroutine return */ |
| 105313 | 106057 | assert( ExprUseYSub(pExpr) ); |
| 105314 | | - sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn); |
| 106058 | + assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn |
| 106059 | + || pParse->nErr ); |
| 106060 | + sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn, 0, |
| 106061 | + pExpr->y.sub.iAddr-1); |
| 105315 | 106062 | sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1); |
| 105316 | 106063 | sqlite3ClearTempRegCache(pParse); |
| 105317 | 106064 | return rReg; |
| 105318 | 106065 | } |
| 105319 | 106066 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| | @@ -105515,11 +106262,10 @@ |
| 105515 | 106262 | if( destIfNull==destIfFalse ){ |
| 105516 | 106263 | destStep2 = destIfFalse; |
| 105517 | 106264 | }else{ |
| 105518 | 106265 | destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse); |
| 105519 | 106266 | } |
| 105520 | | -// if( pParse->nErr ) goto sqlite3ExprCodeIN_finished; |
| 105521 | 106267 | for(i=0; i<nVector; i++){ |
| 105522 | 106268 | Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i); |
| 105523 | 106269 | if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error; |
| 105524 | 106270 | if( sqlite3ExprCanBeNull(p) ){ |
| 105525 | 106271 | sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2); |
| | @@ -105656,15 +106402,16 @@ |
| 105656 | 106402 | const char *z = pExpr->u.zToken; |
| 105657 | 106403 | assert( z!=0 ); |
| 105658 | 106404 | c = sqlite3DecOrHexToI64(z, &value); |
| 105659 | 106405 | if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){ |
| 105660 | 106406 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 105661 | | - sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); |
| 106407 | + sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr); |
| 105662 | 106408 | #else |
| 105663 | 106409 | #ifndef SQLITE_OMIT_HEX_INTEGER |
| 105664 | 106410 | if( sqlite3_strnicmp(z,"0x",2)==0 ){ |
| 105665 | | - sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z); |
| 106411 | + sqlite3ErrorMsg(pParse, "hex literal too big: %s%#T", |
| 106412 | + negFlag?"-":"",pExpr); |
| 105666 | 106413 | }else |
| 105667 | 106414 | #endif |
| 105668 | 106415 | { |
| 105669 | 106416 | codeReal(v, z, negFlag, iMem); |
| 105670 | 106417 | } |
| | @@ -106336,11 +107083,11 @@ |
| 106336 | 107083 | if( pInfo==0 |
| 106337 | 107084 | || NEVER(pExpr->iAgg<0) |
| 106338 | 107085 | || NEVER(pExpr->iAgg>=pInfo->nFunc) |
| 106339 | 107086 | ){ |
| 106340 | 107087 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 106341 | | - sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); |
| 107088 | + sqlite3ErrorMsg(pParse, "misuse of aggregate: %#T()", pExpr); |
| 106342 | 107089 | }else{ |
| 106343 | 107090 | return pInfo->aFunc[pExpr->iAgg].iMem; |
| 106344 | 107091 | } |
| 106345 | 107092 | break; |
| 106346 | 107093 | } |
| | @@ -106377,11 +107124,11 @@ |
| 106377 | 107124 | if( pDef==0 && pParse->explain ){ |
| 106378 | 107125 | pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0); |
| 106379 | 107126 | } |
| 106380 | 107127 | #endif |
| 106381 | 107128 | if( pDef==0 || pDef->xFinalize!=0 ){ |
| 106382 | | - sqlite3ErrorMsg(pParse, "unknown function: %s()", zId); |
| 107129 | + sqlite3ErrorMsg(pParse, "unknown function: %#T()", pExpr); |
| 106383 | 107130 | break; |
| 106384 | 107131 | } |
| 106385 | 107132 | if( pDef->funcFlags & SQLITE_FUNC_INLINE ){ |
| 106386 | 107133 | assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 ); |
| 106387 | 107134 | assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 ); |
| | @@ -109000,11 +109747,11 @@ |
| 109000 | 109747 | if( !zOld ) goto exit_rename_column; |
| 109001 | 109748 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 109002 | 109749 | if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break; |
| 109003 | 109750 | } |
| 109004 | 109751 | if( iCol==pTab->nCol ){ |
| 109005 | | - sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld); |
| 109752 | + sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pOld); |
| 109006 | 109753 | goto exit_rename_column; |
| 109007 | 109754 | } |
| 109008 | 109755 | |
| 109009 | 109756 | /* Ensure the schema contains no double-quoted strings */ |
| 109010 | 109757 | renameTestSchema(pParse, zDb, iSchema==1, "", 0); |
| | @@ -109430,16 +110177,16 @@ |
| 109430 | 110177 | ){ |
| 109431 | 110178 | const char *zT = (const char*)sqlite3_value_text(pType); |
| 109432 | 110179 | const char *zN = (const char*)sqlite3_value_text(pObject); |
| 109433 | 110180 | char *zErr; |
| 109434 | 110181 | |
| 109435 | | - zErr = sqlite3_mprintf("error in %s %s%s%s: %s", |
| 110182 | + zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s", |
| 109436 | 110183 | zT, zN, (zWhen[0] ? " " : ""), zWhen, |
| 109437 | 110184 | pParse->zErrMsg |
| 109438 | 110185 | ); |
| 109439 | 110186 | sqlite3_result_error(pCtx, zErr, -1); |
| 109440 | | - sqlite3_free(zErr); |
| 110187 | + sqlite3DbFree(pParse->db, zErr); |
| 109441 | 110188 | } |
| 109442 | 110189 | |
| 109443 | 110190 | /* |
| 109444 | 110191 | ** For each name in the the expression-list pEList (i.e. each |
| 109445 | 110192 | ** pEList->a[i].zName) that matches the string in zOld, extract the |
| | @@ -109500,23 +110247,25 @@ |
| 109500 | 110247 | const char *zSql, /* SQL to parse */ |
| 109501 | 110248 | int bTemp /* True if SQL is from temp schema */ |
| 109502 | 110249 | ){ |
| 109503 | 110250 | int rc; |
| 109504 | 110251 | |
| 109505 | | - db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb); |
| 109506 | | - |
| 109507 | | - /* Parse the SQL statement passed as the first argument. If no error |
| 109508 | | - ** occurs and the parse does not result in a new table, index or |
| 109509 | | - ** trigger object, the database must be corrupt. */ |
| 109510 | 110252 | sqlite3ParseObjectInit(p, db); |
| 110253 | + if( zSql==0 ){ |
| 110254 | + return SQLITE_NOMEM; |
| 110255 | + } |
| 110256 | + if( sqlite3StrNICmp(zSql,"CREATE ",7)!=0 ){ |
| 110257 | + return SQLITE_CORRUPT_BKPT; |
| 110258 | + } |
| 110259 | + db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb); |
| 109511 | 110260 | p->eParseMode = PARSE_MODE_RENAME; |
| 109512 | 110261 | p->db = db; |
| 109513 | 110262 | p->nQueryLoop = 1; |
| 109514 | | - rc = zSql ? sqlite3RunParser(p, zSql) : SQLITE_NOMEM; |
| 110263 | + rc = sqlite3RunParser(p, zSql); |
| 109515 | 110264 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
| 109516 | 110265 | if( rc==SQLITE_OK |
| 109517 | | - && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0 |
| 110266 | + && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0) |
| 109518 | 110267 | ){ |
| 109519 | 110268 | rc = SQLITE_CORRUPT_BKPT; |
| 109520 | 110269 | } |
| 109521 | 110270 | |
| 109522 | 110271 | #ifdef SQLITE_DEBUG |
| | @@ -109796,11 +110545,11 @@ |
| 109796 | 110545 | } |
| 109797 | 110546 | |
| 109798 | 110547 | /* |
| 109799 | 110548 | ** SQL function: |
| 109800 | 110549 | ** |
| 109801 | | -** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld) |
| 110550 | +** sqlite_rename_column(SQL,TYPE,OBJ,DB,TABLE,COL,NEWNAME,QUOTE,TEMP) |
| 109802 | 110551 | ** |
| 109803 | 110552 | ** 0. zSql: SQL statement to rewrite |
| 109804 | 110553 | ** 1. type: Type of object ("table", "view" etc.) |
| 109805 | 110554 | ** 2. object: Name of object |
| 109806 | 110555 | ** 3. Database: Database name (e.g. "main") |
| | @@ -109814,11 +110563,12 @@ |
| 109814 | 110563 | ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol |
| 109815 | 110564 | ** into zNew. The name should be quoted if bQuote is true. |
| 109816 | 110565 | ** |
| 109817 | 110566 | ** This function is used internally by the ALTER TABLE RENAME COLUMN command. |
| 109818 | 110567 | ** It is only accessible to SQL created using sqlite3NestedParse(). It is |
| 109819 | | -** not reachable from ordinary SQL passed into sqlite3_prepare(). |
| 110568 | +** not reachable from ordinary SQL passed into sqlite3_prepare() unless the |
| 110569 | +** SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test setting is enabled. |
| 109820 | 110570 | */ |
| 109821 | 110571 | static void renameColumnFunc( |
| 109822 | 110572 | sqlite3_context *context, |
| 109823 | 110573 | int NotUsed, |
| 109824 | 110574 | sqlite3_value **argv |
| | @@ -109963,11 +110713,13 @@ |
| 109963 | 110713 | assert( rc==SQLITE_OK ); |
| 109964 | 110714 | rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote); |
| 109965 | 110715 | |
| 109966 | 110716 | renameColumnFunc_done: |
| 109967 | 110717 | if( rc!=SQLITE_OK ){ |
| 109968 | | - if( sParse.zErrMsg ){ |
| 110718 | + if( rc==SQLITE_ERROR && sqlite3WritableSchema(db) ){ |
| 110719 | + sqlite3_result_value(context, argv[0]); |
| 110720 | + }else if( sParse.zErrMsg ){ |
| 109969 | 110721 | renameColumnParseError(context, "", argv[1], argv[2], &sParse); |
| 109970 | 110722 | }else{ |
| 109971 | 110723 | sqlite3_result_error_code(context, rc); |
| 109972 | 110724 | } |
| 109973 | 110725 | } |
| | @@ -110162,11 +110914,13 @@ |
| 110162 | 110914 | |
| 110163 | 110915 | if( rc==SQLITE_OK ){ |
| 110164 | 110916 | rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote); |
| 110165 | 110917 | } |
| 110166 | 110918 | if( rc!=SQLITE_OK ){ |
| 110167 | | - if( sParse.zErrMsg ){ |
| 110919 | + if( rc==SQLITE_ERROR && sqlite3WritableSchema(db) ){ |
| 110920 | + sqlite3_result_value(context, argv[3]); |
| 110921 | + }else if( sParse.zErrMsg ){ |
| 110168 | 110922 | renameColumnParseError(context, "", argv[1], argv[2], &sParse); |
| 110169 | 110923 | }else{ |
| 110170 | 110924 | sqlite3_result_error_code(context, rc); |
| 110171 | 110925 | } |
| 110172 | 110926 | } |
| | @@ -110187,14 +110941,14 @@ |
| 110187 | 110941 | renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr); |
| 110188 | 110942 | } |
| 110189 | 110943 | return WRC_Continue; |
| 110190 | 110944 | } |
| 110191 | 110945 | |
| 110192 | | -/* |
| 110193 | | -** The implementation of an SQL scalar function that rewrites DDL statements |
| 110194 | | -** so that any string literals that use double-quotes are modified so that |
| 110195 | | -** they use single quotes. |
| 110946 | +/* SQL function: sqlite_rename_quotefix(DB,SQL) |
| 110947 | +** |
| 110948 | +** Rewrite the DDL statement "SQL" so that any string literals that use |
| 110949 | +** double-quotes use single quotes instead. |
| 110196 | 110950 | ** |
| 110197 | 110951 | ** Two arguments must be passed: |
| 110198 | 110952 | ** |
| 110199 | 110953 | ** 0: Database name ("main", "temp" etc.). |
| 110200 | 110954 | ** 1: SQL statement to edit. |
| | @@ -110209,10 +110963,14 @@ |
| 110209 | 110963 | ** ); |
| 110210 | 110964 | ** |
| 110211 | 110965 | ** returns the string: |
| 110212 | 110966 | ** |
| 110213 | 110967 | ** CREATE VIEW v1 AS SELECT "a", 'string' FROM t1 |
| 110968 | +** |
| 110969 | +** If there is a error in the input SQL, then raise an error, except |
| 110970 | +** if PRAGMA writable_schema=ON, then just return the input string |
| 110971 | +** unmodified following an error. |
| 110214 | 110972 | */ |
| 110215 | 110973 | static void renameQuotefixFunc( |
| 110216 | 110974 | sqlite3_context *context, |
| 110217 | 110975 | int NotUsed, |
| 110218 | 110976 | sqlite3_value **argv |
| | @@ -110283,11 +111041,15 @@ |
| 110283 | 111041 | rc = renameEditSql(context, &sCtx, zInput, 0, 0); |
| 110284 | 111042 | } |
| 110285 | 111043 | renameTokenFree(db, sCtx.pList); |
| 110286 | 111044 | } |
| 110287 | 111045 | if( rc!=SQLITE_OK ){ |
| 110288 | | - sqlite3_result_error_code(context, rc); |
| 111046 | + if( sqlite3WritableSchema(db) && rc==SQLITE_ERROR ){ |
| 111047 | + sqlite3_result_value(context, argv[1]); |
| 111048 | + }else{ |
| 111049 | + sqlite3_result_error_code(context, rc); |
| 111050 | + } |
| 110289 | 111051 | } |
| 110290 | 111052 | renameParseCleanup(&sParse); |
| 110291 | 111053 | } |
| 110292 | 111054 | |
| 110293 | 111055 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| | @@ -110295,11 +111057,12 @@ |
| 110295 | 111057 | #endif |
| 110296 | 111058 | |
| 110297 | 111059 | sqlite3BtreeLeaveAll(db); |
| 110298 | 111060 | } |
| 110299 | 111061 | |
| 110300 | | -/* |
| 111062 | +/* Function: sqlite_rename_test(DB,SQL,TYPE,NAME,ISTEMP,WHEN,DQS) |
| 111063 | +** |
| 110301 | 111064 | ** An SQL user function that checks that there are no parse or symbol |
| 110302 | 111065 | ** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement. |
| 110303 | 111066 | ** After an ALTER TABLE .. RENAME operation is performed and the schema |
| 110304 | 111067 | ** reloaded, this function is called on each SQL statement in the schema |
| 110305 | 111068 | ** to ensure that it is still usable. |
| | @@ -110310,15 +111073,17 @@ |
| 110310 | 111073 | ** 3: Object name. |
| 110311 | 111074 | ** 4: True if object is from temp schema. |
| 110312 | 111075 | ** 5: "when" part of error message. |
| 110313 | 111076 | ** 6: True to disable the DQS quirk when parsing SQL. |
| 110314 | 111077 | ** |
| 110315 | | -** Unless it finds an error, this function normally returns NULL. However, it |
| 110316 | | -** returns integer value 1 if: |
| 111078 | +** The return value is computed as follows: |
| 110317 | 111079 | ** |
| 110318 | | -** * the SQL argument creates a trigger, and |
| 110319 | | -** * the table that the trigger is attached to is in database zDb. |
| 111080 | +** A. If an error is seen and not in PRAGMA writable_schema=ON mode, |
| 111081 | +** then raise the error. |
| 111082 | +** B. Else if a trigger is created and the the table that the trigger is |
| 111083 | +** attached to is in database zDb, then return 1. |
| 111084 | +** C. Otherwise return NULL. |
| 110320 | 111085 | */ |
| 110321 | 111086 | static void renameTableTest( |
| 110322 | 111087 | sqlite3_context *context, |
| 110323 | 111088 | int NotUsed, |
| 110324 | 111089 | sqlite3_value **argv |
| | @@ -110359,16 +111124,20 @@ |
| 110359 | 111124 | rc = renameResolveTrigger(&sParse); |
| 110360 | 111125 | } |
| 110361 | 111126 | if( rc==SQLITE_OK ){ |
| 110362 | 111127 | int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema); |
| 110363 | 111128 | int i2 = sqlite3FindDbName(db, zDb); |
| 110364 | | - if( i1==i2 ) sqlite3_result_int(context, 1); |
| 111129 | + if( i1==i2 ){ |
| 111130 | + /* Handle output case B */ |
| 111131 | + sqlite3_result_int(context, 1); |
| 111132 | + } |
| 110365 | 111133 | } |
| 110366 | 111134 | } |
| 110367 | 111135 | } |
| 110368 | 111136 | |
| 110369 | | - if( rc!=SQLITE_OK && zWhen ){ |
| 111137 | + if( rc!=SQLITE_OK && zWhen && !sqlite3WritableSchema(db) ){ |
| 111138 | + /* Output case A */ |
| 110370 | 111139 | renameColumnParseError(context, zWhen, argv[2], argv[3],&sParse); |
| 110371 | 111140 | } |
| 110372 | 111141 | renameParseCleanup(&sParse); |
| 110373 | 111142 | } |
| 110374 | 111143 | |
| | @@ -110480,11 +111249,11 @@ |
| 110480 | 111249 | assert( db->mallocFailed ); |
| 110481 | 111250 | goto exit_drop_column; |
| 110482 | 111251 | } |
| 110483 | 111252 | iCol = sqlite3ColumnIndex(pTab, zCol); |
| 110484 | 111253 | if( iCol<0 ){ |
| 110485 | | - sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zCol); |
| 111254 | + sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pName); |
| 110486 | 111255 | goto exit_drop_column; |
| 110487 | 111256 | } |
| 110488 | 111257 | |
| 110489 | 111258 | /* Do not allow the user to drop a PRIMARY KEY column or a column |
| 110490 | 111259 | ** constrained by a UNIQUE constraint. */ |
| | @@ -111633,11 +112402,11 @@ |
| 111633 | 112402 | if( pStat1==0 ) return; |
| 111634 | 112403 | pStat1->zName = (char*)&pStat1[1]; |
| 111635 | 112404 | memcpy(pStat1->zName, "sqlite_stat1", 13); |
| 111636 | 112405 | pStat1->nCol = 3; |
| 111637 | 112406 | pStat1->iPKey = -1; |
| 111638 | | - sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNBLOB); |
| 112407 | + sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC); |
| 111639 | 112408 | } |
| 111640 | 112409 | #endif |
| 111641 | 112410 | |
| 111642 | 112411 | /* Establish a read-lock on the table at the shared-cache level. |
| 111643 | 112412 | ** Open a read-only cursor on the table. Also allocate a cursor number |
| | @@ -113629,11 +114398,13 @@ |
| 113629 | 114398 | && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr) |
| 113630 | 114399 | ){ |
| 113631 | 114400 | int iDb, i; |
| 113632 | 114401 | assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); |
| 113633 | 114402 | sqlite3VdbeJumpHere(v, 0); |
| 113634 | | - for(iDb=0; iDb<db->nDb; iDb++){ |
| 114403 | + assert( db->nDb>0 ); |
| 114404 | + iDb = 0; |
| 114405 | + do{ |
| 113635 | 114406 | Schema *pSchema; |
| 113636 | 114407 | if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; |
| 113637 | 114408 | sqlite3VdbeUsesBtree(v, iDb); |
| 113638 | 114409 | pSchema = db->aDb[iDb].pSchema; |
| 113639 | 114410 | sqlite3VdbeAddOp4Int(v, |
| | @@ -113644,11 +114415,11 @@ |
| 113644 | 114415 | pSchema->iGeneration /* P4 */ |
| 113645 | 114416 | ); |
| 113646 | 114417 | if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); |
| 113647 | 114418 | VdbeComment((v, |
| 113648 | 114419 | "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); |
| 113649 | | - } |
| 114420 | + }while( ++iDb<db->nDb ); |
| 113650 | 114421 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 113651 | 114422 | for(i=0; i<pParse->nVtabLock; i++){ |
| 113652 | 114423 | char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
| 113653 | 114424 | sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
| 113654 | 114425 | } |
| | @@ -113748,10 +114519,12 @@ |
| 113748 | 114519 | pParse->nested++; |
| 113749 | 114520 | memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); |
| 113750 | 114521 | memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
| 113751 | 114522 | db->mDbFlags |= DBFLAG_PreferBuiltin; |
| 113752 | 114523 | sqlite3RunParser(pParse, zSql); |
| 114524 | + sqlite3DbFree(db, pParse->zErrMsg); |
| 114525 | + pParse->zErrMsg = 0; |
| 113753 | 114526 | db->mDbFlags = savedDbFlags; |
| 113754 | 114527 | sqlite3DbFree(db, zSql); |
| 113755 | 114528 | memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); |
| 113756 | 114529 | pParse->nested--; |
| 113757 | 114530 | } |
| | @@ -114708,11 +115481,12 @@ |
| 114708 | 115481 | goto begin_table_error; |
| 114709 | 115482 | } |
| 114710 | 115483 | pTable = sqlite3FindTable(db, zName, zDb); |
| 114711 | 115484 | if( pTable ){ |
| 114712 | 115485 | if( !noErr ){ |
| 114713 | | - sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
| 115486 | + sqlite3ErrorMsg(pParse, "%s %T already exists", |
| 115487 | + (IsView(pTable)? "view" : "table"), pName); |
| 114714 | 115488 | }else{ |
| 114715 | 115489 | assert( !db->init.busy || CORRUPT_DB ); |
| 114716 | 115490 | sqlite3CodeVerifySchema(pParse, iDb); |
| 114717 | 115491 | sqlite3ForceNotReadOnly(pParse); |
| 114718 | 115492 | } |
| | @@ -116238,10 +117012,15 @@ |
| 116238 | 117012 | int regRec; /* A record to be insert into the new table */ |
| 116239 | 117013 | int regRowid; /* Rowid of the next row to insert */ |
| 116240 | 117014 | int addrInsLoop; /* Top of the loop for inserting rows */ |
| 116241 | 117015 | Table *pSelTab; /* A table that describes the SELECT results */ |
| 116242 | 117016 | |
| 117017 | + if( IN_SPECIAL_PARSE ){ |
| 117018 | + pParse->rc = SQLITE_ERROR; |
| 117019 | + pParse->nErr++; |
| 117020 | + return; |
| 117021 | + } |
| 116243 | 117022 | regYield = ++pParse->nMem; |
| 116244 | 117023 | regRec = ++pParse->nMem; |
| 116245 | 117024 | regRowid = ++pParse->nMem; |
| 116246 | 117025 | assert(pParse->nTab==1); |
| 116247 | 117026 | sqlite3MayAbort(pParse); |
| | @@ -120069,11 +120848,11 @@ |
| 120069 | 120848 | ** |
| 120070 | 120849 | ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values. |
| 120071 | 120850 | ** ONEPASS_SINGLE: One-pass approach - at most one row deleted. |
| 120072 | 120851 | ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted. |
| 120073 | 120852 | */ |
| 120074 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1); |
| 120853 | + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1); |
| 120075 | 120854 | if( pWInfo==0 ) goto delete_from_cleanup; |
| 120076 | 120855 | eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
| 120077 | 120856 | assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI ); |
| 120078 | 120857 | assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF ); |
| 120079 | 120858 | if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse); |
| | @@ -120682,10 +121461,11 @@ |
| 120682 | 121461 | static void subtypeFunc( |
| 120683 | 121462 | sqlite3_context *context, |
| 120684 | 121463 | int argc, |
| 120685 | 121464 | sqlite3_value **argv |
| 120686 | 121465 | ){ |
| 121466 | + UNUSED_PARAMETER(argc); |
| 120687 | 121467 | sqlite3_result_int(context, sqlite3_value_subtype(argv[0])); |
| 120688 | 121468 | } |
| 120689 | 121469 | |
| 120690 | 121470 | /* |
| 120691 | 121471 | ** Implementation of the length() function |
| | @@ -121695,12 +122475,13 @@ |
| 121695 | 122475 | UNUSED_PARAMETER(argc); |
| 121696 | 122476 | sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); |
| 121697 | 122477 | sqlite3QuoteValue(&str,argv[0]); |
| 121698 | 122478 | sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar, |
| 121699 | 122479 | SQLITE_DYNAMIC); |
| 121700 | | - if( str.accError==SQLITE_NOMEM ){ |
| 121701 | | - sqlite3_result_error_nomem(context); |
| 122480 | + if( str.accError!=SQLITE_OK ){ |
| 122481 | + sqlite3_result_null(context); |
| 122482 | + sqlite3_result_error_code(context, str.accError); |
| 121702 | 122483 | } |
| 121703 | 122484 | } |
| 121704 | 122485 | |
| 121705 | 122486 | /* |
| 121706 | 122487 | ** The unicode() function. Return the integer unicode code-point value |
| | @@ -122815,12 +123596,12 @@ |
| 122815 | 123596 | #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 122816 | 123597 | INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), |
| 122817 | 123598 | INLINE_FUNC(likelihood, 2, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), |
| 122818 | 123599 | INLINE_FUNC(likely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), |
| 122819 | 123600 | #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 122820 | | - FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET| |
| 122821 | | - SQLITE_FUNC_TYPEOF), |
| 123601 | + {1, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_OFFSET|SQLITE_FUNC_TYPEOF, |
| 123602 | + 0, 0, noopFunc, 0, 0, 0, "sqlite_offset", {0} }, |
| 122822 | 123603 | #endif |
| 122823 | 123604 | FUNCTION(ltrim, 1, 1, 0, trimFunc ), |
| 122824 | 123605 | FUNCTION(ltrim, 2, 1, 0, trimFunc ), |
| 122825 | 123606 | FUNCTION(rtrim, 1, 2, 0, trimFunc ), |
| 122826 | 123607 | FUNCTION(rtrim, 2, 2, 0, trimFunc ), |
| | @@ -123612,11 +124393,11 @@ |
| 123612 | 124393 | |
| 123613 | 124394 | /* Create VDBE to loop through the entries in pSrc that match the WHERE |
| 123614 | 124395 | ** clause. For each row found, increment either the deferred or immediate |
| 123615 | 124396 | ** foreign key constraint counter. */ |
| 123616 | 124397 | if( pParse->nErr==0 ){ |
| 123617 | | - pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0); |
| 124398 | + pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0, 0); |
| 123618 | 124399 | sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr); |
| 123619 | 124400 | if( pWInfo ){ |
| 123620 | 124401 | sqlite3WhereEnd(pWInfo); |
| 123621 | 124402 | } |
| 123622 | 124403 | } |
| | @@ -124620,11 +125401,11 @@ |
| 124620 | 125401 | sqlite3OomFault(db); |
| 124621 | 125402 | return; |
| 124622 | 125403 | } |
| 124623 | 125404 | |
| 124624 | 125405 | for(i=j=0; i<pTab->nCol; i++){ |
| 124625 | | - assert( pTab->aCol[i].affinity!=0 ); |
| 125406 | + assert( pTab->aCol[i].affinity!=0 || sqlite3VdbeParser(v)->nErr>0 ); |
| 124626 | 125407 | if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){ |
| 124627 | 125408 | zColAff[j++] = pTab->aCol[i].affinity; |
| 124628 | 125409 | } |
| 124629 | 125410 | } |
| 124630 | 125411 | do{ |
| | @@ -125234,11 +126015,15 @@ |
| 125234 | 126015 | ** Then special optimizations can be applied that make the transfer |
| 125235 | 126016 | ** very fast and which reduce fragmentation of indices. |
| 125236 | 126017 | ** |
| 125237 | 126018 | ** This is the 2nd template. |
| 125238 | 126019 | */ |
| 125239 | | - if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){ |
| 126020 | + if( pColumn==0 |
| 126021 | + && pSelect!=0 |
| 126022 | + && pTrigger==0 |
| 126023 | + && xferOptimization(pParse, pTab, pSelect, onError, iDb) |
| 126024 | + ){ |
| 125240 | 126025 | assert( !pTrigger ); |
| 125241 | 126026 | assert( pList==0 ); |
| 125242 | 126027 | goto insert_end; |
| 125243 | 126028 | } |
| 125244 | 126029 | #endif /* SQLITE_OMIT_XFER_OPT */ |
| | @@ -126717,11 +127502,11 @@ |
| 126717 | 127502 | } |
| 126718 | 127503 | } |
| 126719 | 127504 | if( isUpdate ){ |
| 126720 | 127505 | /* If currently processing the PRIMARY KEY of a WITHOUT ROWID |
| 126721 | 127506 | ** table, only conflict if the new PRIMARY KEY values are actually |
| 126722 | | - ** different from the old. |
| 127507 | + ** different from the old. See TH3 withoutrowid04.test. |
| 126723 | 127508 | ** |
| 126724 | 127509 | ** For a UNIQUE index, only conflict if the PRIMARY KEY values |
| 126725 | 127510 | ** of the matched index row are different from the original PRIMARY |
| 126726 | 127511 | ** KEY values of this row before the update. */ |
| 126727 | 127512 | int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol; |
| | @@ -127205,22 +127990,17 @@ |
| 127205 | 127990 | Vdbe *v; /* The VDBE we are building */ |
| 127206 | 127991 | int regAutoinc; /* Memory register used by AUTOINC */ |
| 127207 | 127992 | int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ |
| 127208 | 127993 | int regData, regRowid; /* Registers holding data and rowid */ |
| 127209 | 127994 | |
| 127210 | | - if( pSelect==0 ){ |
| 127211 | | - return 0; /* Must be of the form INSERT INTO ... SELECT ... */ |
| 127212 | | - } |
| 127995 | + assert( pSelect!=0 ); |
| 127213 | 127996 | if( pParse->pWith || pSelect->pWith ){ |
| 127214 | 127997 | /* Do not attempt to process this query if there are an WITH clauses |
| 127215 | 127998 | ** attached to it. Proceeding may generate a false "no such table: xxx" |
| 127216 | 127999 | ** error if pSelect reads from a CTE named "xxx". */ |
| 127217 | 128000 | return 0; |
| 127218 | 128001 | } |
| 127219 | | - if( sqlite3TriggerList(pParse, pDest) ){ |
| 127220 | | - return 0; /* tab1 must not have triggers */ |
| 127221 | | - } |
| 127222 | 128002 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 127223 | 128003 | if( IsVirtual(pDest) ){ |
| 127224 | 128004 | return 0; /* tab1 must not be a virtual table */ |
| 127225 | 128005 | } |
| 127226 | 128006 | #endif |
| | @@ -128083,10 +128863,18 @@ |
| 128083 | 128863 | void*, void(*)(void*)); |
| 128084 | 128864 | /* Version 3.38.0 and later */ |
| 128085 | 128865 | int (*error_offset)(sqlite3*); |
| 128086 | 128866 | int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**); |
| 128087 | 128867 | int (*vtab_distinct)(sqlite3_index_info*); |
| 128868 | + int (*vtab_in)(sqlite3_index_info*,int,int); |
| 128869 | + int (*vtab_in_first)(sqlite3_value*,sqlite3_value**); |
| 128870 | + int (*vtab_in_next)(sqlite3_value*,sqlite3_value**); |
| 128871 | + /* Version 3.39.0 and later */ |
| 128872 | + int (*deserialize)(sqlite3*,const char*,unsigned char*, |
| 128873 | + sqlite3_int64,sqlite3_int64,unsigned); |
| 128874 | + unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*, |
| 128875 | + unsigned int); |
| 128088 | 128876 | }; |
| 128089 | 128877 | |
| 128090 | 128878 | /* |
| 128091 | 128879 | ** This is the function signature used for all extension entry points. It |
| 128092 | 128880 | ** is also defined in the file "loadext.c". |
| | @@ -128398,10 +129186,17 @@ |
| 128398 | 129186 | #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages |
| 128399 | 129187 | /* Version 3.38.0 and later */ |
| 128400 | 129188 | #define sqlite3_error_offset sqlite3_api->error_offset |
| 128401 | 129189 | #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value |
| 128402 | 129190 | #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct |
| 129191 | +#define sqlite3_vtab_in sqlite3_api->vtab_in |
| 129192 | +#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first |
| 129193 | +#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next |
| 129194 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 129195 | +#define sqlite3_deserialize sqlite3_api->deserialize |
| 129196 | +#define sqlite3_serialize sqlite3_api->serialize |
| 129197 | +#endif |
| 128403 | 129198 | #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 128404 | 129199 | |
| 128405 | 129200 | #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 128406 | 129201 | /* This case when the file really is being compiled as a loadable |
| 128407 | 129202 | ** extension */ |
| | @@ -128891,10 +129686,21 @@ |
| 128891 | 129686 | sqlite3_autovacuum_pages, |
| 128892 | 129687 | /* Version 3.38.0 and later */ |
| 128893 | 129688 | sqlite3_error_offset, |
| 128894 | 129689 | sqlite3_vtab_rhs_value, |
| 128895 | 129690 | sqlite3_vtab_distinct, |
| 129691 | + sqlite3_vtab_in, |
| 129692 | + sqlite3_vtab_in_first, |
| 129693 | + sqlite3_vtab_in_next, |
| 129694 | + /* Version 3.39.0 and later */ |
| 129695 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 129696 | + sqlite3_deserialize, |
| 129697 | + sqlite3_serialize |
| 129698 | +#else |
| 129699 | + 0, |
| 129700 | + 0 |
| 129701 | +#endif |
| 128896 | 129702 | }; |
| 128897 | 129703 | |
| 128898 | 129704 | /* True if x is the directory separator character |
| 128899 | 129705 | */ |
| 128900 | 129706 | #if SQLITE_OS_WIN |
| | @@ -129561,11 +130367,11 @@ |
| 129561 | 130367 | /* iArg: */ BTREE_DATA_VERSION }, |
| 129562 | 130368 | #endif |
| 129563 | 130369 | #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) |
| 129564 | 130370 | {/* zName: */ "database_list", |
| 129565 | 130371 | /* ePragTyp: */ PragTyp_DATABASE_LIST, |
| 129566 | | - /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0, |
| 130372 | + /* ePragFlg: */ PragFlg_Result0, |
| 129567 | 130373 | /* ColNames: */ 47, 3, |
| 129568 | 130374 | /* iArg: */ 0 }, |
| 129569 | 130375 | #endif |
| 129570 | 130376 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) |
| 129571 | 130377 | {/* zName: */ "default_cache_size", |
| | @@ -130249,19 +131055,20 @@ |
| 130249 | 131055 | Vdbe *v, /* The prepared statement being created */ |
| 130250 | 131056 | FuncDef *p, /* A particular function definition */ |
| 130251 | 131057 | int isBuiltin, /* True if this is a built-in function */ |
| 130252 | 131058 | int showInternFuncs /* True if showing internal functions */ |
| 130253 | 131059 | ){ |
| 131060 | + u32 mask = |
| 131061 | + SQLITE_DETERMINISTIC | |
| 131062 | + SQLITE_DIRECTONLY | |
| 131063 | + SQLITE_SUBTYPE | |
| 131064 | + SQLITE_INNOCUOUS | |
| 131065 | + SQLITE_FUNC_INTERNAL |
| 131066 | + ; |
| 131067 | + if( showInternFuncs ) mask = 0xffffffff; |
| 130254 | 131068 | for(; p; p=p->pNext){ |
| 130255 | 131069 | const char *zType; |
| 130256 | | - static const u32 mask = |
| 130257 | | - SQLITE_DETERMINISTIC | |
| 130258 | | - SQLITE_DIRECTONLY | |
| 130259 | | - SQLITE_SUBTYPE | |
| 130260 | | - SQLITE_INNOCUOUS | |
| 130261 | | - SQLITE_FUNC_INTERNAL |
| 130262 | | - ; |
| 130263 | 131070 | static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" }; |
| 130264 | 131071 | |
| 130265 | 131072 | assert( SQLITE_FUNC_ENCMASK==0x3 ); |
| 130266 | 131073 | assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 ); |
| 130267 | 131074 | assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 ); |
| | @@ -130749,11 +131556,11 @@ |
| 130749 | 131556 | ** |
| 130750 | 131557 | ** Do N steps of incremental vacuuming on a database. |
| 130751 | 131558 | */ |
| 130752 | 131559 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 130753 | 131560 | case PragTyp_INCREMENTAL_VACUUM: { |
| 130754 | | - int iLimit, addr; |
| 131561 | + int iLimit = 0, addr; |
| 130755 | 131562 | if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ |
| 130756 | 131563 | iLimit = 0x7fffffff; |
| 130757 | 131564 | } |
| 130758 | 131565 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 130759 | 131566 | sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1); |
| | @@ -133053,11 +133860,11 @@ |
| 133053 | 133860 | if( db->mallocFailed ){ |
| 133054 | 133861 | rc = SQLITE_NOMEM_BKPT; |
| 133055 | 133862 | sqlite3ResetAllSchemasOfConnection(db); |
| 133056 | 133863 | pDb = &db->aDb[iDb]; |
| 133057 | 133864 | }else |
| 133058 | | - if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){ |
| 133865 | + if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){ |
| 133059 | 133866 | /* Hack: If the SQLITE_NoSchemaError flag is set, then consider |
| 133060 | 133867 | ** the schema loaded, even if errors (other than OOM) occurred. In |
| 133061 | 133868 | ** this situation the current sqlite3_prepare() operation will fail, |
| 133062 | 133869 | ** but the following one will attempt to compile the supplied statement |
| 133063 | 133870 | ** against whatever subset of the schema was loaded before the error |
| | @@ -133324,10 +134131,11 @@ |
| 133324 | 134131 | memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
| 133325 | 134132 | assert( db->pParse!=pParse ); |
| 133326 | 134133 | pParse->pOuterParse = db->pParse; |
| 133327 | 134134 | db->pParse = pParse; |
| 133328 | 134135 | pParse->db = db; |
| 134136 | + if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory"); |
| 133329 | 134137 | } |
| 133330 | 134138 | |
| 133331 | 134139 | /* |
| 133332 | 134140 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 133333 | 134141 | */ |
| | @@ -133350,11 +134158,11 @@ |
| 133350 | 134158 | sParse.pOuterParse = db->pParse; |
| 133351 | 134159 | db->pParse = &sParse; |
| 133352 | 134160 | sParse.db = db; |
| 133353 | 134161 | sParse.pReprepare = pReprepare; |
| 133354 | 134162 | assert( ppStmt && *ppStmt==0 ); |
| 133355 | | - /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */ |
| 134163 | + if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory"); |
| 133356 | 134164 | assert( sqlite3_mutex_held(db->mutex) ); |
| 133357 | 134165 | |
| 133358 | 134166 | /* For a long-term use prepared statement avoid the use of |
| 133359 | 134167 | ** lookaside memory. |
| 133360 | 134168 | */ |
| | @@ -133741,11 +134549,11 @@ |
| 133741 | 134549 | ** how to process the DISTINCT keyword, to simplify passing that information |
| 133742 | 134550 | ** into the selectInnerLoop() routine. |
| 133743 | 134551 | */ |
| 133744 | 134552 | typedef struct DistinctCtx DistinctCtx; |
| 133745 | 134553 | struct DistinctCtx { |
| 133746 | | - u8 isTnct; /* True if the DISTINCT keyword is present */ |
| 134554 | + u8 isTnct; /* 0: Not distinct. 1: DISTICT 2: DISTINCT and ORDER BY */ |
| 133747 | 134555 | u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */ |
| 133748 | 134556 | int tabTnct; /* Ephemeral table used for DISTINCT processing */ |
| 133749 | 134557 | int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */ |
| 133750 | 134558 | }; |
| 133751 | 134559 | |
| | @@ -134074,29 +134882,29 @@ |
| 134074 | 134882 | ** sqlite3PExpr(). */ |
| 134075 | 134883 | if( pEq && isOuterJoin ){ |
| 134076 | 134884 | ExprSetProperty(pEq, EP_FromJoin); |
| 134077 | 134885 | assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) ); |
| 134078 | 134886 | ExprSetVVAProperty(pEq, EP_NoReduce); |
| 134079 | | - pEq->iRightJoinTable = pE2->iTable; |
| 134887 | + pEq->w.iRightJoinTable = pE2->iTable; |
| 134080 | 134888 | } |
| 134081 | 134889 | *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq); |
| 134082 | 134890 | } |
| 134083 | 134891 | |
| 134084 | 134892 | /* |
| 134085 | 134893 | ** Set the EP_FromJoin property on all terms of the given expression. |
| 134086 | | -** And set the Expr.iRightJoinTable to iTable for every term in the |
| 134894 | +** And set the Expr.w.iRightJoinTable to iTable for every term in the |
| 134087 | 134895 | ** expression. |
| 134088 | 134896 | ** |
| 134089 | 134897 | ** The EP_FromJoin property is used on terms of an expression to tell |
| 134090 | 134898 | ** the LEFT OUTER JOIN processing logic that this term is part of the |
| 134091 | 134899 | ** join restriction specified in the ON or USING clause and not a part |
| 134092 | 134900 | ** of the more general WHERE clause. These terms are moved over to the |
| 134093 | 134901 | ** WHERE clause during join processing but we need to remember that they |
| 134094 | 134902 | ** originated in the ON or USING clause. |
| 134095 | 134903 | ** |
| 134096 | | -** The Expr.iRightJoinTable tells the WHERE clause processing that the |
| 134097 | | -** expression depends on table iRightJoinTable even if that table is not |
| 134904 | +** The Expr.w.iRightJoinTable tells the WHERE clause processing that the |
| 134905 | +** expression depends on table w.iRightJoinTable even if that table is not |
| 134098 | 134906 | ** explicitly mentioned in the expression. That information is needed |
| 134099 | 134907 | ** for cases like this: |
| 134100 | 134908 | ** |
| 134101 | 134909 | ** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5 |
| 134102 | 134910 | ** |
| | @@ -134110,11 +134918,11 @@ |
| 134110 | 134918 | SQLITE_PRIVATE void sqlite3SetJoinExpr(Expr *p, int iTable){ |
| 134111 | 134919 | while( p ){ |
| 134112 | 134920 | ExprSetProperty(p, EP_FromJoin); |
| 134113 | 134921 | assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); |
| 134114 | 134922 | ExprSetVVAProperty(p, EP_NoReduce); |
| 134115 | | - p->iRightJoinTable = iTable; |
| 134923 | + p->w.iRightJoinTable = iTable; |
| 134116 | 134924 | if( p->op==TK_FUNCTION ){ |
| 134117 | 134925 | assert( ExprUseXList(p) ); |
| 134118 | 134926 | if( p->x.pList ){ |
| 134119 | 134927 | int i; |
| 134120 | 134928 | for(i=0; i<p->x.pList->nExpr; i++){ |
| | @@ -134126,19 +134934,19 @@ |
| 134126 | 134934 | p = p->pRight; |
| 134127 | 134935 | } |
| 134128 | 134936 | } |
| 134129 | 134937 | |
| 134130 | 134938 | /* Undo the work of sqlite3SetJoinExpr(). In the expression p, convert every |
| 134131 | | -** term that is marked with EP_FromJoin and iRightJoinTable==iTable into |
| 134939 | +** term that is marked with EP_FromJoin and w.iRightJoinTable==iTable into |
| 134132 | 134940 | ** an ordinary term that omits the EP_FromJoin mark. |
| 134133 | 134941 | ** |
| 134134 | 134942 | ** This happens when a LEFT JOIN is simplified into an ordinary JOIN. |
| 134135 | 134943 | */ |
| 134136 | 134944 | static void unsetJoinExpr(Expr *p, int iTable){ |
| 134137 | 134945 | while( p ){ |
| 134138 | 134946 | if( ExprHasProperty(p, EP_FromJoin) |
| 134139 | | - && (iTable<0 || p->iRightJoinTable==iTable) ){ |
| 134947 | + && (iTable<0 || p->w.iRightJoinTable==iTable) ){ |
| 134140 | 134948 | ExprClearProperty(p, EP_FromJoin); |
| 134141 | 134949 | } |
| 134142 | 134950 | if( p->op==TK_COLUMN && p->iTable==iTable ){ |
| 134143 | 134951 | ExprClearProperty(p, EP_CanBeNull); |
| 134144 | 134952 | } |
| | @@ -136343,11 +137151,11 @@ |
| 136343 | 137151 | ** (3) There is no ORDER BY clause |
| 136344 | 137152 | ** |
| 136345 | 137153 | ** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES |
| 136346 | 137154 | ** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))"). |
| 136347 | 137155 | ** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case. |
| 136348 | | -** Since the limit is exactly 1, we only need to evalutes the left-most VALUES. |
| 137156 | +** Since the limit is exactly 1, we only need to evaluate the left-most VALUES. |
| 136349 | 137157 | */ |
| 136350 | 137158 | static int multiSelectValues( |
| 136351 | 137159 | Parse *pParse, /* Parsing context */ |
| 136352 | 137160 | Select *p, /* The right-most of SELECTs to be coded */ |
| 136353 | 137161 | SelectDest *pDest /* What to do with query results */ |
| | @@ -137368,13 +138176,13 @@ |
| 137368 | 138176 | SubstContext *pSubst, /* Description of the substitution */ |
| 137369 | 138177 | Expr *pExpr /* Expr in which substitution occurs */ |
| 137370 | 138178 | ){ |
| 137371 | 138179 | if( pExpr==0 ) return 0; |
| 137372 | 138180 | if( ExprHasProperty(pExpr, EP_FromJoin) |
| 137373 | | - && pExpr->iRightJoinTable==pSubst->iTable |
| 138181 | + && pExpr->w.iRightJoinTable==pSubst->iTable |
| 137374 | 138182 | ){ |
| 137375 | | - pExpr->iRightJoinTable = pSubst->iNewTable; |
| 138183 | + pExpr->w.iRightJoinTable = pSubst->iNewTable; |
| 137376 | 138184 | } |
| 137377 | 138185 | if( pExpr->op==TK_COLUMN |
| 137378 | 138186 | && pExpr->iTable==pSubst->iTable |
| 137379 | 138187 | && !ExprHasProperty(pExpr, EP_FixedCol) |
| 137380 | 138188 | ){ |
| | @@ -137409,11 +138217,11 @@ |
| 137409 | 138217 | } |
| 137410 | 138218 | if( pSubst->isLeftJoin ){ |
| 137411 | 138219 | ExprSetProperty(pNew, EP_CanBeNull); |
| 137412 | 138220 | } |
| 137413 | 138221 | if( ExprHasProperty(pExpr,EP_FromJoin) ){ |
| 137414 | | - sqlite3SetJoinExpr(pNew, pExpr->iRightJoinTable); |
| 138222 | + sqlite3SetJoinExpr(pNew, pExpr->w.iRightJoinTable); |
| 137415 | 138223 | } |
| 137416 | 138224 | sqlite3ExprDelete(db, pExpr); |
| 137417 | 138225 | pExpr = pNew; |
| 137418 | 138226 | |
| 137419 | 138227 | /* Ensure that the expression now has an implicit collation sequence, |
| | @@ -137574,11 +138382,11 @@ |
| 137574 | 138382 | int op = pExpr->op; |
| 137575 | 138383 | if( op==TK_COLUMN || op==TK_IF_NULL_ROW ){ |
| 137576 | 138384 | renumberCursorDoMapping(pWalker, &pExpr->iTable); |
| 137577 | 138385 | } |
| 137578 | 138386 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 137579 | | - renumberCursorDoMapping(pWalker, &pExpr->iRightJoinTable); |
| 138387 | + renumberCursorDoMapping(pWalker, &pExpr->w.iRightJoinTable); |
| 137580 | 138388 | } |
| 137581 | 138389 | return WRC_Continue; |
| 137582 | 138390 | } |
| 137583 | 138391 | |
| 137584 | 138392 | /* |
| | @@ -138584,15 +139392,17 @@ |
| 138584 | 139392 | iCursor, isLeftJoin); |
| 138585 | 139393 | pWhere = pWhere->pLeft; |
| 138586 | 139394 | } |
| 138587 | 139395 | if( isLeftJoin |
| 138588 | 139396 | && (ExprHasProperty(pWhere,EP_FromJoin)==0 |
| 138589 | | - || pWhere->iRightJoinTable!=iCursor) |
| 139397 | + || pWhere->w.iRightJoinTable!=iCursor) |
| 138590 | 139398 | ){ |
| 138591 | 139399 | return 0; /* restriction (4) */ |
| 138592 | 139400 | } |
| 138593 | | - if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){ |
| 139401 | + if( ExprHasProperty(pWhere,EP_FromJoin) |
| 139402 | + && pWhere->w.iRightJoinTable!=iCursor |
| 139403 | + ){ |
| 138594 | 139404 | return 0; /* restriction (5) */ |
| 138595 | 139405 | } |
| 138596 | 139406 | if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){ |
| 138597 | 139407 | nChng++; |
| 138598 | 139408 | pSubq->selFlags |= SF_PushDown; |
| | @@ -140539,10 +141349,11 @@ |
| 140539 | 141349 | p->selFlags |= SF_Aggregate; |
| 140540 | 141350 | /* Notice that even thought SF_Distinct has been cleared from p->selFlags, |
| 140541 | 141351 | ** the sDistinct.isTnct is still set. Hence, isTnct represents the |
| 140542 | 141352 | ** original setting of the SF_Distinct flag, not the current setting */ |
| 140543 | 141353 | assert( sDistinct.isTnct ); |
| 141354 | + sDistinct.isTnct = 2; |
| 140544 | 141355 | |
| 140545 | 141356 | #if SELECTTRACE_ENABLED |
| 140546 | 141357 | if( sqlite3SelectTrace & 0x400 ){ |
| 140547 | 141358 | SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n")); |
| 140548 | 141359 | sqlite3TreeViewSelect(0, p, 0); |
| | @@ -140618,11 +141429,11 @@ |
| 140618 | 141429 | |
| 140619 | 141430 | |
| 140620 | 141431 | /* Begin the database scan. */ |
| 140621 | 141432 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 140622 | 141433 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy, |
| 140623 | | - p->pEList, wctrlFlags, p->nSelectRow); |
| 141434 | + p->pEList, p, wctrlFlags, p->nSelectRow); |
| 140624 | 141435 | if( pWInfo==0 ) goto select_end; |
| 140625 | 141436 | if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){ |
| 140626 | 141437 | p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo); |
| 140627 | 141438 | } |
| 140628 | 141439 | if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){ |
| | @@ -140882,11 +141693,12 @@ |
| 140882 | 141693 | ** in the right order to begin with. |
| 140883 | 141694 | */ |
| 140884 | 141695 | sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); |
| 140885 | 141696 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 140886 | 141697 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct, |
| 140887 | | - WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0 |
| 141698 | + 0, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY) |
| 141699 | + | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0 |
| 140888 | 141700 | ); |
| 140889 | 141701 | if( pWInfo==0 ){ |
| 140890 | 141702 | sqlite3ExprListDelete(db, pDistinct); |
| 140891 | 141703 | goto select_end; |
| 140892 | 141704 | } |
| | @@ -141064,11 +141876,11 @@ |
| 141064 | 141876 | resetAccumulator(pParse, pAggInfo); |
| 141065 | 141877 | sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag); |
| 141066 | 141878 | VdbeComment((v, "indicate accumulator empty")); |
| 141067 | 141879 | sqlite3VdbeAddOp1(v, OP_Return, regReset); |
| 141068 | 141880 | |
| 141069 | | - if( eDist!=WHERE_DISTINCT_NOOP ){ |
| 141881 | + if( distFlag!=0 && eDist!=WHERE_DISTINCT_NOOP ){ |
| 141070 | 141882 | struct AggInfo_func *pF = &pAggInfo->aFunc[0]; |
| 141071 | 141883 | fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr); |
| 141072 | 141884 | } |
| 141073 | 141885 | } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */ |
| 141074 | 141886 | else { |
| | @@ -141180,11 +141992,11 @@ |
| 141180 | 141992 | assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 ); |
| 141181 | 141993 | assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 ); |
| 141182 | 141994 | |
| 141183 | 141995 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 141184 | 141996 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy, |
| 141185 | | - pDistinct, minMaxFlag|distFlag, 0); |
| 141997 | + pDistinct, 0, minMaxFlag|distFlag, 0); |
| 141186 | 141998 | if( pWInfo==0 ){ |
| 141187 | 141999 | goto select_end; |
| 141188 | 142000 | } |
| 141189 | 142001 | SELECTTRACE(1,pParse,p,("WhereBegin returns\n")); |
| 141190 | 142002 | eDist = sqlite3WhereIsDistinct(pWInfo); |
| | @@ -141538,15 +142350,14 @@ |
| 141538 | 142350 | && 0==sqlite3StrICmp(pTrig->table, pTab->zName) |
| 141539 | 142351 | && pTrig->pTabSchema!=pTmpSchema |
| 141540 | 142352 | ){ |
| 141541 | 142353 | pTrig->pNext = pList; |
| 141542 | 142354 | pList = pTrig; |
| 141543 | | - }else if( pTrig->op==TK_RETURNING |
| 142355 | + }else if( pTrig->op==TK_RETURNING ){ |
| 141544 | 142356 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 141545 | | - && pParse->db->pVtabCtx==0 |
| 142357 | + assert( pParse->db->pVtabCtx==0 ); |
| 141546 | 142358 | #endif |
| 141547 | | - ){ |
| 141548 | 142359 | assert( pParse->bReturning ); |
| 141549 | 142360 | assert( &(pParse->u1.pReturning->retTrig) == pTrig ); |
| 141550 | 142361 | pTrig->table = pTab->zName; |
| 141551 | 142362 | pTrig->pTabSchema = pTab->pSchema; |
| 141552 | 142363 | pTrig->pNext = pList; |
| | @@ -141917,10 +142728,11 @@ |
| 141917 | 142728 | const char *zEnd /* End of SQL text */ |
| 141918 | 142729 | ){ |
| 141919 | 142730 | sqlite3 *db = pParse->db; |
| 141920 | 142731 | TriggerStep *pTriggerStep; |
| 141921 | 142732 | |
| 142733 | + if( pParse->nErr ) return 0; |
| 141922 | 142734 | pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1); |
| 141923 | 142735 | if( pTriggerStep ){ |
| 141924 | 142736 | char *z = (char*)&pTriggerStep[1]; |
| 141925 | 142737 | memcpy(z, pName->z, pName->n); |
| 141926 | 142738 | sqlite3Dequote(z); |
| | @@ -142707,10 +143519,11 @@ |
| 142707 | 143519 | ); |
| 142708 | 143520 | |
| 142709 | 143521 | /* If an existing TriggerPrg could not be located, create a new one. */ |
| 142710 | 143522 | if( !pPrg ){ |
| 142711 | 143523 | pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf); |
| 143524 | + pParse->db->errByteOffset = -1; |
| 142712 | 143525 | } |
| 142713 | 143526 | |
| 142714 | 143527 | return pPrg; |
| 142715 | 143528 | } |
| 142716 | 143529 | |
| | @@ -143623,11 +144436,11 @@ |
| 143623 | 144436 | ** or index, causing a single-pass approach to malfunction. */ |
| 143624 | 144437 | flags = WHERE_ONEPASS_DESIRED; |
| 143625 | 144438 | if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){ |
| 143626 | 144439 | flags |= WHERE_ONEPASS_MULTIROW; |
| 143627 | 144440 | } |
| 143628 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags,iIdxCur); |
| 144441 | + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur); |
| 143629 | 144442 | if( pWInfo==0 ) goto update_cleanup; |
| 143630 | 144443 | |
| 143631 | 144444 | /* A one-pass strategy that might update more than one row may not |
| 143632 | 144445 | ** be used if any column of the index used for the scan is being |
| 143633 | 144446 | ** updated. Otherwise, if there is an index on "b", statements like |
| | @@ -144145,11 +144958,13 @@ |
| 144145 | 144958 | }else{ |
| 144146 | 144959 | regRec = ++pParse->nMem; |
| 144147 | 144960 | regRowid = ++pParse->nMem; |
| 144148 | 144961 | |
| 144149 | 144962 | /* Start scanning the virtual table */ |
| 144150 | | - pWInfo = sqlite3WhereBegin(pParse, pSrc,pWhere,0,0,WHERE_ONEPASS_DESIRED,0); |
| 144963 | + pWInfo = sqlite3WhereBegin( |
| 144964 | + pParse, pSrc, pWhere, 0, 0, 0, WHERE_ONEPASS_DESIRED, 0 |
| 144965 | + ); |
| 144151 | 144966 | if( pWInfo==0 ) return; |
| 144152 | 144967 | |
| 144153 | 144968 | /* Populate the argument registers. */ |
| 144154 | 144969 | for(i=0; i<pTab->nCol; i++){ |
| 144155 | 144970 | assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ); |
| | @@ -145792,10 +146607,11 @@ |
| 145792 | 146607 | pTab = pCtx->pTab; |
| 145793 | 146608 | assert( IsVirtual(pTab) ); |
| 145794 | 146609 | |
| 145795 | 146610 | sqlite3ParseObjectInit(&sParse, db); |
| 145796 | 146611 | sParse.eParseMode = PARSE_MODE_DECLARE_VTAB; |
| 146612 | + sParse.disableTriggers = 1; |
| 145797 | 146613 | /* We should never be able to reach this point while loading the |
| 145798 | 146614 | ** schema. Nevertheless, defend against that (turn off db->init.busy) |
| 145799 | 146615 | ** in case a bug arises. */ |
| 145800 | 146616 | assert( db->init.busy==0 ); |
| 145801 | 146617 | initBusy = db->init.busy; |
| | @@ -146456,14 +147272,16 @@ |
| 146456 | 147272 | u16 nDistinctCol; /* Index columns used to sort for DISTINCT */ |
| 146457 | 147273 | Index *pIndex; /* Index used, or NULL */ |
| 146458 | 147274 | } btree; |
| 146459 | 147275 | struct { /* Information for virtual tables */ |
| 146460 | 147276 | int idxNum; /* Index number */ |
| 146461 | | - u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 147277 | + u32 needFree : 1; /* True if sqlite3_free(idxStr) is needed */ |
| 147278 | + u32 bOmitOffset : 1; /* True to let virtual table handle offset */ |
| 146462 | 147279 | i8 isOrdered; /* True if satisfies ORDER BY */ |
| 146463 | 147280 | u16 omitMask; /* Terms that may be omitted */ |
| 146464 | 147281 | char *idxStr; /* Index identifier string */ |
| 147282 | + u32 mHandleIn; /* Terms to handle as IN(...) instead of == */ |
| 146465 | 147283 | } vtab; |
| 146466 | 147284 | } u; |
| 146467 | 147285 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 146468 | 147286 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 146469 | 147287 | u16 nSkip; /* Number of NULL aLTerm[] entries */ |
| | @@ -146616,10 +147434,11 @@ |
| 146616 | 147434 | #ifdef SQLITE_ENABLE_STAT4 |
| 146617 | 147435 | # define TERM_HIGHTRUTH 0x4000 /* Term excludes few rows */ |
| 146618 | 147436 | #else |
| 146619 | 147437 | # define TERM_HIGHTRUTH 0 /* Only used with STAT4 */ |
| 146620 | 147438 | #endif |
| 147439 | +#define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */ |
| 146621 | 147440 | |
| 146622 | 147441 | /* |
| 146623 | 147442 | ** An instance of the WhereScan object is used as an iterator for locating |
| 146624 | 147443 | ** terms in the WHERE clause that are useful to the query planner. |
| 146625 | 147444 | */ |
| | @@ -146786,10 +147605,13 @@ |
| 146786 | 147605 | Parse *pParse; /* Parsing and code generating context */ |
| 146787 | 147606 | SrcList *pTabList; /* List of tables in the join */ |
| 146788 | 147607 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
| 146789 | 147608 | ExprList *pResultSet; /* Result set of the query */ |
| 146790 | 147609 | Expr *pWhere; /* The complete WHERE clause */ |
| 147610 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 147611 | + Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */ |
| 147612 | +#endif |
| 146791 | 147613 | int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ |
| 146792 | 147614 | int iContinue; /* Jump here to continue with next record */ |
| 146793 | 147615 | int iBreak; /* Jump here to break out of the loop */ |
| 146794 | 147616 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
| 146795 | 147617 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
| | @@ -146871,10 +147693,11 @@ |
| 146871 | 147693 | |
| 146872 | 147694 | /* whereexpr.c: */ |
| 146873 | 147695 | SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*); |
| 146874 | 147696 | SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*); |
| 146875 | 147697 | SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8); |
| 147698 | +SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause*, Select*); |
| 146876 | 147699 | SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); |
| 146877 | 147700 | SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*); |
| 146878 | 147701 | SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); |
| 146879 | 147702 | SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); |
| 146880 | 147703 | SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*); |
| | @@ -146941,10 +147764,11 @@ |
| 146941 | 147764 | #define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */ |
| 146942 | 147765 | #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */ |
| 146943 | 147766 | #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */ |
| 146944 | 147767 | #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */ |
| 146945 | 147768 | #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */ |
| 147769 | +#define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */ |
| 146946 | 147770 | |
| 146947 | 147771 | #endif /* !defined(SQLITE_WHEREINT_H) */ |
| 146948 | 147772 | |
| 146949 | 147773 | /************** End of whereInt.h ********************************************/ |
| 146950 | 147774 | /************** Continuing where we left off in wherecode.c ******************/ |
| | @@ -147719,10 +148543,11 @@ |
| 147719 | 148543 | sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); |
| 147720 | 148544 | VdbeCoverageIf(v, bRev==0); |
| 147721 | 148545 | VdbeCoverageIf(v, bRev!=0); |
| 147722 | 148546 | VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); |
| 147723 | 148547 | j = sqlite3VdbeAddOp0(v, OP_Goto); |
| 148548 | + assert( pLevel->addrSkip==0 ); |
| 147724 | 148549 | pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), |
| 147725 | 148550 | iIdxCur, 0, regBase, nSkip); |
| 147726 | 148551 | VdbeCoverageIf(v, bRev==0); |
| 147727 | 148552 | VdbeCoverageIf(v, bRev!=0); |
| 147728 | 148553 | sqlite3VdbeJumpHere(v, j); |
| | @@ -147989,11 +148814,11 @@ |
| 147989 | 148814 | ** are also excluded. See codeCursorHintIsOrFunction() for details. |
| 147990 | 148815 | */ |
| 147991 | 148816 | if( pTabItem->fg.jointype & JT_LEFT ){ |
| 147992 | 148817 | Expr *pExpr = pTerm->pExpr; |
| 147993 | 148818 | if( !ExprHasProperty(pExpr, EP_FromJoin) |
| 147994 | | - || pExpr->iRightJoinTable!=pTabItem->iCursor |
| 148819 | + || pExpr->w.iRightJoinTable!=pTabItem->iCursor |
| 147995 | 148820 | ){ |
| 147996 | 148821 | sWalker.eCode = 0; |
| 147997 | 148822 | sWalker.xExprCallback = codeCursorHintIsOrFunction; |
| 147998 | 148823 | sqlite3WalkExpr(&sWalker, pTerm->pExpr); |
| 147999 | 148824 | if( sWalker.eCode ) continue; |
| | @@ -148044,18 +148869,26 @@ |
| 148044 | 148869 | ** rowid stored in register iRowid. |
| 148045 | 148870 | ** |
| 148046 | 148871 | ** Normally, this is just: |
| 148047 | 148872 | ** |
| 148048 | 148873 | ** OP_DeferredSeek $iCur $iRowid |
| 148874 | +** |
| 148875 | +** Which causes a seek on $iCur to the row with rowid $iRowid. |
| 148049 | 148876 | ** |
| 148050 | 148877 | ** However, if the scan currently being coded is a branch of an OR-loop and |
| 148051 | | -** the statement currently being coded is a SELECT, then P3 of OP_DeferredSeek |
| 148052 | | -** is set to iIdxCur and P4 is set to point to an array of integers |
| 148053 | | -** containing one entry for each column of the table cursor iCur is open |
| 148054 | | -** on. For each table column, if the column is the i'th column of the |
| 148055 | | -** index, then the corresponding array entry is set to (i+1). If the column |
| 148056 | | -** does not appear in the index at all, the array entry is set to 0. |
| 148878 | +** the statement currently being coded is a SELECT, then additional information |
| 148879 | +** is added that might allow OP_Column to omit the seek and instead do its |
| 148880 | +** lookup on the index, thus avoiding an expensive seek operation. To |
| 148881 | +** enable this optimization, the P3 of OP_DeferredSeek is set to iIdxCur |
| 148882 | +** and P4 is set to an array of integers containing one entry for each column |
| 148883 | +** in the table. For each table column, if the column is the i'th |
| 148884 | +** column of the index, then the corresponding array entry is set to (i+1). |
| 148885 | +** If the column does not appear in the index at all, the array entry is set |
| 148886 | +** to 0. The OP_Column opcode can check this array to see if the column it |
| 148887 | +** wants is in the index and if it is, it will substitute the index cursor |
| 148888 | +** and column number and continue with those new values, rather than seeking |
| 148889 | +** the table cursor. |
| 148057 | 148890 | */ |
| 148058 | 148891 | static void codeDeferredSeek( |
| 148059 | 148892 | WhereInfo *pWInfo, /* Where clause context */ |
| 148060 | 148893 | Index *pIdx, /* Index scan is using */ |
| 148061 | 148894 | int iCur, /* Cursor for IPK b-tree */ |
| | @@ -148316,10 +149149,11 @@ |
| 148316 | 149149 | ){ |
| 148317 | 149150 | while( ++iLevel < pWInfo->nLevel ){ |
| 148318 | 149151 | WhereLevel *pLevel = &pWInfo->a[iLevel]; |
| 148319 | 149152 | WhereLoop *pLoop = pLevel->pWLoop; |
| 148320 | 149153 | if( pLevel->regFilter==0 ) continue; |
| 149154 | + if( pLevel->pWLoop->nSkip ) continue; |
| 148321 | 149155 | /* ,--- Because sqlite3ConstructBloomFilter() has will not have set |
| 148322 | 149156 | ** vvvvv--' pLevel->regFilter if this were true. */ |
| 148323 | 149157 | if( NEVER(pLoop->prereq & notReady) ) continue; |
| 148324 | 149158 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 148325 | 149159 | WhereTerm *pTerm = pLoop->aLTerm[0]; |
| | @@ -148450,24 +149284,39 @@ |
| 148450 | 149284 | ** to access the data. |
| 148451 | 149285 | */ |
| 148452 | 149286 | int iReg; /* P3 Value for OP_VFilter */ |
| 148453 | 149287 | int addrNotFound; |
| 148454 | 149288 | int nConstraint = pLoop->nLTerm; |
| 148455 | | - int iIn; /* Counter for IN constraints */ |
| 148456 | 149289 | |
| 148457 | 149290 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
| 148458 | 149291 | addrNotFound = pLevel->addrBrk; |
| 148459 | 149292 | for(j=0; j<nConstraint; j++){ |
| 148460 | 149293 | int iTarget = iReg+j+2; |
| 148461 | 149294 | pTerm = pLoop->aLTerm[j]; |
| 148462 | 149295 | if( NEVER(pTerm==0) ) continue; |
| 148463 | 149296 | if( pTerm->eOperator & WO_IN ){ |
| 148464 | | - codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 148465 | | - addrNotFound = pLevel->addrNxt; |
| 149297 | + if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){ |
| 149298 | + int iTab = pParse->nTab++; |
| 149299 | + int iCache = ++pParse->nMem; |
| 149300 | + sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab); |
| 149301 | + sqlite3VdbeAddOp3(v, OP_VInitIn, iTab, iTarget, iCache); |
| 149302 | + }else{ |
| 149303 | + codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 149304 | + addrNotFound = pLevel->addrNxt; |
| 149305 | + } |
| 148466 | 149306 | }else{ |
| 148467 | 149307 | Expr *pRight = pTerm->pExpr->pRight; |
| 148468 | 149308 | codeExprOrVector(pParse, pRight, iTarget, 1); |
| 149309 | + if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET |
| 149310 | + && pLoop->u.vtab.bOmitOffset |
| 149311 | + ){ |
| 149312 | + assert( pTerm->eOperator==WO_AUX ); |
| 149313 | + assert( pWInfo->pLimit!=0 ); |
| 149314 | + assert( pWInfo->pLimit->iOffset>0 ); |
| 149315 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pLimit->iOffset); |
| 149316 | + VdbeComment((v,"Zero OFFSET counter")); |
| 149317 | + } |
| 148469 | 149318 | } |
| 148470 | 149319 | } |
| 148471 | 149320 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
| 148472 | 149321 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
| 148473 | 149322 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| | @@ -148480,61 +149329,71 @@ |
| 148480 | 149329 | if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0; |
| 148481 | 149330 | pLevel->p1 = iCur; |
| 148482 | 149331 | pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext; |
| 148483 | 149332 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 148484 | 149333 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 148485 | | - if( pLoop->wsFlags & WHERE_IN_ABLE ){ |
| 148486 | | - iIn = pLevel->u.in.nIn; |
| 148487 | | - }else{ |
| 148488 | | - iIn = 0; |
| 148489 | | - } |
| 148490 | | - for(j=nConstraint-1; j>=0; j--){ |
| 149334 | + |
| 149335 | + for(j=0; j<nConstraint; j++){ |
| 148491 | 149336 | pTerm = pLoop->aLTerm[j]; |
| 148492 | | - if( (pTerm->eOperator & WO_IN)!=0 ) iIn--; |
| 148493 | 149337 | if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){ |
| 148494 | 149338 | disableTerm(pLevel, pTerm); |
| 148495 | | - }else if( (pTerm->eOperator & WO_IN)!=0 |
| 148496 | | - && sqlite3ExprVectorSize(pTerm->pExpr->pLeft)==1 |
| 149339 | + continue; |
| 149340 | + } |
| 149341 | + if( (pTerm->eOperator & WO_IN)!=0 |
| 149342 | + && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0 |
| 149343 | + && !db->mallocFailed |
| 148497 | 149344 | ){ |
| 148498 | 149345 | Expr *pCompare; /* The comparison operator */ |
| 148499 | 149346 | Expr *pRight; /* RHS of the comparison */ |
| 148500 | 149347 | VdbeOp *pOp; /* Opcode to access the value of the IN constraint */ |
| 149348 | + int iIn; /* IN loop corresponding to the j-th constraint */ |
| 148501 | 149349 | |
| 148502 | 149350 | /* Reload the constraint value into reg[iReg+j+2]. The same value |
| 148503 | 149351 | ** was loaded into the same register prior to the OP_VFilter, but |
| 148504 | 149352 | ** the xFilter implementation might have changed the datatype or |
| 148505 | | - ** encoding of the value in the register, so it *must* be reloaded. */ |
| 148506 | | - assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed ); |
| 148507 | | - if( !db->mallocFailed ){ |
| 148508 | | - assert( iIn>=0 && iIn<pLevel->u.in.nIn ); |
| 149353 | + ** encoding of the value in the register, so it *must* be reloaded. |
| 149354 | + */ |
| 149355 | + for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){ |
| 148509 | 149356 | pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop); |
| 148510 | | - assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid ); |
| 148511 | | - assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 ); |
| 148512 | | - assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 ); |
| 148513 | | - testcase( pOp->opcode==OP_Rowid ); |
| 148514 | | - sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3); |
| 149357 | + if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2) |
| 149358 | + || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2) |
| 149359 | + ){ |
| 149360 | + testcase( pOp->opcode==OP_Rowid ); |
| 149361 | + sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3); |
| 149362 | + break; |
| 149363 | + } |
| 148515 | 149364 | } |
| 148516 | 149365 | |
| 148517 | 149366 | /* Generate code that will continue to the next row if |
| 148518 | | - ** the IN constraint is not satisfied */ |
| 149367 | + ** the IN constraint is not satisfied |
| 149368 | + */ |
| 148519 | 149369 | pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0); |
| 148520 | | - assert( pCompare!=0 || db->mallocFailed ); |
| 148521 | | - if( pCompare ){ |
| 148522 | | - pCompare->pLeft = pTerm->pExpr->pLeft; |
| 149370 | + if( !db->mallocFailed ){ |
| 149371 | + int iFld = pTerm->u.x.iField; |
| 149372 | + Expr *pLeft = pTerm->pExpr->pLeft; |
| 149373 | + assert( pLeft!=0 ); |
| 149374 | + if( iFld>0 ){ |
| 149375 | + assert( pLeft->op==TK_VECTOR ); |
| 149376 | + assert( ExprUseXList(pLeft) ); |
| 149377 | + assert( iFld<=pLeft->x.pList->nExpr ); |
| 149378 | + pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr; |
| 149379 | + }else{ |
| 149380 | + pCompare->pLeft = pLeft; |
| 149381 | + } |
| 148523 | 149382 | pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0); |
| 148524 | 149383 | if( pRight ){ |
| 148525 | 149384 | pRight->iTable = iReg+j+2; |
| 148526 | 149385 | sqlite3ExprIfFalse( |
| 148527 | 149386 | pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL |
| 148528 | 149387 | ); |
| 148529 | 149388 | } |
| 148530 | 149389 | pCompare->pLeft = 0; |
| 148531 | | - sqlite3ExprDelete(db, pCompare); |
| 148532 | 149390 | } |
| 149391 | + sqlite3ExprDelete(db, pCompare); |
| 148533 | 149392 | } |
| 148534 | 149393 | } |
| 148535 | | - assert( iIn==0 || db->mallocFailed ); |
| 149394 | + |
| 148536 | 149395 | /* These registers need to be preserved in case there is an IN operator |
| 148537 | 149396 | ** loop. So we could deallocate the registers here (and potentially |
| 148538 | 149397 | ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems |
| 148539 | 149398 | ** simpler and safer to simply not reuse the registers. |
| 148540 | 149399 | ** |
| | @@ -149222,11 +150081,11 @@ |
| 149222 | 150081 | regRowid = ++pParse->nMem; |
| 149223 | 150082 | } |
| 149224 | 150083 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 149225 | 150084 | |
| 149226 | 150085 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 149227 | | - ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 150086 | + ** Then for every term xN, evaluate as the subexpression: xN AND y |
| 149228 | 150087 | ** That way, terms in y that are factored into the disjunction will |
| 149229 | 150088 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
| 149230 | 150089 | ** |
| 149231 | 150090 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 149232 | 150091 | ** the "interesting" terms of z - terms that did not originate in the |
| | @@ -149234,21 +150093,38 @@ |
| 149234 | 150093 | ** indices. |
| 149235 | 150094 | ** |
| 149236 | 150095 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 149237 | 150096 | ** is not contained in the ON clause of a LEFT JOIN. |
| 149238 | 150097 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
| 150098 | + ** |
| 150099 | + ** 2022-02-04: Do not push down slices of a row-value comparison. |
| 150100 | + ** In other words, "w" or "y" may not be a slice of a vector. Otherwise, |
| 150101 | + ** the initialization of the right-hand operand of the vector comparison |
| 150102 | + ** might not occur, or might occur only in an OR branch that is not |
| 150103 | + ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1. |
| 150104 | + ** |
| 150105 | + ** 2022-03-03: Do not push down expressions that involve subqueries. |
| 150106 | + ** The subquery might get coded as a subroutine. Any table-references |
| 150107 | + ** in the subquery might be resolved to index-references for the index on |
| 150108 | + ** the OR branch in which the subroutine is coded. But if the subroutine |
| 150109 | + ** is invoked from a different OR branch that uses a different index, such |
| 150110 | + ** index-references will not work. tag-20220303a |
| 150111 | + ** https://sqlite.org/forum/forumpost/36937b197273d403 |
| 149239 | 150112 | */ |
| 149240 | 150113 | if( pWC->nTerm>1 ){ |
| 149241 | 150114 | int iTerm; |
| 149242 | 150115 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 149243 | 150116 | Expr *pExpr = pWC->a[iTerm].pExpr; |
| 149244 | 150117 | if( &pWC->a[iTerm] == pTerm ) continue; |
| 149245 | 150118 | testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL ); |
| 149246 | 150119 | testcase( pWC->a[iTerm].wtFlags & TERM_CODED ); |
| 149247 | | - if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue; |
| 150120 | + testcase( pWC->a[iTerm].wtFlags & TERM_SLICE ); |
| 150121 | + if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){ |
| 150122 | + continue; |
| 150123 | + } |
| 149248 | 150124 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
| 149249 | | - testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
| 150125 | + if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */ |
| 149250 | 150126 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 149251 | 150127 | pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr); |
| 149252 | 150128 | } |
| 149253 | 150129 | if( pAndExpr ){ |
| 149254 | 150130 | /* The extra 0x10000 bit on the opcode is masked off and does not |
| | @@ -149285,11 +150161,11 @@ |
| 149285 | 150161 | pOrExpr = pAndExpr; |
| 149286 | 150162 | } |
| 149287 | 150163 | /* Loop through table entries that match term pOrTerm. */ |
| 149288 | 150164 | ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1)); |
| 149289 | 150165 | WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); |
| 149290 | | - pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 150166 | + pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0, |
| 149291 | 150167 | WHERE_OR_SUBCLAUSE, iCovCur); |
| 149292 | 150168 | assert( pSubWInfo || pParse->nErr ); |
| 149293 | 150169 | if( pSubWInfo ){ |
| 149294 | 150170 | WhereLoop *pSubLoop; |
| 149295 | 150171 | int addrExplain = sqlite3WhereExplainOneScan( |
| | @@ -150025,11 +150901,11 @@ |
| 150025 | 150901 | void *pNotUsed; |
| 150026 | 150902 | pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab; |
| 150027 | 150903 | assert( pVtab!=0 ); |
| 150028 | 150904 | assert( pVtab->pModule!=0 ); |
| 150029 | 150905 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 150030 | | - pMod = (sqlite3_module *)pVtab->pModule; |
| 150906 | + pMod = (sqlite3_module *)pVtab->pModule; |
| 150031 | 150907 | if( pMod->xFindFunction!=0 ){ |
| 150032 | 150908 | i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed); |
| 150033 | 150909 | if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){ |
| 150034 | 150910 | *peOp2 = i; |
| 150035 | 150911 | *ppRight = pList->a[1].pExpr; |
| | @@ -150069,11 +150945,11 @@ |
| 150069 | 150945 | ** a join, then transfer the appropriate markings over to derived. |
| 150070 | 150946 | */ |
| 150071 | 150947 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
| 150072 | 150948 | if( pDerived ){ |
| 150073 | 150949 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 150074 | | - pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 150950 | + pDerived->w.iRightJoinTable = pBase->w.iRightJoinTable; |
| 150075 | 150951 | } |
| 150076 | 150952 | } |
| 150077 | 150953 | |
| 150078 | 150954 | /* |
| 150079 | 150955 | ** Mark term iChild as being a child of term iParent |
| | @@ -150714,11 +151590,11 @@ |
| 150714 | 151590 | abort(); |
| 150715 | 151591 | } |
| 150716 | 151592 | #endif |
| 150717 | 151593 | |
| 150718 | 151594 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 150719 | | - Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable); |
| 151595 | + Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iRightJoinTable); |
| 150720 | 151596 | prereqAll |= x; |
| 150721 | 151597 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 150722 | 151598 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
| 150723 | 151599 | if( (prereqAll>>1)>=x ){ |
| 150724 | 151600 | sqlite3ErrorMsg(pParse, "ON clause references tables to its right"); |
| | @@ -150982,11 +151858,14 @@ |
| 150982 | 151858 | ** new terms for each component comparison - "a = ?" and "b = ?". The |
| 150983 | 151859 | ** new terms completely replace the original vector comparison, which is |
| 150984 | 151860 | ** no longer used. |
| 150985 | 151861 | ** |
| 150986 | 151862 | ** This is only required if at least one side of the comparison operation |
| 150987 | | - ** is not a sub-select. */ |
| 151863 | + ** is not a sub-select. |
| 151864 | + ** |
| 151865 | + ** tag-20220128a |
| 151866 | + */ |
| 150988 | 151867 | if( (pExpr->op==TK_EQ || pExpr->op==TK_IS) |
| 150989 | 151868 | && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1 |
| 150990 | 151869 | && sqlite3ExprVectorSize(pExpr->pRight)==nLeft |
| 150991 | 151870 | && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 |
| 150992 | 151871 | || (pExpr->pRight->flags & EP_xIsSelect)==0) |
| | @@ -150999,11 +151878,11 @@ |
| 150999 | 151878 | Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft); |
| 151000 | 151879 | Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft); |
| 151001 | 151880 | |
| 151002 | 151881 | pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight); |
| 151003 | 151882 | transferJoinMarkings(pNew, pExpr); |
| 151004 | | - idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC); |
| 151883 | + idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_SLICE); |
| 151005 | 151884 | exprAnalyze(pSrc, pWC, idxNew); |
| 151006 | 151885 | } |
| 151007 | 151886 | pTerm = &pWC->a[idxTerm]; |
| 151008 | 151887 | pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */ |
| 151009 | 151888 | pTerm->eOperator = 0; |
| | @@ -151029,11 +151908,11 @@ |
| 151029 | 151908 | && pWC->op==TK_AND |
| 151030 | 151909 | ){ |
| 151031 | 151910 | int i; |
| 151032 | 151911 | for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){ |
| 151033 | 151912 | int idxNew; |
| 151034 | | - idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL); |
| 151913 | + idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL|TERM_SLICE); |
| 151035 | 151914 | pWC->a[idxNew].u.x.iField = i+1; |
| 151036 | 151915 | exprAnalyze(pSrc, pWC, idxNew); |
| 151037 | 151916 | markTermAsChild(pWC, idxNew, idxTerm); |
| 151038 | 151917 | } |
| 151039 | 151918 | } |
| | @@ -151062,11 +151941,11 @@ |
| 151062 | 151941 | Expr *pNewExpr; |
| 151063 | 151942 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 151064 | 151943 | 0, sqlite3ExprDup(db, pRight, 0)); |
| 151065 | 151944 | if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){ |
| 151066 | 151945 | ExprSetProperty(pNewExpr, EP_FromJoin); |
| 151067 | | - pNewExpr->iRightJoinTable = pExpr->iRightJoinTable; |
| 151946 | + pNewExpr->w.iRightJoinTable = pExpr->w.iRightJoinTable; |
| 151068 | 151947 | } |
| 151069 | 151948 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| 151070 | 151949 | testcase( idxNew==0 ); |
| 151071 | 151950 | pNewTerm = &pWC->a[idxNew]; |
| 151072 | 151951 | pNewTerm->prereqRight = prereqExpr; |
| | @@ -151124,10 +152003,117 @@ |
| 151124 | 152003 | }else{ |
| 151125 | 152004 | sqlite3WhereSplit(pWC, pE2->pLeft, op); |
| 151126 | 152005 | sqlite3WhereSplit(pWC, pE2->pRight, op); |
| 151127 | 152006 | } |
| 151128 | 152007 | } |
| 152008 | + |
| 152009 | +/* |
| 152010 | +** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or |
| 152011 | +** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the |
| 152012 | +** where-clause passed as the first argument. The value for the term |
| 152013 | +** is found in register iReg. |
| 152014 | +** |
| 152015 | +** In the common case where the value is a simple integer |
| 152016 | +** (example: "LIMIT 5 OFFSET 10") then the expression codes as a |
| 152017 | +** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value(). |
| 152018 | +** If not, then it codes as a TK_REGISTER expression. |
| 152019 | +*/ |
| 152020 | +static void whereAddLimitExpr( |
| 152021 | + WhereClause *pWC, /* Add the constraint to this WHERE clause */ |
| 152022 | + int iReg, /* Register that will hold value of the limit/offset */ |
| 152023 | + Expr *pExpr, /* Expression that defines the limit/offset */ |
| 152024 | + int iCsr, /* Cursor to which the constraint applies */ |
| 152025 | + int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */ |
| 152026 | +){ |
| 152027 | + Parse *pParse = pWC->pWInfo->pParse; |
| 152028 | + sqlite3 *db = pParse->db; |
| 152029 | + Expr *pNew; |
| 152030 | + int iVal = 0; |
| 152031 | + |
| 152032 | + if( sqlite3ExprIsInteger(pExpr, &iVal) && iVal>=0 ){ |
| 152033 | + Expr *pVal = sqlite3Expr(db, TK_INTEGER, 0); |
| 152034 | + if( pVal==0 ) return; |
| 152035 | + ExprSetProperty(pVal, EP_IntValue); |
| 152036 | + pVal->u.iValue = iVal; |
| 152037 | + pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); |
| 152038 | + }else{ |
| 152039 | + Expr *pVal = sqlite3Expr(db, TK_REGISTER, 0); |
| 152040 | + if( pVal==0 ) return; |
| 152041 | + pVal->iTable = iReg; |
| 152042 | + pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); |
| 152043 | + } |
| 152044 | + if( pNew ){ |
| 152045 | + WhereTerm *pTerm; |
| 152046 | + int idx; |
| 152047 | + idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_VIRTUAL); |
| 152048 | + pTerm = &pWC->a[idx]; |
| 152049 | + pTerm->leftCursor = iCsr; |
| 152050 | + pTerm->eOperator = WO_AUX; |
| 152051 | + pTerm->eMatchOp = eMatchOp; |
| 152052 | + } |
| 152053 | +} |
| 152054 | + |
| 152055 | +/* |
| 152056 | +** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the |
| 152057 | +** SELECT statement passed as the second argument. These terms are only |
| 152058 | +** added if: |
| 152059 | +** |
| 152060 | +** 1. The SELECT statement has a LIMIT clause, and |
| 152061 | +** 2. The SELECT statement is not an aggregate or DISTINCT query, and |
| 152062 | +** 3. The SELECT statement has exactly one object in its from clause, and |
| 152063 | +** that object is a virtual table, and |
| 152064 | +** 4. There are no terms in the WHERE clause that will not be passed |
| 152065 | +** to the virtual table xBestIndex method. |
| 152066 | +** 5. The ORDER BY clause, if any, will be made available to the xBestIndex |
| 152067 | +** method. |
| 152068 | +** |
| 152069 | +** LIMIT and OFFSET terms are ignored by most of the planner code. They |
| 152070 | +** exist only so that they may be passed to the xBestIndex method of the |
| 152071 | +** single virtual table in the FROM clause of the SELECT. |
| 152072 | +*/ |
| 152073 | +SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause *pWC, Select *p){ |
| 152074 | + assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) ); |
| 152075 | + if( (p && p->pLimit) /* 1 */ |
| 152076 | + && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */ |
| 152077 | + && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */ |
| 152078 | + ){ |
| 152079 | + ExprList *pOrderBy = p->pOrderBy; |
| 152080 | + int iCsr = p->pSrc->a[0].iCursor; |
| 152081 | + int ii; |
| 152082 | + |
| 152083 | + /* Check condition (4). Return early if it is not met. */ |
| 152084 | + for(ii=0; ii<pWC->nTerm; ii++){ |
| 152085 | + if( pWC->a[ii].wtFlags & TERM_CODED ){ |
| 152086 | + /* This term is a vector operation that has been decomposed into |
| 152087 | + ** other, subsequent terms. It can be ignored. See tag-20220128a */ |
| 152088 | + assert( pWC->a[ii].wtFlags & TERM_VIRTUAL ); |
| 152089 | + assert( pWC->a[ii].eOperator==0 ); |
| 152090 | + continue; |
| 152091 | + } |
| 152092 | + if( pWC->a[ii].leftCursor!=iCsr ) return; |
| 152093 | + } |
| 152094 | + |
| 152095 | + /* Check condition (5). Return early if it is not met. */ |
| 152096 | + if( pOrderBy ){ |
| 152097 | + for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 152098 | + Expr *pExpr = pOrderBy->a[ii].pExpr; |
| 152099 | + if( pExpr->op!=TK_COLUMN ) return; |
| 152100 | + if( pExpr->iTable!=iCsr ) return; |
| 152101 | + if( pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_BIGNULL ) return; |
| 152102 | + } |
| 152103 | + } |
| 152104 | + |
| 152105 | + /* All conditions are met. Add the terms to the where-clause object. */ |
| 152106 | + assert( p->pLimit->op==TK_LIMIT ); |
| 152107 | + whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft, |
| 152108 | + iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT); |
| 152109 | + if( p->iOffset>0 ){ |
| 152110 | + whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight, |
| 152111 | + iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET); |
| 152112 | + } |
| 152113 | + } |
| 152114 | +} |
| 151129 | 152115 | |
| 151130 | 152116 | /* |
| 151131 | 152117 | ** Initialize a preallocated WhereClause structure. |
| 151132 | 152118 | */ |
| 151133 | 152119 | SQLITE_PRIVATE void sqlite3WhereClauseInit( |
| | @@ -151160,10 +152146,11 @@ |
| 151160 | 152146 | for(i=pWC->nBase; i<pWC->nTerm; i++){ |
| 151161 | 152147 | assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 ); |
| 151162 | 152148 | } |
| 151163 | 152149 | #endif |
| 151164 | 152150 | while(1){ |
| 152151 | + assert( a->eMatchOp==0 || a->eOperator==WO_AUX ); |
| 151165 | 152152 | if( a->wtFlags & TERM_DYNAMIC ){ |
| 151166 | 152153 | sqlite3ExprDelete(db, a->pExpr); |
| 151167 | 152154 | } |
| 151168 | 152155 | if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){ |
| 151169 | 152156 | if( a->wtFlags & TERM_ORINFO ){ |
| | @@ -151317,10 +152304,11 @@ |
| 151317 | 152304 | if( pColRef==0 ) return; |
| 151318 | 152305 | pColRef->iTable = pItem->iCursor; |
| 151319 | 152306 | pColRef->iColumn = k++; |
| 151320 | 152307 | assert( ExprUseYTab(pColRef) ); |
| 151321 | 152308 | pColRef->y.pTab = pTab; |
| 152309 | + pItem->colUsed |= sqlite3ExprColUsed(pColRef); |
| 151322 | 152310 | pRhs = sqlite3PExpr(pParse, TK_UPLUS, |
| 151323 | 152311 | sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0); |
| 151324 | 152312 | pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs); |
| 151325 | 152313 | if( pItem->fg.jointype & JT_LEFT ){ |
| 151326 | 152314 | sqlite3SetJoinExpr(pTerm, pItem->iCursor); |
| | @@ -151364,10 +152352,12 @@ |
| 151364 | 152352 | typedef struct HiddenIndexInfo HiddenIndexInfo; |
| 151365 | 152353 | struct HiddenIndexInfo { |
| 151366 | 152354 | WhereClause *pWC; /* The Where clause being analyzed */ |
| 151367 | 152355 | Parse *pParse; /* The parsing context */ |
| 151368 | 152356 | int eDistinct; /* Value to return from sqlite3_vtab_distinct() */ |
| 152357 | + u32 mIn; /* Mask of terms that are <col> IN (...) */ |
| 152358 | + u32 mHandleIn; /* Terms that vtab will handle as <col> IN (...) */ |
| 151369 | 152359 | sqlite3_value *aRhs[1]; /* RHS values for constraints. MUST BE LAST |
| 151370 | 152360 | ** because extra space is allocated to hold up |
| 151371 | 152361 | ** to nTerm such values */ |
| 151372 | 152362 | }; |
| 151373 | 152363 | |
| | @@ -152407,11 +153397,14 @@ |
| 152407 | 153397 | VdbeCoverage(v); |
| 152408 | 153398 | sqlite3VdbeJumpHere(v, addrTop); |
| 152409 | 153399 | pLoop->wsFlags &= ~WHERE_BLOOMFILTER; |
| 152410 | 153400 | if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break; |
| 152411 | 153401 | while( ++iLevel < pWInfo->nLevel ){ |
| 153402 | + const SrcItem *pTabItem; |
| 152412 | 153403 | pLevel = &pWInfo->a[iLevel]; |
| 153404 | + pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 153405 | + if( pTabItem->fg.jointype & JT_LEFT ) continue; |
| 152413 | 153406 | pLoop = pLevel->pWLoop; |
| 152414 | 153407 | if( NEVER(pLoop==0) ) continue; |
| 152415 | 153408 | if( pLoop->prereq & notReady ) continue; |
| 152416 | 153409 | if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN)) |
| 152417 | 153410 | ==WHERE_BLOOMFILTER |
| | @@ -152474,10 +153467,11 @@ |
| 152474 | 153467 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 152475 | 153468 | testcase( pTerm->eOperator & WO_IS ); |
| 152476 | 153469 | testcase( pTerm->eOperator & WO_ALL ); |
| 152477 | 153470 | if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue; |
| 152478 | 153471 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
| 153472 | + |
| 152479 | 153473 | assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 152480 | 153474 | assert( pTerm->u.x.leftColumn>=XN_ROWID ); |
| 152481 | 153475 | assert( pTerm->u.x.leftColumn<pTab->nCol ); |
| 152482 | 153476 | |
| 152483 | 153477 | /* tag-20191211-002: WHERE-clause constraints are not useful to the |
| | @@ -152535,14 +153529,16 @@ |
| 152535 | 153529 | } |
| 152536 | 153530 | |
| 152537 | 153531 | /* No matches cause a break out of the loop */ |
| 152538 | 153532 | break; |
| 152539 | 153533 | } |
| 152540 | | - if( i==n){ |
| 153534 | + if( i==n ){ |
| 152541 | 153535 | nOrderBy = n; |
| 152542 | | - if( (pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY)) ){ |
| 152543 | | - eDistinct = 1 + ((pWInfo->wctrlFlags & WHERE_DISTINCTBY)!=0); |
| 153536 | + if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) ){ |
| 153537 | + eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0); |
| 153538 | + }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){ |
| 153539 | + eDistinct = 1; |
| 152544 | 153540 | } |
| 152545 | 153541 | } |
| 152546 | 153542 | } |
| 152547 | 153543 | |
| 152548 | 153544 | /* Allocate the sqlite3_index_info structure |
| | @@ -152563,17 +153559,23 @@ |
| 152563 | 153559 | pIdxInfo->aOrderBy = pIdxOrderBy; |
| 152564 | 153560 | pIdxInfo->aConstraintUsage = pUsage; |
| 152565 | 153561 | pHidden->pWC = pWC; |
| 152566 | 153562 | pHidden->pParse = pParse; |
| 152567 | 153563 | pHidden->eDistinct = eDistinct; |
| 153564 | + pHidden->mIn = 0; |
| 152568 | 153565 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 152569 | 153566 | u16 op; |
| 152570 | 153567 | if( (pTerm->wtFlags & TERM_OK)==0 ) continue; |
| 152571 | 153568 | pIdxCons[j].iColumn = pTerm->u.x.leftColumn; |
| 152572 | 153569 | pIdxCons[j].iTermOffset = i; |
| 152573 | 153570 | op = pTerm->eOperator & WO_ALL; |
| 152574 | | - if( op==WO_IN ) op = WO_EQ; |
| 153571 | + if( op==WO_IN ){ |
| 153572 | + if( (pTerm->wtFlags & TERM_SLICE)==0 ){ |
| 153573 | + pHidden->mIn |= SMASKBIT32(j); |
| 153574 | + } |
| 153575 | + op = WO_EQ; |
| 153576 | + } |
| 152575 | 153577 | if( op==WO_AUX ){ |
| 152576 | 153578 | pIdxCons[j].op = pTerm->eMatchOp; |
| 152577 | 153579 | }else if( op & (WO_ISNULL|WO_IS) ){ |
| 152578 | 153580 | if( op==WO_ISNULL ){ |
| 152579 | 153581 | pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL; |
| | @@ -152659,11 +153661,13 @@ |
| 152659 | 153661 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
| 152660 | 153662 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
| 152661 | 153663 | int rc; |
| 152662 | 153664 | |
| 152663 | 153665 | whereTraceIndexInfoInputs(p); |
| 153666 | + pParse->db->nSchemaLock++; |
| 152664 | 153667 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 153668 | + pParse->db->nSchemaLock--; |
| 152665 | 153669 | whereTraceIndexInfoOutputs(p); |
| 152666 | 153670 | |
| 152667 | 153671 | if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){ |
| 152668 | 153672 | if( rc==SQLITE_NOMEM ){ |
| 152669 | 153673 | sqlite3OomFault(pParse->db); |
| | @@ -153911,12 +154915,21 @@ |
| 153911 | 154915 | if( j<0 ){ |
| 153912 | 154916 | if( pLoop->maskSelf==pTerm->prereqAll ){ |
| 153913 | 154917 | /* If there are extra terms in the WHERE clause not used by an index |
| 153914 | 154918 | ** that depend only on the table being scanned, and that will tend to |
| 153915 | 154919 | ** cause many rows to be omitted, then mark that table as |
| 153916 | | - ** "self-culling". */ |
| 153917 | | - pLoop->wsFlags |= WHERE_SELFCULL; |
| 154920 | + ** "self-culling". |
| 154921 | + ** |
| 154922 | + ** 2022-03-24: Self-culling only applies if either the extra terms |
| 154923 | + ** are straight comparison operators that are non-true with NULL |
| 154924 | + ** operand, or if the loop is not a LEFT JOIN. |
| 154925 | + */ |
| 154926 | + if( (pTerm->eOperator & 0x3f)!=0 |
| 154927 | + || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 |
| 154928 | + ){ |
| 154929 | + pLoop->wsFlags |= WHERE_SELFCULL; |
| 154930 | + } |
| 153918 | 154931 | } |
| 153919 | 154932 | if( pTerm->truthProb<=0 ){ |
| 153920 | 154933 | /* If a truth probability is specified using the likelihood() hints, |
| 153921 | 154934 | ** then use the probability provided by the application. */ |
| 153922 | 154935 | pLoop->nOut += pTerm->truthProb; |
| | @@ -154488,11 +155501,11 @@ |
| 154488 | 155501 | } |
| 154489 | 155502 | if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0; |
| 154490 | 155503 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 154491 | 155504 | Expr *pExpr; |
| 154492 | 155505 | pExpr = pTerm->pExpr; |
| 154493 | | - if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab) |
| 155506 | + if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->w.iRightJoinTable==iTab) |
| 154494 | 155507 | && (isLeft==0 || ExprHasProperty(pExpr, EP_FromJoin)) |
| 154495 | 155508 | && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab) |
| 154496 | 155509 | && (pTerm->wtFlags & TERM_VNULL)==0 |
| 154497 | 155510 | ){ |
| 154498 | 155511 | return 1; |
| | @@ -154789,10 +155802,19 @@ |
| 154789 | 155802 | } |
| 154790 | 155803 | return rc; |
| 154791 | 155804 | } |
| 154792 | 155805 | |
| 154793 | 155806 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 155807 | + |
| 155808 | +/* |
| 155809 | +** Return true if pTerm is a virtual table LIMIT or OFFSET term. |
| 155810 | +*/ |
| 155811 | +static int isLimitTerm(WhereTerm *pTerm){ |
| 155812 | + assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 ); |
| 155813 | + return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT |
| 155814 | + && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET; |
| 155815 | +} |
| 154794 | 155816 | |
| 154795 | 155817 | /* |
| 154796 | 155818 | ** Argument pIdxInfo is already populated with all constraints that may |
| 154797 | 155819 | ** be used by the virtual table identified by pBuilder->pNew->iTab. This |
| 154798 | 155820 | ** function marks a subset of those constraints usable, invokes the |
| | @@ -154817,13 +155839,15 @@ |
| 154817 | 155839 | Bitmask mPrereq, /* Mask of tables that must be used. */ |
| 154818 | 155840 | Bitmask mUsable, /* Mask of usable tables */ |
| 154819 | 155841 | u16 mExclude, /* Exclude terms using these operators */ |
| 154820 | 155842 | sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */ |
| 154821 | 155843 | u16 mNoOmit, /* Do not omit these constraints */ |
| 154822 | | - int *pbIn /* OUT: True if plan uses an IN(...) op */ |
| 155844 | + int *pbIn, /* OUT: True if plan uses an IN(...) op */ |
| 155845 | + int *pbRetryLimit /* OUT: Retry without LIMIT/OFFSET */ |
| 154823 | 155846 | ){ |
| 154824 | 155847 | WhereClause *pWC = pBuilder->pWC; |
| 155848 | + HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; |
| 154825 | 155849 | struct sqlite3_index_constraint *pIdxCons; |
| 154826 | 155850 | struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage; |
| 154827 | 155851 | int i; |
| 154828 | 155852 | int mxTerm; |
| 154829 | 155853 | int rc = SQLITE_OK; |
| | @@ -154842,10 +155866,11 @@ |
| 154842 | 155866 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 154843 | 155867 | WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset]; |
| 154844 | 155868 | pIdxCons->usable = 0; |
| 154845 | 155869 | if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight |
| 154846 | 155870 | && (pTerm->eOperator & mExclude)==0 |
| 155871 | + && (pbRetryLimit || !isLimitTerm(pTerm)) |
| 154847 | 155872 | ){ |
| 154848 | 155873 | pIdxCons->usable = 1; |
| 154849 | 155874 | } |
| 154850 | 155875 | } |
| 154851 | 155876 | |
| | @@ -154857,10 +155882,11 @@ |
| 154857 | 155882 | pIdxInfo->orderByConsumed = 0; |
| 154858 | 155883 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
| 154859 | 155884 | pIdxInfo->estimatedRows = 25; |
| 154860 | 155885 | pIdxInfo->idxFlags = 0; |
| 154861 | 155886 | pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed; |
| 155887 | + pHidden->mHandleIn = 0; |
| 154862 | 155888 | |
| 154863 | 155889 | /* Invoke the virtual table xBestIndex() method */ |
| 154864 | 155890 | rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo); |
| 154865 | 155891 | if( rc ){ |
| 154866 | 155892 | if( rc==SQLITE_CONSTRAINT ){ |
| | @@ -154874,12 +155900,12 @@ |
| 154874 | 155900 | return rc; |
| 154875 | 155901 | } |
| 154876 | 155902 | |
| 154877 | 155903 | mxTerm = -1; |
| 154878 | 155904 | assert( pNew->nLSlot>=nConstraint ); |
| 154879 | | - for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
| 154880 | | - pNew->u.vtab.omitMask = 0; |
| 155905 | + memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint ); |
| 155906 | + memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab)); |
| 154881 | 155907 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 154882 | 155908 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 154883 | 155909 | int iTerm; |
| 154884 | 155910 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 154885 | 155911 | WhereTerm *pTerm; |
| | @@ -154909,21 +155935,41 @@ |
| 154909 | 155935 | testcase( i!=iTerm ); |
| 154910 | 155936 | pNew->u.vtab.omitMask |= 1<<iTerm; |
| 154911 | 155937 | }else{ |
| 154912 | 155938 | testcase( i!=iTerm ); |
| 154913 | 155939 | } |
| 155940 | + if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){ |
| 155941 | + pNew->u.vtab.bOmitOffset = 1; |
| 155942 | + } |
| 154914 | 155943 | } |
| 154915 | | - if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 155944 | + if( SMASKBIT32(i) & pHidden->mHandleIn ){ |
| 155945 | + pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm); |
| 155946 | + }else if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 154916 | 155947 | /* A virtual table that is constrained by an IN clause may not |
| 154917 | 155948 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 154918 | 155949 | ** is not necessarily related to the order of output terms and |
| 154919 | 155950 | ** (2) Multiple outputs from a single IN value will not merge |
| 154920 | 155951 | ** together. */ |
| 154921 | 155952 | pIdxInfo->orderByConsumed = 0; |
| 154922 | 155953 | pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE; |
| 154923 | 155954 | *pbIn = 1; assert( (mExclude & WO_IN)==0 ); |
| 154924 | 155955 | } |
| 155956 | + |
| 155957 | + if( isLimitTerm(pTerm) && *pbIn ){ |
| 155958 | + /* If there is an IN(...) term handled as an == (separate call to |
| 155959 | + ** xFilter for each value on the RHS of the IN) and a LIMIT or |
| 155960 | + ** OFFSET term handled as well, the plan is unusable. Set output |
| 155961 | + ** variable *pbRetryLimit to true to tell the caller to retry with |
| 155962 | + ** LIMIT and OFFSET disabled. */ |
| 155963 | + if( pIdxInfo->needToFreeIdxStr ){ |
| 155964 | + sqlite3_free(pIdxInfo->idxStr); |
| 155965 | + pIdxInfo->idxStr = 0; |
| 155966 | + pIdxInfo->needToFreeIdxStr = 0; |
| 155967 | + } |
| 155968 | + *pbRetryLimit = 1; |
| 155969 | + return SQLITE_OK; |
| 155970 | + } |
| 154925 | 155971 | } |
| 154926 | 155972 | } |
| 154927 | 155973 | |
| 154928 | 155974 | pNew->nLTerm = mxTerm+1; |
| 154929 | 155975 | for(i=0; i<=mxTerm; i++){ |
| | @@ -154992,10 +156038,29 @@ |
| 154992 | 156038 | } |
| 154993 | 156039 | zRet = (pC ? pC->zName : sqlite3StrBINARY); |
| 154994 | 156040 | } |
| 154995 | 156041 | return zRet; |
| 154996 | 156042 | } |
| 156043 | + |
| 156044 | +/* |
| 156045 | +** Return true if constraint iCons is really an IN(...) constraint, or |
| 156046 | +** false otherwise. If iCons is an IN(...) constraint, set (if bHandle!=0) |
| 156047 | +** or clear (if bHandle==0) the flag to handle it using an iterator. |
| 156048 | +*/ |
| 156049 | +SQLITE_API int sqlite3_vtab_in(sqlite3_index_info *pIdxInfo, int iCons, int bHandle){ |
| 156050 | + HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; |
| 156051 | + u32 m = SMASKBIT32(iCons); |
| 156052 | + if( m & pHidden->mIn ){ |
| 156053 | + if( bHandle==0 ){ |
| 156054 | + pHidden->mHandleIn &= ~m; |
| 156055 | + }else if( bHandle>0 ){ |
| 156056 | + pHidden->mHandleIn |= m; |
| 156057 | + } |
| 156058 | + return 1; |
| 156059 | + } |
| 156060 | + return 0; |
| 156061 | +} |
| 154997 | 156062 | |
| 154998 | 156063 | /* |
| 154999 | 156064 | ** This interface is callable from within the xBestIndex callback only. |
| 155000 | 156065 | ** |
| 155001 | 156066 | ** If possible, set (*ppVal) to point to an object containing the value |
| | @@ -155029,21 +156094,34 @@ |
| 155029 | 156094 | } |
| 155030 | 156095 | |
| 155031 | 156096 | return rc; |
| 155032 | 156097 | } |
| 155033 | 156098 | |
| 155034 | | - |
| 155035 | 156099 | /* |
| 155036 | 156100 | ** Return true if ORDER BY clause may be handled as DISTINCT. |
| 155037 | 156101 | */ |
| 155038 | 156102 | SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info *pIdxInfo){ |
| 155039 | 156103 | HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; |
| 155040 | | - assert( pHidden->eDistinct==0 |
| 155041 | | - || pHidden->eDistinct==1 |
| 155042 | | - || pHidden->eDistinct==2 ); |
| 156104 | + assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 ); |
| 155043 | 156105 | return pHidden->eDistinct; |
| 155044 | 156106 | } |
| 156107 | + |
| 156108 | +#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \ |
| 156109 | + && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 156110 | +/* |
| 156111 | +** Cause the prepared statement that is associated with a call to |
| 156112 | +** xBestIndex to open write transactions on all attached schemas. |
| 156113 | +** This is used by the (built-in) sqlite_dbpage virtual table. |
| 156114 | +*/ |
| 156115 | +SQLITE_PRIVATE void sqlite3VtabWriteAll(sqlite3_index_info *pIdxInfo){ |
| 156116 | + HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; |
| 156117 | + Parse *pParse = pHidden->pParse; |
| 156118 | + int nDb = pParse->db->nDb; |
| 156119 | + int i; |
| 156120 | + for(i=0; i<nDb; i++) sqlite3BeginWriteOperation(pParse, 0, i); |
| 156121 | +} |
| 156122 | +#endif |
| 155045 | 156123 | |
| 155046 | 156124 | /* |
| 155047 | 156125 | ** Add all WhereLoop objects for a table of the join identified by |
| 155048 | 156126 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
| 155049 | 156127 | ** |
| | @@ -155082,10 +156160,11 @@ |
| 155082 | 156160 | int nConstraint; /* Number of constraints in p */ |
| 155083 | 156161 | int bIn; /* True if plan uses IN(...) operator */ |
| 155084 | 156162 | WhereLoop *pNew; |
| 155085 | 156163 | Bitmask mBest; /* Tables used by best possible plan */ |
| 155086 | 156164 | u16 mNoOmit; |
| 156165 | + int bRetry = 0; /* True to retry with LIMIT/OFFSET disabled */ |
| 155087 | 156166 | |
| 155088 | 156167 | assert( (mPrereq & mUnusable)==0 ); |
| 155089 | 156168 | pWInfo = pBuilder->pWInfo; |
| 155090 | 156169 | pParse = pWInfo->pParse; |
| 155091 | 156170 | pWC = pBuilder->pWC; |
| | @@ -155105,11 +156184,19 @@ |
| 155105 | 156184 | } |
| 155106 | 156185 | |
| 155107 | 156186 | /* First call xBestIndex() with all constraints usable. */ |
| 155108 | 156187 | WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName)); |
| 155109 | 156188 | WHERETRACE(0x40, (" VirtualOne: all usable\n")); |
| 155110 | | - rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn); |
| 156189 | + rc = whereLoopAddVirtualOne( |
| 156190 | + pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, &bRetry |
| 156191 | + ); |
| 156192 | + if( bRetry ){ |
| 156193 | + assert( rc==SQLITE_OK ); |
| 156194 | + rc = whereLoopAddVirtualOne( |
| 156195 | + pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, 0 |
| 156196 | + ); |
| 156197 | + } |
| 155111 | 156198 | |
| 155112 | 156199 | /* If the call to xBestIndex() with all terms enabled produced a plan |
| 155113 | 156200 | ** that does not require any source tables (IOW: a plan with mBest==0) |
| 155114 | 156201 | ** and does not use an IN(...) operator, then there is no point in making |
| 155115 | 156202 | ** any further calls to xBestIndex() since they will all return the same |
| | @@ -155123,11 +156210,11 @@ |
| 155123 | 156210 | /* If the plan produced by the earlier call uses an IN(...) term, call |
| 155124 | 156211 | ** xBestIndex again, this time with IN(...) terms disabled. */ |
| 155125 | 156212 | if( bIn ){ |
| 155126 | 156213 | WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n")); |
| 155127 | 156214 | rc = whereLoopAddVirtualOne( |
| 155128 | | - pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn); |
| 156215 | + pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn, 0); |
| 155129 | 156216 | assert( bIn==0 ); |
| 155130 | 156217 | mBestNoIn = pNew->prereq & ~mPrereq; |
| 155131 | 156218 | if( mBestNoIn==0 ){ |
| 155132 | 156219 | seenZero = 1; |
| 155133 | 156220 | seenZeroNoIN = 1; |
| | @@ -155150,11 +156237,11 @@ |
| 155150 | 156237 | if( mNext==ALLBITS ) break; |
| 155151 | 156238 | if( mNext==mBest || mNext==mBestNoIn ) continue; |
| 155152 | 156239 | WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n", |
| 155153 | 156240 | (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext)); |
| 155154 | 156241 | rc = whereLoopAddVirtualOne( |
| 155155 | | - pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn); |
| 156242 | + pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0); |
| 155156 | 156243 | if( pNew->prereq==mPrereq ){ |
| 155157 | 156244 | seenZero = 1; |
| 155158 | 156245 | if( bIn==0 ) seenZeroNoIN = 1; |
| 155159 | 156246 | } |
| 155160 | 156247 | } |
| | @@ -155163,21 +156250,21 @@ |
| 155163 | 156250 | ** that requires no source tables at all (i.e. one guaranteed to be |
| 155164 | 156251 | ** usable), make a call here with all source tables disabled */ |
| 155165 | 156252 | if( rc==SQLITE_OK && seenZero==0 ){ |
| 155166 | 156253 | WHERETRACE(0x40, (" VirtualOne: all disabled\n")); |
| 155167 | 156254 | rc = whereLoopAddVirtualOne( |
| 155168 | | - pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn); |
| 156255 | + pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0); |
| 155169 | 156256 | if( bIn==0 ) seenZeroNoIN = 1; |
| 155170 | 156257 | } |
| 155171 | 156258 | |
| 155172 | 156259 | /* If the calls to xBestIndex() have so far failed to find a plan |
| 155173 | 156260 | ** that requires no source tables at all and does not use an IN(...) |
| 155174 | 156261 | ** operator, make a final call to obtain one here. */ |
| 155175 | 156262 | if( rc==SQLITE_OK && seenZeroNoIN==0 ){ |
| 155176 | 156263 | WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n")); |
| 155177 | 156264 | rc = whereLoopAddVirtualOne( |
| 155178 | | - pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn); |
| 156265 | + pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0); |
| 155179 | 156266 | } |
| 155180 | 156267 | } |
| 155181 | 156268 | |
| 155182 | 156269 | if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr); |
| 155183 | 156270 | freeIndexInfo(pParse->db, p); |
| | @@ -155471,11 +156558,13 @@ |
| 155471 | 156558 | if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue; |
| 155472 | 156559 | }else{ |
| 155473 | 156560 | pLoop = pLast; |
| 155474 | 156561 | } |
| 155475 | 156562 | if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){ |
| 155476 | | - if( pLoop->u.vtab.isOrdered && (wctrlFlags & WHERE_DISTINCTBY)==0 ){ |
| 156563 | + if( pLoop->u.vtab.isOrdered |
| 156564 | + && ((wctrlFlags&(WHERE_DISTINCTBY|WHERE_SORTBYGROUP))!=WHERE_DISTINCTBY) |
| 156565 | + ){ |
| 155477 | 156566 | obSat = obDone; |
| 155478 | 156567 | } |
| 155479 | 156568 | break; |
| 155480 | 156569 | }else if( wctrlFlags & WHERE_DISTINCTBY ){ |
| 155481 | 156570 | pLoop->u.btree.nDistinctCol = 0; |
| | @@ -155738,11 +156827,11 @@ |
| 155738 | 156827 | ** |
| 155739 | 156828 | ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1 |
| 155740 | 156829 | ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0 |
| 155741 | 156830 | */ |
| 155742 | 156831 | SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){ |
| 155743 | | - assert( pWInfo->wctrlFlags & WHERE_GROUPBY ); |
| 156832 | + assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) ); |
| 155744 | 156833 | assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP ); |
| 155745 | 156834 | return pWInfo->sorted; |
| 155746 | 156835 | } |
| 155747 | 156836 | |
| 155748 | 156837 | #ifdef WHERETRACE_ENABLED |
| | @@ -156139,16 +157228,16 @@ |
| 156139 | 157228 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 156140 | 157229 | } |
| 156141 | 157230 | } |
| 156142 | 157231 | pWInfo->bOrderedInnerLoop = 0; |
| 156143 | 157232 | if( pWInfo->pOrderBy ){ |
| 157233 | + pWInfo->nOBSat = pFrom->isOrdered; |
| 156144 | 157234 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
| 156145 | 157235 | if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){ |
| 156146 | 157236 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 156147 | 157237 | } |
| 156148 | 157238 | }else{ |
| 156149 | | - pWInfo->nOBSat = pFrom->isOrdered; |
| 156150 | 157239 | pWInfo->revMask = pFrom->revLoop; |
| 156151 | 157240 | if( pWInfo->nOBSat<=0 ){ |
| 156152 | 157241 | pWInfo->nOBSat = 0; |
| 156153 | 157242 | if( nLoop>0 ){ |
| 156154 | 157243 | u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags; |
| | @@ -156407,11 +157496,11 @@ |
| 156407 | 157496 | if( (tabUsed & pLoop->maskSelf)!=0 ) continue; |
| 156408 | 157497 | pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm; |
| 156409 | 157498 | for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){ |
| 156410 | 157499 | if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){ |
| 156411 | 157500 | if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 156412 | | - || pTerm->pExpr->iRightJoinTable!=pItem->iCursor |
| 157501 | + || pTerm->pExpr->w.iRightJoinTable!=pItem->iCursor |
| 156413 | 157502 | ){ |
| 156414 | 157503 | break; |
| 156415 | 157504 | } |
| 156416 | 157505 | } |
| 156417 | 157506 | } |
| | @@ -156579,10 +157668,11 @@ |
| 156579 | 157668 | Parse *pParse, /* The parser context */ |
| 156580 | 157669 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
| 156581 | 157670 | Expr *pWhere, /* The WHERE clause */ |
| 156582 | 157671 | ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ |
| 156583 | 157672 | ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */ |
| 157673 | + Select *pLimit, /* Use this LIMIT/OFFSET clause, if any */ |
| 156584 | 157674 | u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */ |
| 156585 | 157675 | int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number |
| 156586 | 157676 | ** If WHERE_USE_LIMIT, then the limit amount */ |
| 156587 | 157677 | ){ |
| 156588 | 157678 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
| | @@ -156655,10 +157745,13 @@ |
| 156655 | 157745 | pWInfo->nLevel = nTabList; |
| 156656 | 157746 | pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse); |
| 156657 | 157747 | pWInfo->wctrlFlags = wctrlFlags; |
| 156658 | 157748 | pWInfo->iLimit = iAuxArg; |
| 156659 | 157749 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 157750 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 157751 | + pWInfo->pLimit = pLimit; |
| 157752 | +#endif |
| 156660 | 157753 | memset(&pWInfo->nOBSat, 0, |
| 156661 | 157754 | offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat)); |
| 156662 | 157755 | memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel)); |
| 156663 | 157756 | assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */ |
| 156664 | 157757 | pMaskSet = &pWInfo->sMaskSet; |
| | @@ -156723,10 +157816,11 @@ |
| 156723 | 157816 | #endif |
| 156724 | 157817 | } |
| 156725 | 157818 | |
| 156726 | 157819 | /* Analyze all of the subexpressions. */ |
| 156727 | 157820 | sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); |
| 157821 | + sqlite3WhereAddLimit(&pWInfo->sWC, pLimit); |
| 156728 | 157822 | if( db->mallocFailed ) goto whereBeginError; |
| 156729 | 157823 | |
| 156730 | 157824 | /* Special case: WHERE terms that do not refer to any tables in the join |
| 156731 | 157825 | ** (constant expressions). Evaluate each such term, and jump over all the |
| 156732 | 157826 | ** generated code if the result is not true. |
| | @@ -156969,10 +158063,11 @@ |
| 156969 | 158063 | testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 ); |
| 156970 | 158064 | testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS ); |
| 156971 | 158065 | if( pWInfo->eOnePass==ONEPASS_OFF |
| 156972 | 158066 | && pTab->nCol<BMS |
| 156973 | 158067 | && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0 |
| 158068 | + && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0 |
| 156974 | 158069 | ){ |
| 156975 | 158070 | /* If we know that only a prefix of the record will be used, |
| 156976 | 158071 | ** it is advantageous to reduce the "column count" field in |
| 156977 | 158072 | ** the P4 operand of the OP_OpenRead/Write opcode. */ |
| 156978 | 158073 | Bitmask b = pTabItem->colUsed; |
| | @@ -157129,10 +158224,30 @@ |
| 157129 | 158224 | if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return; |
| 157130 | 158225 | sqlite3VdbePrintOp(0, pc, pOp); |
| 157131 | 158226 | } |
| 157132 | 158227 | #endif |
| 157133 | 158228 | |
| 158229 | +#ifdef SQLITE_DEBUG |
| 158230 | +/* |
| 158231 | +** Return true if cursor iCur is opened by instruction k of the |
| 158232 | +** bytecode. Used inside of assert() only. |
| 158233 | +*/ |
| 158234 | +static int cursorIsOpen(Vdbe *v, int iCur, int k){ |
| 158235 | + while( k>=0 ){ |
| 158236 | + VdbeOp *pOp = sqlite3VdbeGetOp(v,k--); |
| 158237 | + if( pOp->p1!=iCur ) continue; |
| 158238 | + if( pOp->opcode==OP_Close ) return 0; |
| 158239 | + if( pOp->opcode==OP_OpenRead ) return 1; |
| 158240 | + if( pOp->opcode==OP_OpenWrite ) return 1; |
| 158241 | + if( pOp->opcode==OP_OpenDup ) return 1; |
| 158242 | + if( pOp->opcode==OP_OpenAutoindex ) return 1; |
| 158243 | + if( pOp->opcode==OP_OpenEphemeral ) return 1; |
| 158244 | + } |
| 158245 | + return 0; |
| 158246 | +} |
| 158247 | +#endif /* SQLITE_DEBUG */ |
| 158248 | + |
| 157134 | 158249 | /* |
| 157135 | 158250 | ** Generate the end of the WHERE loop. See comments on |
| 157136 | 158251 | ** sqlite3WhereBegin() for additional information. |
| 157137 | 158252 | */ |
| 157138 | 158253 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ |
| | @@ -157381,10 +158496,15 @@ |
| 157381 | 158496 | || pOp->opcode==OP_Offset |
| 157382 | 158497 | #endif |
| 157383 | 158498 | ){ |
| 157384 | 158499 | int x = pOp->p2; |
| 157385 | 158500 | assert( pIdx->pTable==pTab ); |
| 158501 | +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 158502 | + if( pOp->opcode==OP_Offset ){ |
| 158503 | + /* Do not need to translate the column number */ |
| 158504 | + }else |
| 158505 | +#endif |
| 157386 | 158506 | if( !HasRowid(pTab) ){ |
| 157387 | 158507 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 157388 | 158508 | x = pPk->aiColumn[x]; |
| 157389 | 158509 | assert( x>=0 ); |
| 157390 | 158510 | }else{ |
| | @@ -157394,13 +158514,26 @@ |
| 157394 | 158514 | x = sqlite3TableColumnToIndex(pIdx, x); |
| 157395 | 158515 | if( x>=0 ){ |
| 157396 | 158516 | pOp->p2 = x; |
| 157397 | 158517 | pOp->p1 = pLevel->iIdxCur; |
| 157398 | 158518 | OpcodeRewriteTrace(db, k, pOp); |
| 158519 | + }else{ |
| 158520 | + /* Unable to translate the table reference into an index |
| 158521 | + ** reference. Verify that this is harmless - that the |
| 158522 | + ** table being referenced really is open. |
| 158523 | + */ |
| 158524 | +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 158525 | + assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 158526 | + || cursorIsOpen(v,pOp->p1,k) |
| 158527 | + || pOp->opcode==OP_Offset |
| 158528 | + ); |
| 158529 | +#else |
| 158530 | + assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 158531 | + || cursorIsOpen(v,pOp->p1,k) |
| 158532 | + ); |
| 158533 | +#endif |
| 157399 | 158534 | } |
| 157400 | | - assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 |
| 157401 | | - || pWInfo->eOnePass ); |
| 157402 | 158535 | }else if( pOp->opcode==OP_Rowid ){ |
| 157403 | 158536 | pOp->p1 = pLevel->iIdxCur; |
| 157404 | 158537 | pOp->opcode = OP_IdxRowid; |
| 157405 | 158538 | OpcodeRewriteTrace(db, k, pOp); |
| 157406 | 158539 | }else if( pOp->opcode==OP_IfNullRow ){ |
| | @@ -158151,11 +159284,11 @@ |
| 158151 | 159284 | break; |
| 158152 | 159285 | } |
| 158153 | 159286 | } |
| 158154 | 159287 | } |
| 158155 | 159288 | } |
| 158156 | | - pWin->pFunc = pFunc; |
| 159289 | + pWin->pWFunc = pFunc; |
| 158157 | 159290 | } |
| 158158 | 159291 | |
| 158159 | 159292 | /* |
| 158160 | 159293 | ** Context object passed through sqlite3WalkExprList() to |
| 158161 | 159294 | ** selectWindowRewriteExprCb() by selectWindowRewriteEList(). |
| | @@ -158384,11 +159517,15 @@ |
| 158384 | 159517 | ** are invoked in the correct order as described under "SELECT REWRITING" |
| 158385 | 159518 | ** at the top of this file. |
| 158386 | 159519 | */ |
| 158387 | 159520 | SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){ |
| 158388 | 159521 | int rc = SQLITE_OK; |
| 158389 | | - if( p->pWin && p->pPrior==0 && ALWAYS((p->selFlags & SF_WinRewrite)==0) ){ |
| 159522 | + if( p->pWin |
| 159523 | + && p->pPrior==0 |
| 159524 | + && ALWAYS((p->selFlags & SF_WinRewrite)==0) |
| 159525 | + && ALWAYS(!IN_RENAME_OBJECT) |
| 159526 | + ){ |
| 158390 | 159527 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 158391 | 159528 | sqlite3 *db = pParse->db; |
| 158392 | 159529 | Select *pSub = 0; /* The subquery */ |
| 158393 | 159530 | SrcList *pSrc = p->pSrc; |
| 158394 | 159531 | Expr *pWhere = p->pWhere; |
| | @@ -158459,12 +159596,13 @@ |
| 158459 | 159596 | ** window function - one for the accumulator, another for interim |
| 158460 | 159597 | ** results. */ |
| 158461 | 159598 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 158462 | 159599 | ExprList *pArgs; |
| 158463 | 159600 | assert( ExprUseXList(pWin->pOwner) ); |
| 159601 | + assert( pWin->pWFunc!=0 ); |
| 158464 | 159602 | pArgs = pWin->pOwner->x.pList; |
| 158465 | | - if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){ |
| 159603 | + if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){ |
| 158466 | 159604 | selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist); |
| 158467 | 159605 | pWin->iArgCol = (pSublist ? pSublist->nExpr : 0); |
| 158468 | 159606 | pWin->bExprArgs = 1; |
| 158469 | 159607 | }else{ |
| 158470 | 159608 | pWin->iArgCol = (pSublist ? pSublist->nExpr : 0); |
| | @@ -158842,11 +159980,11 @@ |
| 158842 | 159980 | sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr); |
| 158843 | 159981 | return; |
| 158844 | 159982 | } |
| 158845 | 159983 | |
| 158846 | 159984 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 158847 | | - FuncDef *p = pWin->pFunc; |
| 159985 | + FuncDef *p = pWin->pWFunc; |
| 158848 | 159986 | if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){ |
| 158849 | 159987 | /* The inline versions of min() and max() require a single ephemeral |
| 158850 | 159988 | ** table and 3 registers. The registers are used as follows: |
| 158851 | 159989 | ** |
| 158852 | 159990 | ** regApp+0: slot to copy min()/max() argument to for MakeRecord |
| | @@ -158859,11 +159997,11 @@ |
| 158859 | 159997 | pList = pWin->pOwner->x.pList; |
| 158860 | 159998 | pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0); |
| 158861 | 159999 | pWin->csrApp = pParse->nTab++; |
| 158862 | 160000 | pWin->regApp = pParse->nMem+1; |
| 158863 | 160001 | pParse->nMem += 3; |
| 158864 | | - if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){ |
| 160002 | + if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){ |
| 158865 | 160003 | assert( pKeyInfo->aSortFlags[0]==0 ); |
| 158866 | 160004 | pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC; |
| 158867 | 160005 | } |
| 158868 | 160006 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2); |
| 158869 | 160007 | sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO); |
| | @@ -159082,11 +160220,11 @@ |
| 159082 | 160220 | ){ |
| 159083 | 160221 | Parse *pParse = p->pParse; |
| 159084 | 160222 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 159085 | 160223 | Window *pWin; |
| 159086 | 160224 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 159087 | | - FuncDef *pFunc = pWin->pFunc; |
| 160225 | + FuncDef *pFunc = pWin->pWFunc; |
| 159088 | 160226 | int regArg; |
| 159089 | 160227 | int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin); |
| 159090 | 160228 | int i; |
| 159091 | 160229 | |
| 159092 | 160230 | assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED ); |
| | @@ -159196,11 +160334,11 @@ |
| 159196 | 160334 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 159197 | 160335 | Window *pWin; |
| 159198 | 160336 | |
| 159199 | 160337 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 159200 | 160338 | if( pMWin->regStartRowid==0 |
| 159201 | | - && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) |
| 160339 | + && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX) |
| 159202 | 160340 | && (pWin->eStart!=TK_UNBOUNDED) |
| 159203 | 160341 | ){ |
| 159204 | 160342 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); |
| 159205 | 160343 | sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp); |
| 159206 | 160344 | VdbeCoverage(v); |
| | @@ -159210,16 +160348,16 @@ |
| 159210 | 160348 | assert( pMWin->regStartRowid==0 ); |
| 159211 | 160349 | }else{ |
| 159212 | 160350 | int nArg = windowArgCount(pWin); |
| 159213 | 160351 | if( bFin ){ |
| 159214 | 160352 | sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg); |
| 159215 | | - sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF); |
| 160353 | + sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF); |
| 159216 | 160354 | sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult); |
| 159217 | 160355 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); |
| 159218 | 160356 | }else{ |
| 159219 | 160357 | sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult); |
| 159220 | | - sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF); |
| 160358 | + sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF); |
| 159221 | 160359 | } |
| 159222 | 160360 | } |
| 159223 | 160361 | } |
| 159224 | 160362 | } |
| 159225 | 160363 | |
| | @@ -159344,11 +160482,11 @@ |
| 159344 | 160482 | }else{ |
| 159345 | 160483 | Parse *pParse = p->pParse; |
| 159346 | 160484 | Window *pWin; |
| 159347 | 160485 | |
| 159348 | 160486 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 159349 | | - FuncDef *pFunc = pWin->pFunc; |
| 160487 | + FuncDef *pFunc = pWin->pWFunc; |
| 159350 | 160488 | assert( ExprUseXList(pWin->pOwner) ); |
| 159351 | 160489 | if( pFunc->zName==nth_valueName |
| 159352 | 160490 | || pFunc->zName==first_valueName |
| 159353 | 160491 | ){ |
| 159354 | 160492 | int csr = pWin->csrApp; |
| | @@ -159416,11 +160554,11 @@ |
| 159416 | 160554 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 159417 | 160555 | int regArg; |
| 159418 | 160556 | int nArg = 0; |
| 159419 | 160557 | Window *pWin; |
| 159420 | 160558 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 159421 | | - FuncDef *pFunc = pWin->pFunc; |
| 160559 | + FuncDef *pFunc = pWin->pWFunc; |
| 159422 | 160560 | assert( pWin->regAccum ); |
| 159423 | 160561 | sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); |
| 159424 | 160562 | nArg = MAX(nArg, windowArgCount(pWin)); |
| 159425 | 160563 | if( pMWin->regStartRowid==0 ){ |
| 159426 | 160564 | if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){ |
| | @@ -159446,11 +160584,11 @@ |
| 159446 | 160584 | */ |
| 159447 | 160585 | static int windowCacheFrame(Window *pMWin){ |
| 159448 | 160586 | Window *pWin; |
| 159449 | 160587 | if( pMWin->regStartRowid ) return 1; |
| 159450 | 160588 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 159451 | | - FuncDef *pFunc = pWin->pFunc; |
| 160589 | + FuncDef *pFunc = pWin->pWFunc; |
| 159452 | 160590 | if( (pFunc->zName==nth_valueName) |
| 159453 | 160591 | || (pFunc->zName==first_valueName) |
| 159454 | 160592 | || (pFunc->zName==leadName) |
| 159455 | 160593 | || (pFunc->zName==lagName) |
| 159456 | 160594 | ){ |
| | @@ -159804,11 +160942,11 @@ |
| 159804 | 160942 | pNew = sqlite3DbMallocZero(db, sizeof(Window)); |
| 159805 | 160943 | if( pNew ){ |
| 159806 | 160944 | pNew->zName = sqlite3DbStrDup(db, p->zName); |
| 159807 | 160945 | pNew->zBase = sqlite3DbStrDup(db, p->zBase); |
| 159808 | 160946 | pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0); |
| 159809 | | - pNew->pFunc = p->pFunc; |
| 160947 | + pNew->pWFunc = p->pWFunc; |
| 159810 | 160948 | pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0); |
| 159811 | 160949 | pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0); |
| 159812 | 160950 | pNew->eFrmType = p->eFrmType; |
| 159813 | 160951 | pNew->eEnd = p->eEnd; |
| 159814 | 160952 | pNew->eStart = p->eStart; |
| | @@ -160681,14 +161819,11 @@ |
| 160681 | 161819 | } |
| 160682 | 161820 | return pSelect; |
| 160683 | 161821 | } |
| 160684 | 161822 | |
| 160685 | 161823 | |
| 160686 | | - /* Construct a new Expr object from a single identifier. Use the |
| 160687 | | - ** new Expr to populate pOut. Set the span of pOut to be the identifier |
| 160688 | | - ** that created the expression. |
| 160689 | | - */ |
| 161824 | + /* Construct a new Expr object from a single token */ |
| 160690 | 161825 | static Expr *tokenExpr(Parse *pParse, int op, Token t){ |
| 160691 | 161826 | Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1); |
| 160692 | 161827 | if( p ){ |
| 160693 | 161828 | /* memset(p, 0, sizeof(Expr)); */ |
| 160694 | 161829 | p->op = (u8)op; |
| | @@ -160704,10 +161839,11 @@ |
| 160704 | 161839 | p->iTable = 0; |
| 160705 | 161840 | p->iColumn = 0; |
| 160706 | 161841 | p->u.zToken = (char*)&p[1]; |
| 160707 | 161842 | memcpy(p->u.zToken, t.z, t.n); |
| 160708 | 161843 | p->u.zToken[t.n] = 0; |
| 161844 | + p->w.iOfst = (int)(t.z - pParse->zTail); |
| 160709 | 161845 | if( sqlite3Isquote(p->u.zToken[0]) ){ |
| 160710 | 161846 | sqlite3DequoteExpr(p); |
| 160711 | 161847 | } |
| 160712 | 161848 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 160713 | 161849 | p->nHeight = 1; |
| | @@ -164515,11 +165651,11 @@ |
| 164515 | 165651 | } |
| 164516 | 165652 | break; |
| 164517 | 165653 | case 102: /* selcollist ::= sclp scanpt nm DOT STAR */ |
| 164518 | 165654 | { |
| 164519 | 165655 | Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0); |
| 164520 | | - Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1); |
| 165656 | + Expr *pLeft = tokenExpr(pParse, TK_ID, yymsp[-2].minor.yy0); |
| 164521 | 165657 | Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight); |
| 164522 | 165658 | yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, pDot); |
| 164523 | 165659 | } |
| 164524 | 165660 | break; |
| 164525 | 165661 | case 103: /* as ::= AS nm */ |
| | @@ -164800,29 +165936,24 @@ |
| 164800 | 165936 | case 179: /* expr ::= JOIN_KW */ yytestcase(yyruleno==179); |
| 164801 | 165937 | {yymsp[0].minor.yy528=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/} |
| 164802 | 165938 | break; |
| 164803 | 165939 | case 180: /* expr ::= nm DOT nm */ |
| 164804 | 165940 | { |
| 164805 | | - Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1); |
| 164806 | | - Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1); |
| 164807 | | - if( IN_RENAME_OBJECT ){ |
| 164808 | | - sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[0].minor.yy0); |
| 164809 | | - sqlite3RenameTokenMap(pParse, (void*)temp1, &yymsp[-2].minor.yy0); |
| 164810 | | - } |
| 165941 | + Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0); |
| 165942 | + Expr *temp2 = tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); |
| 164811 | 165943 | yylhsminor.yy528 = sqlite3PExpr(pParse, TK_DOT, temp1, temp2); |
| 164812 | 165944 | } |
| 164813 | 165945 | yymsp[-2].minor.yy528 = yylhsminor.yy528; |
| 164814 | 165946 | break; |
| 164815 | 165947 | case 181: /* expr ::= nm DOT nm DOT nm */ |
| 164816 | 165948 | { |
| 164817 | | - Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-4].minor.yy0, 1); |
| 164818 | | - Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1); |
| 164819 | | - Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1); |
| 165949 | + Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-4].minor.yy0); |
| 165950 | + Expr *temp2 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0); |
| 165951 | + Expr *temp3 = tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); |
| 164820 | 165952 | Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3); |
| 164821 | 165953 | if( IN_RENAME_OBJECT ){ |
| 164822 | | - sqlite3RenameTokenMap(pParse, (void*)temp3, &yymsp[0].minor.yy0); |
| 164823 | | - sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[-2].minor.yy0); |
| 165954 | + sqlite3RenameTokenRemap(pParse, 0, temp1); |
| 164824 | 165955 | } |
| 164825 | 165956 | yylhsminor.yy528 = sqlite3PExpr(pParse, TK_DOT, temp1, temp4); |
| 164826 | 165957 | } |
| 164827 | 165958 | yymsp[-4].minor.yy528 = yylhsminor.yy528; |
| 164828 | 165959 | break; |
| | @@ -164831,10 +165962,11 @@ |
| 164831 | 165962 | {yymsp[0].minor.yy528=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/} |
| 164832 | 165963 | break; |
| 164833 | 165964 | case 184: /* term ::= INTEGER */ |
| 164834 | 165965 | { |
| 164835 | 165966 | yylhsminor.yy528 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1); |
| 165967 | + if( yylhsminor.yy528 ) yylhsminor.yy528->w.iOfst = (int)(yymsp[0].minor.yy0.z - pParse->zTail); |
| 164836 | 165968 | } |
| 164837 | 165969 | yymsp[0].minor.yy528 = yylhsminor.yy528; |
| 164838 | 165970 | break; |
| 164839 | 165971 | case 185: /* expr ::= VARIABLE */ |
| 164840 | 165972 | { |
| | @@ -167075,11 +168207,14 @@ |
| 167075 | 168207 | }else if( tokenType==TK_FILTER ){ |
| 167076 | 168208 | assert( n==6 ); |
| 167077 | 168209 | tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed); |
| 167078 | 168210 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 167079 | 168211 | }else{ |
| 167080 | | - sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql); |
| 168212 | + Token x; |
| 168213 | + x.z = zSql; |
| 168214 | + x.n = n; |
| 168215 | + sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", &x); |
| 167081 | 168216 | break; |
| 167082 | 168217 | } |
| 167083 | 168218 | } |
| 167084 | 168219 | pParse->sLastToken.z = zSql; |
| 167085 | 168220 | pParse->sLastToken.n = n; |
| | @@ -167125,11 +168260,11 @@ |
| 167125 | 168260 | sqlite3DeleteTable(db, pParse->pNewTable); |
| 167126 | 168261 | } |
| 167127 | 168262 | if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){ |
| 167128 | 168263 | sqlite3DeleteTrigger(db, pParse->pNewTrigger); |
| 167129 | 168264 | } |
| 167130 | | - sqlite3DbFree(db, pParse->pVList); |
| 168265 | + if( pParse->pVList ) sqlite3DbFreeNN(db, pParse->pVList); |
| 167131 | 168266 | db->pParse = pParentParse; |
| 167132 | 168267 | assert( nErr==0 || pParse->rc!=SQLITE_OK ); |
| 167133 | 168268 | return nErr; |
| 167134 | 168269 | } |
| 167135 | 168270 | |
| | @@ -171686,16 +172821,20 @@ |
| 171686 | 172821 | ** is called immediately after installing the new callback and the return |
| 171687 | 172822 | ** value from sqlite3FaultSim(0) becomes the return from |
| 171688 | 172823 | ** sqlite3_test_control(). |
| 171689 | 172824 | */ |
| 171690 | 172825 | case SQLITE_TESTCTRL_FAULT_INSTALL: { |
| 171691 | | - /* MSVC is picky about pulling func ptrs from va lists. |
| 171692 | | - ** http://support.microsoft.com/kb/47961 |
| 172826 | + /* A bug in MSVC prevents it from understanding pointers to functions |
| 172827 | + ** types in the second argument to va_arg(). Work around the problem |
| 172828 | + ** using a typedef. |
| 172829 | + ** http://support.microsoft.com/kb/47961 <-- dead hyperlink |
| 172830 | + ** Search at http://web.archive.org/ to find the 2015-03-16 archive |
| 172831 | + ** of the link above to see the original text. |
| 171693 | 172832 | ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int)); |
| 171694 | 172833 | */ |
| 171695 | | - typedef int(*TESTCALLBACKFUNC_t)(int); |
| 171696 | | - sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t); |
| 172834 | + typedef int(*sqlite3FaultFuncType)(int); |
| 172835 | + sqlite3GlobalConfig.xTestCallback = va_arg(ap, sqlite3FaultFuncType); |
| 171697 | 172836 | rc = sqlite3FaultSim(0); |
| 171698 | 172837 | break; |
| 171699 | 172838 | } |
| 171700 | 172839 | |
| 171701 | 172840 | /* |
| | @@ -171818,17 +172957,31 @@ |
| 171818 | 172957 | sqlite3 *db = va_arg(ap, sqlite3*); |
| 171819 | 172958 | db->dbOptFlags = va_arg(ap, u32); |
| 171820 | 172959 | break; |
| 171821 | 172960 | } |
| 171822 | 172961 | |
| 171823 | | - /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff); |
| 172962 | + /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, onoff, xAlt); |
| 172963 | + ** |
| 172964 | + ** If parameter onoff is 1, subsequent calls to localtime() fail. |
| 172965 | + ** If 2, then invoke xAlt() instead of localtime(). If 0, normal |
| 172966 | + ** processing. |
| 172967 | + ** |
| 172968 | + ** xAlt arguments are void pointers, but they really want to be: |
| 172969 | + ** |
| 172970 | + ** int xAlt(const time_t*, struct tm*); |
| 171824 | 172971 | ** |
| 171825 | | - ** If parameter onoff is non-zero, subsequent calls to localtime() |
| 171826 | | - ** and its variants fail. If onoff is zero, undo this setting. |
| 172972 | + ** xAlt should write results in to struct tm object of its 2nd argument |
| 172973 | + ** and return zero on success, or return non-zero on failure. |
| 171827 | 172974 | */ |
| 171828 | 172975 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: { |
| 171829 | 172976 | sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); |
| 172977 | + if( sqlite3GlobalConfig.bLocaltimeFault==2 ){ |
| 172978 | + typedef int(*sqlite3LocaltimeType)(const void*,void*); |
| 172979 | + sqlite3GlobalConfig.xAltLocaltime = va_arg(ap, sqlite3LocaltimeType); |
| 172980 | + }else{ |
| 172981 | + sqlite3GlobalConfig.xAltLocaltime = 0; |
| 172982 | + } |
| 171830 | 172983 | break; |
| 171831 | 172984 | } |
| 171832 | 172985 | |
| 171833 | 172986 | /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*); |
| 171834 | 172987 | ** |
| | @@ -172026,14 +173179,16 @@ |
| 172026 | 173179 | ** Test access for the LogEst conversion routines. |
| 172027 | 173180 | */ |
| 172028 | 173181 | case SQLITE_TESTCTRL_LOGEST: { |
| 172029 | 173182 | double rIn = va_arg(ap, double); |
| 172030 | 173183 | LogEst rLogEst = sqlite3LogEstFromDouble(rIn); |
| 172031 | | - u64 iInt = sqlite3LogEstToInt(rLogEst); |
| 172032 | | - va_arg(ap, int*)[0] = rLogEst; |
| 172033 | | - va_arg(ap, u64*)[0] = iInt; |
| 172034 | | - va_arg(ap, int*)[0] = sqlite3LogEst(iInt); |
| 173184 | + int *pI1 = va_arg(ap,int*); |
| 173185 | + u64 *pU64 = va_arg(ap,u64*); |
| 173186 | + int *pI2 = va_arg(ap,int*); |
| 173187 | + *pI1 = rLogEst; |
| 173188 | + *pU64 = sqlite3LogEstToInt(rLogEst); |
| 173189 | + *pI2 = sqlite3LogEst(*pU64); |
| 172035 | 173190 | break; |
| 172036 | 173191 | } |
| 172037 | 173192 | |
| 172038 | 173193 | |
| 172039 | 173194 | #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD) |
| | @@ -193960,11 +195115,11 @@ |
| 193960 | 195115 | u32 iRoot; |
| 193961 | 195116 | JsonNode *pTarget; |
| 193962 | 195117 | if( pPatch->eType!=JSON_OBJECT ){ |
| 193963 | 195118 | return pPatch; |
| 193964 | 195119 | } |
| 193965 | | - assert( iTarget>=0 && iTarget<pParse->nNode ); |
| 195120 | + assert( iTarget<pParse->nNode ); |
| 193966 | 195121 | pTarget = &pParse->aNode[iTarget]; |
| 193967 | 195122 | assert( (pPatch->jnFlags & JNODE_APPEND)==0 ); |
| 193968 | 195123 | if( pTarget->eType!=JSON_OBJECT ){ |
| 193969 | 195124 | jsonRemoveAllNulls(pPatch); |
| 193970 | 195125 | return pPatch; |
| | @@ -195001,11 +196156,11 @@ |
| 195001 | 196156 | sqlite3_module *pModule; |
| 195002 | 196157 | } aMod[] = { |
| 195003 | 196158 | { "json_each", &jsonEachModule }, |
| 195004 | 196159 | { "json_tree", &jsonTreeModule }, |
| 195005 | 196160 | }; |
| 195006 | | - int i; |
| 196161 | + unsigned int i; |
| 195007 | 196162 | for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){ |
| 195008 | 196163 | rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0); |
| 195009 | 196164 | } |
| 195010 | 196165 | return rc; |
| 195011 | 196166 | } |
| | @@ -209255,10 +210410,11 @@ |
| 209255 | 210410 | && pIdxInfo->aOrderBy[0].iColumn<=0 |
| 209256 | 210411 | && pIdxInfo->aOrderBy[0].desc==0 |
| 209257 | 210412 | ){ |
| 209258 | 210413 | pIdxInfo->orderByConsumed = 1; |
| 209259 | 210414 | } |
| 210415 | + sqlite3VtabWriteAll(pIdxInfo); |
| 209260 | 210416 | return SQLITE_OK; |
| 209261 | 210417 | } |
| 209262 | 210418 | |
| 209263 | 210419 | /* |
| 209264 | 210420 | ** Open a new dbpagevfs cursor. |
| | @@ -209432,11 +210588,11 @@ |
| 209432 | 210588 | if( iDb<0 ){ |
| 209433 | 210589 | zErr = "no such schema"; |
| 209434 | 210590 | goto update_fail; |
| 209435 | 210591 | } |
| 209436 | 210592 | pBt = pTab->db->aDb[iDb].pBt; |
| 209437 | | - if( pgno<1 || pBt==0 || pgno>(int)sqlite3BtreeLastPage(pBt) ){ |
| 210593 | + if( pgno<1 || pBt==0 || pgno>sqlite3BtreeLastPage(pBt) ){ |
| 209438 | 210594 | zErr = "bad page number"; |
| 209439 | 210595 | goto update_fail; |
| 209440 | 210596 | } |
| 209441 | 210597 | szPage = sqlite3BtreeGetPageSize(pBt); |
| 209442 | 210598 | if( sqlite3_value_type(argv[3])!=SQLITE_BLOB |
| | @@ -231609,11 +232765,11 @@ |
| 231609 | 232765 | int rc; |
| 231610 | 232766 | |
| 231611 | 232767 | rc = sqlite3_step(pSorter->pStmt); |
| 231612 | 232768 | if( rc==SQLITE_DONE ){ |
| 231613 | 232769 | rc = SQLITE_OK; |
| 231614 | | - CsrFlagSet(pCsr, FTS5CSR_EOF); |
| 232770 | + CsrFlagSet(pCsr, FTS5CSR_EOF|FTS5CSR_REQUIRE_CONTENT); |
| 231615 | 232771 | }else if( rc==SQLITE_ROW ){ |
| 231616 | 232772 | const u8 *a; |
| 231617 | 232773 | const u8 *aBlob; |
| 231618 | 232774 | int nBlob; |
| 231619 | 232775 | int i; |
| | @@ -233598,11 +234754,11 @@ |
| 233598 | 234754 | int nArg, /* Number of args */ |
| 233599 | 234755 | sqlite3_value **apUnused /* Function arguments */ |
| 233600 | 234756 | ){ |
| 233601 | 234757 | assert( nArg==0 ); |
| 233602 | 234758 | UNUSED_PARAM2(nArg, apUnused); |
| 233603 | | - sqlite3_result_text(pCtx, "fts5: 2022-01-25 00:03:25 a8db69411b0d1275909adeb21027784ada17af24efe3a59ae0ae2a897659ff17", -1, SQLITE_TRANSIENT); |
| 234759 | + sqlite3_result_text(pCtx, "fts5: 2022-03-31 11:12:56 f2d9262e4427ab37ba26c004fc7a4790c86c1856d695a6b4ec3e72732ea54c09", -1, SQLITE_TRANSIENT); |
| 233604 | 234760 | } |
| 233605 | 234761 | |
| 233606 | 234762 | /* |
| 233607 | 234763 | ** Return true if zName is the extension on one of the shadow tables used |
| 233608 | 234764 | ** by this module. |
| 233609 | 234765 | |