| | @@ -452,11 +452,11 @@ |
| 452 | 452 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 453 | 453 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 454 | 454 | */ |
| 455 | 455 | #define SQLITE_VERSION "3.38.0" |
| 456 | 456 | #define SQLITE_VERSION_NUMBER 3038000 |
| 457 | | -#define SQLITE_SOURCE_ID "2022-01-12 00:28:12 adebb9d7478d092f16fb0ef7d5246ce152b166479d6f949110b5878b89ea2cec" |
| 457 | +#define SQLITE_SOURCE_ID "2022-02-05 01:01:07 1ec747d1c34ced9877709dd306e674376e79145de08b9c316d12bc5e06efc03e" |
| 458 | 458 | |
| 459 | 459 | /* |
| 460 | 460 | ** CAPI3REF: Run-Time Library Version Numbers |
| 461 | 461 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 462 | 462 | ** |
| | @@ -4656,10 +4656,12 @@ |
| 4656 | 4656 | ** still make the distinction between protected and unprotected |
| 4657 | 4657 | ** sqlite3_value objects even when not strictly required. |
| 4658 | 4658 | ** |
| 4659 | 4659 | ** ^The sqlite3_value objects that are passed as parameters into the |
| 4660 | 4660 | ** implementation of [application-defined SQL functions] are protected. |
| 4661 | +** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()] |
| 4662 | +** are protected. |
| 4661 | 4663 | ** ^The sqlite3_value object returned by |
| 4662 | 4664 | ** [sqlite3_column_value()] is unprotected. |
| 4663 | 4665 | ** Unprotected sqlite3_value objects may only be used as arguments |
| 4664 | 4666 | ** to [sqlite3_result_value()], [sqlite3_bind_value()], and |
| 4665 | 4667 | ** [sqlite3_value_dup()]. |
| | @@ -7435,28 +7437,60 @@ |
| 7435 | 7437 | /* |
| 7436 | 7438 | ** CAPI3REF: Virtual Table Constraint Operator Codes |
| 7437 | 7439 | ** |
| 7438 | 7440 | ** These macros define the allowed values for the |
| 7439 | 7441 | ** [sqlite3_index_info].aConstraint[].op field. Each value represents |
| 7440 | | -** an operator that is part of a constraint term in the wHERE clause of |
| 7442 | +** an operator that is part of a constraint term in the WHERE clause of |
| 7441 | 7443 | ** a query that uses a [virtual table]. |
| 7444 | +** |
| 7445 | +** ^The left-hand operand of the operator is given by the corresponding |
| 7446 | +** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand |
| 7447 | +** operand is the rowid. |
| 7448 | +** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET |
| 7449 | +** operators have no left-hand operand, and so for those operators the |
| 7450 | +** corresponding aConstraint[].iColumn is meaningless and should not be |
| 7451 | +** used. |
| 7452 | +** |
| 7453 | +** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through |
| 7454 | +** value 255 are reserved to represent functions that are overloaded |
| 7455 | +** by the [xFindFunction|xFindFunction method] of the virtual table |
| 7456 | +** implementation. |
| 7457 | +** |
| 7458 | +** The right-hand operands for each constraint might be accessible using |
| 7459 | +** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand |
| 7460 | +** operand is only available if it appears as a single constant literal |
| 7461 | +** in the input SQL. If the right-hand operand is another column or an |
| 7462 | +** expression (even a constant expression) or a parameter, then the |
| 7463 | +** sqlite3_vtab_rhs_value() probably will not be able to extract it. |
| 7464 | +** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and |
| 7465 | +** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand |
| 7466 | +** and hence calls to sqlite3_vtab_rhs_value() for those operators will |
| 7467 | +** always return SQLITE_NOTFOUND. |
| 7468 | +** |
| 7469 | +** The collating sequence to be used for comparison can be found using |
| 7470 | +** the [sqlite3_vtab_collation()] interface. For most real-world virtual |
| 7471 | +** tables, the collating sequence of constraints does not matter (for example |
| 7472 | +** because the constraints are numeric) and so the sqlite3_vtab_collation() |
| 7473 | +** interface is no commonly needed. |
| 7442 | 7474 | */ |
| 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 |
| 7475 | +#define SQLITE_INDEX_CONSTRAINT_EQ 2 |
| 7476 | +#define SQLITE_INDEX_CONSTRAINT_GT 4 |
| 7477 | +#define SQLITE_INDEX_CONSTRAINT_LE 8 |
| 7478 | +#define SQLITE_INDEX_CONSTRAINT_LT 16 |
| 7479 | +#define SQLITE_INDEX_CONSTRAINT_GE 32 |
| 7480 | +#define SQLITE_INDEX_CONSTRAINT_MATCH 64 |
| 7481 | +#define SQLITE_INDEX_CONSTRAINT_LIKE 65 |
| 7482 | +#define SQLITE_INDEX_CONSTRAINT_GLOB 66 |
| 7483 | +#define SQLITE_INDEX_CONSTRAINT_REGEXP 67 |
| 7484 | +#define SQLITE_INDEX_CONSTRAINT_NE 68 |
| 7485 | +#define SQLITE_INDEX_CONSTRAINT_ISNOT 69 |
| 7486 | +#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70 |
| 7487 | +#define SQLITE_INDEX_CONSTRAINT_ISNULL 71 |
| 7488 | +#define SQLITE_INDEX_CONSTRAINT_IS 72 |
| 7489 | +#define SQLITE_INDEX_CONSTRAINT_LIMIT 73 |
| 7490 | +#define SQLITE_INDEX_CONSTRAINT_OFFSET 74 |
| 7491 | +#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150 |
| 7458 | 7492 | |
| 7459 | 7493 | /* |
| 7460 | 7494 | ** CAPI3REF: Register A Virtual Table Implementation |
| 7461 | 7495 | ** METHOD: sqlite3 |
| 7462 | 7496 | ** |
| | @@ -9775,25 +9809,26 @@ |
| 9775 | 9809 | */ |
| 9776 | 9810 | SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*); |
| 9777 | 9811 | |
| 9778 | 9812 | /* |
| 9779 | 9813 | ** CAPI3REF: Determine The Collation For a Virtual Table Constraint |
| 9814 | +** METHOD: sqlite3_index_info |
| 9780 | 9815 | ** |
| 9781 | 9816 | ** This function may only be called from within a call to the [xBestIndex] |
| 9782 | 9817 | ** method of a [virtual table]. This function returns a pointer to a string |
| 9783 | 9818 | ** that is the name of the appropriate collation sequence to use for text |
| 9784 | 9819 | ** comparisons on the constraint identified by its arguments. |
| 9785 | 9820 | ** |
| 9786 | | -** The first argument must be the pointer to the sqlite3_index_info object |
| 9821 | +** The first argument must be the pointer to the [sqlite3_index_info] object |
| 9787 | 9822 | ** that is the first parameter to the xBestIndex() method. The second argument |
| 9788 | 9823 | ** must be an index into the aConstraint[] array belonging to the |
| 9789 | 9824 | ** sqlite3_index_info structure passed to xBestIndex. |
| 9790 | 9825 | ** |
| 9791 | 9826 | ** Important: |
| 9792 | 9827 | ** The first parameter must be the same pointer that is passed into the |
| 9793 | 9828 | ** xBestMethod() method. The first parameter may not be a pointer to a |
| 9794 | | -** different sqlite3_index_info object, even an exact copy. |
| 9829 | +** different [sqlite3_index_info] object, even an exact copy. |
| 9795 | 9830 | ** |
| 9796 | 9831 | ** The return value is computed as follows: |
| 9797 | 9832 | ** |
| 9798 | 9833 | ** <ol> |
| 9799 | 9834 | ** <li><p> If the constraint comes from a WHERE clause expression that contains |
| | @@ -9807,10 +9842,240 @@ |
| 9807 | 9842 | ** <li><p> Otherwise, "BINARY" is returned. |
| 9808 | 9843 | ** </ol> |
| 9809 | 9844 | */ |
| 9810 | 9845 | SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int); |
| 9811 | 9846 | |
| 9847 | +/* |
| 9848 | +** CAPI3REF: Determine if a virtual table query is DISTINCT |
| 9849 | +** METHOD: sqlite3_index_info |
| 9850 | +** |
| 9851 | +** This API may only be used from within an [xBestIndex|xBestIndex method] |
| 9852 | +** of a [virtual table] implementation. The result of calling this |
| 9853 | +** interface from outside of xBestIndex() is undefined and probably harmful. |
| 9854 | +** |
| 9855 | +** ^The sqlite3_vtab_distinct() interface returns an integer that is |
| 9856 | +** either 0, 1, or 2. The integer returned by sqlite3_vtab_distinct() |
| 9857 | +** gives the virtual table additional information about how the query |
| 9858 | +** planner wants the output to be ordered. As long as the virtual table |
| 9859 | +** can meet the ordering requirements of the query planner, it may set |
| 9860 | +** the "orderByConsumed" flag. |
| 9861 | +** |
| 9862 | +** <ol><li value="0"><p> |
| 9863 | +** ^If the sqlite3_vtab_distinct() interface returns 0, that means |
| 9864 | +** that the query planner needs the virtual table to return all rows in the |
| 9865 | +** sort order defined by the "nOrderBy" and "aOrderBy" fields of the |
| 9866 | +** [sqlite3_index_info] object. This is the default expectation. If the |
| 9867 | +** virtual table outputs all rows in sorted order, then it is always safe for |
| 9868 | +** the xBestIndex method to set the "orderByConsumed" flag, regardless of |
| 9869 | +** the return value from sqlite3_vtab_distinct(). |
| 9870 | +** <li value="1"><p> |
| 9871 | +** ^(If the sqlite3_vtab_distinct() interface returns 1, that means |
| 9872 | +** that the query planner does not need the rows to be returned in sorted order |
| 9873 | +** as long as all rows with the same values in all columns identified by the |
| 9874 | +** "aOrderBy" field are adjacent.)^ This mode is used when the query planner |
| 9875 | +** is doing a GROUP BY. |
| 9876 | +** <li value="2"><p> |
| 9877 | +** ^(If the sqlite3_vtab_distinct() interface returns 2, that means |
| 9878 | +** that the query planner does not need the rows returned in any particular |
| 9879 | +** order, as long as rows with the same values in all "aOrderBy" columns |
| 9880 | +** are adjacent.)^ ^(Furthermore, only a single row for each particular |
| 9881 | +** combination of values in the columns identified by the "aOrderBy" field |
| 9882 | +** needs to be returned.)^ ^It is always ok for two or more rows with the same |
| 9883 | +** values in all "aOrderBy" columns to be returned, as long as all such rows |
| 9884 | +** are adjacent. ^The virtual table may, if it chooses, omit extra rows |
| 9885 | +** that have the same value for all columns identified by "aOrderBy". |
| 9886 | +** ^However omitting the extra rows is optional. |
| 9887 | +** This mode is used for a DISTINCT query. |
| 9888 | +** </ol> |
| 9889 | +** |
| 9890 | +** ^For the purposes of comparing virtual table output values to see if the |
| 9891 | +** values are same value for sorting purposes, two NULL values are considered |
| 9892 | +** to be the same. In other words, the comparison operator is "IS" |
| 9893 | +** (or "IS NOT DISTINCT FROM") and not "==". |
| 9894 | +** |
| 9895 | +** If a virtual table implementation is unable to meet the requirements |
| 9896 | +** specified above, then it must not set the "orderByConsumed" flag in the |
| 9897 | +** [sqlite3_index_info] object or an incorrect answer may result. |
| 9898 | +** |
| 9899 | +** ^A virtual table implementation is always free to return rows in any order |
| 9900 | +** it wants, as long as the "orderByConsumed" flag is not set. ^When the |
| 9901 | +** the "orderByConsumed" flag is unset, the query planner will add extra |
| 9902 | +** [bytecode] to ensure that the final results returned by the SQL query are |
| 9903 | +** ordered correctly. The use of the "orderByConsumed" flag and the |
| 9904 | +** sqlite3_vtab_distinct() interface is merely an optimization. ^Careful |
| 9905 | +** use of the sqlite3_vtab_distinct() interface and the "orderByConsumed" |
| 9906 | +** flag might help queries against a virtual table to run faster. Being |
| 9907 | +** overly aggressive and setting the "orderByConsumed" flag when it is not |
| 9908 | +** valid to do so, on the other hand, might cause SQLite to return incorrect |
| 9909 | +** results. |
| 9910 | +*/ |
| 9911 | +SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info*); |
| 9912 | + |
| 9913 | +/* |
| 9914 | +** CAPI3REF: Identify and handle IN constraints in xBestIndex |
| 9915 | +** |
| 9916 | +** This interface may only be used from within an |
| 9917 | +** [xBestIndex|xBestIndex() method] of a [virtual table] implementation. |
| 9918 | +** The result of invoking this interface from any other context is |
| 9919 | +** undefined and probably harmful. |
| 9920 | +** |
| 9921 | +** ^(A constraint on a virtual table of the form |
| 9922 | +** "[IN operator|column IN (...)]" is |
| 9923 | +** communicated to the xBestIndex method as a |
| 9924 | +** [SQLITE_INDEX_CONSTRAINT_EQ] constraint.)^ If xBestIndex wants to use |
| 9925 | +** this constraint, it must set the corresponding |
| 9926 | +** aConstraintUsage[].argvIndex to a postive integer. ^(Then, under |
| 9927 | +** the usual mode of handling IN operators, SQLite generates [bytecode] |
| 9928 | +** that invokes the [xFilter|xFilter() method] once for each value |
| 9929 | +** on the right-hand side of the IN operator.)^ Thus the virtual table |
| 9930 | +** only sees a single value from the right-hand side of the IN operator |
| 9931 | +** at a time. |
| 9932 | +** |
| 9933 | +** In some cases, however, it would be advantageous for the virtual |
| 9934 | +** table to see all values on the right-hand of the IN operator all at |
| 9935 | +** once. The sqlite3_vtab_in() interfaces facilitates this in two ways: |
| 9936 | +** |
| 9937 | +** <ol> |
| 9938 | +** <li><p> |
| 9939 | +** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero) |
| 9940 | +** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint |
| 9941 | +** is an [IN operator] that can be processed all at once. ^In other words, |
| 9942 | +** sqlite3_vtab_in() with -1 in the third argument is a mechanism |
| 9943 | +** by which the virtual table can ask SQLite if all-at-once processing |
| 9944 | +** of the IN operator is even possible. |
| 9945 | +** |
| 9946 | +** <li><p> |
| 9947 | +** ^A call to sqlite3_vtab_in(P,N,F) with F==1 or F==0 indicates |
| 9948 | +** to SQLite that the virtual table does or does not want to process |
| 9949 | +** the IN operator all-at-once, respectively. ^Thus when the third |
| 9950 | +** parameter (F) is non-negative, this interface is the mechanism by |
| 9951 | +** which the virtual table tells SQLite how it wants to process the |
| 9952 | +** IN operator. |
| 9953 | +** </ol> |
| 9954 | +** |
| 9955 | +** ^The sqlite3_vtab_in(P,N,F) interface can be invoked multiple times |
| 9956 | +** within the same xBestIndex method call. ^For any given P,N pair, |
| 9957 | +** the return value from sqlite3_vtab_in(P,N,F) will always be the same |
| 9958 | +** within the same xBestIndex call. ^If the interface returns true |
| 9959 | +** (non-zero), that means that the constraint is an IN operator |
| 9960 | +** that can be processed all-at-once. ^If the constraint is not an IN |
| 9961 | +** operator or cannot be processed all-at-once, then the interface returns |
| 9962 | +** false. |
| 9963 | +** |
| 9964 | +** ^(All-at-once processing of the IN operator is selected if both of the |
| 9965 | +** following conditions are met: |
| 9966 | +** |
| 9967 | +** <ol> |
| 9968 | +** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive |
| 9969 | +** integer. This is how the virtual table tells SQLite that it wants to |
| 9970 | +** use the N-th constraint. |
| 9971 | +** |
| 9972 | +** <li><p> The last call to sqlite3_vtab_in(P,N,F) for which F was |
| 9973 | +** non-negative had F>=1. |
| 9974 | +** </ol>)^ |
| 9975 | +** |
| 9976 | +** ^If either or both of the conditions above are false, then SQLite uses |
| 9977 | +** the traditional one-at-a-time processing strategy for the IN constraint. |
| 9978 | +** ^If both conditions are true, then the argvIndex-th parameter to the |
| 9979 | +** xFilter method will be an [sqlite3_value] that appears to be NULL, |
| 9980 | +** but which can be passed to [sqlite3_vtab_in_first()] and |
| 9981 | +** [sqlite3_vtab_in_next()] to find all values on the right-hand side |
| 9982 | +** of the IN constraint. |
| 9983 | +*/ |
| 9984 | +SQLITE_API int sqlite3_vtab_in(sqlite3_index_info*, int iCons, int bHandle); |
| 9985 | + |
| 9986 | +/* |
| 9987 | +** CAPI3REF: Find all elements on the right-hand side of an IN constraint. |
| 9988 | +** |
| 9989 | +** These interfaces are only useful from within the |
| 9990 | +** [xFilter|xFilter() method] of a [virtual table] implementation. |
| 9991 | +** The result of invoking these interfaces from any other context |
| 9992 | +** is undefined and probably harmful. |
| 9993 | +** |
| 9994 | +** The X parameter in a call to sqlite3_vtab_in_first(X,P) or |
| 9995 | +** sqlite3_vtab_in_next(X,P) must be one of the parameters to the |
| 9996 | +** xFilter method which invokes these routines, and specifically |
| 9997 | +** a parameter that was previously selected for all-at-once IN constraint |
| 9998 | +** processing use the [sqlite3_vtab_in()] interface in the |
| 9999 | +** [xBestIndex|xBestIndex method]. ^(If the X parameter is not |
| 10000 | +** an xFilter argument that was selected for all-at-once IN constraint |
| 10001 | +** processing, then these routines return [SQLITE_MISUSE])^ or perhaps |
| 10002 | +** exhibit some other undefined or harmful behavior. |
| 10003 | +** |
| 10004 | +** ^(Use these routines to access all values on the right-hand side |
| 10005 | +** of the IN constraint using code like the following: |
| 10006 | +** |
| 10007 | +** <blockquote><pre> |
| 10008 | +** for(rc=sqlite3_vtab_in_first(pList, &pVal); |
| 10009 | +** rc==SQLITE_OK && pVal |
| 10010 | +** rc=sqlite3_vtab_in_next(pList, &pVal) |
| 10011 | +** ){ |
| 10012 | +** // do something with pVal |
| 10013 | +** } |
| 10014 | +** if( rc!=SQLITE_OK ){ |
| 10015 | +** // an error has occurred |
| 10016 | +** } |
| 10017 | +** </pre></blockquote>)^ |
| 10018 | +** |
| 10019 | +** ^On success, the sqlite3_vtab_in_first(X,P) and sqlite3_vtab_in_next(X,P) |
| 10020 | +** routines return SQLITE_OK and set *P to point to the first or next value |
| 10021 | +** on the RHS of the IN constraint. ^If there are no more values on the |
| 10022 | +** right hand side of the IN constraint, then *P is set to NULL and these |
| 10023 | +** routines return [SQLITE_DONE]. ^The return value might be |
| 10024 | +** some other value, such as SQLITE_NOMEM, in the event of a malfunction. |
| 10025 | +** |
| 10026 | +** The *ppOut values returned by these routines are only valid until the |
| 10027 | +** next call to either of these routines or until the end of the xFilter |
| 10028 | +** method from which these routines were called. If the virtual table |
| 10029 | +** implementation needs to retain the *ppOut values for longer, it must make |
| 10030 | +** copies. The *ppOut values are [protected sqlite3_value|protected]. |
| 10031 | +*/ |
| 10032 | +SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut); |
| 10033 | +SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut); |
| 10034 | + |
| 10035 | +/* |
| 10036 | +** CAPI3REF: Constraint values in xBestIndex() |
| 10037 | +** METHOD: sqlite3_index_info |
| 10038 | +** |
| 10039 | +** This API may only be used from within the [xBestIndex|xBestIndex method] |
| 10040 | +** of a [virtual table] implementation. The result of calling this interface |
| 10041 | +** from outside of an xBestIndex method are undefined and probably harmful. |
| 10042 | +** |
| 10043 | +** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within |
| 10044 | +** the [xBestIndex] method of a [virtual table] implementation, with P being |
| 10045 | +** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and |
| 10046 | +** J being a 0-based index into P->aConstraint[], then this routine |
| 10047 | +** attempts to set *V to the value of the right-hand operand of |
| 10048 | +** that constraint if the right-hand operand is known. ^If the |
| 10049 | +** right-hand operand is not known, then *V is set to a NULL pointer. |
| 10050 | +** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if |
| 10051 | +** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V) |
| 10052 | +** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th |
| 10053 | +** constraint is not available. ^The sqlite3_vtab_rhs_value() interface |
| 10054 | +** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if |
| 10055 | +** something goes wrong. |
| 10056 | +** |
| 10057 | +** The sqlite3_vtab_rhs_value() interface is usually only successful if |
| 10058 | +** the right-hand operand of a constraint is a literal value in the original |
| 10059 | +** SQL statement. If the right-hand operand is an expression or a reference |
| 10060 | +** to some other column or a [host parameter], then sqlite3_vtab_rhs_value() |
| 10061 | +** will probably return [SQLITE_NOTFOUND]. |
| 10062 | +** |
| 10063 | +** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and |
| 10064 | +** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such |
| 10065 | +** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^ |
| 10066 | +** |
| 10067 | +** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value |
| 10068 | +** and remains valid for the duration of the xBestIndex method call. |
| 10069 | +** ^When xBestIndex returns, the sqlite3_value object returned by |
| 10070 | +** sqlite3_vtab_rhs_value() is automatically deallocated. |
| 10071 | +** |
| 10072 | +** The "_rhs_" in the name of this routine is an appreviation for |
| 10073 | +** "Right-Hand Side". |
| 10074 | +*/ |
| 10075 | +SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal); |
| 10076 | + |
| 9812 | 10077 | /* |
| 9813 | 10078 | ** CAPI3REF: Conflict resolution modes |
| 9814 | 10079 | ** KEYWORDS: {conflict resolution mode} |
| 9815 | 10080 | ** |
| 9816 | 10081 | ** These constants are returned by [sqlite3_vtab_on_conflict()] to |
| | @@ -14374,14 +14639,15 @@ |
| 14374 | 14639 | #define BMS ((int)(sizeof(Bitmask)*8)) |
| 14375 | 14640 | |
| 14376 | 14641 | /* |
| 14377 | 14642 | ** A bit in a Bitmask |
| 14378 | 14643 | */ |
| 14379 | | -#define MASKBIT(n) (((Bitmask)1)<<(n)) |
| 14380 | | -#define MASKBIT64(n) (((u64)1)<<(n)) |
| 14381 | | -#define MASKBIT32(n) (((unsigned int)1)<<(n)) |
| 14382 | | -#define ALLBITS ((Bitmask)-1) |
| 14644 | +#define MASKBIT(n) (((Bitmask)1)<<(n)) |
| 14645 | +#define MASKBIT64(n) (((u64)1)<<(n)) |
| 14646 | +#define MASKBIT32(n) (((unsigned int)1)<<(n)) |
| 14647 | +#define SMASKBIT32(n) ((n)<=31?((unsigned int)1)<<(n):0) |
| 14648 | +#define ALLBITS ((Bitmask)-1) |
| 14383 | 14649 | |
| 14384 | 14650 | /* A VList object records a mapping between parameters/variables/wildcards |
| 14385 | 14651 | ** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer |
| 14386 | 14652 | ** variable number associated with that parameter. See the format description |
| 14387 | 14653 | ** on the sqlite3VListAdd() routine for more information. A VList is really |
| | @@ -15403,21 +15669,22 @@ |
| 15403 | 15669 | #define OP_TableLock 168 /* synopsis: iDb=P1 root=P2 write=P3 */ |
| 15404 | 15670 | #define OP_VBegin 169 |
| 15405 | 15671 | #define OP_VCreate 170 |
| 15406 | 15672 | #define OP_VDestroy 171 |
| 15407 | 15673 | #define OP_VOpen 172 |
| 15408 | | -#define OP_VColumn 173 /* synopsis: r[P3]=vcolumn(P2) */ |
| 15409 | | -#define OP_VRename 174 |
| 15410 | | -#define OP_Pagecount 175 |
| 15411 | | -#define OP_MaxPgcnt 176 |
| 15412 | | -#define OP_FilterAdd 177 /* synopsis: filter(P1) += key(P3@P4) */ |
| 15413 | | -#define OP_Trace 178 |
| 15414 | | -#define OP_CursorHint 179 |
| 15415 | | -#define OP_ReleaseReg 180 /* synopsis: release r[P1@P2] mask P3 */ |
| 15416 | | -#define OP_Noop 181 |
| 15417 | | -#define OP_Explain 182 |
| 15418 | | -#define OP_Abortable 183 |
| 15674 | +#define OP_VInitIn 173 /* synopsis: r[P2]=ValueList(P1,P3) */ |
| 15675 | +#define OP_VColumn 174 /* synopsis: r[P3]=vcolumn(P2) */ |
| 15676 | +#define OP_VRename 175 |
| 15677 | +#define OP_Pagecount 176 |
| 15678 | +#define OP_MaxPgcnt 177 |
| 15679 | +#define OP_FilterAdd 178 /* synopsis: filter(P1) += key(P3@P4) */ |
| 15680 | +#define OP_Trace 179 |
| 15681 | +#define OP_CursorHint 180 |
| 15682 | +#define OP_ReleaseReg 181 /* synopsis: release r[P1@P2] mask P3 */ |
| 15683 | +#define OP_Noop 182 |
| 15684 | +#define OP_Explain 183 |
| 15685 | +#define OP_Abortable 184 |
| 15419 | 15686 | |
| 15420 | 15687 | /* Properties such as "out2" or "jump" that are specified in |
| 15421 | 15688 | ** comments following the "case" for each opcode in the vdbe.c |
| 15422 | 15689 | ** are encoded into bitvectors as follows: |
| 15423 | 15690 | */ |
| | @@ -15447,13 +15714,13 @@ |
| 15447 | 15714 | /* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,\ |
| 15448 | 15715 | /* 136 */ 0x00, 0x04, 0x04, 0x00, 0x00, 0x10, 0x00, 0x10,\ |
| 15449 | 15716 | /* 144 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 15450 | 15717 | /* 152 */ 0x00, 0x10, 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a,\ |
| 15451 | 15718 | /* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 15452 | | -/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\ |
| 15453 | | -/* 176 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 15454 | | -} |
| 15719 | +/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,\ |
| 15720 | +/* 176 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 15721 | +/* 184 */ 0x00,} |
| 15455 | 15722 | |
| 15456 | 15723 | /* The resolve3P2Values() routine is able to run faster if it knows |
| 15457 | 15724 | ** the value of the largest JUMP opcode. The smaller the maximum |
| 15458 | 15725 | ** JUMP opcode the better, so the mkopcodeh.tcl script that |
| 15459 | 15726 | ** generated this include file strives to group all JUMP opcodes |
| | @@ -18576,10 +18843,11 @@ |
| 18576 | 18843 | ** initialized as they will be set before being used. The boundary is |
| 18577 | 18844 | ** determined by offsetof(Parse,aTempReg). |
| 18578 | 18845 | **************************************************************************/ |
| 18579 | 18846 | |
| 18580 | 18847 | int aTempReg[8]; /* Holding area for temporary registers */ |
| 18848 | + Parse *pOuterParse; /* Outer Parse object when nested */ |
| 18581 | 18849 | Token sNameToken; /* Token with unqualified schema object name */ |
| 18582 | 18850 | |
| 18583 | 18851 | /************************************************************************ |
| 18584 | 18852 | ** Above is constant between recursions. Below is reset before and after |
| 18585 | 18853 | ** each recursion. The boundary between these two regions is determined |
| | @@ -18626,11 +18894,12 @@ |
| 18626 | 18894 | #define PARSE_MODE_UNMAP 3 |
| 18627 | 18895 | |
| 18628 | 18896 | /* |
| 18629 | 18897 | ** Sizes and pointers of various parts of the Parse object. |
| 18630 | 18898 | */ |
| 18631 | | -#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/ |
| 18899 | +#define PARSE_HDR(X) (((char*)(X))+offsetof(Parse,zErrMsg)) |
| 18900 | +#define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColCache*/ |
| 18632 | 18901 | #define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */ |
| 18633 | 18902 | #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */ |
| 18634 | 18903 | #define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */ |
| 18635 | 18904 | |
| 18636 | 18905 | /* |
| | @@ -19566,11 +19835,12 @@ |
| 19566 | 19835 | #endif |
| 19567 | 19836 | SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe*,int,const char*); |
| 19568 | 19837 | SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*); |
| 19569 | 19838 | SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*, |
| 19570 | 19839 | Upsert*); |
| 19571 | | -SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int); |
| 19840 | +SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*, |
| 19841 | + ExprList*,Select*,u16,int); |
| 19572 | 19842 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*); |
| 19573 | 19843 | SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*); |
| 19574 | 19844 | SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*); |
| 19575 | 19845 | SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*); |
| 19576 | 19846 | SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*); |
| | @@ -19966,11 +20236,11 @@ |
| 19966 | 20236 | void (*)(sqlite3_context*), |
| 19967 | 20237 | void (*)(sqlite3_context*,int,sqlite3_value **), |
| 19968 | 20238 | FuncDestructor *pDestructor |
| 19969 | 20239 | ); |
| 19970 | 20240 | SQLITE_PRIVATE void sqlite3NoopDestructor(void*); |
| 19971 | | -SQLITE_PRIVATE void sqlite3OomFault(sqlite3*); |
| 20241 | +SQLITE_PRIVATE void *sqlite3OomFault(sqlite3*); |
| 19972 | 20242 | SQLITE_PRIVATE void sqlite3OomClear(sqlite3*); |
| 19973 | 20243 | SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int); |
| 19974 | 20244 | SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); |
| 19975 | 20245 | |
| 19976 | 20246 | SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int); |
| | @@ -20087,11 +20357,12 @@ |
| 20087 | 20357 | SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *); |
| 20088 | 20358 | SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); |
| 20089 | 20359 | SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); |
| 20090 | 20360 | SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); |
| 20091 | 20361 | SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); |
| 20092 | | -SQLITE_PRIVATE void sqlite3ParserReset(Parse*); |
| 20362 | +SQLITE_PRIVATE void sqlite3ParseObjectInit(Parse*,sqlite3*); |
| 20363 | +SQLITE_PRIVATE void sqlite3ParseObjectReset(Parse*); |
| 20093 | 20364 | SQLITE_PRIVATE void *sqlite3ParserAddCleanup(Parse*,void(*)(sqlite3*,void*),void*); |
| 20094 | 20365 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 20095 | 20366 | SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*); |
| 20096 | 20367 | #endif |
| 20097 | 20368 | SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*); |
| | @@ -20520,10 +20791,18 @@ |
| 20520 | 20791 | |
| 20521 | 20792 | #endif /* !defined(_OS_COMMON_H_) */ |
| 20522 | 20793 | |
| 20523 | 20794 | /************** End of os_common.h *******************************************/ |
| 20524 | 20795 | /************** Begin file ctime.c *******************************************/ |
| 20796 | +/* DO NOT EDIT! |
| 20797 | +** This file is automatically generated by the script in the canonical |
| 20798 | +** SQLite source tree at tool/mkctimec.tcl. |
| 20799 | +** |
| 20800 | +** To modify this header, edit any of the various lists in that script |
| 20801 | +** which specify categories of generated conditionals in this file. |
| 20802 | +*/ |
| 20803 | + |
| 20525 | 20804 | /* |
| 20526 | 20805 | ** 2010 February 23 |
| 20527 | 20806 | ** |
| 20528 | 20807 | ** The author disclaims copyright to this source code. In place of |
| 20529 | 20808 | ** a legal notice, here is a blessing: |
| | @@ -20568,13 +20847,10 @@ |
| 20568 | 20847 | ** only a handful of compile-time options, so most times this array is usually |
| 20569 | 20848 | ** rather short and uses little memory space. |
| 20570 | 20849 | */ |
| 20571 | 20850 | static const char * const sqlite3azCompileOpt[] = { |
| 20572 | 20851 | |
| 20573 | | -/* |
| 20574 | | -** BEGIN CODE GENERATED BY tool/mkctime.tcl |
| 20575 | | -*/ |
| 20576 | 20852 | #ifdef SQLITE_32BIT_ROWID |
| 20577 | 20853 | "32BIT_ROWID", |
| 20578 | 20854 | #endif |
| 20579 | 20855 | #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC |
| 20580 | 20856 | "4_BYTE_ALIGNED_MALLOC", |
| | @@ -20701,13 +20977,10 @@ |
| 20701 | 20977 | "DISABLE_FTS4_DEFERRED", |
| 20702 | 20978 | #endif |
| 20703 | 20979 | #ifdef SQLITE_DISABLE_INTRINSIC |
| 20704 | 20980 | "DISABLE_INTRINSIC", |
| 20705 | 20981 | #endif |
| 20706 | | -#ifdef SQLITE_DISABLE_JSON |
| 20707 | | - "DISABLE_JSON", |
| 20708 | | -#endif |
| 20709 | 20982 | #ifdef SQLITE_DISABLE_LFS |
| 20710 | 20983 | "DISABLE_LFS", |
| 20711 | 20984 | #endif |
| 20712 | 20985 | #ifdef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS |
| 20713 | 20986 | "DISABLE_PAGECACHE_OVERFLOW_STATS", |
| | @@ -21104,10 +21377,13 @@ |
| 21104 | 21377 | #ifdef SQLITE_OMIT_INTEGRITY_CHECK |
| 21105 | 21378 | "OMIT_INTEGRITY_CHECK", |
| 21106 | 21379 | #endif |
| 21107 | 21380 | #ifdef SQLITE_OMIT_INTROSPECTION_PRAGMAS |
| 21108 | 21381 | "OMIT_INTROSPECTION_PRAGMAS", |
| 21382 | +#endif |
| 21383 | +#ifdef SQLITE_OMIT_JSON |
| 21384 | + "OMIT_JSON", |
| 21109 | 21385 | #endif |
| 21110 | 21386 | #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 21111 | 21387 | "OMIT_LIKE_OPTIMIZATION", |
| 21112 | 21388 | #endif |
| 21113 | 21389 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| | @@ -21293,14 +21569,12 @@ |
| 21293 | 21569 | "WIN32_MALLOC", |
| 21294 | 21570 | #endif |
| 21295 | 21571 | #ifdef SQLITE_ZERO_MALLOC |
| 21296 | 21572 | "ZERO_MALLOC", |
| 21297 | 21573 | #endif |
| 21298 | | -/* |
| 21299 | | -** END CODE GENERATED BY tool/mkctime.tcl |
| 21300 | | -*/ |
| 21301 | | -}; |
| 21574 | + |
| 21575 | +} ; |
| 21302 | 21576 | |
| 21303 | 21577 | SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt){ |
| 21304 | 21578 | *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]); |
| 21305 | 21579 | return (const char**)sqlite3azCompileOpt; |
| 21306 | 21580 | } |
| | @@ -22214,10 +22488,28 @@ |
| 22214 | 22488 | i64 iKey2; /* Second key value passed to hook */ |
| 22215 | 22489 | Mem *aNew; /* Array of new.* values */ |
| 22216 | 22490 | Table *pTab; /* Schema object being upated */ |
| 22217 | 22491 | Index *pPk; /* PK index if pTab is WITHOUT ROWID */ |
| 22218 | 22492 | }; |
| 22493 | + |
| 22494 | +/* |
| 22495 | +** An instance of this object is used to pass an vector of values into |
| 22496 | +** OP_VFilter, the xFilter method of a virtual table. The vector is the |
| 22497 | +** set of values on the right-hand side of an IN constraint. |
| 22498 | +** |
| 22499 | +** The value as passed into xFilter is an sqlite3_value with a "pointer" |
| 22500 | +** type, such as is generated by sqlite3_result_pointer() and read by |
| 22501 | +** sqlite3_value_pointer. Such values have MEM_Term|MEM_Subtype|MEM_Null |
| 22502 | +** and a subtype of 'p'. The sqlite3_vtab_in_first() and _next() interfaces |
| 22503 | +** know how to use this object to step through all the values in the |
| 22504 | +** right operand of the IN constraint. |
| 22505 | +*/ |
| 22506 | +typedef struct ValueList ValueList; |
| 22507 | +struct ValueList { |
| 22508 | + BtCursor *pCsr; /* An ephemeral table holding all values */ |
| 22509 | + sqlite3_value *pOut; /* Register to hold each decoded output value */ |
| 22510 | +}; |
| 22219 | 22511 | |
| 22220 | 22512 | /* |
| 22221 | 22513 | ** Function prototypes |
| 22222 | 22514 | */ |
| 22223 | 22515 | SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...); |
| | @@ -23406,11 +23698,12 @@ |
| 23406 | 23698 | */ |
| 23407 | 23699 | static int parseModifier( |
| 23408 | 23700 | sqlite3_context *pCtx, /* Function context */ |
| 23409 | 23701 | const char *z, /* The text of the modifier */ |
| 23410 | 23702 | int n, /* Length of zMod in bytes */ |
| 23411 | | - DateTime *p /* The date/time value to be modified */ |
| 23703 | + DateTime *p, /* The date/time value to be modified */ |
| 23704 | + int idx /* Parameter index of the modifier */ |
| 23412 | 23705 | ){ |
| 23413 | 23706 | int rc = 1; |
| 23414 | 23707 | double r; |
| 23415 | 23708 | switch(sqlite3UpperToLower[(u8)z[0]] ){ |
| 23416 | 23709 | case 'a': { |
| | @@ -23419,10 +23712,11 @@ |
| 23419 | 23712 | ** |
| 23420 | 23713 | ** If rawS is available, then interpret as a julian day number, or |
| 23421 | 23714 | ** a unix timestamp, depending on its magnitude. |
| 23422 | 23715 | */ |
| 23423 | 23716 | if( sqlite3_stricmp(z, "auto")==0 ){ |
| 23717 | + if( idx>1 ) return 1; /* IMP: R-33611-57934 */ |
| 23424 | 23718 | if( !p->rawS || p->validJD ){ |
| 23425 | 23719 | rc = 0; |
| 23426 | 23720 | p->rawS = 0; |
| 23427 | 23721 | }else if( p->s>=-210866760000 && p->s<=253402300799 ){ |
| 23428 | 23722 | r = p->s*1000.0 + 210866760000000.0; |
| | @@ -23443,10 +23737,11 @@ |
| 23443 | 23737 | ** is not the first modifier, or if the prior argument is not a numeric |
| 23444 | 23738 | ** value in the allowed range of julian day numbers understood by |
| 23445 | 23739 | ** SQLite (0..5373484.5) then the result will be NULL. |
| 23446 | 23740 | */ |
| 23447 | 23741 | if( sqlite3_stricmp(z, "julianday")==0 ){ |
| 23742 | + if( idx>1 ) return 1; /* IMP: R-31176-64601 */ |
| 23448 | 23743 | if( p->validJD && p->rawS ){ |
| 23449 | 23744 | rc = 0; |
| 23450 | 23745 | p->rawS = 0; |
| 23451 | 23746 | } |
| 23452 | 23747 | } |
| | @@ -23473,10 +23768,11 @@ |
| 23473 | 23768 | ** |
| 23474 | 23769 | ** Treat the current value of p->s as the number of |
| 23475 | 23770 | ** seconds since 1970. Convert to a real julian day number. |
| 23476 | 23771 | */ |
| 23477 | 23772 | if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ |
| 23773 | + if( idx>1 ) return 1; /* IMP: R-49255-55373 */ |
| 23478 | 23774 | r = p->s*1000.0 + 210866760000000.0; |
| 23479 | 23775 | if( r>=0.0 && r<464269060800000.0 ){ |
| 23480 | 23776 | clearYMD_HMS_TZ(p); |
| 23481 | 23777 | p->iJD = (sqlite3_int64)(r + 0.5); |
| 23482 | 23778 | p->validJD = 1; |
| | @@ -23686,11 +23982,11 @@ |
| 23686 | 23982 | } |
| 23687 | 23983 | } |
| 23688 | 23984 | for(i=1; i<argc; i++){ |
| 23689 | 23985 | z = sqlite3_value_text(argv[i]); |
| 23690 | 23986 | n = sqlite3_value_bytes(argv[i]); |
| 23691 | | - if( z==0 || parseModifier(context, (char*)z, n, p) ) return 1; |
| 23987 | + if( z==0 || parseModifier(context, (char*)z, n, p, i) ) return 1; |
| 23692 | 23988 | } |
| 23693 | 23989 | computeJD(p); |
| 23694 | 23990 | if( p->isError || !validJulianDay(p->iJD) ) return 1; |
| 23695 | 23991 | return 0; |
| 23696 | 23992 | } |
| | @@ -28956,22 +29252,31 @@ |
| 28956 | 29252 | /* |
| 28957 | 29253 | ** Call this routine to record the fact that an OOM (out-of-memory) error |
| 28958 | 29254 | ** has happened. This routine will set db->mallocFailed, and also |
| 28959 | 29255 | ** temporarily disable the lookaside memory allocator and interrupt |
| 28960 | 29256 | ** any running VDBEs. |
| 29257 | +** |
| 29258 | +** Always return a NULL pointer so that this routine can be invoked using |
| 29259 | +** |
| 29260 | +** return sqlite3OomFault(db); |
| 29261 | +** |
| 29262 | +** and thereby avoid unnecessary stack frame allocations for the overwhelmingly |
| 29263 | +** common case where no OOM occurs. |
| 28961 | 29264 | */ |
| 28962 | | -SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){ |
| 29265 | +SQLITE_PRIVATE void *sqlite3OomFault(sqlite3 *db){ |
| 28963 | 29266 | if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ |
| 28964 | 29267 | db->mallocFailed = 1; |
| 28965 | 29268 | if( db->nVdbeExec>0 ){ |
| 28966 | 29269 | AtomicStore(&db->u1.isInterrupted, 1); |
| 28967 | 29270 | } |
| 28968 | 29271 | DisableLookaside; |
| 28969 | 29272 | if( db->pParse ){ |
| 29273 | + sqlite3ErrorMsg(db->pParse, "out of memory"); |
| 28970 | 29274 | db->pParse->rc = SQLITE_NOMEM_BKPT; |
| 28971 | 29275 | } |
| 28972 | 29276 | } |
| 29277 | + return 0; |
| 28973 | 29278 | } |
| 28974 | 29279 | |
| 28975 | 29280 | /* |
| 28976 | 29281 | ** This routine reactivates the memory allocator and clears the |
| 28977 | 29282 | ** db->mallocFailed flag as necessary. |
| | @@ -32356,17 +32661,23 @@ |
| 32356 | 32661 | */ |
| 32357 | 32662 | SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
| 32358 | 32663 | char *zMsg; |
| 32359 | 32664 | va_list ap; |
| 32360 | 32665 | sqlite3 *db = pParse->db; |
| 32666 | + assert( db!=0 ); |
| 32667 | + assert( db->pParse==pParse ); |
| 32361 | 32668 | db->errByteOffset = -2; |
| 32362 | 32669 | va_start(ap, zFormat); |
| 32363 | 32670 | zMsg = sqlite3VMPrintf(db, zFormat, ap); |
| 32364 | 32671 | va_end(ap); |
| 32365 | 32672 | if( db->errByteOffset<-1 ) db->errByteOffset = -1; |
| 32366 | 32673 | if( db->suppressErr ){ |
| 32367 | 32674 | sqlite3DbFree(db, zMsg); |
| 32675 | + if( db->mallocFailed ){ |
| 32676 | + pParse->nErr++; |
| 32677 | + pParse->rc = SQLITE_NOMEM; |
| 32678 | + } |
| 32368 | 32679 | }else{ |
| 32369 | 32680 | pParse->nErr++; |
| 32370 | 32681 | sqlite3DbFree(db, pParse->zErrMsg); |
| 32371 | 32682 | pParse->zErrMsg = zMsg; |
| 32372 | 32683 | pParse->rc = SQLITE_ERROR; |
| | @@ -34334,21 +34645,22 @@ |
| 34334 | 34645 | /* 168 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), |
| 34335 | 34646 | /* 169 */ "VBegin" OpHelp(""), |
| 34336 | 34647 | /* 170 */ "VCreate" OpHelp(""), |
| 34337 | 34648 | /* 171 */ "VDestroy" OpHelp(""), |
| 34338 | 34649 | /* 172 */ "VOpen" OpHelp(""), |
| 34339 | | - /* 173 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 34340 | | - /* 174 */ "VRename" OpHelp(""), |
| 34341 | | - /* 175 */ "Pagecount" OpHelp(""), |
| 34342 | | - /* 176 */ "MaxPgcnt" OpHelp(""), |
| 34343 | | - /* 177 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"), |
| 34344 | | - /* 178 */ "Trace" OpHelp(""), |
| 34345 | | - /* 179 */ "CursorHint" OpHelp(""), |
| 34346 | | - /* 180 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"), |
| 34347 | | - /* 181 */ "Noop" OpHelp(""), |
| 34348 | | - /* 182 */ "Explain" OpHelp(""), |
| 34349 | | - /* 183 */ "Abortable" OpHelp(""), |
| 34650 | + /* 173 */ "VInitIn" OpHelp("r[P2]=ValueList(P1,P3)"), |
| 34651 | + /* 174 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 34652 | + /* 175 */ "VRename" OpHelp(""), |
| 34653 | + /* 176 */ "Pagecount" OpHelp(""), |
| 34654 | + /* 177 */ "MaxPgcnt" OpHelp(""), |
| 34655 | + /* 178 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"), |
| 34656 | + /* 179 */ "Trace" OpHelp(""), |
| 34657 | + /* 180 */ "CursorHint" OpHelp(""), |
| 34658 | + /* 181 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"), |
| 34659 | + /* 182 */ "Noop" OpHelp(""), |
| 34660 | + /* 183 */ "Explain" OpHelp(""), |
| 34661 | + /* 184 */ "Abortable" OpHelp(""), |
| 34350 | 34662 | }; |
| 34351 | 34663 | return azName[i]; |
| 34352 | 34664 | } |
| 34353 | 34665 | #endif |
| 34354 | 34666 | |
| | @@ -56722,12 +57034,11 @@ |
| 56722 | 57034 | ** Once this function has been called, the transaction must either be |
| 56723 | 57035 | ** rolled back or committed. It is not safe to call this function and |
| 56724 | 57036 | ** then continue writing to the database. |
| 56725 | 57037 | */ |
| 56726 | 57038 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ |
| 56727 | | - assert( pPager->dbSize>=nPage || CORRUPT_DB ); |
| 56728 | | - testcase( pPager->dbSize<nPage ); |
| 57039 | + assert( pPager->dbSize>=nPage ); |
| 56729 | 57040 | assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); |
| 56730 | 57041 | pPager->dbSize = nPage; |
| 56731 | 57042 | |
| 56732 | 57043 | /* At one point the code here called assertTruncateConstraint() to |
| 56733 | 57044 | ** ensure that all pages being truncated away by this operation are, |
| | @@ -63102,11 +63413,13 @@ |
| 63102 | 63413 | rc = WAL_RETRY; |
| 63103 | 63414 | goto begin_unreliable_shm_out; |
| 63104 | 63415 | } |
| 63105 | 63416 | |
| 63106 | 63417 | /* Allocate a buffer to read frames into */ |
| 63107 | | - szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE; |
| 63418 | + assert( (pWal->szPage & (pWal->szPage-1))==0 ); |
| 63419 | + assert( pWal->szPage>=512 && pWal->szPage<=65536 ); |
| 63420 | + szFrame = pWal->szPage + WAL_FRAME_HDRSIZE; |
| 63108 | 63421 | aFrame = (u8 *)sqlite3_malloc64(szFrame); |
| 63109 | 63422 | if( aFrame==0 ){ |
| 63110 | 63423 | rc = SQLITE_NOMEM_BKPT; |
| 63111 | 63424 | goto begin_unreliable_shm_out; |
| 63112 | 63425 | } |
| | @@ -63116,11 +63429,11 @@ |
| 63116 | 63429 | ** wal file since the heap-memory wal-index was created. If so, the |
| 63117 | 63430 | ** heap-memory wal-index is discarded and WAL_RETRY returned to |
| 63118 | 63431 | ** the caller. */ |
| 63119 | 63432 | aSaveCksum[0] = pWal->hdr.aFrameCksum[0]; |
| 63120 | 63433 | aSaveCksum[1] = pWal->hdr.aFrameCksum[1]; |
| 63121 | | - for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage); |
| 63434 | + for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage); |
| 63122 | 63435 | iOffset+szFrame<=szWal; |
| 63123 | 63436 | iOffset+=szFrame |
| 63124 | 63437 | ){ |
| 63125 | 63438 | u32 pgno; /* Database page number for frame */ |
| 63126 | 63439 | u32 nTruncate; /* dbsize field from frame header */ |
| | @@ -68934,13 +69247,17 @@ |
| 68934 | 69247 | freeTempSpace(pBt); |
| 68935 | 69248 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, |
| 68936 | 69249 | pageSize-usableSize); |
| 68937 | 69250 | return rc; |
| 68938 | 69251 | } |
| 68939 | | - if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){ |
| 68940 | | - rc = SQLITE_CORRUPT_BKPT; |
| 68941 | | - goto page1_init_failed; |
| 69252 | + if( nPage>nPageFile ){ |
| 69253 | + if( sqlite3WritableSchema(pBt->db)==0 ){ |
| 69254 | + rc = SQLITE_CORRUPT_BKPT; |
| 69255 | + goto page1_init_failed; |
| 69256 | + }else{ |
| 69257 | + nPage = nPageFile; |
| 69258 | + } |
| 68942 | 69259 | } |
| 68943 | 69260 | /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to |
| 68944 | 69261 | ** be less than 480. In other words, if the page size is 512, then the |
| 68945 | 69262 | ** reserved space size cannot exceed 32. */ |
| 68946 | 69263 | if( usableSize<480 ){ |
| | @@ -76691,18 +77008,17 @@ |
| 76691 | 77008 | int i = sqlite3FindDbName(pDb, zDb); |
| 76692 | 77009 | |
| 76693 | 77010 | if( i==1 ){ |
| 76694 | 77011 | Parse sParse; |
| 76695 | 77012 | int rc = 0; |
| 76696 | | - memset(&sParse, 0, sizeof(sParse)); |
| 76697 | | - sParse.db = pDb; |
| 77013 | + sqlite3ParseObjectInit(&sParse,pDb); |
| 76698 | 77014 | if( sqlite3OpenTempDatabase(&sParse) ){ |
| 76699 | 77015 | sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg); |
| 76700 | 77016 | rc = SQLITE_ERROR; |
| 76701 | 77017 | } |
| 76702 | 77018 | sqlite3DbFree(pErrorDb, sParse.zErrMsg); |
| 76703 | | - sqlite3ParserReset(&sParse); |
| 77019 | + sqlite3ParseObjectReset(&sParse); |
| 76704 | 77020 | if( rc ){ |
| 76705 | 77021 | return 0; |
| 76706 | 77022 | } |
| 76707 | 77023 | } |
| 76708 | 77024 | |
| | @@ -78910,15 +79226,11 @@ |
| 78910 | 79226 | const char *zNeg = ""; |
| 78911 | 79227 | int rc = SQLITE_OK; |
| 78912 | 79228 | |
| 78913 | 79229 | assert( pExpr!=0 ); |
| 78914 | 79230 | while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft; |
| 78915 | | -#if defined(SQLITE_ENABLE_STAT4) |
| 78916 | 79231 | if( op==TK_REGISTER ) op = pExpr->op2; |
| 78917 | | -#else |
| 78918 | | - if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; |
| 78919 | | -#endif |
| 78920 | 79232 | |
| 78921 | 79233 | /* Compressed expressions only appear when parsing the DEFAULT clause |
| 78922 | 79234 | ** on a table column definition, and hence only when pCtx==0. This |
| 78923 | 79235 | ** check ensures that an EP_TokenOnly expression is never passed down |
| 78924 | 79236 | ** into valueFromFunction(). */ |
| | @@ -80707,12 +81019,11 @@ |
| 80707 | 81019 | ** makes the code easier to read during debugging. None of this happens |
| 80708 | 81020 | ** in a production build. |
| 80709 | 81021 | */ |
| 80710 | 81022 | static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ |
| 80711 | 81023 | assert( p->nOp>0 || p->aOp==0 ); |
| 80712 | | - assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed |
| 80713 | | - || p->pParse->nErr>0 ); |
| 81024 | + assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 ); |
| 80714 | 81025 | if( p->nOp ){ |
| 80715 | 81026 | assert( p->aOp ); |
| 80716 | 81027 | sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment); |
| 80717 | 81028 | p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap); |
| 80718 | 81029 | } |
| | @@ -85436,10 +85747,74 @@ |
| 85436 | 85747 | */ |
| 85437 | 85748 | SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){ |
| 85438 | 85749 | assert( p ); |
| 85439 | 85750 | return sqlite3_value_nochange(p->pOut); |
| 85440 | 85751 | } |
| 85752 | + |
| 85753 | +/* |
| 85754 | +** Implementation of sqlite3_vtab_in_first() (if bNext==0) and |
| 85755 | +** sqlite3_vtab_in_next() (if bNext!=0). |
| 85756 | +*/ |
| 85757 | +static int valueFromValueList( |
| 85758 | + sqlite3_value *pVal, /* Pointer to the ValueList object */ |
| 85759 | + sqlite3_value **ppOut, /* Store the next value from the list here */ |
| 85760 | + int bNext /* 1 for _next(). 0 for _first() */ |
| 85761 | +){ |
| 85762 | + int rc; |
| 85763 | + ValueList *pRhs; |
| 85764 | + |
| 85765 | + *ppOut = 0; |
| 85766 | + if( pVal==0 ) return SQLITE_MISUSE; |
| 85767 | + pRhs = (ValueList*)sqlite3_value_pointer(pVal, "ValueList"); |
| 85768 | + if( pRhs==0 ) return SQLITE_MISUSE; |
| 85769 | + if( bNext ){ |
| 85770 | + rc = sqlite3BtreeNext(pRhs->pCsr, 0); |
| 85771 | + }else{ |
| 85772 | + int dummy = 0; |
| 85773 | + rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy); |
| 85774 | + assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) ); |
| 85775 | + if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE; |
| 85776 | + } |
| 85777 | + if( rc==SQLITE_OK ){ |
| 85778 | + u32 sz; /* Size of current row in bytes */ |
| 85779 | + Mem sMem; /* Raw content of current row */ |
| 85780 | + memset(&sMem, 0, sizeof(sMem)); |
| 85781 | + sz = sqlite3BtreePayloadSize(pRhs->pCsr); |
| 85782 | + rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem); |
| 85783 | + if( rc==SQLITE_OK ){ |
| 85784 | + u8 *zBuf = (u8*)sMem.z; |
| 85785 | + u32 iSerial; |
| 85786 | + sqlite3_value *pOut = pRhs->pOut; |
| 85787 | + int iOff = 1 + getVarint32(&zBuf[1], iSerial); |
| 85788 | + sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut); |
| 85789 | + pOut->enc = ENC(pOut->db); |
| 85790 | + if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){ |
| 85791 | + rc = SQLITE_NOMEM; |
| 85792 | + }else{ |
| 85793 | + *ppOut = pOut; |
| 85794 | + } |
| 85795 | + } |
| 85796 | + sqlite3VdbeMemRelease(&sMem); |
| 85797 | + } |
| 85798 | + return rc; |
| 85799 | +} |
| 85800 | + |
| 85801 | +/* |
| 85802 | +** Set the iterator value pVal to point to the first value in the set. |
| 85803 | +** Set (*ppOut) to point to this value before returning. |
| 85804 | +*/ |
| 85805 | +SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){ |
| 85806 | + return valueFromValueList(pVal, ppOut, 0); |
| 85807 | +} |
| 85808 | + |
| 85809 | +/* |
| 85810 | +** Set the iterator value pVal to point to the next value in the set. |
| 85811 | +** Set (*ppOut) to point to this value before returning. |
| 85812 | +*/ |
| 85813 | +SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){ |
| 85814 | + return valueFromValueList(pVal, ppOut, 1); |
| 85815 | +} |
| 85441 | 85816 | |
| 85442 | 85817 | /* |
| 85443 | 85818 | ** Return the current time for a statement. If the current time |
| 85444 | 85819 | ** is requested more than once within the same run of a single prepared |
| 85445 | 85820 | ** statement, the exact same time is returned for each invocation regardless |
| | @@ -87537,11 +87912,10 @@ |
| 87537 | 87912 | */ |
| 87538 | 87913 | static u64 filterHash(const Mem *aMem, const Op *pOp){ |
| 87539 | 87914 | int i, mx; |
| 87540 | 87915 | u64 h = 0; |
| 87541 | 87916 | |
| 87542 | | - i = pOp->p3; |
| 87543 | 87917 | assert( pOp->p4type==P4_INT32 ); |
| 87544 | 87918 | for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){ |
| 87545 | 87919 | const Mem *p = &aMem[i]; |
| 87546 | 87920 | if( p->flags & (MEM_Int|MEM_IntReal) ){ |
| 87547 | 87921 | h += p->u.i; |
| | @@ -89020,11 +89394,11 @@ |
| 89020 | 89394 | testcase( pIn1->flags & MEM_Real ); |
| 89021 | 89395 | testcase( pIn1->flags & MEM_IntReal ); |
| 89022 | 89396 | sqlite3VdbeMemStringify(pIn1, encoding, 1); |
| 89023 | 89397 | testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); |
| 89024 | 89398 | flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); |
| 89025 | | - if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str; |
| 89399 | + if( pIn1==pIn3 ) flags3 = flags1 | MEM_Str; |
| 89026 | 89400 | } |
| 89027 | 89401 | if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ |
| 89028 | 89402 | testcase( pIn3->flags & MEM_Int ); |
| 89029 | 89403 | testcase( pIn3->flags & MEM_Real ); |
| 89030 | 89404 | testcase( pIn3->flags & MEM_IntReal ); |
| | @@ -89846,10 +90220,12 @@ |
| 89846 | 90220 | case COLTYPE_TEXT: { |
| 89847 | 90221 | if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error; |
| 89848 | 90222 | break; |
| 89849 | 90223 | } |
| 89850 | 90224 | case COLTYPE_REAL: { |
| 90225 | + testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real ); |
| 90226 | + testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_IntReal ); |
| 89851 | 90227 | if( pIn1->flags & MEM_Int ){ |
| 89852 | 90228 | /* When applying REAL affinity, if the result is still an MEM_Int |
| 89853 | 90229 | ** that will fit in 6 bytes, then change the type to MEM_IntReal |
| 89854 | 90230 | ** so that we keep the high-resolution integer value but know that |
| 89855 | 90231 | ** the type really wants to be REAL. */ |
| | @@ -89863,11 +90239,11 @@ |
| 89863 | 90239 | }else{ |
| 89864 | 90240 | pIn1->u.r = (double)pIn1->u.i; |
| 89865 | 90241 | pIn1->flags |= MEM_Real; |
| 89866 | 90242 | pIn1->flags &= ~MEM_Int; |
| 89867 | 90243 | } |
| 89868 | | - }else if( (pIn1->flags & MEM_Real)==0 ){ |
| 90244 | + }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){ |
| 89869 | 90245 | goto vdbe_type_error; |
| 89870 | 90246 | } |
| 89871 | 90247 | break; |
| 89872 | 90248 | } |
| 89873 | 90249 | default: { |
| | @@ -94592,10 +94968,38 @@ |
| 94592 | 94968 | goto no_mem; |
| 94593 | 94969 | } |
| 94594 | 94970 | break; |
| 94595 | 94971 | } |
| 94596 | 94972 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 94973 | + |
| 94974 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 94975 | +/* Opcode: VInitIn P1 P2 P3 * * |
| 94976 | +** Synopsis: r[P2]=ValueList(P1,P3) |
| 94977 | +** |
| 94978 | +** Set register P2 to be a pointer to a ValueList object for cursor P1 |
| 94979 | +** with cache register P3 and output register P3+1. This ValueList object |
| 94980 | +** can be used as the first argument to sqlite3_vtab_in_first() and |
| 94981 | +** sqlite3_vtab_in_next() to extract all of the values stored in the P1 |
| 94982 | +** cursor. Register P3 is used to hold the values returned by |
| 94983 | +** sqlite3_vtab_in_first() and sqlite3_vtab_in_next(). |
| 94984 | +*/ |
| 94985 | +case OP_VInitIn: { /* out2 */ |
| 94986 | + VdbeCursor *pC; /* The cursor containing the RHS values */ |
| 94987 | + ValueList *pRhs; /* New ValueList object to put in reg[P2] */ |
| 94988 | + |
| 94989 | + pC = p->apCsr[pOp->p1]; |
| 94990 | + pRhs = sqlite3_malloc64( sizeof(*pRhs) ); |
| 94991 | + if( pRhs==0 ) goto no_mem; |
| 94992 | + pRhs->pCsr = pC->uc.pCursor; |
| 94993 | + pRhs->pOut = &aMem[pOp->p3]; |
| 94994 | + pOut = out2Prerelease(p, pOp); |
| 94995 | + pOut->flags = MEM_Null; |
| 94996 | + sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3_free); |
| 94997 | + break; |
| 94998 | +} |
| 94999 | +#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 95000 | + |
| 94597 | 95001 | |
| 94598 | 95002 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 94599 | 95003 | /* Opcode: VFilter P1 P2 P3 P4 * |
| 94600 | 95004 | ** Synopsis: iplan=r[P3] zplan='P4' |
| 94601 | 95005 | ** |
| | @@ -95579,14 +95983,13 @@ |
| 95579 | 95983 | wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */ |
| 95580 | 95984 | |
| 95581 | 95985 | sqlite3_mutex_enter(db->mutex); |
| 95582 | 95986 | |
| 95583 | 95987 | pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); |
| 95584 | | - do { |
| 95585 | | - memset(&sParse, 0, sizeof(Parse)); |
| 95988 | + while(1){ |
| 95989 | + sqlite3ParseObjectInit(&sParse,db); |
| 95586 | 95990 | if( !pBlob ) goto blob_open_out; |
| 95587 | | - sParse.db = db; |
| 95588 | 95991 | sqlite3DbFree(db, zErr); |
| 95589 | 95992 | zErr = 0; |
| 95590 | 95993 | |
| 95591 | 95994 | sqlite3BtreeEnterAll(db); |
| 95592 | 95995 | pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb); |
| | @@ -95759,11 +96162,13 @@ |
| 95759 | 96162 | sqlite3BtreeLeaveAll(db); |
| 95760 | 96163 | if( db->mallocFailed ){ |
| 95761 | 96164 | goto blob_open_out; |
| 95762 | 96165 | } |
| 95763 | 96166 | rc = blobSeekToRow(pBlob, iRow, &zErr); |
| 95764 | | - } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA ); |
| 96167 | + if( (++nAttempt)>=SQLITE_MAX_SCHEMA_RETRY || rc!=SQLITE_SCHEMA ) break; |
| 96168 | + sqlite3ParseObjectReset(&sParse); |
| 96169 | + } |
| 95765 | 96170 | |
| 95766 | 96171 | blob_open_out: |
| 95767 | 96172 | if( rc==SQLITE_OK && db->mallocFailed==0 ){ |
| 95768 | 96173 | *ppBlob = (sqlite3_blob *)pBlob; |
| 95769 | 96174 | }else{ |
| | @@ -95770,11 +96175,11 @@ |
| 95770 | 96175 | if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt); |
| 95771 | 96176 | sqlite3DbFree(db, pBlob); |
| 95772 | 96177 | } |
| 95773 | 96178 | sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr); |
| 95774 | 96179 | sqlite3DbFree(db, zErr); |
| 95775 | | - sqlite3ParserReset(&sParse); |
| 96180 | + sqlite3ParseObjectReset(&sParse); |
| 95776 | 96181 | rc = sqlite3ApiExit(db, rc); |
| 95777 | 96182 | sqlite3_mutex_leave(db->mutex); |
| 95778 | 96183 | return rc; |
| 95779 | 96184 | } |
| 95780 | 96185 | |
| | @@ -100835,11 +101240,11 @@ |
| 100835 | 101240 | && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0 |
| 100836 | 101241 | ){ |
| 100837 | 101242 | /* Internal-use-only functions are disallowed unless the |
| 100838 | 101243 | ** SQL is being compiled using sqlite3NestedParse() or |
| 100839 | 101244 | ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be |
| 100840 | | - ** used to activate internal functionsn for testing purposes */ |
| 101245 | + ** used to activate internal functions for testing purposes */ |
| 100841 | 101246 | no_such_func = 1; |
| 100842 | 101247 | pDef = 0; |
| 100843 | 101248 | }else |
| 100844 | 101249 | if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 |
| 100845 | 101250 | && !IN_RENAME_OBJECT |
| | @@ -101054,11 +101459,12 @@ |
| 101054 | 101459 | sqlite3ErrorMsg(pParse, "row value misused"); |
| 101055 | 101460 | } |
| 101056 | 101461 | break; |
| 101057 | 101462 | } |
| 101058 | 101463 | } |
| 101059 | | - return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue; |
| 101464 | + assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 ); |
| 101465 | + return pParse->nErr ? WRC_Abort : WRC_Continue; |
| 101060 | 101466 | } |
| 101061 | 101467 | |
| 101062 | 101468 | /* |
| 101063 | 101469 | ** pEList is a list of expressions which are really the result set of the |
| 101064 | 101470 | ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause. |
| | @@ -101468,11 +101874,11 @@ |
| 101468 | 101874 | ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and |
| 101469 | 101875 | ** this routine in the correct order. |
| 101470 | 101876 | */ |
| 101471 | 101877 | if( (p->selFlags & SF_Expanded)==0 ){ |
| 101472 | 101878 | sqlite3SelectPrep(pParse, p, pOuterNC); |
| 101473 | | - return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune; |
| 101879 | + return pParse->nErr ? WRC_Abort : WRC_Prune; |
| 101474 | 101880 | } |
| 101475 | 101881 | |
| 101476 | 101882 | isCompound = p->pPrior!=0; |
| 101477 | 101883 | nCompound = 0; |
| 101478 | 101884 | pLeftmost = p; |
| | @@ -101516,11 +101922,12 @@ |
| 101516 | 101922 | const char *zSavedContext = pParse->zAuthContext; |
| 101517 | 101923 | |
| 101518 | 101924 | if( pItem->zName ) pParse->zAuthContext = pItem->zName; |
| 101519 | 101925 | sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC); |
| 101520 | 101926 | pParse->zAuthContext = zSavedContext; |
| 101521 | | - if( pParse->nErr || db->mallocFailed ) return WRC_Abort; |
| 101927 | + if( pParse->nErr ) return WRC_Abort; |
| 101928 | + assert( db->mallocFailed==0 ); |
| 101522 | 101929 | |
| 101523 | 101930 | /* If the number of references to the outer context changed when |
| 101524 | 101931 | ** expressions in the sub-select were resolved, the sub-select |
| 101525 | 101932 | ** is correlated. It is not required to check the refcount on any |
| 101526 | 101933 | ** but the innermost outer context object, as lookupName() increments |
| | @@ -104696,12 +105103,11 @@ |
| 104696 | 105103 | Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); |
| 104697 | 105104 | Expr *pRhs = pEList->a[i].pExpr; |
| 104698 | 105105 | CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); |
| 104699 | 105106 | int j; |
| 104700 | 105107 | |
| 104701 | | - assert( pReq!=0 || pRhs->iColumn==XN_ROWID |
| 104702 | | - || pParse->nErr || db->mallocFailed ); |
| 105108 | + assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr ); |
| 104703 | 105109 | for(j=0; j<nExpr; j++){ |
| 104704 | 105110 | if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue; |
| 104705 | 105111 | assert( pIdx->azColl[j] ); |
| 104706 | 105112 | if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){ |
| 104707 | 105113 | continue; |
| | @@ -105173,14 +105579,12 @@ |
| 105173 | 105579 | pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1"); |
| 105174 | 105580 | pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0); |
| 105175 | 105581 | } |
| 105176 | 105582 | pSel->iLimit = 0; |
| 105177 | 105583 | if( sqlite3Select(pParse, pSel, &dest) ){ |
| 105178 | | - if( pParse->nErr ){ |
| 105179 | | - pExpr->op2 = pExpr->op; |
| 105180 | | - pExpr->op = TK_ERROR; |
| 105181 | | - } |
| 105584 | + pExpr->op2 = pExpr->op; |
| 105585 | + pExpr->op = TK_ERROR; |
| 105182 | 105586 | return 0; |
| 105183 | 105587 | } |
| 105184 | 105588 | pExpr->iTable = rReg = dest.iSDParm; |
| 105185 | 105589 | ExprSetVVAProperty(pExpr, EP_NoReduce); |
| 105186 | 105590 | if( addrOnce ){ |
| | @@ -105393,14 +105797,13 @@ |
| 105393 | 105797 | if( destIfNull==destIfFalse ){ |
| 105394 | 105798 | destStep2 = destIfFalse; |
| 105395 | 105799 | }else{ |
| 105396 | 105800 | destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse); |
| 105397 | 105801 | } |
| 105398 | | - if( pParse->nErr ) goto sqlite3ExprCodeIN_finished; |
| 105399 | 105802 | for(i=0; i<nVector; i++){ |
| 105400 | 105803 | Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i); |
| 105401 | | - if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error; |
| 105804 | + if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error; |
| 105402 | 105805 | if( sqlite3ExprCanBeNull(p) ){ |
| 105403 | 105806 | sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2); |
| 105404 | 105807 | VdbeCoverage(v); |
| 105405 | 105808 | } |
| 105406 | 105809 | } |
| | @@ -108571,11 +108974,13 @@ |
| 108571 | 108974 | sqlite3 *db; /* The database connection; */ |
| 108572 | 108975 | Vdbe *v; /* The prepared statement under construction */ |
| 108573 | 108976 | int r1; /* Temporary registers */ |
| 108574 | 108977 | |
| 108575 | 108978 | db = pParse->db; |
| 108576 | | - if( pParse->nErr || db->mallocFailed ) return; |
| 108979 | + assert( db->pParse==pParse ); |
| 108980 | + if( pParse->nErr ) return; |
| 108981 | + assert( db->mallocFailed==0 ); |
| 108577 | 108982 | pNew = pParse->pNewTable; |
| 108578 | 108983 | assert( pNew ); |
| 108579 | 108984 | |
| 108580 | 108985 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 108581 | 108986 | iDb = sqlite3SchemaToIndex(db, pNew->pSchema); |
| | @@ -108697,11 +109102,11 @@ |
| 108697 | 109102 | sqlite3NestedParse(pParse, |
| 108698 | 109103 | "SELECT CASE WHEN quick_check GLOB 'CHECK*'" |
| 108699 | 109104 | " THEN raise(ABORT,'CHECK constraint failed')" |
| 108700 | 109105 | " ELSE raise(ABORT,'NOT NULL constraint failed')" |
| 108701 | 109106 | " END" |
| 108702 | | - " FROM pragma_quick_check(\"%w\",\"%w\")" |
| 109107 | + " FROM pragma_quick_check(%Q,%Q)" |
| 108703 | 109108 | " WHERE quick_check GLOB 'CHECK*' OR quick_check GLOB 'NULL*'", |
| 108704 | 109109 | zTab, zDb |
| 108705 | 109110 | ); |
| 108706 | 109111 | } |
| 108707 | 109112 | } |
| | @@ -108982,11 +109387,13 @@ |
| 108982 | 109387 | ** |
| 108983 | 109388 | ** Technically, as x no longer points into a valid object or to the byte |
| 108984 | 109389 | ** following a valid object, it may not be used in comparison operations. |
| 108985 | 109390 | */ |
| 108986 | 109391 | static void renameTokenCheckAll(Parse *pParse, const void *pPtr){ |
| 108987 | | - if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){ |
| 109392 | + assert( pParse==pParse->db->pParse ); |
| 109393 | + assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 ); |
| 109394 | + if( pParse->nErr==0 ){ |
| 108988 | 109395 | const RenameToken *p; |
| 108989 | 109396 | u8 i = 0; |
| 108990 | 109397 | for(p=pParse->pRename; p; p=p->pNext){ |
| 108991 | 109398 | if( p->p ){ |
| 108992 | 109399 | assert( p->p!=pPtr ); |
| | @@ -109379,11 +109786,11 @@ |
| 109379 | 109786 | db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb); |
| 109380 | 109787 | |
| 109381 | 109788 | /* Parse the SQL statement passed as the first argument. If no error |
| 109382 | 109789 | ** occurs and the parse does not result in a new table, index or |
| 109383 | 109790 | ** trigger object, the database must be corrupt. */ |
| 109384 | | - memset(p, 0, sizeof(Parse)); |
| 109791 | + sqlite3ParseObjectInit(p, db); |
| 109385 | 109792 | p->eParseMode = PARSE_MODE_RENAME; |
| 109386 | 109793 | p->db = db; |
| 109387 | 109794 | p->nQueryLoop = 1; |
| 109388 | 109795 | rc = zSql ? sqlite3RunParser(p, zSql) : SQLITE_NOMEM; |
| 109389 | 109796 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
| | @@ -109664,11 +110071,11 @@ |
| 109664 | 110071 | sqlite3FreeIndex(db, pIdx); |
| 109665 | 110072 | } |
| 109666 | 110073 | sqlite3DeleteTrigger(db, pParse->pNewTrigger); |
| 109667 | 110074 | sqlite3DbFree(db, pParse->zErrMsg); |
| 109668 | 110075 | renameTokenFree(db, pParse->pRename); |
| 109669 | | - sqlite3ParserReset(pParse); |
| 110076 | + sqlite3ParseObjectReset(pParse); |
| 109670 | 110077 | } |
| 109671 | 110078 | |
| 109672 | 110079 | /* |
| 109673 | 110080 | ** SQL function: |
| 109674 | 110081 | ** |
| | @@ -110378,10 +110785,16 @@ |
| 110378 | 110785 | |
| 110379 | 110786 | /* Edit the sqlite_schema table */ |
| 110380 | 110787 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 110381 | 110788 | assert( iDb>=0 ); |
| 110382 | 110789 | zDb = db->aDb[iDb].zDbSName; |
| 110790 | +#ifndef SQLITE_OMIT_AUTHORIZATION |
| 110791 | + /* Invoke the authorization callback. */ |
| 110792 | + if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){ |
| 110793 | + goto exit_drop_column; |
| 110794 | + } |
| 110795 | +#endif |
| 110383 | 110796 | renameTestSchema(pParse, zDb, iDb==1, "", 0); |
| 110384 | 110797 | renameFixQuotes(pParse, zDb, iDb==1); |
| 110385 | 110798 | sqlite3NestedParse(pParse, |
| 110386 | 110799 | "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " |
| 110387 | 110800 | "sql = sqlite_drop_column(%d, sql, %d) " |
| | @@ -112765,11 +113178,11 @@ |
| 112765 | 113178 | ){ |
| 112766 | 113179 | goto attach_end; |
| 112767 | 113180 | } |
| 112768 | 113181 | |
| 112769 | 113182 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 112770 | | - if( pAuthArg ){ |
| 113183 | + if( ALWAYS(pAuthArg) ){ |
| 112771 | 113184 | char *zAuthArg; |
| 112772 | 113185 | if( pAuthArg->op==TK_STRING ){ |
| 112773 | 113186 | assert( !ExprHasProperty(pAuthArg, EP_IntValue) ); |
| 112774 | 113187 | zAuthArg = pAuthArg->u.zToken; |
| 112775 | 113188 | }else{ |
| | @@ -113426,15 +113839,17 @@ |
| 113426 | 113839 | sqlite3 *db; |
| 113427 | 113840 | Vdbe *v; |
| 113428 | 113841 | |
| 113429 | 113842 | assert( pParse->pToplevel==0 ); |
| 113430 | 113843 | db = pParse->db; |
| 113844 | + assert( db->pParse==pParse ); |
| 113431 | 113845 | if( pParse->nested ) return; |
| 113432 | | - if( db->mallocFailed || pParse->nErr ){ |
| 113433 | | - if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR; |
| 113846 | + if( pParse->nErr ){ |
| 113847 | + if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM; |
| 113434 | 113848 | return; |
| 113435 | 113849 | } |
| 113850 | + assert( db->mallocFailed==0 ); |
| 113436 | 113851 | |
| 113437 | 113852 | /* Begin by generating some termination code at the end of the |
| 113438 | 113853 | ** vdbe program |
| 113439 | 113854 | */ |
| 113440 | 113855 | v = pParse->pVdbe; |
| | @@ -113563,11 +113978,13 @@ |
| 113563 | 113978 | } |
| 113564 | 113979 | } |
| 113565 | 113980 | |
| 113566 | 113981 | /* Get the VDBE program ready for execution |
| 113567 | 113982 | */ |
| 113568 | | - if( v && pParse->nErr==0 && !db->mallocFailed ){ |
| 113983 | + assert( v!=0 || pParse->nErr ); |
| 113984 | + assert( db->mallocFailed==0 || pParse->nErr ); |
| 113985 | + if( pParse->nErr==0 ){ |
| 113569 | 113986 | /* A minimum of one cursor is required if autoincrement is used |
| 113570 | 113987 | * See ticket [a696379c1f08866] */ |
| 113571 | 113988 | assert( pParse->pAinc==0 || pParse->nTab>0 ); |
| 113572 | 113989 | sqlite3VdbeMakeReady(v, pParse); |
| 113573 | 113990 | pParse->rc = SQLITE_DONE; |
| | @@ -115667,14 +116084,15 @@ |
| 115667 | 116084 | pList->a[0].sortFlags = pParse->iPkSortOrder; |
| 115668 | 116085 | assert( pParse->pNewTable==pTab ); |
| 115669 | 116086 | pTab->iPKey = -1; |
| 115670 | 116087 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, |
| 115671 | 116088 | SQLITE_IDXTYPE_PRIMARYKEY); |
| 115672 | | - if( db->mallocFailed || pParse->nErr ){ |
| 116089 | + if( pParse->nErr ){ |
| 115673 | 116090 | pTab->tabFlags &= ~TF_WithoutRowid; |
| 115674 | 116091 | return; |
| 115675 | 116092 | } |
| 116093 | + assert( db->mallocFailed==0 ); |
| 115676 | 116094 | pPk = sqlite3PrimaryKeyIndex(pTab); |
| 115677 | 116095 | assert( pPk->nKeyCol==1 ); |
| 115678 | 116096 | }else{ |
| 115679 | 116097 | pPk = sqlite3PrimaryKeyIndex(pTab); |
| 115680 | 116098 | assert( pPk!=0 ); |
| | @@ -116411,14 +116829,14 @@ |
| 116411 | 116829 | ** normally holds CHECK constraints on an ordinary table, but for |
| 116412 | 116830 | ** a VIEW it holds the list of column names. |
| 116413 | 116831 | */ |
| 116414 | 116832 | sqlite3ColumnsFromExprList(pParse, pTable->pCheck, |
| 116415 | 116833 | &pTable->nCol, &pTable->aCol); |
| 116416 | | - if( db->mallocFailed==0 |
| 116417 | | - && pParse->nErr==0 |
| 116834 | + if( pParse->nErr==0 |
| 116418 | 116835 | && pTable->nCol==pSel->pEList->nExpr |
| 116419 | 116836 | ){ |
| 116837 | + assert( db->mallocFailed==0 ); |
| 116420 | 116838 | sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel, |
| 116421 | 116839 | SQLITE_AFF_NONE); |
| 116422 | 116840 | } |
| 116423 | 116841 | }else{ |
| 116424 | 116842 | /* CREATE VIEW name AS... without an argument list. Construct |
| | @@ -117033,11 +117451,11 @@ |
| 117033 | 117451 | tnum = (Pgno)memRootPage; |
| 117034 | 117452 | }else{ |
| 117035 | 117453 | tnum = pIndex->tnum; |
| 117036 | 117454 | } |
| 117037 | 117455 | pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); |
| 117038 | | - assert( pKey!=0 || db->mallocFailed || pParse->nErr ); |
| 117456 | + assert( pKey!=0 || pParse->nErr ); |
| 117039 | 117457 | |
| 117040 | 117458 | /* Open the sorter cursor if we are to use one. */ |
| 117041 | 117459 | iSorter = pParse->nTab++; |
| 117042 | 117460 | sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) |
| 117043 | 117461 | sqlite3KeyInfoRef(pKey), P4_KEYINFO); |
| | @@ -117197,13 +117615,15 @@ |
| 117197 | 117615 | int nExtra = 0; /* Space allocated for zExtra[] */ |
| 117198 | 117616 | int nExtraCol; /* Number of extra columns needed */ |
| 117199 | 117617 | char *zExtra = 0; /* Extra space after the Index object */ |
| 117200 | 117618 | Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ |
| 117201 | 117619 | |
| 117202 | | - if( db->mallocFailed || pParse->nErr>0 ){ |
| 117620 | + assert( db->pParse==pParse ); |
| 117621 | + if( pParse->nErr ){ |
| 117203 | 117622 | goto exit_create_index; |
| 117204 | 117623 | } |
| 117624 | + assert( db->mallocFailed==0 ); |
| 117205 | 117625 | if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ |
| 117206 | 117626 | goto exit_create_index; |
| 117207 | 117627 | } |
| 117208 | 117628 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 117209 | 117629 | goto exit_create_index; |
| | @@ -117263,11 +117683,10 @@ |
| 117263 | 117683 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 117264 | 117684 | } |
| 117265 | 117685 | pDb = &db->aDb[iDb]; |
| 117266 | 117686 | |
| 117267 | 117687 | assert( pTab!=0 ); |
| 117268 | | - assert( pParse->nErr==0 ); |
| 117269 | 117688 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
| 117270 | 117689 | && db->init.busy==0 |
| 117271 | 117690 | && pTblName!=0 |
| 117272 | 117691 | #if SQLITE_USER_AUTHENTICATION |
| 117273 | 117692 | && sqlite3UserAuthTable(pTab->zName)==0 |
| | @@ -117827,14 +118246,14 @@ |
| 117827 | 118246 | Index *pIndex; |
| 117828 | 118247 | Vdbe *v; |
| 117829 | 118248 | sqlite3 *db = pParse->db; |
| 117830 | 118249 | int iDb; |
| 117831 | 118250 | |
| 117832 | | - assert( pParse->nErr==0 ); /* Never called with prior errors */ |
| 117833 | 118251 | if( db->mallocFailed ){ |
| 117834 | 118252 | goto exit_drop_index; |
| 117835 | 118253 | } |
| 118254 | + assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */ |
| 117836 | 118255 | assert( pName->nSrc==1 ); |
| 117837 | 118256 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 117838 | 118257 | goto exit_drop_index; |
| 117839 | 118258 | } |
| 117840 | 118259 | pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); |
| | @@ -119746,13 +120165,15 @@ |
| 119746 | 120165 | Trigger *pTrigger; /* List of table triggers, if required */ |
| 119747 | 120166 | #endif |
| 119748 | 120167 | |
| 119749 | 120168 | memset(&sContext, 0, sizeof(sContext)); |
| 119750 | 120169 | db = pParse->db; |
| 119751 | | - if( pParse->nErr || db->mallocFailed ){ |
| 120170 | + assert( db->pParse==pParse ); |
| 120171 | + if( pParse->nErr ){ |
| 119752 | 120172 | goto delete_from_cleanup; |
| 119753 | 120173 | } |
| 120174 | + assert( db->mallocFailed==0 ); |
| 119754 | 120175 | assert( pTabList->nSrc==1 ); |
| 119755 | 120176 | |
| 119756 | 120177 | |
| 119757 | 120178 | /* Locate the table which we want to delete. This table has to be |
| 119758 | 120179 | ** put in an SrcList structure because some of the subroutines we |
| | @@ -119929,11 +120350,11 @@ |
| 119929 | 120350 | ** |
| 119930 | 120351 | ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values. |
| 119931 | 120352 | ** ONEPASS_SINGLE: One-pass approach - at most one row deleted. |
| 119932 | 120353 | ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted. |
| 119933 | 120354 | */ |
| 119934 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1); |
| 120355 | + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1); |
| 119935 | 120356 | if( pWInfo==0 ) goto delete_from_cleanup; |
| 119936 | 120357 | eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
| 119937 | 120358 | assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI ); |
| 119938 | 120359 | assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF ); |
| 119939 | 120360 | if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse); |
| | @@ -120542,10 +120963,11 @@ |
| 120542 | 120963 | static void subtypeFunc( |
| 120543 | 120964 | sqlite3_context *context, |
| 120544 | 120965 | int argc, |
| 120545 | 120966 | sqlite3_value **argv |
| 120546 | 120967 | ){ |
| 120968 | + UNUSED_PARAMETER(argc); |
| 120547 | 120969 | sqlite3_result_int(context, sqlite3_value_subtype(argv[0])); |
| 120548 | 120970 | } |
| 120549 | 120971 | |
| 120550 | 120972 | /* |
| 120551 | 120973 | ** Implementation of the length() function |
| | @@ -123472,11 +123894,11 @@ |
| 123472 | 123894 | |
| 123473 | 123895 | /* Create VDBE to loop through the entries in pSrc that match the WHERE |
| 123474 | 123896 | ** clause. For each row found, increment either the deferred or immediate |
| 123475 | 123897 | ** foreign key constraint counter. */ |
| 123476 | 123898 | if( pParse->nErr==0 ){ |
| 123477 | | - pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0); |
| 123899 | + pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0, 0); |
| 123478 | 123900 | sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr); |
| 123479 | 123901 | if( pWInfo ){ |
| 123480 | 123902 | sqlite3WhereEnd(pWInfo); |
| 123481 | 123903 | } |
| 123482 | 123904 | } |
| | @@ -125014,13 +125436,15 @@ |
| 125014 | 125436 | Trigger *pTrigger; /* List of triggers on pTab, if required */ |
| 125015 | 125437 | int tmask; /* Mask of trigger times */ |
| 125016 | 125438 | #endif |
| 125017 | 125439 | |
| 125018 | 125440 | db = pParse->db; |
| 125019 | | - if( pParse->nErr || db->mallocFailed ){ |
| 125441 | + assert( db->pParse==pParse ); |
| 125442 | + if( pParse->nErr ){ |
| 125020 | 125443 | goto insert_cleanup; |
| 125021 | 125444 | } |
| 125445 | + assert( db->mallocFailed==0 ); |
| 125022 | 125446 | dest.iSDParm = 0; /* Suppress a harmless compiler warning */ |
| 125023 | 125447 | |
| 125024 | 125448 | /* If the Select object is really just a simple VALUES() list with a |
| 125025 | 125449 | ** single row (the common case) then keep that one row of values |
| 125026 | 125450 | ** and discard the other (unused) parts of the pSelect object |
| | @@ -125192,11 +125616,13 @@ |
| 125192 | 125616 | sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
| 125193 | 125617 | dest.iSdst = bIdListInOrder ? regData : 0; |
| 125194 | 125618 | dest.nSdst = pTab->nCol; |
| 125195 | 125619 | rc = sqlite3Select(pParse, pSelect, &dest); |
| 125196 | 125620 | regFromSelect = dest.iSdst; |
| 125197 | | - if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup; |
| 125621 | + assert( db->pParse==pParse ); |
| 125622 | + if( rc || pParse->nErr ) goto insert_cleanup; |
| 125623 | + assert( db->mallocFailed==0 ); |
| 125198 | 125624 | sqlite3VdbeEndCoroutine(v, regYield); |
| 125199 | 125625 | sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ |
| 125200 | 125626 | assert( pSelect->pEList ); |
| 125201 | 125627 | nColumn = pSelect->pEList->nExpr; |
| 125202 | 125628 | |
| | @@ -127937,10 +128363,15 @@ |
| 127937 | 128363 | int (*autovacuum_pages)(sqlite3*, |
| 127938 | 128364 | unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), |
| 127939 | 128365 | void*, void(*)(void*)); |
| 127940 | 128366 | /* Version 3.38.0 and later */ |
| 127941 | 128367 | int (*error_offset)(sqlite3*); |
| 128368 | + int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**); |
| 128369 | + int (*vtab_distinct)(sqlite3_index_info*); |
| 128370 | + int (*vtab_in)(sqlite3_index_info*,int,int); |
| 128371 | + int (*vtab_in_first)(sqlite3_value*,sqlite3_value**); |
| 128372 | + int (*vtab_in_next)(sqlite3_value*,sqlite3_value**); |
| 127942 | 128373 | }; |
| 127943 | 128374 | |
| 127944 | 128375 | /* |
| 127945 | 128376 | ** This is the function signature used for all extension entry points. It |
| 127946 | 128377 | ** is also defined in the file "loadext.c". |
| | @@ -128250,10 +128681,15 @@ |
| 128250 | 128681 | #define sqlite3_total_changes64 sqlite3_api->total_changes64 |
| 128251 | 128682 | /* Version 3.37.0 and later */ |
| 128252 | 128683 | #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages |
| 128253 | 128684 | /* Version 3.38.0 and later */ |
| 128254 | 128685 | #define sqlite3_error_offset sqlite3_api->error_offset |
| 128686 | +#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value |
| 128687 | +#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct |
| 128688 | +#define sqlite3_vtab_in sqlite3_api->vtab_in |
| 128689 | +#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first |
| 128690 | +#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next |
| 128255 | 128691 | #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 128256 | 128692 | |
| 128257 | 128693 | #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 128258 | 128694 | /* This case when the file really is being compiled as a loadable |
| 128259 | 128695 | ** extension */ |
| | @@ -128741,10 +129177,15 @@ |
| 128741 | 129177 | sqlite3_total_changes64, |
| 128742 | 129178 | /* Version 3.37.0 and later */ |
| 128743 | 129179 | sqlite3_autovacuum_pages, |
| 128744 | 129180 | /* Version 3.38.0 and later */ |
| 128745 | 129181 | sqlite3_error_offset, |
| 129182 | + sqlite3_vtab_rhs_value, |
| 129183 | + sqlite3_vtab_distinct, |
| 129184 | + sqlite3_vtab_in, |
| 129185 | + sqlite3_vtab_in_first, |
| 129186 | + sqlite3_vtab_in_next |
| 128746 | 129187 | }; |
| 128747 | 129188 | |
| 128748 | 129189 | /* True if x is the directory separator character |
| 128749 | 129190 | */ |
| 128750 | 129191 | #if SQLITE_OS_WIN |
| | @@ -131042,10 +131483,14 @@ |
| 131042 | 131483 | sqlite3_stmt *pDummy = 0; |
| 131043 | 131484 | (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0); |
| 131044 | 131485 | (void)sqlite3_finalize(pDummy); |
| 131045 | 131486 | sqlite3DbFree(db, zSql); |
| 131046 | 131487 | } |
| 131488 | + if( db->mallocFailed ){ |
| 131489 | + sqlite3ErrorMsg(db->pParse, "out of memory"); |
| 131490 | + db->pParse->rc = SQLITE_NOMEM_BKPT; |
| 131491 | + } |
| 131047 | 131492 | pHash = &db->aDb[ii].pSchema->tblHash; |
| 131048 | 131493 | break; |
| 131049 | 131494 | } |
| 131050 | 131495 | } |
| 131051 | 131496 | } |
| | @@ -133078,12 +133523,14 @@ |
| 133078 | 133523 | } |
| 133079 | 133524 | |
| 133080 | 133525 | /* |
| 133081 | 133526 | ** Free all memory allocations in the pParse object |
| 133082 | 133527 | */ |
| 133083 | | -SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){ |
| 133528 | +SQLITE_PRIVATE void sqlite3ParseObjectReset(Parse *pParse){ |
| 133084 | 133529 | sqlite3 *db = pParse->db; |
| 133530 | + assert( db!=0 ); |
| 133531 | + assert( db->pParse==pParse ); |
| 133085 | 133532 | assert( pParse->nested==0 ); |
| 133086 | 133533 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 133087 | 133534 | sqlite3DbFree(db, pParse->aTableLock); |
| 133088 | 133535 | #endif |
| 133089 | 133536 | while( pParse->pCleanup ){ |
| | @@ -133094,15 +133541,16 @@ |
| 133094 | 133541 | } |
| 133095 | 133542 | sqlite3DbFree(db, pParse->aLabel); |
| 133096 | 133543 | if( pParse->pConstExpr ){ |
| 133097 | 133544 | sqlite3ExprListDelete(db, pParse->pConstExpr); |
| 133098 | 133545 | } |
| 133099 | | - if( db ){ |
| 133100 | | - assert( db->lookaside.bDisable >= pParse->disableLookaside ); |
| 133101 | | - db->lookaside.bDisable -= pParse->disableLookaside; |
| 133102 | | - db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue; |
| 133103 | | - } |
| 133546 | + assert( db->lookaside.bDisable >= pParse->disableLookaside ); |
| 133547 | + db->lookaside.bDisable -= pParse->disableLookaside; |
| 133548 | + db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue; |
| 133549 | + assert( pParse->db->pParse==pParse ); |
| 133550 | + db->pParse = pParse->pOuterParse; |
| 133551 | + pParse->db = 0; |
| 133104 | 133552 | pParse->disableLookaside = 0; |
| 133105 | 133553 | } |
| 133106 | 133554 | |
| 133107 | 133555 | /* |
| 133108 | 133556 | ** Add a new cleanup operation to a Parser. The cleanup should happen when |
| | @@ -133111,11 +133559,11 @@ |
| 133111 | 133559 | ** |
| 133112 | 133560 | ** Use this mechanism for uncommon cleanups. There is a higher setup |
| 133113 | 133561 | ** cost for this mechansim (an extra malloc), so it should not be used |
| 133114 | 133562 | ** for common cleanups that happen on most calls. But for less |
| 133115 | 133563 | ** common cleanups, we save a single NULL-pointer comparison in |
| 133116 | | -** sqlite3ParserReset(), which reduces the total CPU cycle count. |
| 133564 | +** sqlite3ParseObjectReset(), which reduces the total CPU cycle count. |
| 133117 | 133565 | ** |
| 133118 | 133566 | ** If a memory allocation error occurs, then the cleanup happens immediately. |
| 133119 | 133567 | ** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the |
| 133120 | 133568 | ** pParse->earlyCleanup flag is set in that case. Calling code show verify |
| 133121 | 133569 | ** that test cases exist for which this happens, to guard against possible |
| | @@ -133150,10 +133598,29 @@ |
| 133150 | 133598 | pParse->earlyCleanup = 1; |
| 133151 | 133599 | #endif |
| 133152 | 133600 | } |
| 133153 | 133601 | return pPtr; |
| 133154 | 133602 | } |
| 133603 | + |
| 133604 | +/* |
| 133605 | +** Turn bulk memory into a valid Parse object and link that Parse object |
| 133606 | +** into database connection db. |
| 133607 | +** |
| 133608 | +** Call sqlite3ParseObjectReset() to undo this operation. |
| 133609 | +** |
| 133610 | +** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which |
| 133611 | +** is generated by Lemon. |
| 133612 | +*/ |
| 133613 | +SQLITE_PRIVATE void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){ |
| 133614 | + memset(PARSE_HDR(pParse), 0, PARSE_HDR_SZ); |
| 133615 | + memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
| 133616 | + assert( db->pParse!=pParse ); |
| 133617 | + pParse->pOuterParse = db->pParse; |
| 133618 | + db->pParse = pParse; |
| 133619 | + pParse->db = db; |
| 133620 | + if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory"); |
| 133621 | +} |
| 133155 | 133622 | |
| 133156 | 133623 | /* |
| 133157 | 133624 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 133158 | 133625 | */ |
| 133159 | 133626 | static int sqlite3Prepare( |
| | @@ -133167,15 +133634,19 @@ |
| 133167 | 133634 | ){ |
| 133168 | 133635 | int rc = SQLITE_OK; /* Result code */ |
| 133169 | 133636 | int i; /* Loop counter */ |
| 133170 | 133637 | Parse sParse; /* Parsing context */ |
| 133171 | 133638 | |
| 133172 | | - memset(&sParse, 0, PARSE_HDR_SZ); |
| 133639 | + /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */ |
| 133640 | + memset(PARSE_HDR(&sParse), 0, PARSE_HDR_SZ); |
| 133173 | 133641 | memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ); |
| 133642 | + sParse.pOuterParse = db->pParse; |
| 133643 | + db->pParse = &sParse; |
| 133644 | + sParse.db = db; |
| 133174 | 133645 | sParse.pReprepare = pReprepare; |
| 133175 | 133646 | assert( ppStmt && *ppStmt==0 ); |
| 133176 | | - /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */ |
| 133647 | + if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory"); |
| 133177 | 133648 | assert( sqlite3_mutex_held(db->mutex) ); |
| 133178 | 133649 | |
| 133179 | 133650 | /* For a long-term use prepared statement avoid the use of |
| 133180 | 133651 | ** lookaside memory. |
| 133181 | 133652 | */ |
| | @@ -133224,11 +133695,10 @@ |
| 133224 | 133695 | } |
| 133225 | 133696 | } |
| 133226 | 133697 | |
| 133227 | 133698 | sqlite3VtabUnlockList(db); |
| 133228 | 133699 | |
| 133229 | | - sParse.db = db; |
| 133230 | 133700 | if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
| 133231 | 133701 | char *zSqlCopy; |
| 133232 | 133702 | int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 133233 | 133703 | testcase( nBytes==mxLen ); |
| 133234 | 133704 | testcase( nBytes==mxLen+1 ); |
| | @@ -133291,11 +133761,11 @@ |
| 133291 | 133761 | sqlite3DbFree(db, pT); |
| 133292 | 133762 | } |
| 133293 | 133763 | |
| 133294 | 133764 | end_prepare: |
| 133295 | 133765 | |
| 133296 | | - sqlite3ParserReset(&sParse); |
| 133766 | + sqlite3ParseObjectReset(&sParse); |
| 133297 | 133767 | return rc; |
| 133298 | 133768 | } |
| 133299 | 133769 | static int sqlite3LockAndPrepare( |
| 133300 | 133770 | sqlite3 *db, /* Database handle. */ |
| 133301 | 133771 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| | @@ -134946,11 +135416,11 @@ |
| 134946 | 135416 | p->enc = ENC(db); |
| 134947 | 135417 | p->db = db; |
| 134948 | 135418 | p->nRef = 1; |
| 134949 | 135419 | memset(&p[1], 0, nExtra); |
| 134950 | 135420 | }else{ |
| 134951 | | - sqlite3OomFault(db); |
| 135421 | + return (KeyInfo*)sqlite3OomFault(db); |
| 134952 | 135422 | } |
| 134953 | 135423 | return p; |
| 134954 | 135424 | } |
| 134955 | 135425 | |
| 134956 | 135426 | /* |
| | @@ -135117,10 +135587,13 @@ |
| 135117 | 135587 | } |
| 135118 | 135588 | #endif |
| 135119 | 135589 | |
| 135120 | 135590 | iTab = pSort->iECursor; |
| 135121 | 135591 | if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){ |
| 135592 | + if( eDest==SRT_Mem && p->iOffset ){ |
| 135593 | + sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst); |
| 135594 | + } |
| 135122 | 135595 | regRowid = 0; |
| 135123 | 135596 | regRow = pDest->iSdst; |
| 135124 | 135597 | }else{ |
| 135125 | 135598 | regRowid = sqlite3GetTempReg(pParse); |
| 135126 | 135599 | if( eDest==SRT_EphemTab || eDest==SRT_Table ){ |
| | @@ -136975,10 +137448,11 @@ |
| 136975 | 137448 | }else{ |
| 136976 | 137449 | pSplit = p; |
| 136977 | 137450 | for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; } |
| 136978 | 137451 | } |
| 136979 | 137452 | pPrior = pSplit->pPrior; |
| 137453 | + assert( pPrior!=0 ); |
| 136980 | 137454 | pSplit->pPrior = 0; |
| 136981 | 137455 | pPrior->pNext = 0; |
| 136982 | 137456 | assert( p->pOrderBy == pOrderBy ); |
| 136983 | 137457 | assert( pOrderBy!=0 || db->mallocFailed ); |
| 136984 | 137458 | pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0); |
| | @@ -139126,11 +139600,12 @@ |
| 139126 | 139600 | } |
| 139127 | 139601 | } |
| 139128 | 139602 | |
| 139129 | 139603 | /* Process NATURAL keywords, and ON and USING clauses of joins. |
| 139130 | 139604 | */ |
| 139131 | | - if( pParse->nErr || db->mallocFailed || sqliteProcessJoin(pParse, p) ){ |
| 139605 | + assert( db->mallocFailed==0 || pParse->nErr!=0 ); |
| 139606 | + if( pParse->nErr || sqliteProcessJoin(pParse, p) ){ |
| 139132 | 139607 | return WRC_Abort; |
| 139133 | 139608 | } |
| 139134 | 139609 | |
| 139135 | 139610 | /* For every "*" that occurs in the column list, insert the names of |
| 139136 | 139611 | ** all columns in all tables. And for every TABLE.* insert the names |
| | @@ -139423,16 +139898,17 @@ |
| 139423 | 139898 | Parse *pParse, /* The parser context */ |
| 139424 | 139899 | Select *p, /* The SELECT statement being coded. */ |
| 139425 | 139900 | NameContext *pOuterNC /* Name context for container */ |
| 139426 | 139901 | ){ |
| 139427 | 139902 | assert( p!=0 || pParse->db->mallocFailed ); |
| 139903 | + assert( pParse->db->pParse==pParse ); |
| 139428 | 139904 | if( pParse->db->mallocFailed ) return; |
| 139429 | 139905 | if( p->selFlags & SF_HasTypeInfo ) return; |
| 139430 | 139906 | sqlite3SelectExpand(pParse, p); |
| 139431 | | - if( pParse->nErr || pParse->db->mallocFailed ) return; |
| 139907 | + if( pParse->nErr ) return; |
| 139432 | 139908 | sqlite3ResolveSelectNames(pParse, p, pOuterNC); |
| 139433 | | - if( pParse->nErr || pParse->db->mallocFailed ) return; |
| 139909 | + if( pParse->nErr ) return; |
| 139434 | 139910 | sqlite3SelectAddTypeInfo(pParse, p); |
| 139435 | 139911 | } |
| 139436 | 139912 | |
| 139437 | 139913 | /* |
| 139438 | 139914 | ** Reset the aggregate accumulator. |
| | @@ -139445,12 +139921,14 @@ |
| 139445 | 139921 | static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){ |
| 139446 | 139922 | Vdbe *v = pParse->pVdbe; |
| 139447 | 139923 | int i; |
| 139448 | 139924 | struct AggInfo_func *pFunc; |
| 139449 | 139925 | int nReg = pAggInfo->nFunc + pAggInfo->nColumn; |
| 139926 | + assert( pParse->db->pParse==pParse ); |
| 139927 | + assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 ); |
| 139450 | 139928 | if( nReg==0 ) return; |
| 139451 | | - if( pParse->nErr || pParse->db->mallocFailed ) return; |
| 139929 | + if( pParse->nErr ) return; |
| 139452 | 139930 | #ifdef SQLITE_DEBUG |
| 139453 | 139931 | /* Verify that all AggInfo registers are within the range specified by |
| 139454 | 139932 | ** AggInfo.mnReg..AggInfo.mxReg */ |
| 139455 | 139933 | assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 ); |
| 139456 | 139934 | for(i=0; i<pAggInfo->nColumn; i++){ |
| | @@ -139869,14 +140347,16 @@ |
| 139869 | 140347 | sqlite3 *db; /* The database connection */ |
| 139870 | 140348 | ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */ |
| 139871 | 140349 | u8 minMaxFlag; /* Flag for min/max queries */ |
| 139872 | 140350 | |
| 139873 | 140351 | db = pParse->db; |
| 140352 | + assert( pParse==db->pParse ); |
| 139874 | 140353 | v = sqlite3GetVdbe(pParse); |
| 139875 | | - if( p==0 || db->mallocFailed || pParse->nErr ){ |
| 140354 | + if( p==0 || pParse->nErr ){ |
| 139876 | 140355 | return 1; |
| 139877 | 140356 | } |
| 140357 | + assert( db->mallocFailed==0 ); |
| 139878 | 140358 | if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; |
| 139879 | 140359 | #if SELECTTRACE_ENABLED |
| 139880 | 140360 | SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain)); |
| 139881 | 140361 | if( sqlite3SelectTrace & 0x100 ){ |
| 139882 | 140362 | sqlite3TreeViewSelect(0, p, 0); |
| | @@ -139907,13 +140387,14 @@ |
| 139907 | 140387 | } |
| 139908 | 140388 | p->selFlags &= ~SF_Distinct; |
| 139909 | 140389 | p->selFlags |= SF_NoopOrderBy; |
| 139910 | 140390 | } |
| 139911 | 140391 | sqlite3SelectPrep(pParse, p, 0); |
| 139912 | | - if( pParse->nErr || db->mallocFailed ){ |
| 140392 | + if( pParse->nErr ){ |
| 139913 | 140393 | goto select_end; |
| 139914 | 140394 | } |
| 140395 | + assert( db->mallocFailed==0 ); |
| 139915 | 140396 | assert( p->pEList!=0 ); |
| 139916 | 140397 | #if SELECTTRACE_ENABLED |
| 139917 | 140398 | if( sqlite3SelectTrace & 0x104 ){ |
| 139918 | 140399 | SELECTTRACE(0x104,pParse,p, ("after name resolution:\n")); |
| 139919 | 140400 | sqlite3TreeViewSelect(0, p, 0); |
| | @@ -139953,11 +140434,11 @@ |
| 139953 | 140434 | sqlite3GenerateColumnNames(pParse, p); |
| 139954 | 140435 | } |
| 139955 | 140436 | |
| 139956 | 140437 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 139957 | 140438 | if( sqlite3WindowRewrite(pParse, p) ){ |
| 139958 | | - assert( db->mallocFailed || pParse->nErr>0 ); |
| 140439 | + assert( pParse->nErr ); |
| 139959 | 140440 | goto select_end; |
| 139960 | 140441 | } |
| 139961 | 140442 | #if SELECTTRACE_ENABLED |
| 139962 | 140443 | if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){ |
| 139963 | 140444 | SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n")); |
| | @@ -140429,11 +140910,11 @@ |
| 140429 | 140910 | |
| 140430 | 140911 | |
| 140431 | 140912 | /* Begin the database scan. */ |
| 140432 | 140913 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 140433 | 140914 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy, |
| 140434 | | - p->pEList, wctrlFlags, p->nSelectRow); |
| 140915 | + p->pEList, p, wctrlFlags, p->nSelectRow); |
| 140435 | 140916 | if( pWInfo==0 ) goto select_end; |
| 140436 | 140917 | if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){ |
| 140437 | 140918 | p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo); |
| 140438 | 140919 | } |
| 140439 | 140920 | if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){ |
| | @@ -140693,11 +141174,11 @@ |
| 140693 | 141174 | ** in the right order to begin with. |
| 140694 | 141175 | */ |
| 140695 | 141176 | sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); |
| 140696 | 141177 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 140697 | 141178 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct, |
| 140698 | | - WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0 |
| 141179 | + 0, (WHERE_GROUPBY|(orderByGrp ? WHERE_SORTBYGROUP : 0)|distFlag), 0 |
| 140699 | 141180 | ); |
| 140700 | 141181 | if( pWInfo==0 ){ |
| 140701 | 141182 | sqlite3ExprListDelete(db, pDistinct); |
| 140702 | 141183 | goto select_end; |
| 140703 | 141184 | } |
| | @@ -140991,11 +141472,11 @@ |
| 140991 | 141472 | assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 ); |
| 140992 | 141473 | assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 ); |
| 140993 | 141474 | |
| 140994 | 141475 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 140995 | 141476 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy, |
| 140996 | | - pDistinct, minMaxFlag|distFlag, 0); |
| 141477 | + pDistinct, 0, minMaxFlag|distFlag, 0); |
| 140997 | 141478 | if( pWInfo==0 ){ |
| 140998 | 141479 | goto select_end; |
| 140999 | 141480 | } |
| 141000 | 141481 | SELECTTRACE(1,pParse,p,("WhereBegin returns\n")); |
| 141001 | 141482 | eDist = sqlite3WhereIsDistinct(pWInfo); |
| | @@ -141048,11 +141529,11 @@ |
| 141048 | 141529 | /* Control jumps to here if an error is encountered above, or upon |
| 141049 | 141530 | ** successful coding of the SELECT. |
| 141050 | 141531 | */ |
| 141051 | 141532 | select_end: |
| 141052 | 141533 | assert( db->mallocFailed==0 || db->mallocFailed==1 ); |
| 141053 | | - pParse->nErr += db->mallocFailed; |
| 141534 | + assert( db->mallocFailed==0 || pParse->nErr!=0 ); |
| 141054 | 141535 | sqlite3ExprListDelete(db, pMinMaxOrderBy); |
| 141055 | 141536 | #ifdef SQLITE_DEBUG |
| 141056 | 141537 | if( pAggInfo && !db->mallocFailed ){ |
| 141057 | 141538 | for(i=0; i<pAggInfo->nColumn; i++){ |
| 141058 | 141539 | Expr *pExpr = pAggInfo->aCol[i].pCExpr; |
| | @@ -142200,10 +142681,11 @@ |
| 142200 | 142681 | Select sSelect; |
| 142201 | 142682 | SrcList sFrom; |
| 142202 | 142683 | |
| 142203 | 142684 | assert( v!=0 ); |
| 142204 | 142685 | assert( pParse->bReturning ); |
| 142686 | + assert( db->pParse==pParse ); |
| 142205 | 142687 | pReturning = pParse->u1.pReturning; |
| 142206 | 142688 | assert( pTrigger == &(pReturning->retTrig) ); |
| 142207 | 142689 | memset(&sSelect, 0, sizeof(sSelect)); |
| 142208 | 142690 | memset(&sFrom, 0, sizeof(sFrom)); |
| 142209 | 142691 | sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0); |
| | @@ -142210,11 +142692,12 @@ |
| 142210 | 142692 | sSelect.pSrc = &sFrom; |
| 142211 | 142693 | sFrom.nSrc = 1; |
| 142212 | 142694 | sFrom.a[0].pTab = pTab; |
| 142213 | 142695 | sFrom.a[0].iCursor = -1; |
| 142214 | 142696 | sqlite3SelectPrep(pParse, &sSelect, 0); |
| 142215 | | - if( db->mallocFailed==0 && pParse->nErr==0 ){ |
| 142697 | + if( pParse->nErr==0 ){ |
| 142698 | + assert( db->mallocFailed==0 ); |
| 142216 | 142699 | sqlite3GenerateColumnNames(pParse, &sSelect); |
| 142217 | 142700 | } |
| 142218 | 142701 | sqlite3ExprListDelete(db, sSelect.pEList); |
| 142219 | 142702 | pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab); |
| 142220 | 142703 | if( !db->mallocFailed ){ |
| | @@ -142228,11 +142711,11 @@ |
| 142228 | 142711 | sNC.uNC.iBaseReg = regIn; |
| 142229 | 142712 | sNC.ncFlags = NC_UBaseReg; |
| 142230 | 142713 | pParse->eTriggerOp = pTrigger->op; |
| 142231 | 142714 | pParse->pTriggerTab = pTab; |
| 142232 | 142715 | if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK |
| 142233 | | - && !db->mallocFailed |
| 142716 | + && ALWAYS(!db->mallocFailed) |
| 142234 | 142717 | ){ |
| 142235 | 142718 | int i; |
| 142236 | 142719 | int nCol = pNew->nExpr; |
| 142237 | 142720 | int reg = pParse->nMem+1; |
| 142238 | 142721 | pParse->nMem += nCol+2; |
| | @@ -142392,12 +142875,12 @@ |
| 142392 | 142875 | TriggerPrg *pPrg; /* Value to return */ |
| 142393 | 142876 | Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */ |
| 142394 | 142877 | Vdbe *v; /* Temporary VM */ |
| 142395 | 142878 | NameContext sNC; /* Name context for sub-vdbe */ |
| 142396 | 142879 | SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */ |
| 142397 | | - Parse *pSubParse; /* Parse context for sub-vdbe */ |
| 142398 | 142880 | int iEndTrigger = 0; /* Label to jump to if WHEN is false */ |
| 142881 | + Parse sSubParse; /* Parse context for sub-vdbe */ |
| 142399 | 142882 | |
| 142400 | 142883 | assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); |
| 142401 | 142884 | assert( pTop->pVdbe ); |
| 142402 | 142885 | |
| 142403 | 142886 | /* Allocate the TriggerPrg and SubProgram objects. To ensure that they |
| | @@ -142415,23 +142898,21 @@ |
| 142415 | 142898 | pPrg->aColmask[0] = 0xffffffff; |
| 142416 | 142899 | pPrg->aColmask[1] = 0xffffffff; |
| 142417 | 142900 | |
| 142418 | 142901 | /* Allocate and populate a new Parse context to use for coding the |
| 142419 | 142902 | ** trigger sub-program. */ |
| 142420 | | - pSubParse = sqlite3StackAllocZero(db, sizeof(Parse)); |
| 142421 | | - if( !pSubParse ) return 0; |
| 142903 | + sqlite3ParseObjectInit(&sSubParse, db); |
| 142422 | 142904 | memset(&sNC, 0, sizeof(sNC)); |
| 142423 | | - sNC.pParse = pSubParse; |
| 142424 | | - pSubParse->db = db; |
| 142425 | | - pSubParse->pTriggerTab = pTab; |
| 142426 | | - pSubParse->pToplevel = pTop; |
| 142427 | | - pSubParse->zAuthContext = pTrigger->zName; |
| 142428 | | - pSubParse->eTriggerOp = pTrigger->op; |
| 142429 | | - pSubParse->nQueryLoop = pParse->nQueryLoop; |
| 142430 | | - pSubParse->disableVtab = pParse->disableVtab; |
| 142431 | | - |
| 142432 | | - v = sqlite3GetVdbe(pSubParse); |
| 142905 | + sNC.pParse = &sSubParse; |
| 142906 | + sSubParse.pTriggerTab = pTab; |
| 142907 | + sSubParse.pToplevel = pTop; |
| 142908 | + sSubParse.zAuthContext = pTrigger->zName; |
| 142909 | + sSubParse.eTriggerOp = pTrigger->op; |
| 142910 | + sSubParse.nQueryLoop = pParse->nQueryLoop; |
| 142911 | + sSubParse.disableVtab = pParse->disableVtab; |
| 142912 | + |
| 142913 | + v = sqlite3GetVdbe(&sSubParse); |
| 142433 | 142914 | if( v ){ |
| 142434 | 142915 | VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", |
| 142435 | 142916 | pTrigger->zName, onErrorText(orconf), |
| 142436 | 142917 | (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"), |
| 142437 | 142918 | (pTrigger->op==TK_UPDATE ? "UPDATE" : ""), |
| | @@ -142453,42 +142934,43 @@ |
| 142453 | 142934 | if( pTrigger->pWhen ){ |
| 142454 | 142935 | pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0); |
| 142455 | 142936 | if( db->mallocFailed==0 |
| 142456 | 142937 | && SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) |
| 142457 | 142938 | ){ |
| 142458 | | - iEndTrigger = sqlite3VdbeMakeLabel(pSubParse); |
| 142459 | | - sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL); |
| 142939 | + iEndTrigger = sqlite3VdbeMakeLabel(&sSubParse); |
| 142940 | + sqlite3ExprIfFalse(&sSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL); |
| 142460 | 142941 | } |
| 142461 | 142942 | sqlite3ExprDelete(db, pWhen); |
| 142462 | 142943 | } |
| 142463 | 142944 | |
| 142464 | 142945 | /* Code the trigger program into the sub-vdbe. */ |
| 142465 | | - codeTriggerProgram(pSubParse, pTrigger->step_list, orconf); |
| 142946 | + codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf); |
| 142466 | 142947 | |
| 142467 | 142948 | /* Insert an OP_Halt at the end of the sub-program. */ |
| 142468 | 142949 | if( iEndTrigger ){ |
| 142469 | 142950 | sqlite3VdbeResolveLabel(v, iEndTrigger); |
| 142470 | 142951 | } |
| 142471 | 142952 | sqlite3VdbeAddOp0(v, OP_Halt); |
| 142472 | 142953 | VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf))); |
| 142954 | + transferParseError(pParse, &sSubParse); |
| 142473 | 142955 | |
| 142474 | | - transferParseError(pParse, pSubParse); |
| 142475 | | - if( db->mallocFailed==0 && pParse->nErr==0 ){ |
| 142956 | + if( pParse->nErr==0 ){ |
| 142957 | + assert( db->mallocFailed==0 ); |
| 142476 | 142958 | pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg); |
| 142477 | 142959 | } |
| 142478 | | - pProgram->nMem = pSubParse->nMem; |
| 142479 | | - pProgram->nCsr = pSubParse->nTab; |
| 142960 | + pProgram->nMem = sSubParse.nMem; |
| 142961 | + pProgram->nCsr = sSubParse.nTab; |
| 142480 | 142962 | pProgram->token = (void *)pTrigger; |
| 142481 | | - pPrg->aColmask[0] = pSubParse->oldmask; |
| 142482 | | - pPrg->aColmask[1] = pSubParse->newmask; |
| 142963 | + pPrg->aColmask[0] = sSubParse.oldmask; |
| 142964 | + pPrg->aColmask[1] = sSubParse.newmask; |
| 142483 | 142965 | sqlite3VdbeDelete(v); |
| 142966 | + }else{ |
| 142967 | + transferParseError(pParse, &sSubParse); |
| 142484 | 142968 | } |
| 142485 | 142969 | |
| 142486 | | - assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); |
| 142487 | | - sqlite3ParserReset(pSubParse); |
| 142488 | | - sqlite3StackFree(db, pSubParse); |
| 142489 | | - |
| 142970 | + assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg ); |
| 142971 | + sqlite3ParseObjectReset(&sSubParse); |
| 142490 | 142972 | return pPrg; |
| 142491 | 142973 | } |
| 142492 | 142974 | |
| 142493 | 142975 | /* |
| 142494 | 142976 | ** Return a pointer to a TriggerPrg object containing the sub-program for |
| | @@ -142539,11 +143021,11 @@ |
| 142539 | 143021 | int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ |
| 142540 | 143022 | ){ |
| 142541 | 143023 | Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */ |
| 142542 | 143024 | TriggerPrg *pPrg; |
| 142543 | 143025 | pPrg = getRowTrigger(pParse, p, pTab, orconf); |
| 142544 | | - assert( pPrg || pParse->nErr || pParse->db->mallocFailed ); |
| 143026 | + assert( pPrg || pParse->nErr ); |
| 142545 | 143027 | |
| 142546 | 143028 | /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program |
| 142547 | 143029 | ** is a pointer to the sub-vdbe containing the trigger program. */ |
| 142548 | 143030 | if( pPrg ){ |
| 142549 | 143031 | int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers)); |
| | @@ -143057,13 +143539,15 @@ |
| 143057 | 143539 | int regRowSet = 0; /* Rowset of rows to be updated */ |
| 143058 | 143540 | int regKey = 0; /* composite PRIMARY KEY value */ |
| 143059 | 143541 | |
| 143060 | 143542 | memset(&sContext, 0, sizeof(sContext)); |
| 143061 | 143543 | db = pParse->db; |
| 143062 | | - if( pParse->nErr || db->mallocFailed ){ |
| 143544 | + assert( db->pParse==pParse ); |
| 143545 | + if( pParse->nErr ){ |
| 143063 | 143546 | goto update_cleanup; |
| 143064 | 143547 | } |
| 143548 | + assert( db->mallocFailed==0 ); |
| 143065 | 143549 | |
| 143066 | 143550 | /* Locate the table which we want to update. |
| 143067 | 143551 | */ |
| 143068 | 143552 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
| 143069 | 143553 | if( pTab==0 ) goto update_cleanup; |
| | @@ -143431,11 +143915,11 @@ |
| 143431 | 143915 | ** or index, causing a single-pass approach to malfunction. */ |
| 143432 | 143916 | flags = WHERE_ONEPASS_DESIRED; |
| 143433 | 143917 | if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){ |
| 143434 | 143918 | flags |= WHERE_ONEPASS_MULTIROW; |
| 143435 | 143919 | } |
| 143436 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags,iIdxCur); |
| 143920 | + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur); |
| 143437 | 143921 | if( pWInfo==0 ) goto update_cleanup; |
| 143438 | 143922 | |
| 143439 | 143923 | /* A one-pass strategy that might update more than one row may not |
| 143440 | 143924 | ** be used if any column of the index used for the scan is being |
| 143441 | 143925 | ** updated. Otherwise, if there is an index on "b", statements like |
| | @@ -143953,11 +144437,13 @@ |
| 143953 | 144437 | }else{ |
| 143954 | 144438 | regRec = ++pParse->nMem; |
| 143955 | 144439 | regRowid = ++pParse->nMem; |
| 143956 | 144440 | |
| 143957 | 144441 | /* Start scanning the virtual table */ |
| 143958 | | - pWInfo = sqlite3WhereBegin(pParse, pSrc,pWhere,0,0,WHERE_ONEPASS_DESIRED,0); |
| 144442 | + pWInfo = sqlite3WhereBegin( |
| 144443 | + pParse, pSrc, pWhere, 0, 0, 0, WHERE_ONEPASS_DESIRED, 0 |
| 144444 | + ); |
| 143959 | 144445 | if( pWInfo==0 ) return; |
| 143960 | 144446 | |
| 143961 | 144447 | /* Populate the argument registers. */ |
| 143962 | 144448 | for(i=0; i<pTab->nCol; i++){ |
| 143963 | 144449 | assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ); |
| | @@ -145598,13 +146084,12 @@ |
| 145598 | 146084 | return SQLITE_MISUSE_BKPT; |
| 145599 | 146085 | } |
| 145600 | 146086 | pTab = pCtx->pTab; |
| 145601 | 146087 | assert( IsVirtual(pTab) ); |
| 145602 | 146088 | |
| 145603 | | - memset(&sParse, 0, sizeof(sParse)); |
| 146089 | + sqlite3ParseObjectInit(&sParse, db); |
| 145604 | 146090 | sParse.eParseMode = PARSE_MODE_DECLARE_VTAB; |
| 145605 | | - sParse.db = db; |
| 145606 | 146091 | /* We should never be able to reach this point while loading the |
| 145607 | 146092 | ** schema. Nevertheless, defend against that (turn off db->init.busy) |
| 145608 | 146093 | ** in case a bug arises. */ |
| 145609 | 146094 | assert( db->init.busy==0 ); |
| 145610 | 146095 | initBusy = db->init.busy; |
| | @@ -145654,11 +146139,11 @@ |
| 145654 | 146139 | |
| 145655 | 146140 | if( sParse.pVdbe ){ |
| 145656 | 146141 | sqlite3VdbeFinalize(sParse.pVdbe); |
| 145657 | 146142 | } |
| 145658 | 146143 | sqlite3DeleteTable(db, sParse.pNewTable); |
| 145659 | | - sqlite3ParserReset(&sParse); |
| 146144 | + sqlite3ParseObjectReset(&sParse); |
| 145660 | 146145 | db->init.busy = initBusy; |
| 145661 | 146146 | |
| 145662 | 146147 | assert( (rc&0xff)==rc ); |
| 145663 | 146148 | rc = sqlite3ApiExit(db, rc); |
| 145664 | 146149 | sqlite3_mutex_leave(db->mutex); |
| | @@ -146265,14 +146750,16 @@ |
| 146265 | 146750 | u16 nDistinctCol; /* Index columns used to sort for DISTINCT */ |
| 146266 | 146751 | Index *pIndex; /* Index used, or NULL */ |
| 146267 | 146752 | } btree; |
| 146268 | 146753 | struct { /* Information for virtual tables */ |
| 146269 | 146754 | int idxNum; /* Index number */ |
| 146270 | | - u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 146755 | + u32 needFree : 1; /* True if sqlite3_free(idxStr) is needed */ |
| 146756 | + u32 bOmitOffset : 1; /* True to let virtual table handle offset */ |
| 146271 | 146757 | i8 isOrdered; /* True if satisfies ORDER BY */ |
| 146272 | 146758 | u16 omitMask; /* Terms that may be omitted */ |
| 146273 | 146759 | char *idxStr; /* Index identifier string */ |
| 146760 | + u32 mHandleIn; /* Terms to handle as IN(...) instead of == */ |
| 146274 | 146761 | } vtab; |
| 146275 | 146762 | } u; |
| 146276 | 146763 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 146277 | 146764 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 146278 | 146765 | u16 nSkip; /* Number of NULL aLTerm[] entries */ |
| | @@ -146425,10 +146912,11 @@ |
| 146425 | 146912 | #ifdef SQLITE_ENABLE_STAT4 |
| 146426 | 146913 | # define TERM_HIGHTRUTH 0x4000 /* Term excludes few rows */ |
| 146427 | 146914 | #else |
| 146428 | 146915 | # define TERM_HIGHTRUTH 0 /* Only used with STAT4 */ |
| 146429 | 146916 | #endif |
| 146917 | +#define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */ |
| 146430 | 146918 | |
| 146431 | 146919 | /* |
| 146432 | 146920 | ** An instance of the WhereScan object is used as an iterator for locating |
| 146433 | 146921 | ** terms in the WHERE clause that are useful to the query planner. |
| 146434 | 146922 | */ |
| | @@ -146528,11 +147016,10 @@ |
| 146528 | 147016 | ** to construct WhereLoop objects for a particular query. |
| 146529 | 147017 | */ |
| 146530 | 147018 | struct WhereLoopBuilder { |
| 146531 | 147019 | WhereInfo *pWInfo; /* Information about this WHERE */ |
| 146532 | 147020 | WhereClause *pWC; /* WHERE clause terms */ |
| 146533 | | - ExprList *pOrderBy; /* ORDER BY clause */ |
| 146534 | 147021 | WhereLoop *pNew; /* Template WhereLoop */ |
| 146535 | 147022 | WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ |
| 146536 | 147023 | #ifdef SQLITE_ENABLE_STAT4 |
| 146537 | 147024 | UnpackedRecord *pRec; /* Probe for stat4 (if required) */ |
| 146538 | 147025 | int nRecValid; /* Number of valid fields currently in pRec */ |
| | @@ -146596,10 +147083,13 @@ |
| 146596 | 147083 | Parse *pParse; /* Parsing and code generating context */ |
| 146597 | 147084 | SrcList *pTabList; /* List of tables in the join */ |
| 146598 | 147085 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
| 146599 | 147086 | ExprList *pResultSet; /* Result set of the query */ |
| 146600 | 147087 | Expr *pWhere; /* The complete WHERE clause */ |
| 147088 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 147089 | + Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */ |
| 147090 | +#endif |
| 146601 | 147091 | int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ |
| 146602 | 147092 | int iContinue; /* Jump here to continue with next record */ |
| 146603 | 147093 | int iBreak; /* Jump here to break out of the loop */ |
| 146604 | 147094 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
| 146605 | 147095 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
| | @@ -146681,10 +147171,11 @@ |
| 146681 | 147171 | |
| 146682 | 147172 | /* whereexpr.c: */ |
| 146683 | 147173 | SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*); |
| 146684 | 147174 | SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*); |
| 146685 | 147175 | SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8); |
| 147176 | +SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause*, Select*); |
| 146686 | 147177 | SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); |
| 146687 | 147178 | SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*); |
| 146688 | 147179 | SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); |
| 146689 | 147180 | SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); |
| 146690 | 147181 | SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*); |
| | @@ -146751,10 +147242,11 @@ |
| 146751 | 147242 | #define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */ |
| 146752 | 147243 | #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */ |
| 146753 | 147244 | #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */ |
| 146754 | 147245 | #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */ |
| 146755 | 147246 | #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */ |
| 147247 | +#define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */ |
| 146756 | 147248 | |
| 146757 | 147249 | #endif /* !defined(SQLITE_WHEREINT_H) */ |
| 146758 | 147250 | |
| 146759 | 147251 | /************** End of whereInt.h ********************************************/ |
| 146760 | 147252 | /************** Continuing where we left off in wherecode.c ******************/ |
| | @@ -147561,10 +148053,13 @@ |
| 147561 | 148053 | regBase = r1; |
| 147562 | 148054 | }else{ |
| 147563 | 148055 | sqlite3VdbeAddOp2(v, OP_Copy, r1, regBase+j); |
| 147564 | 148056 | } |
| 147565 | 148057 | } |
| 148058 | + } |
| 148059 | + for(j=nSkip; j<nEq; j++){ |
| 148060 | + pTerm = pLoop->aLTerm[j]; |
| 147566 | 148061 | if( pTerm->eOperator & WO_IN ){ |
| 147567 | 148062 | if( pTerm->pExpr->flags & EP_xIsSelect ){ |
| 147568 | 148063 | /* No affinity ever needs to be (or should be) applied to a value |
| 147569 | 148064 | ** from the RHS of an "? IN (SELECT ...)" expression. The |
| 147570 | 148065 | ** sqlite3FindInIndex() routine has already ensured that the |
| | @@ -147575,11 +148070,12 @@ |
| 147575 | 148070 | Expr *pRight = pTerm->pExpr->pRight; |
| 147576 | 148071 | if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ |
| 147577 | 148072 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); |
| 147578 | 148073 | VdbeCoverage(v); |
| 147579 | 148074 | } |
| 147580 | | - if( pParse->db->mallocFailed==0 && pParse->nErr==0 ){ |
| 148075 | + if( pParse->nErr==0 ){ |
| 148076 | + assert( pParse->db->mallocFailed==0 ); |
| 147581 | 148077 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){ |
| 147582 | 148078 | zAff[j] = SQLITE_AFF_BLOB; |
| 147583 | 148079 | } |
| 147584 | 148080 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 147585 | 148081 | zAff[j] = SQLITE_AFF_BLOB; |
| | @@ -148265,15 +148761,31 @@ |
| 148265 | 148761 | for(j=0; j<nConstraint; j++){ |
| 148266 | 148762 | int iTarget = iReg+j+2; |
| 148267 | 148763 | pTerm = pLoop->aLTerm[j]; |
| 148268 | 148764 | if( NEVER(pTerm==0) ) continue; |
| 148269 | 148765 | if( pTerm->eOperator & WO_IN ){ |
| 148270 | | - codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 148271 | | - addrNotFound = pLevel->addrNxt; |
| 148766 | + if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){ |
| 148767 | + int iTab = pParse->nTab++; |
| 148768 | + int iCache = ++pParse->nMem; |
| 148769 | + sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab); |
| 148770 | + sqlite3VdbeAddOp3(v, OP_VInitIn, iTab, iTarget, iCache); |
| 148771 | + }else{ |
| 148772 | + codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 148773 | + addrNotFound = pLevel->addrNxt; |
| 148774 | + } |
| 148272 | 148775 | }else{ |
| 148273 | 148776 | Expr *pRight = pTerm->pExpr->pRight; |
| 148274 | 148777 | codeExprOrVector(pParse, pRight, iTarget, 1); |
| 148778 | + if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET |
| 148779 | + && pLoop->u.vtab.bOmitOffset |
| 148780 | + ){ |
| 148781 | + assert( pTerm->eOperator==WO_AUX ); |
| 148782 | + assert( pWInfo->pLimit!=0 ); |
| 148783 | + assert( pWInfo->pLimit->iOffset>0 ); |
| 148784 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pLimit->iOffset); |
| 148785 | + VdbeComment((v,"Zero OFFSET counter")); |
| 148786 | + } |
| 148275 | 148787 | } |
| 148276 | 148788 | } |
| 148277 | 148789 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
| 148278 | 148790 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
| 148279 | 148791 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| | @@ -148292,17 +148804,23 @@ |
| 148292 | 148804 | iIn = pLevel->u.in.nIn; |
| 148293 | 148805 | }else{ |
| 148294 | 148806 | iIn = 0; |
| 148295 | 148807 | } |
| 148296 | 148808 | for(j=nConstraint-1; j>=0; j--){ |
| 148809 | + int bIn; /* True to generate byte code to loop over RHS IN values */ |
| 148297 | 148810 | pTerm = pLoop->aLTerm[j]; |
| 148298 | | - if( (pTerm->eOperator & WO_IN)!=0 ) iIn--; |
| 148811 | + if( (pTerm->eOperator & WO_IN)!=0 |
| 148812 | + && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0 |
| 148813 | + ){ |
| 148814 | + bIn = 1; |
| 148815 | + }else{ |
| 148816 | + bIn = 0; |
| 148817 | + } |
| 148818 | + if( bIn ) iIn--; |
| 148299 | 148819 | if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){ |
| 148300 | 148820 | disableTerm(pLevel, pTerm); |
| 148301 | | - }else if( (pTerm->eOperator & WO_IN)!=0 |
| 148302 | | - && sqlite3ExprVectorSize(pTerm->pExpr->pLeft)==1 |
| 148303 | | - ){ |
| 148821 | + }else if( bIn && sqlite3ExprVectorSize(pTerm->pExpr->pLeft)==1 ){ |
| 148304 | 148822 | Expr *pCompare; /* The comparison operator */ |
| 148305 | 148823 | Expr *pRight; /* RHS of the comparison */ |
| 148306 | 148824 | VdbeOp *pOp; /* Opcode to access the value of the IN constraint */ |
| 148307 | 148825 | |
| 148308 | 148826 | /* Reload the constraint value into reg[iReg+j+2]. The same value |
| | @@ -149028,11 +149546,11 @@ |
| 149028 | 149546 | regRowid = ++pParse->nMem; |
| 149029 | 149547 | } |
| 149030 | 149548 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 149031 | 149549 | |
| 149032 | 149550 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 149033 | | - ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 149551 | + ** Then for every term xN, evaluate as the subexpression: xN AND y |
| 149034 | 149552 | ** That way, terms in y that are factored into the disjunction will |
| 149035 | 149553 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
| 149036 | 149554 | ** |
| 149037 | 149555 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 149038 | 149556 | ** the "interesting" terms of z - terms that did not originate in the |
| | @@ -149040,19 +149558,28 @@ |
| 149040 | 149558 | ** indices. |
| 149041 | 149559 | ** |
| 149042 | 149560 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 149043 | 149561 | ** is not contained in the ON clause of a LEFT JOIN. |
| 149044 | 149562 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
| 149563 | + ** |
| 149564 | + ** 2022-02-04: Do not push down slices of a row-value comparison. |
| 149565 | + ** In other words, "w" or "y" may not be a slice of a vector. Otherwise, |
| 149566 | + ** the initialization of the right-hand operand of the vector comparison |
| 149567 | + ** might not occur, or might occur only in an OR branch that is not |
| 149568 | + ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1. |
| 149045 | 149569 | */ |
| 149046 | 149570 | if( pWC->nTerm>1 ){ |
| 149047 | 149571 | int iTerm; |
| 149048 | 149572 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 149049 | 149573 | Expr *pExpr = pWC->a[iTerm].pExpr; |
| 149050 | 149574 | if( &pWC->a[iTerm] == pTerm ) continue; |
| 149051 | 149575 | testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL ); |
| 149052 | 149576 | testcase( pWC->a[iTerm].wtFlags & TERM_CODED ); |
| 149053 | | - if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue; |
| 149577 | + testcase( pWC->a[iTerm].wtFlags & TERM_SLICE ); |
| 149578 | + if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){ |
| 149579 | + continue; |
| 149580 | + } |
| 149054 | 149581 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
| 149055 | 149582 | testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
| 149056 | 149583 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 149057 | 149584 | pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr); |
| 149058 | 149585 | } |
| | @@ -149091,13 +149618,13 @@ |
| 149091 | 149618 | pOrExpr = pAndExpr; |
| 149092 | 149619 | } |
| 149093 | 149620 | /* Loop through table entries that match term pOrTerm. */ |
| 149094 | 149621 | ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1)); |
| 149095 | 149622 | WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); |
| 149096 | | - pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 149623 | + pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0, |
| 149097 | 149624 | WHERE_OR_SUBCLAUSE, iCovCur); |
| 149098 | | - assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 149625 | + assert( pSubWInfo || pParse->nErr ); |
| 149099 | 149626 | if( pSubWInfo ){ |
| 149100 | 149627 | WhereLoop *pSubLoop; |
| 149101 | 149628 | int addrExplain = sqlite3WhereExplainOneScan( |
| 149102 | 149629 | pParse, pOrTab, &pSubWInfo->a[0], 0 |
| 149103 | 149630 | ); |
| | @@ -149831,11 +150358,11 @@ |
| 149831 | 150358 | void *pNotUsed; |
| 149832 | 150359 | pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab; |
| 149833 | 150360 | assert( pVtab!=0 ); |
| 149834 | 150361 | assert( pVtab->pModule!=0 ); |
| 149835 | 150362 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 149836 | | - pMod = (sqlite3_module *)pVtab->pModule; |
| 150363 | + pMod = (sqlite3_module *)pVtab->pModule; |
| 149837 | 150364 | if( pMod->xFindFunction!=0 ){ |
| 149838 | 150365 | i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed); |
| 149839 | 150366 | if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){ |
| 149840 | 150367 | *peOp2 = i; |
| 149841 | 150368 | *ppRight = pList->a[1].pExpr; |
| | @@ -150788,11 +151315,14 @@ |
| 150788 | 151315 | ** new terms for each component comparison - "a = ?" and "b = ?". The |
| 150789 | 151316 | ** new terms completely replace the original vector comparison, which is |
| 150790 | 151317 | ** no longer used. |
| 150791 | 151318 | ** |
| 150792 | 151319 | ** This is only required if at least one side of the comparison operation |
| 150793 | | - ** is not a sub-select. */ |
| 151320 | + ** is not a sub-select. |
| 151321 | + ** |
| 151322 | + ** tag-20220128a |
| 151323 | + */ |
| 150794 | 151324 | if( (pExpr->op==TK_EQ || pExpr->op==TK_IS) |
| 150795 | 151325 | && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1 |
| 150796 | 151326 | && sqlite3ExprVectorSize(pExpr->pRight)==nLeft |
| 150797 | 151327 | && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 |
| 150798 | 151328 | || (pExpr->pRight->flags & EP_xIsSelect)==0) |
| | @@ -150805,11 +151335,11 @@ |
| 150805 | 151335 | Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft); |
| 150806 | 151336 | Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft); |
| 150807 | 151337 | |
| 150808 | 151338 | pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight); |
| 150809 | 151339 | transferJoinMarkings(pNew, pExpr); |
| 150810 | | - idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC); |
| 151340 | + idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_SLICE); |
| 150811 | 151341 | exprAnalyze(pSrc, pWC, idxNew); |
| 150812 | 151342 | } |
| 150813 | 151343 | pTerm = &pWC->a[idxTerm]; |
| 150814 | 151344 | pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */ |
| 150815 | 151345 | pTerm->eOperator = 0; |
| | @@ -150930,10 +151460,117 @@ |
| 150930 | 151460 | }else{ |
| 150931 | 151461 | sqlite3WhereSplit(pWC, pE2->pLeft, op); |
| 150932 | 151462 | sqlite3WhereSplit(pWC, pE2->pRight, op); |
| 150933 | 151463 | } |
| 150934 | 151464 | } |
| 151465 | + |
| 151466 | +/* |
| 151467 | +** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or |
| 151468 | +** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the |
| 151469 | +** where-clause passed as the first argument. The value for the term |
| 151470 | +** is found in register iReg. |
| 151471 | +** |
| 151472 | +** In the common case where the value is a simple integer |
| 151473 | +** (example: "LIMIT 5 OFFSET 10") then the expression codes as a |
| 151474 | +** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value(). |
| 151475 | +** If not, then it codes as a TK_REGISTER expression. |
| 151476 | +*/ |
| 151477 | +void whereAddLimitExpr( |
| 151478 | + WhereClause *pWC, /* Add the constraint to this WHERE clause */ |
| 151479 | + int iReg, /* Register that will hold value of the limit/offset */ |
| 151480 | + Expr *pExpr, /* Expression that defines the limit/offset */ |
| 151481 | + int iCsr, /* Cursor to which the constraint applies */ |
| 151482 | + int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */ |
| 151483 | +){ |
| 151484 | + Parse *pParse = pWC->pWInfo->pParse; |
| 151485 | + sqlite3 *db = pParse->db; |
| 151486 | + Expr *pNew; |
| 151487 | + int iVal = 0; |
| 151488 | + |
| 151489 | + if( sqlite3ExprIsInteger(pExpr, &iVal) && iVal>=0 ){ |
| 151490 | + Expr *pVal = sqlite3Expr(db, TK_INTEGER, 0); |
| 151491 | + if( pVal==0 ) return; |
| 151492 | + ExprSetProperty(pVal, EP_IntValue); |
| 151493 | + pVal->u.iValue = iVal; |
| 151494 | + pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); |
| 151495 | + }else{ |
| 151496 | + Expr *pVal = sqlite3Expr(db, TK_REGISTER, 0); |
| 151497 | + if( pVal==0 ) return; |
| 151498 | + pVal->iTable = iReg; |
| 151499 | + pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); |
| 151500 | + } |
| 151501 | + if( pNew ){ |
| 151502 | + WhereTerm *pTerm; |
| 151503 | + int idx; |
| 151504 | + idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_VIRTUAL); |
| 151505 | + pTerm = &pWC->a[idx]; |
| 151506 | + pTerm->leftCursor = iCsr; |
| 151507 | + pTerm->eOperator = WO_AUX; |
| 151508 | + pTerm->eMatchOp = eMatchOp; |
| 151509 | + } |
| 151510 | +} |
| 151511 | + |
| 151512 | +/* |
| 151513 | +** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the |
| 151514 | +** SELECT statement passed as the second argument. These terms are only |
| 151515 | +** added if: |
| 151516 | +** |
| 151517 | +** 1. The SELECT statement has a LIMIT clause, and |
| 151518 | +** 2. The SELECT statement is not an aggregate or DISTINCT query, and |
| 151519 | +** 3. The SELECT statement has exactly one object in its from clause, and |
| 151520 | +** that object is a virtual table, and |
| 151521 | +** 4. There are no terms in the WHERE clause that will not be passed |
| 151522 | +** to the virtual table xBestIndex method. |
| 151523 | +** 5. The ORDER BY clause, if any, will be made available to the xBestIndex |
| 151524 | +** method. |
| 151525 | +** |
| 151526 | +** LIMIT and OFFSET terms are ignored by most of the planner code. They |
| 151527 | +** exist only so that they may be passed to the xBestIndex method of the |
| 151528 | +** single virtual table in the FROM clause of the SELECT. |
| 151529 | +*/ |
| 151530 | +SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause *pWC, Select *p){ |
| 151531 | + assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) ); |
| 151532 | + if( (p && p->pLimit) /* 1 */ |
| 151533 | + && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */ |
| 151534 | + && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */ |
| 151535 | + ){ |
| 151536 | + ExprList *pOrderBy = p->pOrderBy; |
| 151537 | + int iCsr = p->pSrc->a[0].iCursor; |
| 151538 | + int ii; |
| 151539 | + |
| 151540 | + /* Check condition (4). Return early if it is not met. */ |
| 151541 | + for(ii=0; ii<pWC->nTerm; ii++){ |
| 151542 | + if( pWC->a[ii].wtFlags & TERM_CODED ){ |
| 151543 | + /* This term is a vector operation that has been decomposed into |
| 151544 | + ** other, subsequent terms. It can be ignored. See tag-20220128a */ |
| 151545 | + assert( pWC->a[ii].wtFlags & TERM_VIRTUAL ); |
| 151546 | + assert( pWC->a[ii].eOperator==0 ); |
| 151547 | + continue; |
| 151548 | + } |
| 151549 | + if( pWC->a[ii].leftCursor!=iCsr ) return; |
| 151550 | + } |
| 151551 | + |
| 151552 | + /* Check condition (5). Return early if it is not met. */ |
| 151553 | + if( pOrderBy ){ |
| 151554 | + for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 151555 | + Expr *pExpr = pOrderBy->a[ii].pExpr; |
| 151556 | + if( pExpr->op!=TK_COLUMN ) return; |
| 151557 | + if( pExpr->iTable!=iCsr ) return; |
| 151558 | + if( pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_BIGNULL ) return; |
| 151559 | + } |
| 151560 | + } |
| 151561 | + |
| 151562 | + /* All conditions are met. Add the terms to the where-clause object. */ |
| 151563 | + assert( p->pLimit->op==TK_LIMIT ); |
| 151564 | + whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft, |
| 151565 | + iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT); |
| 151566 | + if( p->iOffset>0 ){ |
| 151567 | + whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight, |
| 151568 | + iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET); |
| 151569 | + } |
| 151570 | + } |
| 151571 | +} |
| 150935 | 151572 | |
| 150936 | 151573 | /* |
| 150937 | 151574 | ** Initialize a preallocated WhereClause structure. |
| 150938 | 151575 | */ |
| 150939 | 151576 | SQLITE_PRIVATE void sqlite3WhereClauseInit( |
| | @@ -150966,10 +151603,11 @@ |
| 150966 | 151603 | for(i=pWC->nBase; i<pWC->nTerm; i++){ |
| 150967 | 151604 | assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 ); |
| 150968 | 151605 | } |
| 150969 | 151606 | #endif |
| 150970 | 151607 | while(1){ |
| 151608 | + assert( a->eMatchOp==0 || a->eOperator==WO_AUX ); |
| 150971 | 151609 | if( a->wtFlags & TERM_DYNAMIC ){ |
| 150972 | 151610 | sqlite3ExprDelete(db, a->pExpr); |
| 150973 | 151611 | } |
| 150974 | 151612 | if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){ |
| 150975 | 151613 | if( a->wtFlags & TERM_ORINFO ){ |
| | @@ -151123,10 +151761,11 @@ |
| 151123 | 151761 | if( pColRef==0 ) return; |
| 151124 | 151762 | pColRef->iTable = pItem->iCursor; |
| 151125 | 151763 | pColRef->iColumn = k++; |
| 151126 | 151764 | assert( ExprUseYTab(pColRef) ); |
| 151127 | 151765 | pColRef->y.pTab = pTab; |
| 151766 | + pItem->colUsed |= sqlite3ExprColUsed(pColRef); |
| 151128 | 151767 | pRhs = sqlite3PExpr(pParse, TK_UPLUS, |
| 151129 | 151768 | sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0); |
| 151130 | 151769 | pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs); |
| 151131 | 151770 | if( pItem->fg.jointype & JT_LEFT ){ |
| 151132 | 151771 | sqlite3SetJoinExpr(pTerm, pItem->iCursor); |
| | @@ -151167,12 +151806,18 @@ |
| 151167 | 151806 | ** next. As long as allocateIndexInfo() and sqlite3_vtab_collation() |
| 151168 | 151807 | ** agree on the structure, all will be well. |
| 151169 | 151808 | */ |
| 151170 | 151809 | typedef struct HiddenIndexInfo HiddenIndexInfo; |
| 151171 | 151810 | struct HiddenIndexInfo { |
| 151172 | | - WhereClause *pWC; /* The Where clause being analyzed */ |
| 151173 | | - Parse *pParse; /* The parsing context */ |
| 151811 | + WhereClause *pWC; /* The Where clause being analyzed */ |
| 151812 | + Parse *pParse; /* The parsing context */ |
| 151813 | + int eDistinct; /* Value to return from sqlite3_vtab_distinct() */ |
| 151814 | + u32 mIn; /* Mask of terms that are <col> IN (...) */ |
| 151815 | + u32 mHandleIn; /* Terms that vtab will handle as <col> IN (...) */ |
| 151816 | + sqlite3_value *aRhs[1]; /* RHS values for constraints. MUST BE LAST |
| 151817 | + ** because extra space is allocated to hold up |
| 151818 | + ** to nTerm such values */ |
| 151174 | 151819 | }; |
| 151175 | 151820 | |
| 151176 | 151821 | /* Forward declaration of methods */ |
| 151177 | 151822 | static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 151178 | 151823 | |
| | @@ -152232,31 +152877,33 @@ |
| 152232 | 152877 | |
| 152233 | 152878 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 152234 | 152879 | /* |
| 152235 | 152880 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 152236 | 152881 | ** responsibility of the caller to eventually release the structure |
| 152237 | | -** by passing the pointer returned by this function to sqlite3_free(). |
| 152882 | +** by passing the pointer returned by this function to freeIndexInfo(). |
| 152238 | 152883 | */ |
| 152239 | 152884 | static sqlite3_index_info *allocateIndexInfo( |
| 152240 | | - Parse *pParse, /* The parsing context */ |
| 152885 | + WhereInfo *pWInfo, /* The WHERE clause */ |
| 152241 | 152886 | WhereClause *pWC, /* The WHERE clause being analyzed */ |
| 152242 | 152887 | Bitmask mUnusable, /* Ignore terms with these prereqs */ |
| 152243 | 152888 | SrcItem *pSrc, /* The FROM clause term that is the vtab */ |
| 152244 | | - ExprList *pOrderBy, /* The ORDER BY clause */ |
| 152245 | 152889 | u16 *pmNoOmit /* Mask of terms not to omit */ |
| 152246 | 152890 | ){ |
| 152247 | 152891 | int i, j; |
| 152248 | 152892 | int nTerm; |
| 152893 | + Parse *pParse = pWInfo->pParse; |
| 152249 | 152894 | struct sqlite3_index_constraint *pIdxCons; |
| 152250 | 152895 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 152251 | 152896 | struct sqlite3_index_constraint_usage *pUsage; |
| 152252 | 152897 | struct HiddenIndexInfo *pHidden; |
| 152253 | 152898 | WhereTerm *pTerm; |
| 152254 | 152899 | int nOrderBy; |
| 152255 | 152900 | sqlite3_index_info *pIdxInfo; |
| 152256 | 152901 | u16 mNoOmit = 0; |
| 152257 | 152902 | const Table *pTab; |
| 152903 | + int eDistinct = 0; |
| 152904 | + ExprList *pOrderBy = pWInfo->pOrderBy; |
| 152258 | 152905 | |
| 152259 | 152906 | assert( pSrc!=0 ); |
| 152260 | 152907 | pTab = pSrc->pTab; |
| 152261 | 152908 | assert( pTab!=0 ); |
| 152262 | 152909 | assert( IsVirtual(pTab) ); |
| | @@ -152274,10 +152921,11 @@ |
| 152274 | 152921 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 152275 | 152922 | testcase( pTerm->eOperator & WO_IS ); |
| 152276 | 152923 | testcase( pTerm->eOperator & WO_ALL ); |
| 152277 | 152924 | if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue; |
| 152278 | 152925 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
| 152926 | + |
| 152279 | 152927 | assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 152280 | 152928 | assert( pTerm->u.x.leftColumn>=XN_ROWID ); |
| 152281 | 152929 | assert( pTerm->u.x.leftColumn<pTab->nCol ); |
| 152282 | 152930 | |
| 152283 | 152931 | /* tag-20191211-002: WHERE-clause constraints are not useful to the |
| | @@ -152335,40 +152983,49 @@ |
| 152335 | 152983 | } |
| 152336 | 152984 | |
| 152337 | 152985 | /* No matches cause a break out of the loop */ |
| 152338 | 152986 | break; |
| 152339 | 152987 | } |
| 152340 | | - if( i==n){ |
| 152988 | + if( i==n ){ |
| 152341 | 152989 | nOrderBy = n; |
| 152990 | + if( (pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY)) ){ |
| 152991 | + eDistinct = 1 + ((pWInfo->wctrlFlags & WHERE_DISTINCTBY)!=0); |
| 152992 | + } |
| 152342 | 152993 | } |
| 152343 | 152994 | } |
| 152344 | 152995 | |
| 152345 | 152996 | /* Allocate the sqlite3_index_info structure |
| 152346 | 152997 | */ |
| 152347 | 152998 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 152348 | 152999 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 152349 | | - + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) ); |
| 153000 | + + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) |
| 153001 | + + sizeof(sqlite3_value*)*nTerm ); |
| 152350 | 153002 | if( pIdxInfo==0 ){ |
| 152351 | 153003 | sqlite3ErrorMsg(pParse, "out of memory"); |
| 152352 | 153004 | return 0; |
| 152353 | 153005 | } |
| 152354 | 153006 | pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1]; |
| 152355 | | - pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1]; |
| 153007 | + pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm]; |
| 152356 | 153008 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 152357 | 153009 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 152358 | 153010 | pIdxInfo->aConstraint = pIdxCons; |
| 152359 | 153011 | pIdxInfo->aOrderBy = pIdxOrderBy; |
| 152360 | 153012 | pIdxInfo->aConstraintUsage = pUsage; |
| 152361 | 153013 | pHidden->pWC = pWC; |
| 152362 | 153014 | pHidden->pParse = pParse; |
| 153015 | + pHidden->eDistinct = eDistinct; |
| 153016 | + pHidden->mIn = 0; |
| 152363 | 153017 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 152364 | 153018 | u16 op; |
| 152365 | 153019 | if( (pTerm->wtFlags & TERM_OK)==0 ) continue; |
| 152366 | 153020 | pIdxCons[j].iColumn = pTerm->u.x.leftColumn; |
| 152367 | 153021 | pIdxCons[j].iTermOffset = i; |
| 152368 | 153022 | op = pTerm->eOperator & WO_ALL; |
| 152369 | | - if( op==WO_IN ) op = WO_EQ; |
| 153023 | + if( op==WO_IN ){ |
| 153024 | + pHidden->mIn |= SMASKBIT32(j); |
| 153025 | + op = WO_EQ; |
| 153026 | + } |
| 152370 | 153027 | if( op==WO_AUX ){ |
| 152371 | 153028 | pIdxCons[j].op = pTerm->eMatchOp; |
| 152372 | 153029 | }else if( op & (WO_ISNULL|WO_IS) ){ |
| 152373 | 153030 | if( op==WO_ISNULL ){ |
| 152374 | 153031 | pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL; |
| | @@ -152414,10 +153071,28 @@ |
| 152414 | 153071 | pIdxInfo->nOrderBy = j; |
| 152415 | 153072 | |
| 152416 | 153073 | *pmNoOmit = mNoOmit; |
| 152417 | 153074 | return pIdxInfo; |
| 152418 | 153075 | } |
| 153076 | + |
| 153077 | +/* |
| 153078 | +** Free an sqlite3_index_info structure allocated by allocateIndexInfo() |
| 153079 | +** and possibly modified by xBestIndex methods. |
| 153080 | +*/ |
| 153081 | +static void freeIndexInfo(sqlite3 *db, sqlite3_index_info *pIdxInfo){ |
| 153082 | + HiddenIndexInfo *pHidden; |
| 153083 | + int i; |
| 153084 | + assert( pIdxInfo!=0 ); |
| 153085 | + pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; |
| 153086 | + assert( pHidden->pParse!=0 ); |
| 153087 | + assert( pHidden->pParse->db==db ); |
| 153088 | + for(i=0; i<pIdxInfo->nConstraint; i++){ |
| 153089 | + sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */ |
| 153090 | + pHidden->aRhs[i] = 0; |
| 153091 | + } |
| 153092 | + sqlite3DbFree(db, pIdxInfo); |
| 153093 | +} |
| 152419 | 153094 | |
| 152420 | 153095 | /* |
| 152421 | 153096 | ** The table object reference passed as the second argument to this function |
| 152422 | 153097 | ** must represent a virtual table. This function invokes the xBestIndex() |
| 152423 | 153098 | ** method of the virtual table with the sqlite3_index_info object that |
| | @@ -152436,11 +153111,13 @@ |
| 152436 | 153111 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
| 152437 | 153112 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
| 152438 | 153113 | int rc; |
| 152439 | 153114 | |
| 152440 | 153115 | whereTraceIndexInfoInputs(p); |
| 153116 | + pParse->db->nSchemaLock++; |
| 152441 | 153117 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 153118 | + pParse->db->nSchemaLock--; |
| 152442 | 153119 | whereTraceIndexInfoOutputs(p); |
| 152443 | 153120 | |
| 152444 | 153121 | if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){ |
| 152445 | 153122 | if( rc==SQLITE_NOMEM ){ |
| 152446 | 153123 | sqlite3OomFault(pParse->db); |
| | @@ -154566,10 +155243,19 @@ |
| 154566 | 155243 | } |
| 154567 | 155244 | return rc; |
| 154568 | 155245 | } |
| 154569 | 155246 | |
| 154570 | 155247 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 155248 | + |
| 155249 | +/* |
| 155250 | +** Return true if pTerm is a virtual table LIMIT or OFFSET term. |
| 155251 | +*/ |
| 155252 | +static int isLimitTerm(WhereTerm *pTerm){ |
| 155253 | + assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 ); |
| 155254 | + return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT |
| 155255 | + && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET; |
| 155256 | +} |
| 154571 | 155257 | |
| 154572 | 155258 | /* |
| 154573 | 155259 | ** Argument pIdxInfo is already populated with all constraints that may |
| 154574 | 155260 | ** be used by the virtual table identified by pBuilder->pNew->iTab. This |
| 154575 | 155261 | ** function marks a subset of those constraints usable, invokes the |
| | @@ -154594,13 +155280,15 @@ |
| 154594 | 155280 | Bitmask mPrereq, /* Mask of tables that must be used. */ |
| 154595 | 155281 | Bitmask mUsable, /* Mask of usable tables */ |
| 154596 | 155282 | u16 mExclude, /* Exclude terms using these operators */ |
| 154597 | 155283 | sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */ |
| 154598 | 155284 | u16 mNoOmit, /* Do not omit these constraints */ |
| 154599 | | - int *pbIn /* OUT: True if plan uses an IN(...) op */ |
| 155285 | + int *pbIn, /* OUT: True if plan uses an IN(...) op */ |
| 155286 | + int *pbRetryLimit /* OUT: Retry without LIMIT/OFFSET */ |
| 154600 | 155287 | ){ |
| 154601 | 155288 | WhereClause *pWC = pBuilder->pWC; |
| 155289 | + HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; |
| 154602 | 155290 | struct sqlite3_index_constraint *pIdxCons; |
| 154603 | 155291 | struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage; |
| 154604 | 155292 | int i; |
| 154605 | 155293 | int mxTerm; |
| 154606 | 155294 | int rc = SQLITE_OK; |
| | @@ -154619,10 +155307,11 @@ |
| 154619 | 155307 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 154620 | 155308 | WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset]; |
| 154621 | 155309 | pIdxCons->usable = 0; |
| 154622 | 155310 | if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight |
| 154623 | 155311 | && (pTerm->eOperator & mExclude)==0 |
| 155312 | + && (pbRetryLimit || !isLimitTerm(pTerm)) |
| 154624 | 155313 | ){ |
| 154625 | 155314 | pIdxCons->usable = 1; |
| 154626 | 155315 | } |
| 154627 | 155316 | } |
| 154628 | 155317 | |
| | @@ -154634,10 +155323,11 @@ |
| 154634 | 155323 | pIdxInfo->orderByConsumed = 0; |
| 154635 | 155324 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
| 154636 | 155325 | pIdxInfo->estimatedRows = 25; |
| 154637 | 155326 | pIdxInfo->idxFlags = 0; |
| 154638 | 155327 | pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed; |
| 155328 | + pHidden->mHandleIn = 0; |
| 154639 | 155329 | |
| 154640 | 155330 | /* Invoke the virtual table xBestIndex() method */ |
| 154641 | 155331 | rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo); |
| 154642 | 155332 | if( rc ){ |
| 154643 | 155333 | if( rc==SQLITE_CONSTRAINT ){ |
| | @@ -154651,12 +155341,12 @@ |
| 154651 | 155341 | return rc; |
| 154652 | 155342 | } |
| 154653 | 155343 | |
| 154654 | 155344 | mxTerm = -1; |
| 154655 | 155345 | assert( pNew->nLSlot>=nConstraint ); |
| 154656 | | - for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
| 154657 | | - pNew->u.vtab.omitMask = 0; |
| 155346 | + memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint ); |
| 155347 | + memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab)); |
| 154658 | 155348 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 154659 | 155349 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 154660 | 155350 | int iTerm; |
| 154661 | 155351 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 154662 | 155352 | WhereTerm *pTerm; |
| | @@ -154686,21 +155376,41 @@ |
| 154686 | 155376 | testcase( i!=iTerm ); |
| 154687 | 155377 | pNew->u.vtab.omitMask |= 1<<iTerm; |
| 154688 | 155378 | }else{ |
| 154689 | 155379 | testcase( i!=iTerm ); |
| 154690 | 155380 | } |
| 155381 | + if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){ |
| 155382 | + pNew->u.vtab.bOmitOffset = 1; |
| 155383 | + } |
| 154691 | 155384 | } |
| 154692 | | - if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 155385 | + if( SMASKBIT32(i) & pHidden->mHandleIn ){ |
| 155386 | + pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm); |
| 155387 | + }else if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 154693 | 155388 | /* A virtual table that is constrained by an IN clause may not |
| 154694 | 155389 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 154695 | 155390 | ** is not necessarily related to the order of output terms and |
| 154696 | 155391 | ** (2) Multiple outputs from a single IN value will not merge |
| 154697 | 155392 | ** together. */ |
| 154698 | 155393 | pIdxInfo->orderByConsumed = 0; |
| 154699 | 155394 | pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE; |
| 154700 | 155395 | *pbIn = 1; assert( (mExclude & WO_IN)==0 ); |
| 154701 | 155396 | } |
| 155397 | + |
| 155398 | + if( isLimitTerm(pTerm) && *pbIn ){ |
| 155399 | + /* If there is an IN(...) term handled as an == (separate call to |
| 155400 | + ** xFilter for each value on the RHS of the IN) and a LIMIT or |
| 155401 | + ** OFFSET term handled as well, the plan is unusable. Set output |
| 155402 | + ** variable *pbRetryLimit to true to tell the caller to retry with |
| 155403 | + ** LIMIT and OFFSET disabled. */ |
| 155404 | + if( pIdxInfo->needToFreeIdxStr ){ |
| 155405 | + sqlite3_free(pIdxInfo->idxStr); |
| 155406 | + pIdxInfo->idxStr = 0; |
| 155407 | + pIdxInfo->needToFreeIdxStr = 0; |
| 155408 | + } |
| 155409 | + *pbRetryLimit = 1; |
| 155410 | + return SQLITE_OK; |
| 155411 | + } |
| 154702 | 155412 | } |
| 154703 | 155413 | } |
| 154704 | 155414 | |
| 154705 | 155415 | pNew->nLTerm = mxTerm+1; |
| 154706 | 155416 | for(i=0; i<=mxTerm; i++){ |
| | @@ -154769,10 +155479,77 @@ |
| 154769 | 155479 | } |
| 154770 | 155480 | zRet = (pC ? pC->zName : sqlite3StrBINARY); |
| 154771 | 155481 | } |
| 154772 | 155482 | return zRet; |
| 154773 | 155483 | } |
| 155484 | + |
| 155485 | +/* |
| 155486 | +** Return true if constraint iCons is really an IN(...) constraint, or |
| 155487 | +** false otherwise. If iCons is an IN(...) constraint, set (if bHandle!=0) |
| 155488 | +** or clear (if bHandle==0) the flag to handle it using an iterator. |
| 155489 | +*/ |
| 155490 | +SQLITE_API int sqlite3_vtab_in(sqlite3_index_info *pIdxInfo, int iCons, int bHandle){ |
| 155491 | + HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; |
| 155492 | + u32 m = SMASKBIT32(iCons); |
| 155493 | + if( m & pHidden->mIn ){ |
| 155494 | + if( bHandle==0 ){ |
| 155495 | + pHidden->mHandleIn &= ~m; |
| 155496 | + }else if( bHandle>0 ){ |
| 155497 | + pHidden->mHandleIn |= m; |
| 155498 | + } |
| 155499 | + return 1; |
| 155500 | + } |
| 155501 | + return 0; |
| 155502 | +} |
| 155503 | + |
| 155504 | +/* |
| 155505 | +** This interface is callable from within the xBestIndex callback only. |
| 155506 | +** |
| 155507 | +** If possible, set (*ppVal) to point to an object containing the value |
| 155508 | +** on the right-hand-side of constraint iCons. |
| 155509 | +*/ |
| 155510 | +SQLITE_API int sqlite3_vtab_rhs_value( |
| 155511 | + sqlite3_index_info *pIdxInfo, /* Copy of first argument to xBestIndex */ |
| 155512 | + int iCons, /* Constraint for which RHS is wanted */ |
| 155513 | + sqlite3_value **ppVal /* Write value extracted here */ |
| 155514 | +){ |
| 155515 | + HiddenIndexInfo *pH = (HiddenIndexInfo*)&pIdxInfo[1]; |
| 155516 | + sqlite3_value *pVal = 0; |
| 155517 | + int rc = SQLITE_OK; |
| 155518 | + if( iCons<0 || iCons>=pIdxInfo->nConstraint ){ |
| 155519 | + rc = SQLITE_MISUSE; /* EV: R-30545-25046 */ |
| 155520 | + }else{ |
| 155521 | + if( pH->aRhs[iCons]==0 ){ |
| 155522 | + WhereTerm *pTerm = &pH->pWC->a[pIdxInfo->aConstraint[iCons].iTermOffset]; |
| 155523 | + rc = sqlite3ValueFromExpr( |
| 155524 | + pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db), |
| 155525 | + SQLITE_AFF_BLOB, &pH->aRhs[iCons] |
| 155526 | + ); |
| 155527 | + testcase( rc!=SQLITE_OK ); |
| 155528 | + } |
| 155529 | + pVal = pH->aRhs[iCons]; |
| 155530 | + } |
| 155531 | + *ppVal = pVal; |
| 155532 | + |
| 155533 | + if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */ |
| 155534 | + rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */ |
| 155535 | + } |
| 155536 | + |
| 155537 | + return rc; |
| 155538 | +} |
| 155539 | + |
| 155540 | + |
| 155541 | +/* |
| 155542 | +** Return true if ORDER BY clause may be handled as DISTINCT. |
| 155543 | +*/ |
| 155544 | +SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info *pIdxInfo){ |
| 155545 | + HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; |
| 155546 | + assert( pHidden->eDistinct==0 |
| 155547 | + || pHidden->eDistinct==1 |
| 155548 | + || pHidden->eDistinct==2 ); |
| 155549 | + return pHidden->eDistinct; |
| 155550 | +} |
| 154774 | 155551 | |
| 154775 | 155552 | /* |
| 154776 | 155553 | ** Add all WhereLoop objects for a table of the join identified by |
| 154777 | 155554 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
| 154778 | 155555 | ** |
| | @@ -154811,35 +155588,43 @@ |
| 154811 | 155588 | int nConstraint; /* Number of constraints in p */ |
| 154812 | 155589 | int bIn; /* True if plan uses IN(...) operator */ |
| 154813 | 155590 | WhereLoop *pNew; |
| 154814 | 155591 | Bitmask mBest; /* Tables used by best possible plan */ |
| 154815 | 155592 | u16 mNoOmit; |
| 155593 | + int bRetry = 0; /* True to retry with LIMIT/OFFSET disabled */ |
| 154816 | 155594 | |
| 154817 | 155595 | assert( (mPrereq & mUnusable)==0 ); |
| 154818 | 155596 | pWInfo = pBuilder->pWInfo; |
| 154819 | 155597 | pParse = pWInfo->pParse; |
| 154820 | 155598 | pWC = pBuilder->pWC; |
| 154821 | 155599 | pNew = pBuilder->pNew; |
| 154822 | 155600 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
| 154823 | 155601 | assert( IsVirtual(pSrc->pTab) ); |
| 154824 | | - p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy, |
| 154825 | | - &mNoOmit); |
| 155602 | + p = allocateIndexInfo(pWInfo, pWC, mUnusable, pSrc, &mNoOmit); |
| 154826 | 155603 | if( p==0 ) return SQLITE_NOMEM_BKPT; |
| 154827 | 155604 | pNew->rSetup = 0; |
| 154828 | 155605 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
| 154829 | 155606 | pNew->nLTerm = 0; |
| 154830 | 155607 | pNew->u.vtab.needFree = 0; |
| 154831 | 155608 | nConstraint = p->nConstraint; |
| 154832 | 155609 | if( whereLoopResize(pParse->db, pNew, nConstraint) ){ |
| 154833 | | - sqlite3DbFree(pParse->db, p); |
| 155610 | + freeIndexInfo(pParse->db, p); |
| 154834 | 155611 | return SQLITE_NOMEM_BKPT; |
| 154835 | 155612 | } |
| 154836 | 155613 | |
| 154837 | 155614 | /* First call xBestIndex() with all constraints usable. */ |
| 154838 | 155615 | WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName)); |
| 154839 | 155616 | WHERETRACE(0x40, (" VirtualOne: all usable\n")); |
| 154840 | | - rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn); |
| 155617 | + rc = whereLoopAddVirtualOne( |
| 155618 | + pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, &bRetry |
| 155619 | + ); |
| 155620 | + if( bRetry ){ |
| 155621 | + assert( rc==SQLITE_OK ); |
| 155622 | + rc = whereLoopAddVirtualOne( |
| 155623 | + pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, 0 |
| 155624 | + ); |
| 155625 | + } |
| 154841 | 155626 | |
| 154842 | 155627 | /* If the call to xBestIndex() with all terms enabled produced a plan |
| 154843 | 155628 | ** that does not require any source tables (IOW: a plan with mBest==0) |
| 154844 | 155629 | ** and does not use an IN(...) operator, then there is no point in making |
| 154845 | 155630 | ** any further calls to xBestIndex() since they will all return the same |
| | @@ -154853,11 +155638,11 @@ |
| 154853 | 155638 | /* If the plan produced by the earlier call uses an IN(...) term, call |
| 154854 | 155639 | ** xBestIndex again, this time with IN(...) terms disabled. */ |
| 154855 | 155640 | if( bIn ){ |
| 154856 | 155641 | WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n")); |
| 154857 | 155642 | rc = whereLoopAddVirtualOne( |
| 154858 | | - pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn); |
| 155643 | + pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn, 0); |
| 154859 | 155644 | assert( bIn==0 ); |
| 154860 | 155645 | mBestNoIn = pNew->prereq & ~mPrereq; |
| 154861 | 155646 | if( mBestNoIn==0 ){ |
| 154862 | 155647 | seenZero = 1; |
| 154863 | 155648 | seenZeroNoIN = 1; |
| | @@ -154880,11 +155665,11 @@ |
| 154880 | 155665 | if( mNext==ALLBITS ) break; |
| 154881 | 155666 | if( mNext==mBest || mNext==mBestNoIn ) continue; |
| 154882 | 155667 | WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n", |
| 154883 | 155668 | (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext)); |
| 154884 | 155669 | rc = whereLoopAddVirtualOne( |
| 154885 | | - pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn); |
| 155670 | + pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0); |
| 154886 | 155671 | if( pNew->prereq==mPrereq ){ |
| 154887 | 155672 | seenZero = 1; |
| 154888 | 155673 | if( bIn==0 ) seenZeroNoIN = 1; |
| 154889 | 155674 | } |
| 154890 | 155675 | } |
| | @@ -154893,26 +155678,26 @@ |
| 154893 | 155678 | ** that requires no source tables at all (i.e. one guaranteed to be |
| 154894 | 155679 | ** usable), make a call here with all source tables disabled */ |
| 154895 | 155680 | if( rc==SQLITE_OK && seenZero==0 ){ |
| 154896 | 155681 | WHERETRACE(0x40, (" VirtualOne: all disabled\n")); |
| 154897 | 155682 | rc = whereLoopAddVirtualOne( |
| 154898 | | - pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn); |
| 155683 | + pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0); |
| 154899 | 155684 | if( bIn==0 ) seenZeroNoIN = 1; |
| 154900 | 155685 | } |
| 154901 | 155686 | |
| 154902 | 155687 | /* If the calls to xBestIndex() have so far failed to find a plan |
| 154903 | 155688 | ** that requires no source tables at all and does not use an IN(...) |
| 154904 | 155689 | ** operator, make a final call to obtain one here. */ |
| 154905 | 155690 | if( rc==SQLITE_OK && seenZeroNoIN==0 ){ |
| 154906 | 155691 | WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n")); |
| 154907 | 155692 | rc = whereLoopAddVirtualOne( |
| 154908 | | - pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn); |
| 155693 | + pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0); |
| 154909 | 155694 | } |
| 154910 | 155695 | } |
| 154911 | 155696 | |
| 154912 | 155697 | if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr); |
| 154913 | | - sqlite3DbFreeNN(pParse->db, p); |
| 155698 | + freeIndexInfo(pParse->db, p); |
| 154914 | 155699 | WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc)); |
| 154915 | 155700 | return rc; |
| 154916 | 155701 | } |
| 154917 | 155702 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 154918 | 155703 | |
| | @@ -154952,11 +155737,10 @@ |
| 154952 | 155737 | WhereTerm *pOrTerm; |
| 154953 | 155738 | int once = 1; |
| 154954 | 155739 | int i, j; |
| 154955 | 155740 | |
| 154956 | 155741 | sSubBuild = *pBuilder; |
| 154957 | | - sSubBuild.pOrderBy = 0; |
| 154958 | 155742 | sSubBuild.pOrSet = &sCur; |
| 154959 | 155743 | |
| 154960 | 155744 | WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm)); |
| 154961 | 155745 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
| 154962 | 155746 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
| | @@ -156310,10 +157094,11 @@ |
| 156310 | 157094 | Parse *pParse, /* The parser context */ |
| 156311 | 157095 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
| 156312 | 157096 | Expr *pWhere, /* The WHERE clause */ |
| 156313 | 157097 | ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ |
| 156314 | 157098 | ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */ |
| 157099 | + Select *pLimit, /* Use this LIMIT/OFFSET clause, if any */ |
| 156315 | 157100 | u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */ |
| 156316 | 157101 | int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number |
| 156317 | 157102 | ** If WHERE_USE_LIMIT, then the limit amount */ |
| 156318 | 157103 | ){ |
| 156319 | 157104 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
| | @@ -156344,11 +157129,10 @@ |
| 156344 | 157129 | memset(&sWLB, 0, sizeof(sWLB)); |
| 156345 | 157130 | |
| 156346 | 157131 | /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ |
| 156347 | 157132 | testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); |
| 156348 | 157133 | if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; |
| 156349 | | - sWLB.pOrderBy = pOrderBy; |
| 156350 | 157134 | |
| 156351 | 157135 | /* The number of tables in the FROM clause is limited by the number of |
| 156352 | 157136 | ** bits in a Bitmask |
| 156353 | 157137 | */ |
| 156354 | 157138 | testcase( pTabList->nSrc==BMS ); |
| | @@ -156387,10 +157171,13 @@ |
| 156387 | 157171 | pWInfo->nLevel = nTabList; |
| 156388 | 157172 | pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse); |
| 156389 | 157173 | pWInfo->wctrlFlags = wctrlFlags; |
| 156390 | 157174 | pWInfo->iLimit = iAuxArg; |
| 156391 | 157175 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 157176 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 157177 | + pWInfo->pLimit = pLimit; |
| 157178 | +#endif |
| 156392 | 157179 | memset(&pWInfo->nOBSat, 0, |
| 156393 | 157180 | offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat)); |
| 156394 | 157181 | memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel)); |
| 156395 | 157182 | assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */ |
| 156396 | 157183 | pMaskSet = &pWInfo->sMaskSet; |
| | @@ -156455,10 +157242,11 @@ |
| 156455 | 157242 | #endif |
| 156456 | 157243 | } |
| 156457 | 157244 | |
| 156458 | 157245 | /* Analyze all of the subexpressions. */ |
| 156459 | 157246 | sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); |
| 157247 | + sqlite3WhereAddLimit(&pWInfo->sWC, pLimit); |
| 156460 | 157248 | if( db->mallocFailed ) goto whereBeginError; |
| 156461 | 157249 | |
| 156462 | 157250 | /* Special case: WHERE terms that do not refer to any tables in the join |
| 156463 | 157251 | ** (constant expressions). Evaluate each such term, and jump over all the |
| 156464 | 157252 | ** generated code if the result is not true. |
| | @@ -156554,13 +157342,14 @@ |
| 156554 | 157342 | } |
| 156555 | 157343 | } |
| 156556 | 157344 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
| 156557 | 157345 | pWInfo->revMask = ALLBITS; |
| 156558 | 157346 | } |
| 156559 | | - if( pParse->nErr || db->mallocFailed ){ |
| 157347 | + if( pParse->nErr ){ |
| 156560 | 157348 | goto whereBeginError; |
| 156561 | 157349 | } |
| 157350 | + assert( db->mallocFailed==0 ); |
| 156562 | 157351 | #ifdef WHERETRACE_ENABLED |
| 156563 | 157352 | if( sqlite3WhereTrace ){ |
| 156564 | 157353 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 156565 | 157354 | if( pWInfo->nOBSat>0 ){ |
| 156566 | 157355 | sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); |
| | @@ -156700,10 +157489,11 @@ |
| 156700 | 157489 | testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 ); |
| 156701 | 157490 | testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS ); |
| 156702 | 157491 | if( pWInfo->eOnePass==ONEPASS_OFF |
| 156703 | 157492 | && pTab->nCol<BMS |
| 156704 | 157493 | && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0 |
| 157494 | + && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0 |
| 156705 | 157495 | ){ |
| 156706 | 157496 | /* If we know that only a prefix of the record will be used, |
| 156707 | 157497 | ** it is advantageous to reduce the "column count" field in |
| 156708 | 157498 | ** the P4 operand of the OP_OpenRead/Write opcode. */ |
| 156709 | 157499 | Bitmask b = pTabItem->colUsed; |
| | @@ -158264,16 +159054,11 @@ |
| 158264 | 159054 | ** there could still be references to that table embedded in the |
| 158265 | 159055 | ** result-set or ORDER BY clause of the SELECT statement p. */ |
| 158266 | 159056 | sqlite3ParserAddCleanup(pParse, sqlite3DbFree, pTab); |
| 158267 | 159057 | } |
| 158268 | 159058 | |
| 158269 | | - if( rc ){ |
| 158270 | | - if( pParse->nErr==0 ){ |
| 158271 | | - assert( pParse->db->mallocFailed ); |
| 158272 | | - sqlite3ErrorToParser(pParse->db, SQLITE_NOMEM); |
| 158273 | | - } |
| 158274 | | - } |
| 159059 | + assert( rc==SQLITE_OK || pParse->nErr!=0 ); |
| 158275 | 159060 | return rc; |
| 158276 | 159061 | } |
| 158277 | 159062 | |
| 158278 | 159063 | /* |
| 158279 | 159064 | ** Unlink the Window object from the Select to which it is attached, |
| | @@ -193955,12 +194740,12 @@ |
| 193955 | 194740 | sqlite3_result_error_nomem(ctx); |
| 193956 | 194741 | goto jsonSetDone; |
| 193957 | 194742 | }else if( x.nErr ){ |
| 193958 | 194743 | goto jsonSetDone; |
| 193959 | 194744 | }else if( pNode && (bApnd || bIsSet) ){ |
| 193960 | | - testcase( pNode->eU!=0 && pNode->eU!=1 && pNode->eU!=4 ); |
| 193961 | | - assert( pNode->eU!=3 || pNode->eU!=5 ); |
| 194745 | + testcase( pNode->eU!=0 && pNode->eU!=1 ); |
| 194746 | + assert( pNode->eU!=3 && pNode->eU!=5 ); |
| 193962 | 194747 | VVA( pNode->eU = 4 ); |
| 193963 | 194748 | pNode->jnFlags |= (u8)JNODE_REPLACE; |
| 193964 | 194749 | pNode->u.iReplace = i + 1; |
| 193965 | 194750 | } |
| 193966 | 194751 | } |
| | @@ -194737,11 +195522,11 @@ |
| 194737 | 195522 | sqlite3_module *pModule; |
| 194738 | 195523 | } aMod[] = { |
| 194739 | 195524 | { "json_each", &jsonEachModule }, |
| 194740 | 195525 | { "json_tree", &jsonTreeModule }, |
| 194741 | 195526 | }; |
| 194742 | | - int i; |
| 195527 | + unsigned int i; |
| 194743 | 195528 | for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){ |
| 194744 | 195529 | rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0); |
| 194745 | 195530 | } |
| 194746 | 195531 | return rc; |
| 194747 | 195532 | } |
| | @@ -233334,11 +234119,11 @@ |
| 233334 | 234119 | int nArg, /* Number of args */ |
| 233335 | 234120 | sqlite3_value **apUnused /* Function arguments */ |
| 233336 | 234121 | ){ |
| 233337 | 234122 | assert( nArg==0 ); |
| 233338 | 234123 | UNUSED_PARAM2(nArg, apUnused); |
| 233339 | | - sqlite3_result_text(pCtx, "fts5: 2022-01-12 00:28:12 adebb9d7478d092f16fb0ef7d5246ce152b166479d6f949110b5878b89ea2cec", -1, SQLITE_TRANSIENT); |
| 234124 | + sqlite3_result_text(pCtx, "fts5: 2022-01-25 16:28:57 6e4154d414afe2562b488149b10c175d1f15bd1d5060ee479d5ae9386a2e277e", -1, SQLITE_TRANSIENT); |
| 233340 | 234125 | } |
| 233341 | 234126 | |
| 233342 | 234127 | /* |
| 233343 | 234128 | ** Return true if zName is the extension on one of the shadow tables used |
| 233344 | 234129 | ** by this module. |
| 233345 | 234130 | |