| | @@ -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-25 00:03:25 a8db69411b0d1275909adeb21027784ada17af24efe3a59ae0ae2a897659ff17" |
| 457 | +#define SQLITE_SOURCE_ID "2022-01-31 14:14:29 539cef5214446a7181614793e9cf323e95ba00ba0f888585b14b598dd2ff0808" |
| 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 | ** |
| | @@ -9812,28 +9846,29 @@ |
| 9812 | 9846 | |
| 9813 | 9847 | /* |
| 9814 | 9848 | ** CAPI3REF: Determine if a virtual table query is DISTINCT |
| 9815 | 9849 | ** METHOD: sqlite3_index_info |
| 9816 | 9850 | ** |
| 9817 | | -** This API may only be used from within an xBestIndex() callback. The |
| 9818 | | -** results of calling it from outside of an xBestIndex() callback are |
| 9819 | | -** undefined and probably harmful. |
| 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. |
| 9820 | 9854 | ** |
| 9821 | | -** ^The sqlite3_vtab_distinct() returns an integer that is either 0, 1, or |
| 9822 | | -** 2. The integer returned by sqlite3_vtab_distinct() gives the virtual table |
| 9823 | | -** additional information about how the query planner wants the output to be |
| 9824 | | -** ordered. As long as the virtual table can meet the ordering requirements |
| 9825 | | -** of the query planner, it may set the "orderByConsumed" flag. |
| 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. |
| 9826 | 9861 | ** |
| 9827 | 9862 | ** <ol><li value="0"><p> |
| 9828 | 9863 | ** ^If the sqlite3_vtab_distinct() interface returns 0, that means |
| 9829 | 9864 | ** that the query planner needs the virtual table to return all rows in the |
| 9830 | 9865 | ** sort order defined by the "nOrderBy" and "aOrderBy" fields of the |
| 9831 | 9866 | ** [sqlite3_index_info] object. This is the default expectation. If the |
| 9832 | 9867 | ** virtual table outputs all rows in sorted order, then it is always safe for |
| 9833 | 9868 | ** the xBestIndex method to set the "orderByConsumed" flag, regardless of |
| 9834 | | -** what the return value from sqlite3_vtab_distinct(). |
| 9869 | +** the return value from sqlite3_vtab_distinct(). |
| 9835 | 9870 | ** <li value="1"><p> |
| 9836 | 9871 | ** ^(If the sqlite3_vtab_distinct() interface returns 1, that means |
| 9837 | 9872 | ** that the query planner does not need the rows to be returned in sorted order |
| 9838 | 9873 | ** as long as all rows with the same values in all columns identified by the |
| 9839 | 9874 | ** "aOrderBy" field are adjacent.)^ This mode is used when the query planner |
| | @@ -9842,11 +9877,11 @@ |
| 9842 | 9877 | ** ^(If the sqlite3_vtab_distinct() interface returns 2, that means |
| 9843 | 9878 | ** that the query planner does not need the rows returned in any particular |
| 9844 | 9879 | ** order, as long as rows with the same values in all "aOrderBy" columns |
| 9845 | 9880 | ** are adjacent.)^ ^(Furthermore, only a single row for each particular |
| 9846 | 9881 | ** combination of values in the columns identified by the "aOrderBy" field |
| 9847 | | -** needs to be returned.)^ ^It is ok always for two or more rows with the same |
| 9882 | +** needs to be returned.)^ ^It is always ok for two or more rows with the same |
| 9848 | 9883 | ** values in all "aOrderBy" columns to be returned, as long as all such rows |
| 9849 | 9884 | ** are adjacent. ^The virtual table may, if it chooses, omit extra rows |
| 9850 | 9885 | ** that have the same value for all columns identified by "aOrderBy". |
| 9851 | 9886 | ** ^However omitting the extra rows is optional. |
| 9852 | 9887 | ** This mode is used for a DISTINCT query. |
| | @@ -9877,31 +9912,45 @@ |
| 9877 | 9912 | |
| 9878 | 9913 | /* |
| 9879 | 9914 | ** CAPI3REF: Constraint values in xBestIndex() |
| 9880 | 9915 | ** METHOD: sqlite3_index_info |
| 9881 | 9916 | ** |
| 9882 | | -** This API may only be used from within an xBestIndex() callback. The |
| 9883 | | -** results of calling it from outside of an xBestIndex() callback are |
| 9884 | | -** undefined and probably harmful. |
| 9917 | +** This API may only be used from within the [xBestIndex|xBestIndex method] |
| 9918 | +** of a [virtual table] implementation. The result of calling this interface |
| 9919 | +** from outside of an xBestIndex method are undefined and probably harmful. |
| 9885 | 9920 | ** |
| 9886 | 9921 | ** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within |
| 9887 | 9922 | ** the [xBestIndex] method of a [virtual table] implementation, with P being |
| 9888 | 9923 | ** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and |
| 9889 | 9924 | ** J being a 0-based index into P->aConstraint[], then this routine |
| 9890 | | -** attempts to set *V to be the value on the right-hand side of |
| 9891 | | -** that constraint if the right-hand side is a known constant. ^If the |
| 9892 | | -** right-hand side of the constraint is not known, then *V is set to a NULL |
| 9893 | | -** pointer. ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if |
| 9925 | +** attempts to set *V to the value of the right-hand operand of |
| 9926 | +** that constraint if the right-hand operand is known. ^If the |
| 9927 | +** right-hand operand is not known, then *V is set to a NULL pointer. |
| 9928 | +** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if |
| 9894 | 9929 | ** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V) |
| 9895 | 9930 | ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th |
| 9896 | 9931 | ** constraint is not available. ^The sqlite3_vtab_rhs_value() interface |
| 9897 | 9932 | ** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if |
| 9898 | 9933 | ** something goes wrong. |
| 9899 | 9934 | ** |
| 9900 | | -** ^The sqlite3_value object returned in *V remains valid for the duration of |
| 9901 | | -** the xBestIndex method code. ^When xBestIndex returns, the sqlite3_value |
| 9902 | | -** object returned by sqlite3_vtab_rhs_value() is automatically deallocated. |
| 9935 | +** The sqlite3_vtab_rhs_value() interface is usually only successful if |
| 9936 | +** the right-hand operand of a constraint is a literal value in the original |
| 9937 | +** SQL statement. If the right-hand operand is an expression or a reference |
| 9938 | +** to some other column or a [host parameter], then sqlite3_vtab_rhs_value() |
| 9939 | +** will probably return [SQLITE_NOTFOUND]. |
| 9940 | +** |
| 9941 | +** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and |
| 9942 | +** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such |
| 9943 | +** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^ |
| 9944 | +** |
| 9945 | +** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value |
| 9946 | +** and remains valid for the duration of the xBestIndex method call. |
| 9947 | +** ^When xBestIndex returns, the sqlite3_value object returned by |
| 9948 | +** sqlite3_vtab_rhs_value() is automatically deallocated. |
| 9949 | +** |
| 9950 | +** The "_rhs_" in the name of this routine is an appreviation for |
| 9951 | +** "Right-Hand Side". |
| 9903 | 9952 | */ |
| 9904 | 9953 | SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal); |
| 9905 | 9954 | |
| 9906 | 9955 | /* |
| 9907 | 9956 | ** CAPI3REF: Conflict resolution modes |
| | @@ -19662,11 +19711,12 @@ |
| 19662 | 19711 | #endif |
| 19663 | 19712 | SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe*,int,const char*); |
| 19664 | 19713 | SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*); |
| 19665 | 19714 | SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*, |
| 19666 | 19715 | Upsert*); |
| 19667 | | -SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int); |
| 19716 | +SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*, |
| 19717 | + ExprList*,Select*,u16,int); |
| 19668 | 19718 | SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*); |
| 19669 | 19719 | SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*); |
| 19670 | 19720 | SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*); |
| 19671 | 19721 | SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*); |
| 19672 | 19722 | SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*); |
| | @@ -23545,11 +23595,11 @@ |
| 23545 | 23595 | ** is not the first modifier, or if the prior argument is not a numeric |
| 23546 | 23596 | ** value in the allowed range of julian day numbers understood by |
| 23547 | 23597 | ** SQLite (0..5373484.5) then the result will be NULL. |
| 23548 | 23598 | */ |
| 23549 | 23599 | if( sqlite3_stricmp(z, "julianday")==0 ){ |
| 23550 | | - if( idx>1 ) return 1; |
| 23600 | + if( idx>1 ) return 1; /* IMP: R-31176-64601 */ |
| 23551 | 23601 | if( p->validJD && p->rawS ){ |
| 23552 | 23602 | rc = 0; |
| 23553 | 23603 | p->rawS = 0; |
| 23554 | 23604 | } |
| 23555 | 23605 | } |
| | @@ -23576,10 +23626,11 @@ |
| 23576 | 23626 | ** |
| 23577 | 23627 | ** Treat the current value of p->s as the number of |
| 23578 | 23628 | ** seconds since 1970. Convert to a real julian day number. |
| 23579 | 23629 | */ |
| 23580 | 23630 | if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ |
| 23631 | + if( idx>1 ) return 1; /* IMP: R-49255-55373 */ |
| 23581 | 23632 | r = p->s*1000.0 + 210866760000000.0; |
| 23582 | 23633 | if( r>=0.0 && r<464269060800000.0 ){ |
| 23583 | 23634 | clearYMD_HMS_TZ(p); |
| 23584 | 23635 | p->iJD = (sqlite3_int64)(r + 0.5); |
| 23585 | 23636 | p->validJD = 1; |
| | @@ -76814,11 +76865,11 @@ |
| 76814 | 76865 | int i = sqlite3FindDbName(pDb, zDb); |
| 76815 | 76866 | |
| 76816 | 76867 | if( i==1 ){ |
| 76817 | 76868 | Parse sParse; |
| 76818 | 76869 | int rc = 0; |
| 76819 | | - sqlite3ParseObjectInit(&sParse,pErrorDb); |
| 76870 | + sqlite3ParseObjectInit(&sParse,pDb); |
| 76820 | 76871 | if( sqlite3OpenTempDatabase(&sParse) ){ |
| 76821 | 76872 | sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg); |
| 76822 | 76873 | rc = SQLITE_ERROR; |
| 76823 | 76874 | } |
| 76824 | 76875 | sqlite3DbFree(pErrorDb, sParse.zErrMsg); |
| | @@ -79032,15 +79083,11 @@ |
| 79032 | 79083 | const char *zNeg = ""; |
| 79033 | 79084 | int rc = SQLITE_OK; |
| 79034 | 79085 | |
| 79035 | 79086 | assert( pExpr!=0 ); |
| 79036 | 79087 | while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft; |
| 79037 | | -#if defined(SQLITE_ENABLE_STAT4) |
| 79038 | 79088 | if( op==TK_REGISTER ) op = pExpr->op2; |
| 79039 | | -#else |
| 79040 | | - if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; |
| 79041 | | -#endif |
| 79042 | 79089 | |
| 79043 | 79090 | /* Compressed expressions only appear when parsing the DEFAULT clause |
| 79044 | 79091 | ** on a table column definition, and hence only when pCtx==0. This |
| 79045 | 79092 | ** check ensures that an EP_TokenOnly expression is never passed down |
| 79046 | 79093 | ** into valueFromFunction(). */ |
| | @@ -120069,11 +120116,11 @@ |
| 120069 | 120116 | ** |
| 120070 | 120117 | ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values. |
| 120071 | 120118 | ** ONEPASS_SINGLE: One-pass approach - at most one row deleted. |
| 120072 | 120119 | ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted. |
| 120073 | 120120 | */ |
| 120074 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1); |
| 120121 | + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1); |
| 120075 | 120122 | if( pWInfo==0 ) goto delete_from_cleanup; |
| 120076 | 120123 | eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); |
| 120077 | 120124 | assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI ); |
| 120078 | 120125 | assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF ); |
| 120079 | 120126 | if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse); |
| | @@ -123612,11 +123659,11 @@ |
| 123612 | 123659 | |
| 123613 | 123660 | /* Create VDBE to loop through the entries in pSrc that match the WHERE |
| 123614 | 123661 | ** clause. For each row found, increment either the deferred or immediate |
| 123615 | 123662 | ** foreign key constraint counter. */ |
| 123616 | 123663 | if( pParse->nErr==0 ){ |
| 123617 | | - pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0); |
| 123664 | + pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0, 0); |
| 123618 | 123665 | sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr); |
| 123619 | 123666 | if( pWInfo ){ |
| 123620 | 123667 | sqlite3WhereEnd(pWInfo); |
| 123621 | 123668 | } |
| 123622 | 123669 | } |
| | @@ -133324,10 +133371,11 @@ |
| 133324 | 133371 | memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
| 133325 | 133372 | assert( db->pParse!=pParse ); |
| 133326 | 133373 | pParse->pOuterParse = db->pParse; |
| 133327 | 133374 | db->pParse = pParse; |
| 133328 | 133375 | pParse->db = db; |
| 133376 | + if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory"); |
| 133329 | 133377 | } |
| 133330 | 133378 | |
| 133331 | 133379 | /* |
| 133332 | 133380 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 133333 | 133381 | */ |
| | @@ -133350,11 +133398,11 @@ |
| 133350 | 133398 | sParse.pOuterParse = db->pParse; |
| 133351 | 133399 | db->pParse = &sParse; |
| 133352 | 133400 | sParse.db = db; |
| 133353 | 133401 | sParse.pReprepare = pReprepare; |
| 133354 | 133402 | assert( ppStmt && *ppStmt==0 ); |
| 133355 | | - /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */ |
| 133403 | + if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory"); |
| 133356 | 133404 | assert( sqlite3_mutex_held(db->mutex) ); |
| 133357 | 133405 | |
| 133358 | 133406 | /* For a long-term use prepared statement avoid the use of |
| 133359 | 133407 | ** lookaside memory. |
| 133360 | 133408 | */ |
| | @@ -140618,11 +140666,11 @@ |
| 140618 | 140666 | |
| 140619 | 140667 | |
| 140620 | 140668 | /* Begin the database scan. */ |
| 140621 | 140669 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 140622 | 140670 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy, |
| 140623 | | - p->pEList, wctrlFlags, p->nSelectRow); |
| 140671 | + p->pEList, p, wctrlFlags, p->nSelectRow); |
| 140624 | 140672 | if( pWInfo==0 ) goto select_end; |
| 140625 | 140673 | if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){ |
| 140626 | 140674 | p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo); |
| 140627 | 140675 | } |
| 140628 | 140676 | if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){ |
| | @@ -140882,11 +140930,11 @@ |
| 140882 | 140930 | ** in the right order to begin with. |
| 140883 | 140931 | */ |
| 140884 | 140932 | sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); |
| 140885 | 140933 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 140886 | 140934 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct, |
| 140887 | | - WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0 |
| 140935 | + 0, (WHERE_GROUPBY|(orderByGrp ? WHERE_SORTBYGROUP : 0)|distFlag), 0 |
| 140888 | 140936 | ); |
| 140889 | 140937 | if( pWInfo==0 ){ |
| 140890 | 140938 | sqlite3ExprListDelete(db, pDistinct); |
| 140891 | 140939 | goto select_end; |
| 140892 | 140940 | } |
| | @@ -141180,11 +141228,11 @@ |
| 141180 | 141228 | assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 ); |
| 141181 | 141229 | assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 ); |
| 141182 | 141230 | |
| 141183 | 141231 | SELECTTRACE(1,pParse,p,("WhereBegin\n")); |
| 141184 | 141232 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy, |
| 141185 | | - pDistinct, minMaxFlag|distFlag, 0); |
| 141233 | + pDistinct, 0, minMaxFlag|distFlag, 0); |
| 141186 | 141234 | if( pWInfo==0 ){ |
| 141187 | 141235 | goto select_end; |
| 141188 | 141236 | } |
| 141189 | 141237 | SELECTTRACE(1,pParse,p,("WhereBegin returns\n")); |
| 141190 | 141238 | eDist = sqlite3WhereIsDistinct(pWInfo); |
| | @@ -143623,11 +143671,11 @@ |
| 143623 | 143671 | ** or index, causing a single-pass approach to malfunction. */ |
| 143624 | 143672 | flags = WHERE_ONEPASS_DESIRED; |
| 143625 | 143673 | if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){ |
| 143626 | 143674 | flags |= WHERE_ONEPASS_MULTIROW; |
| 143627 | 143675 | } |
| 143628 | | - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags,iIdxCur); |
| 143676 | + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur); |
| 143629 | 143677 | if( pWInfo==0 ) goto update_cleanup; |
| 143630 | 143678 | |
| 143631 | 143679 | /* A one-pass strategy that might update more than one row may not |
| 143632 | 143680 | ** be used if any column of the index used for the scan is being |
| 143633 | 143681 | ** updated. Otherwise, if there is an index on "b", statements like |
| | @@ -144145,11 +144193,13 @@ |
| 144145 | 144193 | }else{ |
| 144146 | 144194 | regRec = ++pParse->nMem; |
| 144147 | 144195 | regRowid = ++pParse->nMem; |
| 144148 | 144196 | |
| 144149 | 144197 | /* Start scanning the virtual table */ |
| 144150 | | - pWInfo = sqlite3WhereBegin(pParse, pSrc,pWhere,0,0,WHERE_ONEPASS_DESIRED,0); |
| 144198 | + pWInfo = sqlite3WhereBegin( |
| 144199 | + pParse, pSrc, pWhere, 0, 0, 0, WHERE_ONEPASS_DESIRED, 0 |
| 144200 | + ); |
| 144151 | 144201 | if( pWInfo==0 ) return; |
| 144152 | 144202 | |
| 144153 | 144203 | /* Populate the argument registers. */ |
| 144154 | 144204 | for(i=0; i<pTab->nCol; i++){ |
| 144155 | 144205 | assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ); |
| | @@ -146456,11 +146506,12 @@ |
| 146456 | 146506 | u16 nDistinctCol; /* Index columns used to sort for DISTINCT */ |
| 146457 | 146507 | Index *pIndex; /* Index used, or NULL */ |
| 146458 | 146508 | } btree; |
| 146459 | 146509 | struct { /* Information for virtual tables */ |
| 146460 | 146510 | int idxNum; /* Index number */ |
| 146461 | | - u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 146511 | + u8 needFree : 1; /* True if sqlite3_free(idxStr) is needed */ |
| 146512 | + u8 bOmitOffset : 1; /* True to let virtual table handle offset */ |
| 146462 | 146513 | i8 isOrdered; /* True if satisfies ORDER BY */ |
| 146463 | 146514 | u16 omitMask; /* Terms that may be omitted */ |
| 146464 | 146515 | char *idxStr; /* Index identifier string */ |
| 146465 | 146516 | } vtab; |
| 146466 | 146517 | } u; |
| | @@ -146786,10 +146837,13 @@ |
| 146786 | 146837 | Parse *pParse; /* Parsing and code generating context */ |
| 146787 | 146838 | SrcList *pTabList; /* List of tables in the join */ |
| 146788 | 146839 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
| 146789 | 146840 | ExprList *pResultSet; /* Result set of the query */ |
| 146790 | 146841 | Expr *pWhere; /* The complete WHERE clause */ |
| 146842 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 146843 | + Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */ |
| 146844 | +#endif |
| 146791 | 146845 | int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ |
| 146792 | 146846 | int iContinue; /* Jump here to continue with next record */ |
| 146793 | 146847 | int iBreak; /* Jump here to break out of the loop */ |
| 146794 | 146848 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
| 146795 | 146849 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
| | @@ -146871,10 +146925,11 @@ |
| 146871 | 146925 | |
| 146872 | 146926 | /* whereexpr.c: */ |
| 146873 | 146927 | SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*); |
| 146874 | 146928 | SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*); |
| 146875 | 146929 | SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8); |
| 146930 | +SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause*, Select*); |
| 146876 | 146931 | SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); |
| 146877 | 146932 | SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*); |
| 146878 | 146933 | SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); |
| 146879 | 146934 | SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); |
| 146880 | 146935 | SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*); |
| | @@ -146941,10 +146996,11 @@ |
| 146941 | 146996 | #define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */ |
| 146942 | 146997 | #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */ |
| 146943 | 146998 | #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */ |
| 146944 | 146999 | #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */ |
| 146945 | 147000 | #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */ |
| 147001 | +#define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */ |
| 146946 | 147002 | |
| 146947 | 147003 | #endif /* !defined(SQLITE_WHEREINT_H) */ |
| 146948 | 147004 | |
| 146949 | 147005 | /************** End of whereInt.h ********************************************/ |
| 146950 | 147006 | /************** Continuing where we left off in wherecode.c ******************/ |
| | @@ -148464,10 +148520,19 @@ |
| 148464 | 148520 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 148465 | 148521 | addrNotFound = pLevel->addrNxt; |
| 148466 | 148522 | }else{ |
| 148467 | 148523 | Expr *pRight = pTerm->pExpr->pRight; |
| 148468 | 148524 | codeExprOrVector(pParse, pRight, iTarget, 1); |
| 148525 | + if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET |
| 148526 | + && pLoop->u.vtab.bOmitOffset |
| 148527 | + ){ |
| 148528 | + assert( pTerm->eOperator==WO_AUX ); |
| 148529 | + assert( pWInfo->pLimit!=0 ); |
| 148530 | + assert( pWInfo->pLimit->iOffset>0 ); |
| 148531 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pLimit->iOffset); |
| 148532 | + VdbeComment((v,"Zero OFFSET counter")); |
| 148533 | + } |
| 148469 | 148534 | } |
| 148470 | 148535 | } |
| 148471 | 148536 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
| 148472 | 148537 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
| 148473 | 148538 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| | @@ -149285,11 +149350,11 @@ |
| 149285 | 149350 | pOrExpr = pAndExpr; |
| 149286 | 149351 | } |
| 149287 | 149352 | /* Loop through table entries that match term pOrTerm. */ |
| 149288 | 149353 | ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1)); |
| 149289 | 149354 | WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); |
| 149290 | | - pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 149355 | + pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0, |
| 149291 | 149356 | WHERE_OR_SUBCLAUSE, iCovCur); |
| 149292 | 149357 | assert( pSubWInfo || pParse->nErr ); |
| 149293 | 149358 | if( pSubWInfo ){ |
| 149294 | 149359 | WhereLoop *pSubLoop; |
| 149295 | 149360 | int addrExplain = sqlite3WhereExplainOneScan( |
| | @@ -150982,11 +151047,14 @@ |
| 150982 | 151047 | ** new terms for each component comparison - "a = ?" and "b = ?". The |
| 150983 | 151048 | ** new terms completely replace the original vector comparison, which is |
| 150984 | 151049 | ** no longer used. |
| 150985 | 151050 | ** |
| 150986 | 151051 | ** This is only required if at least one side of the comparison operation |
| 150987 | | - ** is not a sub-select. */ |
| 151052 | + ** is not a sub-select. |
| 151053 | + ** |
| 151054 | + ** tag-20220128a |
| 151055 | + */ |
| 150988 | 151056 | if( (pExpr->op==TK_EQ || pExpr->op==TK_IS) |
| 150989 | 151057 | && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1 |
| 150990 | 151058 | && sqlite3ExprVectorSize(pExpr->pRight)==nLeft |
| 150991 | 151059 | && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 |
| 150992 | 151060 | || (pExpr->pRight->flags & EP_xIsSelect)==0) |
| | @@ -151124,10 +151192,117 @@ |
| 151124 | 151192 | }else{ |
| 151125 | 151193 | sqlite3WhereSplit(pWC, pE2->pLeft, op); |
| 151126 | 151194 | sqlite3WhereSplit(pWC, pE2->pRight, op); |
| 151127 | 151195 | } |
| 151128 | 151196 | } |
| 151197 | + |
| 151198 | +/* |
| 151199 | +** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or |
| 151200 | +** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the |
| 151201 | +** where-clause passed as the first argument. The value for the term |
| 151202 | +** is found in register iReg. |
| 151203 | +** |
| 151204 | +** In the common case where the value is a simple integer |
| 151205 | +** (example: "LIMIT 5 OFFSET 10") then the expression codes as a |
| 151206 | +** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value(). |
| 151207 | +** If not, then it codes as a TK_REGISTER expression. |
| 151208 | +*/ |
| 151209 | +void whereAddLimitExpr( |
| 151210 | + WhereClause *pWC, /* Add the constraint to this WHERE clause */ |
| 151211 | + int iReg, /* Register that will hold value of the limit/offset */ |
| 151212 | + Expr *pExpr, /* Expression that defines the limit/offset */ |
| 151213 | + int iCsr, /* Cursor to which the constraint applies */ |
| 151214 | + int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */ |
| 151215 | +){ |
| 151216 | + Parse *pParse = pWC->pWInfo->pParse; |
| 151217 | + sqlite3 *db = pParse->db; |
| 151218 | + Expr *pNew; |
| 151219 | + int iVal = 0; |
| 151220 | + |
| 151221 | + if( sqlite3ExprIsInteger(pExpr, &iVal) && iVal>=0 ){ |
| 151222 | + Expr *pVal = sqlite3Expr(db, TK_INTEGER, 0); |
| 151223 | + if( pVal==0 ) return; |
| 151224 | + ExprSetProperty(pVal, EP_IntValue); |
| 151225 | + pVal->u.iValue = iVal; |
| 151226 | + pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); |
| 151227 | + }else{ |
| 151228 | + Expr *pVal = sqlite3Expr(db, TK_REGISTER, 0); |
| 151229 | + if( pVal==0 ) return; |
| 151230 | + pVal->iTable = iReg; |
| 151231 | + pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); |
| 151232 | + } |
| 151233 | + if( pNew ){ |
| 151234 | + WhereTerm *pTerm; |
| 151235 | + int idx; |
| 151236 | + idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_VIRTUAL); |
| 151237 | + pTerm = &pWC->a[idx]; |
| 151238 | + pTerm->leftCursor = iCsr; |
| 151239 | + pTerm->eOperator = WO_AUX; |
| 151240 | + pTerm->eMatchOp = eMatchOp; |
| 151241 | + } |
| 151242 | +} |
| 151243 | + |
| 151244 | +/* |
| 151245 | +** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the |
| 151246 | +** SELECT statement passed as the second argument. These terms are only |
| 151247 | +** added if: |
| 151248 | +** |
| 151249 | +** 1. The SELECT statement has a LIMIT clause, and |
| 151250 | +** 2. The SELECT statement is not an aggregate or DISTINCT query, and |
| 151251 | +** 3. The SELECT statement has exactly one object in its from clause, and |
| 151252 | +** that object is a virtual table, and |
| 151253 | +** 4. There are no terms in the WHERE clause that will not be passed |
| 151254 | +** to the virtual table xBestIndex method. |
| 151255 | +** 5. The ORDER BY clause, if any, will be made available to the xBestIndex |
| 151256 | +** method. |
| 151257 | +** |
| 151258 | +** LIMIT and OFFSET terms are ignored by most of the planner code. They |
| 151259 | +** exist only so that they may be passed to the xBestIndex method of the |
| 151260 | +** single virtual table in the FROM clause of the SELECT. |
| 151261 | +*/ |
| 151262 | +SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause *pWC, Select *p){ |
| 151263 | + assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) ); |
| 151264 | + if( (p && p->pLimit) /* 1 */ |
| 151265 | + && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */ |
| 151266 | + && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */ |
| 151267 | + ){ |
| 151268 | + ExprList *pOrderBy = p->pOrderBy; |
| 151269 | + int iCsr = p->pSrc->a[0].iCursor; |
| 151270 | + int ii; |
| 151271 | + |
| 151272 | + /* Check condition (4). Return early if it is not met. */ |
| 151273 | + for(ii=0; ii<pWC->nTerm; ii++){ |
| 151274 | + if( pWC->a[ii].wtFlags & TERM_CODED ){ |
| 151275 | + /* This term is a vector operation that has been decomposed into |
| 151276 | + ** other, subsequent terms. It can be ignored. See tag-20220128a */ |
| 151277 | + assert( pWC->a[ii].wtFlags & TERM_VIRTUAL ); |
| 151278 | + assert( pWC->a[ii].eOperator==0 ); |
| 151279 | + continue; |
| 151280 | + } |
| 151281 | + if( pWC->a[ii].leftCursor!=iCsr ) return; |
| 151282 | + } |
| 151283 | + |
| 151284 | + /* Check condition (5). Return early if it is not met. */ |
| 151285 | + if( pOrderBy ){ |
| 151286 | + for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 151287 | + Expr *pExpr = pOrderBy->a[ii].pExpr; |
| 151288 | + if( pExpr->op!=TK_COLUMN ) return; |
| 151289 | + if( pExpr->iTable!=iCsr ) return; |
| 151290 | + if( pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_BIGNULL ) return; |
| 151291 | + } |
| 151292 | + } |
| 151293 | + |
| 151294 | + /* All conditions are met. Add the terms to the where-clause object. */ |
| 151295 | + assert( p->pLimit->op==TK_LIMIT ); |
| 151296 | + whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft, |
| 151297 | + iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT); |
| 151298 | + if( p->iOffset>0 ){ |
| 151299 | + whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight, |
| 151300 | + iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET); |
| 151301 | + } |
| 151302 | + } |
| 151303 | +} |
| 151129 | 151304 | |
| 151130 | 151305 | /* |
| 151131 | 151306 | ** Initialize a preallocated WhereClause structure. |
| 151132 | 151307 | */ |
| 151133 | 151308 | SQLITE_PRIVATE void sqlite3WhereClauseInit( |
| | @@ -151160,10 +151335,11 @@ |
| 151160 | 151335 | for(i=pWC->nBase; i<pWC->nTerm; i++){ |
| 151161 | 151336 | assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 ); |
| 151162 | 151337 | } |
| 151163 | 151338 | #endif |
| 151164 | 151339 | while(1){ |
| 151340 | + assert( a->eMatchOp==0 || a->eOperator==WO_AUX ); |
| 151165 | 151341 | if( a->wtFlags & TERM_DYNAMIC ){ |
| 151166 | 151342 | sqlite3ExprDelete(db, a->pExpr); |
| 151167 | 151343 | } |
| 151168 | 151344 | if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){ |
| 151169 | 151345 | if( a->wtFlags & TERM_ORINFO ){ |
| | @@ -151317,10 +151493,11 @@ |
| 151317 | 151493 | if( pColRef==0 ) return; |
| 151318 | 151494 | pColRef->iTable = pItem->iCursor; |
| 151319 | 151495 | pColRef->iColumn = k++; |
| 151320 | 151496 | assert( ExprUseYTab(pColRef) ); |
| 151321 | 151497 | pColRef->y.pTab = pTab; |
| 151498 | + pItem->colUsed |= sqlite3ExprColUsed(pColRef); |
| 151322 | 151499 | pRhs = sqlite3PExpr(pParse, TK_UPLUS, |
| 151323 | 151500 | sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0); |
| 151324 | 151501 | pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs); |
| 151325 | 151502 | if( pItem->fg.jointype & JT_LEFT ){ |
| 151326 | 151503 | sqlite3SetJoinExpr(pTerm, pItem->iCursor); |
| | @@ -154789,10 +154966,19 @@ |
| 154789 | 154966 | } |
| 154790 | 154967 | return rc; |
| 154791 | 154968 | } |
| 154792 | 154969 | |
| 154793 | 154970 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 154971 | + |
| 154972 | +/* |
| 154973 | +** Return true if pTerm is a virtual table LIMIT or OFFSET term. |
| 154974 | +*/ |
| 154975 | +static int isLimitTerm(WhereTerm *pTerm){ |
| 154976 | + assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 ); |
| 154977 | + return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT |
| 154978 | + && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET; |
| 154979 | +} |
| 154794 | 154980 | |
| 154795 | 154981 | /* |
| 154796 | 154982 | ** Argument pIdxInfo is already populated with all constraints that may |
| 154797 | 154983 | ** be used by the virtual table identified by pBuilder->pNew->iTab. This |
| 154798 | 154984 | ** function marks a subset of those constraints usable, invokes the |
| | @@ -154817,11 +155003,12 @@ |
| 154817 | 155003 | Bitmask mPrereq, /* Mask of tables that must be used. */ |
| 154818 | 155004 | Bitmask mUsable, /* Mask of usable tables */ |
| 154819 | 155005 | u16 mExclude, /* Exclude terms using these operators */ |
| 154820 | 155006 | sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */ |
| 154821 | 155007 | u16 mNoOmit, /* Do not omit these constraints */ |
| 154822 | | - int *pbIn /* OUT: True if plan uses an IN(...) op */ |
| 155008 | + int *pbIn, /* OUT: True if plan uses an IN(...) op */ |
| 155009 | + int *pbRetryLimit /* OUT: Retry without LIMIT/OFFSET */ |
| 154823 | 155010 | ){ |
| 154824 | 155011 | WhereClause *pWC = pBuilder->pWC; |
| 154825 | 155012 | struct sqlite3_index_constraint *pIdxCons; |
| 154826 | 155013 | struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage; |
| 154827 | 155014 | int i; |
| | @@ -154842,10 +155029,11 @@ |
| 154842 | 155029 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 154843 | 155030 | WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset]; |
| 154844 | 155031 | pIdxCons->usable = 0; |
| 154845 | 155032 | if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight |
| 154846 | 155033 | && (pTerm->eOperator & mExclude)==0 |
| 155034 | + && (pbRetryLimit || !isLimitTerm(pTerm)) |
| 154847 | 155035 | ){ |
| 154848 | 155036 | pIdxCons->usable = 1; |
| 154849 | 155037 | } |
| 154850 | 155038 | } |
| 154851 | 155039 | |
| | @@ -154874,12 +155062,12 @@ |
| 154874 | 155062 | return rc; |
| 154875 | 155063 | } |
| 154876 | 155064 | |
| 154877 | 155065 | mxTerm = -1; |
| 154878 | 155066 | assert( pNew->nLSlot>=nConstraint ); |
| 154879 | | - for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
| 154880 | | - pNew->u.vtab.omitMask = 0; |
| 155067 | + memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint ); |
| 155068 | + memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab)); |
| 154881 | 155069 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 154882 | 155070 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 154883 | 155071 | int iTerm; |
| 154884 | 155072 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 154885 | 155073 | WhereTerm *pTerm; |
| | @@ -154909,10 +155097,13 @@ |
| 154909 | 155097 | testcase( i!=iTerm ); |
| 154910 | 155098 | pNew->u.vtab.omitMask |= 1<<iTerm; |
| 154911 | 155099 | }else{ |
| 154912 | 155100 | testcase( i!=iTerm ); |
| 154913 | 155101 | } |
| 155102 | + if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){ |
| 155103 | + pNew->u.vtab.bOmitOffset = 1; |
| 155104 | + } |
| 154914 | 155105 | } |
| 154915 | 155106 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 154916 | 155107 | /* A virtual table that is constrained by an IN clause may not |
| 154917 | 155108 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 154918 | 155109 | ** is not necessarily related to the order of output terms and |
| | @@ -154920,10 +155111,25 @@ |
| 154920 | 155111 | ** together. */ |
| 154921 | 155112 | pIdxInfo->orderByConsumed = 0; |
| 154922 | 155113 | pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE; |
| 154923 | 155114 | *pbIn = 1; assert( (mExclude & WO_IN)==0 ); |
| 154924 | 155115 | } |
| 155116 | + |
| 155117 | + if( isLimitTerm(pTerm) && *pbIn ){ |
| 155118 | + /* If there is an IN(...) term handled as an == (separate call to |
| 155119 | + ** xFilter for each value on the RHS of the IN) and a LIMIT or |
| 155120 | + ** OFFSET term handled as well, the plan is unusable. Set output |
| 155121 | + ** variable *pbRetryLimit to true to tell the caller to retry with |
| 155122 | + ** LIMIT and OFFSET disabled. */ |
| 155123 | + if( pIdxInfo->needToFreeIdxStr ){ |
| 155124 | + sqlite3_free(pIdxInfo->idxStr); |
| 155125 | + pIdxInfo->idxStr = 0; |
| 155126 | + pIdxInfo->needToFreeIdxStr = 0; |
| 155127 | + } |
| 155128 | + *pbRetryLimit = 1; |
| 155129 | + return SQLITE_OK; |
| 155130 | + } |
| 154925 | 155131 | } |
| 154926 | 155132 | } |
| 154927 | 155133 | |
| 154928 | 155134 | pNew->nLTerm = mxTerm+1; |
| 154929 | 155135 | for(i=0; i<=mxTerm; i++){ |
| | @@ -155082,10 +155288,11 @@ |
| 155082 | 155288 | int nConstraint; /* Number of constraints in p */ |
| 155083 | 155289 | int bIn; /* True if plan uses IN(...) operator */ |
| 155084 | 155290 | WhereLoop *pNew; |
| 155085 | 155291 | Bitmask mBest; /* Tables used by best possible plan */ |
| 155086 | 155292 | u16 mNoOmit; |
| 155293 | + int bRetry = 0; /* True to retry with LIMIT/OFFSET disabled */ |
| 155087 | 155294 | |
| 155088 | 155295 | assert( (mPrereq & mUnusable)==0 ); |
| 155089 | 155296 | pWInfo = pBuilder->pWInfo; |
| 155090 | 155297 | pParse = pWInfo->pParse; |
| 155091 | 155298 | pWC = pBuilder->pWC; |
| | @@ -155105,11 +155312,19 @@ |
| 155105 | 155312 | } |
| 155106 | 155313 | |
| 155107 | 155314 | /* First call xBestIndex() with all constraints usable. */ |
| 155108 | 155315 | WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName)); |
| 155109 | 155316 | WHERETRACE(0x40, (" VirtualOne: all usable\n")); |
| 155110 | | - rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn); |
| 155317 | + rc = whereLoopAddVirtualOne( |
| 155318 | + pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, &bRetry |
| 155319 | + ); |
| 155320 | + if( bRetry ){ |
| 155321 | + assert( rc==SQLITE_OK ); |
| 155322 | + rc = whereLoopAddVirtualOne( |
| 155323 | + pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, 0 |
| 155324 | + ); |
| 155325 | + } |
| 155111 | 155326 | |
| 155112 | 155327 | /* If the call to xBestIndex() with all terms enabled produced a plan |
| 155113 | 155328 | ** that does not require any source tables (IOW: a plan with mBest==0) |
| 155114 | 155329 | ** and does not use an IN(...) operator, then there is no point in making |
| 155115 | 155330 | ** any further calls to xBestIndex() since they will all return the same |
| | @@ -155123,11 +155338,11 @@ |
| 155123 | 155338 | /* If the plan produced by the earlier call uses an IN(...) term, call |
| 155124 | 155339 | ** xBestIndex again, this time with IN(...) terms disabled. */ |
| 155125 | 155340 | if( bIn ){ |
| 155126 | 155341 | WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n")); |
| 155127 | 155342 | rc = whereLoopAddVirtualOne( |
| 155128 | | - pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn); |
| 155343 | + pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn, 0); |
| 155129 | 155344 | assert( bIn==0 ); |
| 155130 | 155345 | mBestNoIn = pNew->prereq & ~mPrereq; |
| 155131 | 155346 | if( mBestNoIn==0 ){ |
| 155132 | 155347 | seenZero = 1; |
| 155133 | 155348 | seenZeroNoIN = 1; |
| | @@ -155150,11 +155365,11 @@ |
| 155150 | 155365 | if( mNext==ALLBITS ) break; |
| 155151 | 155366 | if( mNext==mBest || mNext==mBestNoIn ) continue; |
| 155152 | 155367 | WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n", |
| 155153 | 155368 | (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext)); |
| 155154 | 155369 | rc = whereLoopAddVirtualOne( |
| 155155 | | - pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn); |
| 155370 | + pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0); |
| 155156 | 155371 | if( pNew->prereq==mPrereq ){ |
| 155157 | 155372 | seenZero = 1; |
| 155158 | 155373 | if( bIn==0 ) seenZeroNoIN = 1; |
| 155159 | 155374 | } |
| 155160 | 155375 | } |
| | @@ -155163,21 +155378,21 @@ |
| 155163 | 155378 | ** that requires no source tables at all (i.e. one guaranteed to be |
| 155164 | 155379 | ** usable), make a call here with all source tables disabled */ |
| 155165 | 155380 | if( rc==SQLITE_OK && seenZero==0 ){ |
| 155166 | 155381 | WHERETRACE(0x40, (" VirtualOne: all disabled\n")); |
| 155167 | 155382 | rc = whereLoopAddVirtualOne( |
| 155168 | | - pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn); |
| 155383 | + pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0); |
| 155169 | 155384 | if( bIn==0 ) seenZeroNoIN = 1; |
| 155170 | 155385 | } |
| 155171 | 155386 | |
| 155172 | 155387 | /* If the calls to xBestIndex() have so far failed to find a plan |
| 155173 | 155388 | ** that requires no source tables at all and does not use an IN(...) |
| 155174 | 155389 | ** operator, make a final call to obtain one here. */ |
| 155175 | 155390 | if( rc==SQLITE_OK && seenZeroNoIN==0 ){ |
| 155176 | 155391 | WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n")); |
| 155177 | 155392 | rc = whereLoopAddVirtualOne( |
| 155178 | | - pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn); |
| 155393 | + pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0); |
| 155179 | 155394 | } |
| 155180 | 155395 | } |
| 155181 | 155396 | |
| 155182 | 155397 | if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr); |
| 155183 | 155398 | freeIndexInfo(pParse->db, p); |
| | @@ -156579,10 +156794,11 @@ |
| 156579 | 156794 | Parse *pParse, /* The parser context */ |
| 156580 | 156795 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
| 156581 | 156796 | Expr *pWhere, /* The WHERE clause */ |
| 156582 | 156797 | ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ |
| 156583 | 156798 | ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */ |
| 156799 | + Select *pLimit, /* Use this LIMIT/OFFSET clause, if any */ |
| 156584 | 156800 | u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */ |
| 156585 | 156801 | int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number |
| 156586 | 156802 | ** If WHERE_USE_LIMIT, then the limit amount */ |
| 156587 | 156803 | ){ |
| 156588 | 156804 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
| | @@ -156655,10 +156871,13 @@ |
| 156655 | 156871 | pWInfo->nLevel = nTabList; |
| 156656 | 156872 | pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse); |
| 156657 | 156873 | pWInfo->wctrlFlags = wctrlFlags; |
| 156658 | 156874 | pWInfo->iLimit = iAuxArg; |
| 156659 | 156875 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 156876 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 156877 | + pWInfo->pLimit = pLimit; |
| 156878 | +#endif |
| 156660 | 156879 | memset(&pWInfo->nOBSat, 0, |
| 156661 | 156880 | offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat)); |
| 156662 | 156881 | memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel)); |
| 156663 | 156882 | assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */ |
| 156664 | 156883 | pMaskSet = &pWInfo->sMaskSet; |
| | @@ -156723,10 +156942,11 @@ |
| 156723 | 156942 | #endif |
| 156724 | 156943 | } |
| 156725 | 156944 | |
| 156726 | 156945 | /* Analyze all of the subexpressions. */ |
| 156727 | 156946 | sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); |
| 156947 | + sqlite3WhereAddLimit(&pWInfo->sWC, pLimit); |
| 156728 | 156948 | if( db->mallocFailed ) goto whereBeginError; |
| 156729 | 156949 | |
| 156730 | 156950 | /* Special case: WHERE terms that do not refer to any tables in the join |
| 156731 | 156951 | ** (constant expressions). Evaluate each such term, and jump over all the |
| 156732 | 156952 | ** generated code if the result is not true. |
| | @@ -233598,11 +233818,11 @@ |
| 233598 | 233818 | int nArg, /* Number of args */ |
| 233599 | 233819 | sqlite3_value **apUnused /* Function arguments */ |
| 233600 | 233820 | ){ |
| 233601 | 233821 | assert( nArg==0 ); |
| 233602 | 233822 | UNUSED_PARAM2(nArg, apUnused); |
| 233603 | | - sqlite3_result_text(pCtx, "fts5: 2022-01-25 00:03:25 a8db69411b0d1275909adeb21027784ada17af24efe3a59ae0ae2a897659ff17", -1, SQLITE_TRANSIENT); |
| 233823 | + sqlite3_result_text(pCtx, "fts5: 2022-01-31 14:14:29 539cef5214446a7181614793e9cf323e95ba00ba0f888585b14b598dd2ff0808", -1, SQLITE_TRANSIENT); |
| 233604 | 233824 | } |
| 233605 | 233825 | |
| 233606 | 233826 | /* |
| 233607 | 233827 | ** Return true if zName is the extension on one of the shadow tables used |
| 233608 | 233828 | ** by this module. |
| 233609 | 233829 | |