| | @@ -16,11 +16,11 @@ |
| 16 | 16 | ** if you want a wrapper to interface SQLite with your choice of programming |
| 17 | 17 | ** language. The code for the "sqlite3" command-line shell is also in a |
| 18 | 18 | ** separate file. This file contains only code for the core SQLite library. |
| 19 | 19 | ** |
| 20 | 20 | ** The content in this amalgamation comes from Fossil check-in |
| 21 | | -** 3bd5456e50ef321f2cafcf67261d556a7606 with changes in files: |
| 21 | +** 4b30d1d7e22b9b74753a6fb4d9e1f884ddce with changes in files: |
| 22 | 22 | ** |
| 23 | 23 | ** |
| 24 | 24 | */ |
| 25 | 25 | #ifndef SQLITE_AMALGAMATION |
| 26 | 26 | #define SQLITE_CORE 1 |
| | @@ -467,14 +467,14 @@ |
| 467 | 467 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 468 | 468 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 469 | 469 | */ |
| 470 | 470 | #define SQLITE_VERSION "3.54.0" |
| 471 | 471 | #define SQLITE_VERSION_NUMBER 3054000 |
| 472 | | -#define SQLITE_SOURCE_ID "2026-08-20 13:25:12 3bd5456e50ef321f2cafcf67261d556a760624ce0caab430ed9454cebec233e7" |
| 472 | +#define SQLITE_SOURCE_ID "2026-08-27 10:31:42 4b30d1d7e22b9b74753a6fb4d9e1f884ddcee12273d6842954ed44de94e5f4b9" |
| 473 | 473 | #define SQLITE_SCM_BRANCH "trunk" |
| 474 | 474 | #define SQLITE_SCM_TAGS "" |
| 475 | | -#define SQLITE_SCM_DATETIME "2026-08-20T13:25:12.207Z" |
| 475 | +#define SQLITE_SCM_DATETIME "2026-08-27T10:31:42.047Z" |
| 476 | 476 | |
| 477 | 477 | /* |
| 478 | 478 | ** CAPI3REF: Run-Time Library Version Numbers |
| 479 | 479 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 480 | 480 | ** |
| | @@ -18208,11 +18208,11 @@ |
| 18208 | 18208 | SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*); |
| 18209 | 18209 | #endif |
| 18210 | 18210 | SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*); |
| 18211 | 18211 | SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*); |
| 18212 | 18212 | SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8); |
| 18213 | | -SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int); |
| 18213 | +SQLITE_PRIVATE void sqlite3VdbeReprepareOnBind(Vdbe*, int, int); |
| 18214 | 18214 | #ifndef SQLITE_OMIT_TRACE |
| 18215 | 18215 | SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*); |
| 18216 | 18216 | #endif |
| 18217 | 18217 | SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); |
| 18218 | 18218 | SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*); |
| | @@ -19088,41 +19088,43 @@ |
| 19088 | 19088 | ** selectively disable various optimizations. |
| 19089 | 19089 | */ |
| 19090 | 19090 | #define SQLITE_QueryFlattener 0x00000001 /* Query flattening */ |
| 19091 | 19091 | #define SQLITE_WindowFunc 0x00000002 /* Use xInverse for window functions */ |
| 19092 | 19092 | #define SQLITE_GroupByOrder 0x00000004 /* GROUPBY cover of ORDERBY */ |
| 19093 | | -#define SQLITE_FactorOutConst 0x00000008 /* Constant factoring */ |
| 19093 | + /* 0x00000008 -- Available for reuse */ |
| 19094 | 19094 | #define SQLITE_DistinctOpt 0x00000010 /* DISTINCT using indexes */ |
| 19095 | 19095 | #define SQLITE_CoverIdxScan 0x00000020 /* Covering index scans */ |
| 19096 | 19096 | #define SQLITE_OrderByIdxJoin 0x00000040 /* ORDER BY of joins via index */ |
| 19097 | 19097 | #define SQLITE_Transitive 0x00000080 /* Transitive constraints */ |
| 19098 | 19098 | #define SQLITE_OmitNoopJoin 0x00000100 /* Omit unused tables in joins */ |
| 19099 | 19099 | #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */ |
| 19100 | 19100 | #define SQLITE_CursorHints 0x00000400 /* Add OP_CursorHint opcodes */ |
| 19101 | 19101 | #define SQLITE_Stat4 0x00000800 /* Use STAT4 data */ |
| 19102 | | - /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */ |
| 19102 | + /* TH3 expects this value ^^^^^^^^^^ */ |
| 19103 | 19103 | #define SQLITE_PushDown 0x00001000 /* WHERE-clause push-down opt */ |
| 19104 | 19104 | #define SQLITE_SimplifyJoin 0x00002000 /* Convert LEFT JOIN to JOIN */ |
| 19105 | 19105 | #define SQLITE_SkipScan 0x00004000 /* Skip-scans */ |
| 19106 | 19106 | #define SQLITE_PropagateConst 0x00008000 /* The constant propagation opt */ |
| 19107 | 19107 | #define SQLITE_MinMaxOpt 0x00010000 /* The min/max optimization */ |
| 19108 | 19108 | #define SQLITE_SeekScan 0x00020000 /* The OP_SeekScan optimization */ |
| 19109 | 19109 | #define SQLITE_OmitOrderBy 0x00040000 /* Omit pointless ORDER BY */ |
| 19110 | | - /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */ |
| 19111 | | -#define SQLITE_BloomFilter 0x00080000 /* Use a Bloom filter on searches */ |
| 19112 | | -#define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */ |
| 19110 | + /* TH3 expects this value ^^^^^^^^^^ */ |
| 19111 | +#define SQLITE_BloomFilter 0x00080000 /* Use a Bloom filters */ |
| 19112 | + /* TH3 expects this value ^^^^^^^^^^ */ |
| 19113 | + /* 0x00100000 -- Available for reuse */ |
| 19113 | 19114 | #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */ |
| 19114 | 19115 | #define SQLITE_ReleaseReg 0x00400000 /* Use OP_ReleaseReg for testing */ |
| 19115 | 19116 | #define SQLITE_FlttnUnionAll 0x00800000 /* Disable the UNION ALL flattener */ |
| 19116 | | - /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */ |
| 19117 | + /* TH3 expects this value ^^^^^^^^^^ */ |
| 19117 | 19118 | #define SQLITE_IndexedExpr 0x01000000 /* Pull exprs from index when able */ |
| 19118 | 19119 | #define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */ |
| 19119 | 19120 | #define SQLITE_NullUnusedCols 0x04000000 /* NULL unused columns in subqueries */ |
| 19120 | 19121 | #define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */ |
| 19121 | 19122 | #define SQLITE_OrderBySubq 0x10000000 /* ORDER BY in subquery helps outer */ |
| 19122 | 19123 | #define SQLITE_StarQuery 0x20000000 /* Heurists for star queries */ |
| 19123 | 19124 | #define SQLITE_ExistsToJoin 0x40000000 /* The EXISTS-to-JOIN optimization */ |
| 19125 | +#define SQLITE_UnionLimit 0x80000000 /* Optimizations for UNION + LIMIT */ |
| 19124 | 19126 | #define SQLITE_AllOpts 0xffffffff /* All optimizations */ |
| 19125 | 19127 | |
| 19126 | 19128 | /* |
| 19127 | 19129 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 19128 | 19130 | */ |
| | @@ -22226,10 +22228,11 @@ |
| 22226 | 22228 | SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int); |
| 22227 | 22229 | SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*); |
| 22228 | 22230 | SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*); |
| 22229 | 22231 | SQLITE_PRIVATE void sqlite3ColumnSetExpr(Parse*,Table*,Column*,Expr*); |
| 22230 | 22232 | SQLITE_PRIVATE Expr *sqlite3ColumnExpr(Table*,Column*); |
| 22233 | +SQLITE_PRIVATE Expr *sqlite3ColumnExprAuth(Table*,Column*,Parse*); |
| 22231 | 22234 | SQLITE_PRIVATE void sqlite3ColumnSetColl(sqlite3*,Column*,const char*zColl); |
| 22232 | 22235 | SQLITE_PRIVATE const char *sqlite3ColumnColl(Column*); |
| 22233 | 22236 | SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*); |
| 22234 | 22237 | SQLITE_PRIVATE void sqlite3GenerateColumnNames(Parse *pParse, Select *pSelect); |
| 22235 | 22238 | SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**); |
| | @@ -22439,11 +22442,11 @@ |
| 22439 | 22442 | SQLITE_PRIVATE int sqlite3ExprIsSingleTableConstraint(Expr*,const SrcList*,int,int); |
| 22440 | 22443 | SQLITE_PRIVATE int sqlite3ExprListIsConstant(Parse *pParse, ExprList *pList, int bNoIs); |
| 22441 | 22444 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
| 22442 | 22445 | SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*); |
| 22443 | 22446 | #endif |
| 22444 | | -SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr*, int*, Parse*); |
| 22447 | +SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr*, int*, Parse*, int); |
| 22445 | 22448 | SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); |
| 22446 | 22449 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); |
| 22447 | 22450 | SQLITE_PRIVATE int sqlite3ExprIsLikeOperator(const Expr*); |
| 22448 | 22451 | SQLITE_PRIVATE int sqlite3IsRowid(const char*); |
| 22449 | 22452 | SQLITE_PRIVATE const char *sqlite3RowidAlias(Table *pTab); |
| | @@ -25047,11 +25050,13 @@ |
| 25047 | 25050 | #endif |
| 25048 | 25051 | void *pFree; /* Free this when deleting the vdbe */ |
| 25049 | 25052 | VdbeFrame *pFrame; /* Parent frame */ |
| 25050 | 25053 | VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */ |
| 25051 | 25054 | int nFrame; /* Number of frames in pFrame list */ |
| 25052 | | - u32 expmask; /* Binding to these vars invalidates VM */ |
| 25055 | + u32 expmask; /* Binding to these vars might invalidate VM */ |
| 25056 | + u32 smimask; /* Only invalid if changing to/from small integer */ |
| 25057 | + /* Note: smimask is always a subset of expmask */ |
| 25053 | 25058 | SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ |
| 25054 | 25059 | AuxData *pAuxData; /* Linked list of auxdata allocations */ |
| 25055 | 25060 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 25056 | 25061 | int nScan; /* Entries in aScan[] */ |
| 25057 | 25062 | ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */ |
| | @@ -33849,14 +33854,17 @@ |
| 33849 | 33854 | /* |
| 33850 | 33855 | ** If pExpr has a byte offset for the start of a token, record that as |
| 33851 | 33856 | ** as the error offset. |
| 33852 | 33857 | */ |
| 33853 | 33858 | SQLITE_PRIVATE void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){ |
| 33854 | | - while( pExpr |
| 33855 | | - && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0) |
| 33856 | | - ){ |
| 33857 | | - pExpr = pExpr->pLeft; |
| 33859 | + while( pExpr ){ |
| 33860 | + if( ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) ) return; |
| 33861 | + if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0 ){ |
| 33862 | + pExpr = pExpr->pLeft; |
| 33863 | + }else{ |
| 33864 | + break; |
| 33865 | + } |
| 33858 | 33866 | } |
| 33859 | 33867 | if( pExpr==0 ) return; |
| 33860 | 33868 | if( ExprHasProperty(pExpr, EP_FromDDL) ) return; |
| 33861 | 33869 | db->errByteOffset = pExpr->w.iOfst; |
| 33862 | 33870 | } |
| | @@ -86910,11 +86918,34 @@ |
| 86910 | 86918 | const Expr *pExpr, /* The expression to evaluate */ |
| 86911 | 86919 | u8 enc, /* Encoding to use */ |
| 86912 | 86920 | u8 affinity, /* Affinity to use */ |
| 86913 | 86921 | sqlite3_value **ppVal /* Write the new value here */ |
| 86914 | 86922 | ){ |
| 86915 | | - return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0; |
| 86923 | + int rc = SQLITE_OK; |
| 86924 | + sqlite3_value *pVal = 0; |
| 86925 | + if( pExpr ){ |
| 86926 | + rc = valueFromExpr(db, pExpr, enc, affinity, &pVal, 0); |
| 86927 | + if( rc==SQLITE_OK && pVal |
| 86928 | + && affinity==SQLITE_AFF_REAL |
| 86929 | + && (pVal->flags & MEM_Int) |
| 86930 | + && (u64)(pVal->u.i<0 ? pVal->u.i : pVal->u.i)>140737488355327LL |
| 86931 | + ){ |
| 86932 | + /* If the integer value is too large to fit in a 6-byte integer and |
| 86933 | + ** the affinity is REAL, convert it to a real value now. In most |
| 86934 | + ** cases an OP_RealAffinity opcode will be used to convert the |
| 86935 | + ** value to an actual real, but this opcode is omitted if the values |
| 86936 | + ** are being read directly from a table in order to create an index |
| 86937 | + ** key. It is important to get a "real" real value for the larger |
| 86938 | + ** magnitude integer values so that comparisons work correctly - |
| 86939 | + ** SQLite by default will convert real values to integers before |
| 86940 | + ** doing the comparison, which is different from converting to real |
| 86941 | + ** first. */ |
| 86942 | + sqlite3VdbeMemRealify(pVal); |
| 86943 | + } |
| 86944 | + } |
| 86945 | + *ppVal = pVal; |
| 86946 | + return rc; |
| 86916 | 86947 | } |
| 86917 | 86948 | |
| 86918 | 86949 | #ifdef SQLITE_ENABLE_STAT4 |
| 86919 | 86950 | /* |
| 86920 | 86951 | ** Attempt to extract a value from pExpr and use it to construct *ppVal. |
| | @@ -86955,11 +86986,11 @@ |
| 86955 | 86986 | sqlite3VdbeMemSetNull((Mem*)pVal); |
| 86956 | 86987 | } |
| 86957 | 86988 | }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){ |
| 86958 | 86989 | Vdbe *v; |
| 86959 | 86990 | int iBindVar = pExpr->iColumn; |
| 86960 | | - sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar); |
| 86991 | + sqlite3VdbeReprepareOnBind(pParse->pVdbe, iBindVar, 0); |
| 86961 | 86992 | if( (v = pParse->pReprepare)!=0 ){ |
| 86962 | 86993 | pVal = valueNew(db, pAlloc); |
| 86963 | 86994 | if( pVal ){ |
| 86964 | 86995 | rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]); |
| 86965 | 86996 | sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)); |
| | @@ -87332,11 +87363,12 @@ |
| 87332 | 87363 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 87333 | 87364 | zTmp = pA->zNormSql; |
| 87334 | 87365 | pA->zNormSql = pB->zNormSql; |
| 87335 | 87366 | pB->zNormSql = zTmp; |
| 87336 | 87367 | #endif |
| 87337 | | - pB->expmask = pA->expmask; |
| 87368 | + pB->expmask |= pA->expmask; |
| 87369 | + pB->smimask |= pA->smimask; |
| 87338 | 87370 | pB->prepFlags = pA->prepFlags; |
| 87339 | 87371 | memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter)); |
| 87340 | 87372 | pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++; |
| 87341 | 87373 | } |
| 87342 | 87374 | |
| | @@ -92520,21 +92552,32 @@ |
| 92520 | 92552 | } |
| 92521 | 92553 | |
| 92522 | 92554 | /* |
| 92523 | 92555 | ** Configure SQL variable iVar so that binding a new value to it signals |
| 92524 | 92556 | ** to sqlite3_reoptimize() that re-preparing the statement may result |
| 92525 | | -** in a better query plan. |
| 92557 | +** in a better query plan. If parameter bSmallint is true, then the |
| 92558 | +** statement is only re-prepared if the new value is integer value 0 or 1. |
| 92559 | +** |
| 92560 | +** The v->expmask bit is always set. expmask means that a reprepare is |
| 92561 | +** possible. The v->smimask bit is only set if we want to restrict |
| 92562 | +** reprepare when the value changes from (0,1) to something else, or from |
| 92563 | +** something else to (0,1). |
| 92526 | 92564 | */ |
| 92527 | | -SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){ |
| 92565 | +SQLITE_PRIVATE void sqlite3VdbeReprepareOnBind(Vdbe *v, int iVar, int bSmallint){ |
| 92566 | + u32 m; |
| 92528 | 92567 | assert( iVar>0 ); |
| 92529 | 92568 | assert( (v->db->flags & SQLITE_EnableQPSG)==0 |
| 92530 | 92569 | || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 ); |
| 92531 | | - if( iVar>=32 ){ |
| 92532 | | - v->expmask |= 0x80000000; |
| 92533 | | - }else{ |
| 92534 | | - v->expmask |= ((u32)1 << (iVar-1)); |
| 92570 | + |
| 92571 | + m = (iVar>=32) ? 0x80000000 : ((u32)1 << (iVar-1)); |
| 92572 | + v->expmask |= m; |
| 92573 | + if( bSmallint ){ |
| 92574 | + v->smimask |= m; |
| 92535 | 92575 | } |
| 92576 | + |
| 92577 | + /* smimask is always a subset of expmask */ |
| 92578 | + assert( (v->smimask & v->expmask) == v->smimask ); |
| 92536 | 92579 | } |
| 92537 | 92580 | |
| 92538 | 92581 | /* |
| 92539 | 92582 | ** Helper function for vdbeIsMatchingIndexKey(). Return true if column |
| 92540 | 92583 | ** iCol should be ignored when comparing a record with a record from |
| | @@ -94695,11 +94738,15 @@ |
| 94695 | 94738 | ** for a statement, then the statement will be automatically recompiled, |
| 94696 | 94739 | ** as if there had been a schema change, on the first sqlite3_step() call |
| 94697 | 94740 | ** following any change to the bindings of that parameter. |
| 94698 | 94741 | */ |
| 94699 | 94742 | assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); |
| 94743 | + assert( (p->expmask & p->smimask)==p->smimask ); |
| 94700 | 94744 | if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){ |
| 94745 | + /* We might avoid a reprepare here if p->smimask is set and the old |
| 94746 | + ** value is an integer other than (0,1). But that is such a corner |
| 94747 | + ** case that it does not seem worth the extra code to implement. */ |
| 94701 | 94748 | p->expired = 1; |
| 94702 | 94749 | } |
| 94703 | 94750 | return SQLITE_OK; |
| 94704 | 94751 | } |
| 94705 | 94752 | |
| | @@ -94794,19 +94841,50 @@ |
| 94794 | 94841 | } |
| 94795 | 94842 | SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ |
| 94796 | 94843 | return sqlite3_bind_int64(p, i, (i64)iValue); |
| 94797 | 94844 | } |
| 94798 | 94845 | SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ |
| 94799 | | - int rc; |
| 94800 | | - Vdbe *p = (Vdbe *)pStmt; |
| 94801 | | - rc = vdbeUnbind(p, (u32)(i-1)); |
| 94802 | | - if( rc==SQLITE_OK ){ |
| 94803 | | - assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ |
| 94804 | | - sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); |
| 94846 | + Mem *pVar; |
| 94847 | + Vdbe *p = (Vdbe*)pStmt; |
| 94848 | + if( vdbeSafetyNotNull(p) ){ |
| 94849 | + return SQLITE_MISUSE_BKPT; |
| 94850 | + } |
| 94851 | + sqlite3_mutex_enter(p->db->mutex); |
| 94852 | + if( p->eVdbeState!=VDBE_READY_STATE ){ |
| 94853 | + sqlite3Error(p->db, SQLITE_MISUSE_BKPT); |
| 94854 | + sqlite3_mutex_leave(p->db->mutex); |
| 94855 | + sqlite3_log(SQLITE_MISUSE, |
| 94856 | + "bind on a busy prepared statement: [%s]", p->zSql); |
| 94857 | + return SQLITE_MISUSE_BKPT; |
| 94858 | + } |
| 94859 | + if( i<=0 || (--i)>=p->nVar ){ |
| 94860 | + sqlite3Error(p->db, SQLITE_RANGE); |
| 94805 | 94861 | sqlite3_mutex_leave(p->db->mutex); |
| 94862 | + return SQLITE_RANGE; |
| 94806 | 94863 | } |
| 94807 | | - return rc; |
| 94864 | + pVar = &p->aVar[i]; |
| 94865 | + if( p->expmask!=0 ){ |
| 94866 | + u32 expireMask = i>=32 ? 0x80000000 : (u32)1 << i; |
| 94867 | + assert( (p->expmask & p->smimask)==p->smimask ); |
| 94868 | + if( (p->expmask & expireMask)!=0 ){ |
| 94869 | + if( (p->smimask & expireMask)!=0 ){ |
| 94870 | + /* If the smimask bit is set, only expire the prepared statement |
| 94871 | + ** if the value is changing to or from (0,1) and something else */ |
| 94872 | + int sm1 = (pVar->flags & MEM_Int)!=0 && pVar->u.i>=0 && pVar->u.i<=1; |
| 94873 | + int sm2 = iValue>=0 && iValue<=1; |
| 94874 | + if( sm1!=sm2 ){ |
| 94875 | + p->expired = 1; |
| 94876 | + } |
| 94877 | + }else if( (pVar->flags & MEM_Int)==0 || pVar->u.i!=iValue ){ |
| 94878 | + /* Always expire if the value really is changing */ |
| 94879 | + p->expired = 1; |
| 94880 | + } |
| 94881 | + } |
| 94882 | + } |
| 94883 | + sqlite3VdbeMemSetInt64(pVar, iValue); |
| 94884 | + sqlite3_mutex_leave(p->db->mutex); |
| 94885 | + return SQLITE_OK; |
| 94808 | 94886 | } |
| 94809 | 94887 | SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ |
| 94810 | 94888 | int rc; |
| 94811 | 94889 | Vdbe *p = (Vdbe*)pStmt; |
| 94812 | 94890 | rc = vdbeUnbind(p, (u32)(i-1)); |
| | @@ -111721,11 +111799,11 @@ |
| 111721 | 111799 | NameContext nc; /* Name context for resolving pE */ |
| 111722 | 111800 | sqlite3 *db; /* Database connection */ |
| 111723 | 111801 | int rc; /* Return code from subprocedures */ |
| 111724 | 111802 | u8 savedSuppErr; /* Saved value of db->suppressErr */ |
| 111725 | 111803 | |
| 111726 | | - assert( sqlite3ExprIsInteger(pE, &i, 0)==0 ); |
| 111804 | + assert( sqlite3ExprIsInteger(pE, &i, 0, 0)==0 ); |
| 111727 | 111805 | pEList = pSelect->pEList; |
| 111728 | 111806 | |
| 111729 | 111807 | /* Resolve all names in the ORDER BY term expression |
| 111730 | 111808 | */ |
| 111731 | 111809 | memset(&nc, 0, sizeof(nc)); |
| | @@ -111820,11 +111898,11 @@ |
| 111820 | 111898 | int iCol = -1; |
| 111821 | 111899 | Expr *pE, *pDup; |
| 111822 | 111900 | if( pItem->fg.done ) continue; |
| 111823 | 111901 | pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr); |
| 111824 | 111902 | if( NEVER(pE==0) ) continue; |
| 111825 | | - if( sqlite3ExprIsInteger(pE, &iCol, 0) ){ |
| 111903 | + if( sqlite3ExprIsInteger(pE, &iCol, 0, 0) ){ |
| 111826 | 111904 | if( iCol<=0 || iCol>pEList->nExpr ){ |
| 111827 | 111905 | resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE); |
| 111828 | 111906 | return 1; |
| 111829 | 111907 | } |
| 111830 | 111908 | }else{ |
| | @@ -112003,11 +112081,11 @@ |
| 112003 | 112081 | ** copy of the iCol-th result-set expression. */ |
| 112004 | 112082 | pItem->u.x.iOrderByCol = (u16)iCol; |
| 112005 | 112083 | continue; |
| 112006 | 112084 | } |
| 112007 | 112085 | } |
| 112008 | | - if( sqlite3ExprIsInteger(pE2, &iCol, 0) ){ |
| 112086 | + if( sqlite3ExprIsInteger(pE2, &iCol, 0, 0) ){ |
| 112009 | 112087 | /* The ORDER BY term is an integer constant. Again, set the column |
| 112010 | 112088 | ** number so that sqlite3ResolveOrderGroupBy() will convert the |
| 112011 | 112089 | ** order-by term to a copy of the result-set expression */ |
| 112012 | 112090 | if( iCol<1 || iCol>0xffff ){ |
| 112013 | 112091 | resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2); |
| | @@ -115041,11 +115119,11 @@ |
| 115041 | 115119 | db = pWalker->pParse->db; |
| 115042 | 115120 | pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0); |
| 115043 | 115121 | if( pDef==0 |
| 115044 | 115122 | || pDef->xFinalize!=0 |
| 115045 | 115123 | || (pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 |
| 115046 | | - || ExprHasProperty(pExpr, EP_WinFunc) |
| 115124 | + || NEVER(ExprHasProperty(pExpr, EP_WinFunc)) |
| 115047 | 115125 | ){ |
| 115048 | 115126 | pWalker->eCode = 0; |
| 115049 | 115127 | return WRC_Abort; |
| 115050 | 115128 | } |
| 115051 | 115129 | return WRC_Prune; |
| | @@ -115089,23 +115167,29 @@ |
| 115089 | 115167 | pWalker->eCode = 0; |
| 115090 | 115168 | return WRC_Abort; |
| 115091 | 115169 | } |
| 115092 | 115170 | |
| 115093 | 115171 | switch( pExpr->op ){ |
| 115094 | | - /* Consider functions to be constant if all their arguments are constant |
| 115095 | | - ** and either pWalker->eCode==4 or 5 or the function has the |
| 115096 | | - ** SQLITE_FUNC_CONST flag. */ |
| 115097 | 115172 | case TK_FUNCTION: |
| 115098 | | - if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc)) |
| 115099 | | - && !ExprHasProperty(pExpr, EP_WinFunc) |
| 115100 | | - ){ |
| 115173 | + if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 115174 | + pWalker->eCode = 0; /* Window functions are never constant */ |
| 115175 | + return WRC_Abort; |
| 115176 | + }else if( pWalker->eCode>=4 && pWalker->eCode<=5 ){ |
| 115177 | + /* Functions used within a CREATE TABLE statement are constant as |
| 115178 | + ** long as all of their arguments are constant */ |
| 115101 | 115179 | if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL); |
| 115102 | 115180 | return WRC_Continue; |
| 115181 | + }else if( ExprHasProperty(pExpr,EP_ConstFunc) ){ |
| 115182 | + /* Functions marked with EP_ConstFunc are constant as long as |
| 115183 | + ** all their arugments are constant and they are not window functions */ |
| 115184 | + return WRC_Continue; |
| 115103 | 115185 | }else if( pWalker->pParse ){ |
| 115186 | + /* Functions are constant if they have SQLITE_FUNC_CONSTANT or |
| 115187 | + ** SQLITE_FUNC_SLOCHNG and if all arguments are constant */ |
| 115104 | 115188 | return exprNodeIsConstantFunction(pWalker, pExpr); |
| 115105 | 115189 | }else{ |
| 115106 | | - pWalker->eCode = 0; |
| 115190 | + pWalker->eCode = 0; /* Function call is not constant */ |
| 115107 | 115191 | return WRC_Abort; |
| 115108 | 115192 | } |
| 115109 | 115193 | case TK_ID: |
| 115110 | 115194 | /* Convert "true" or "false" in a DEFAULT clause into the |
| 115111 | 115195 | ** appropriate TK_TRUEFALSE operator */ |
| | @@ -115449,69 +115533,79 @@ |
| 115449 | 115533 | return w.eCode==0; |
| 115450 | 115534 | } |
| 115451 | 115535 | #endif |
| 115452 | 115536 | |
| 115453 | 115537 | /* |
| 115454 | | -** If the expression p codes a constant integer that is small enough |
| 115455 | | -** to fit in a 32-bit integer, return 1 and put the value of the integer |
| 115456 | | -** in *pValue. If the expression is not an integer or if it is too big |
| 115457 | | -** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. |
| 115538 | +** If the expression p codes a constant integer between 0 and 0x7fffffff, |
| 115539 | +** then return 1 and put the value of the integer in *pValue. If the |
| 115540 | +** expression is not an integer or if it is an integer that is out side |
| 115541 | +** the range of 0...0x7fffffff, then return 0 and leave *pValue unchanged. |
| 115458 | 115542 | ** |
| 115459 | 115543 | ** If the pParse pointer is provided, then allow the expression p to be |
| 115460 | | -** a parameter (TK_VARIABLE) that is bound to an integer. |
| 115461 | | -** But if pParse is NULL, then p must be a pure integer literal. |
| 115544 | +** a parameter (TK_VARIABLE) that is bound to an integer between 0 and |
| 115545 | +** 0x7fffffff. Variables that hold anything other than integers, or that |
| 115546 | +** hold integers outside the range of 0..0x7fffffff are not seen. |
| 115547 | +** But if pParse is NULL, then p must be a pure integer literal between |
| 115548 | +** 0 and 0x7fffffff. |
| 115549 | +** |
| 115550 | +** If pParse is not NULL and expression p is a variable, then the variable |
| 115551 | +** is marked so as to cause the statement to be reprepared each time a new |
| 115552 | +** value is bound to it. Except, if parameter bRSI is true, then the statement |
| 115553 | +** will only be reprepared if the rebind changes the value to or from a |
| 115554 | +** "small integer" (either 0 or 1). Note that if p is a variable then |
| 115555 | +** reprepare is always enabled for that variable, regardless of its current |
| 115556 | +** binding. The RSI is only enabled if the current binding is a small |
| 115557 | +** integer. "RSI" stands for "Reprepare Small Integers". |
| 115462 | 115558 | */ |
| 115463 | | -SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr *p, int *pValue, Parse *pParse){ |
| 115464 | | - int rc = 0; |
| 115465 | | - if( NEVER(p==0) ) return 0; /* Used to only happen following on OOM */ |
| 115466 | | - |
| 115467 | | - /* If an expression is an integer literal that fits in a signed 32-bit |
| 115468 | | - ** integer, then the EP_IntValue flag will have already been set */ |
| 115469 | | - assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 |
| 115470 | | - || sqlite3GetInt32(p->u.zToken, &rc)==0 ); |
| 115471 | | - |
| 115472 | | - if( p->flags & EP_IntValue ){ |
| 115473 | | - *pValue = p->u.iValue; |
| 115474 | | - return 1; |
| 115475 | | - } |
| 115476 | | - switch( p->op ){ |
| 115477 | | - case TK_UPLUS: { |
| 115478 | | - rc = sqlite3ExprIsInteger(p->pLeft, pValue, 0); |
| 115479 | | - break; |
| 115480 | | - } |
| 115481 | | - case TK_UMINUS: { |
| 115482 | | - int v = 0; |
| 115483 | | - if( sqlite3ExprIsInteger(p->pLeft, &v, 0) ){ |
| 115484 | | - assert( ((unsigned int)v)!=0x80000000 ); |
| 115485 | | - *pValue = -v; |
| 115486 | | - rc = 1; |
| 115487 | | - } |
| 115488 | | - break; |
| 115489 | | - } |
| 115490 | | - case TK_VARIABLE: { |
| 115491 | | - sqlite3_value *pVal; |
| 115492 | | - if( pParse==0 ) break; |
| 115559 | +SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr *p, int *pValue, Parse *pParse, int bRSI){ |
| 115560 | + int iSign = 1; /* Either +1 or -1. */ |
| 115561 | + while( 1/*exit-by-break*/ ){ |
| 115562 | + if( NEVER(p==0) ) return 0; |
| 115563 | + if( ExprUseUValue(p) ){ |
| 115564 | + *pValue = p->u.iValue*iSign; |
| 115565 | + return 1; |
| 115566 | + } |
| 115567 | + if( p->op==TK_UPLUS ){ |
| 115568 | + p = p->pLeft; |
| 115569 | + pParse = 0; |
| 115570 | + continue; |
| 115571 | + } |
| 115572 | + if( p->op==TK_UMINUS ){ |
| 115573 | + iSign = -iSign; |
| 115574 | + p = p->pLeft; |
| 115575 | + pParse = 0; |
| 115576 | + continue; |
| 115577 | + } |
| 115578 | + if( p->op==TK_VARIABLE && pParse!=0 ){ |
| 115579 | + sqlite3_value *pVal; /* The variable */ |
| 115580 | + int isSmall = 0; /* Only reprepare if change to/from small integer */ |
| 115581 | + int rc = 0; /* 1 if successful, 0 if failed */ |
| 115582 | + assert( iSign==1 ); |
| 115493 | 115583 | if( NEVER(pParse->pVdbe==0) ) break; |
| 115494 | 115584 | if( (pParse->db->flags & SQLITE_EnableQPSG)!=0 ) break; |
| 115495 | | - sqlite3VdbeSetVarmask(pParse->pVdbe, p->iColumn); |
| 115496 | 115585 | pVal = sqlite3VdbeGetBoundValue(pParse->pReprepare, p->iColumn, |
| 115497 | 115586 | SQLITE_AFF_BLOB); |
| 115498 | 115587 | if( pVal ){ |
| 115499 | | - if( sqlite3_value_type(pVal)==SQLITE_INTEGER ){ |
| 115500 | | - sqlite3_int64 vv = sqlite3_value_int64(pVal); |
| 115501 | | - if( vv == (vv & 0x7fffffff) ){ /* non-negative numbers only */ |
| 115502 | | - *pValue = (int)vv; |
| 115503 | | - rc = 1; |
| 115588 | + i64 vv; |
| 115589 | + if( sqlite3_value_type(pVal)==SQLITE_INTEGER |
| 115590 | + && (vv = sqlite3_value_int64(pVal))>=0 |
| 115591 | + && vv<=0x7fffffff |
| 115592 | + ){ |
| 115593 | + *pValue = (int)vv; |
| 115594 | + if( bRSI ){ |
| 115595 | + isSmall = vv<=1; |
| 115504 | 115596 | } |
| 115597 | + rc = 1; |
| 115505 | 115598 | } |
| 115506 | 115599 | sqlite3ValueFree(pVal); |
| 115507 | 115600 | } |
| 115508 | | - break; |
| 115601 | + sqlite3VdbeReprepareOnBind(pParse->pVdbe, p->iColumn, isSmall); |
| 115602 | + return rc; |
| 115509 | 115603 | } |
| 115510 | | - default: break; |
| 115604 | + break; |
| 115511 | 115605 | } |
| 115512 | | - return rc; |
| 115606 | + return 0; |
| 115513 | 115607 | } |
| 115514 | 115608 | |
| 115515 | 115609 | /* |
| 115516 | 115610 | ** Return FALSE if there is no chance that the expression can be NULL. |
| 115517 | 115611 | ** |
| | @@ -115674,17 +115768,28 @@ |
| 115674 | 115768 | ** Generate code that checks the left-most column of index table iCur to see if |
| 115675 | 115769 | ** it contains any NULL entries. Cause the register at regHasNull to be set |
| 115676 | 115770 | ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull |
| 115677 | 115771 | ** to be set to NULL if iCur contains one or more NULL values. |
| 115678 | 115772 | */ |
| 115679 | | -static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ |
| 115773 | +static void sqlite3SetHasNullFlag( |
| 115774 | + Vdbe *v, /* Write new code into this statement under construction */ |
| 115775 | + int iCur, /* Cursor for the index */ |
| 115776 | + int regHasNull, /* Register in which to store hasNull flag */ |
| 115777 | + int eSortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ |
| 115778 | +){ |
| 115680 | 115779 | int addr1; |
| 115780 | + int op; |
| 115681 | 115781 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); |
| 115682 | | - addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); |
| 115782 | + if( eSortOrder==SQLITE_SO_ASC ){ |
| 115783 | + op = OP_Rewind; |
| 115784 | + }else{ |
| 115785 | + op = OP_Last; |
| 115786 | + } |
| 115787 | + addr1 = sqlite3VdbeAddOp1(v, op, iCur); VdbeCoverage(v); |
| 115683 | 115788 | sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); |
| 115684 | 115789 | sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
| 115685 | | - VdbeComment((v, "first_entry_in(%d)", iCur)); |
| 115790 | + VdbeComment((v, op==OP_Last?"last_entry_in(%d)":"first_entry_in(%d)", iCur)); |
| 115686 | 115791 | sqlite3VdbeJumpHere(v, addr1); |
| 115687 | 115792 | } |
| 115688 | 115793 | #endif |
| 115689 | 115794 | |
| 115690 | 115795 | |
| | @@ -115947,11 +116052,12 @@ |
| 115947 | 116052 | sqlite3VdbeAddOp3(v, OP_ColumnsUsed, |
| 115948 | 116053 | iTab, LOWER32(mask), UPPER32(mask)); |
| 115949 | 116054 | #endif |
| 115950 | 116055 | *prRhsHasNull = ++pParse->nMem; |
| 115951 | 116056 | if( nExpr==1 ){ |
| 115952 | | - sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); |
| 116057 | + sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull, |
| 116058 | + pIdx->aSortOrder[0]); |
| 115953 | 116059 | } |
| 115954 | 116060 | } |
| 115955 | 116061 | sqlite3VdbeJumpHere(v, iAddr); |
| 115956 | 116062 | } |
| 115957 | 116063 | } /* End loop over indexes */ |
| | @@ -115996,11 +116102,11 @@ |
| 115996 | 116102 | ){ |
| 115997 | 116103 | bloomOk = 1; |
| 115998 | 116104 | } |
| 115999 | 116105 | sqlite3CodeRhsOfIN(pParse, pX, iTab, bloomOk); |
| 116000 | 116106 | if( rMayHaveNull ){ |
| 116001 | | - sqlite3SetHasNullFlag(v, iTab, rMayHaveNull); |
| 116107 | + sqlite3SetHasNullFlag(v, iTab, rMayHaveNull, SQLITE_SO_ASC); |
| 116002 | 116108 | } |
| 116003 | 116109 | pParse->nQueryLoop = savedNQueryLoop; |
| 116004 | 116110 | } |
| 116005 | 116111 | |
| 116006 | 116112 | if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){ |
| | @@ -116829,11 +116935,17 @@ |
| 116829 | 116935 | ** |
| 116830 | 116936 | ** For a scalar LHS, it is sufficient to check just the first row |
| 116831 | 116937 | ** of the RHS. |
| 116832 | 116938 | */ |
| 116833 | 116939 | if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6); |
| 116834 | | - addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, destIfFalse); |
| 116940 | + if( eType==IN_INDEX_INDEX_DESC ){ |
| 116941 | + addrTop = sqlite3VdbeAddOp2(v, OP_Last, iTab, destIfFalse); |
| 116942 | + }else{ |
| 116943 | + testcase( eType==IN_INDEX_EPH ); |
| 116944 | + testcase( eType==IN_INDEX_ROWID ); |
| 116945 | + addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, destIfFalse); |
| 116946 | + } |
| 116835 | 116947 | VdbeCoverage(v); |
| 116836 | 116948 | if( nVector>1 ){ |
| 116837 | 116949 | destNotNull = sqlite3VdbeMakeLabel(pParse); |
| 116838 | 116950 | }else{ |
| 116839 | 116951 | /* For nVector==1, combine steps 6 and 7 by immediately returning |
| | @@ -117019,16 +117131,16 @@ |
| 117019 | 117131 | assert( pTab!=0 ); |
| 117020 | 117132 | assert( iCol!=XN_EXPR ); |
| 117021 | 117133 | if( iCol<0 || iCol==pTab->iPKey ){ |
| 117022 | 117134 | sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); |
| 117023 | 117135 | VdbeComment((v, "%s.rowid", pTab->zName)); |
| 117024 | | - }else{ |
| 117025 | | - int op; |
| 117136 | + }else{ |
| 117026 | 117137 | int x; |
| 117027 | 117138 | if( IsVirtual(pTab) ){ |
| 117028 | | - op = OP_VColumn; |
| 117029 | 117139 | x = iCol; |
| 117140 | + sqlite3VdbeAddOp3(v, OP_VColumn, iTabCur, x, regOut); |
| 117141 | + return; |
| 117030 | 117142 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 117031 | 117143 | }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){ |
| 117032 | 117144 | Parse *pParse = sqlite3VdbeParser(v); |
| 117033 | 117145 | if( pCol->colFlags & COLFLAG_BUSY ){ |
| 117034 | 117146 | sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", |
| | @@ -117044,17 +117156,15 @@ |
| 117044 | 117156 | return; |
| 117045 | 117157 | #endif |
| 117046 | 117158 | }else if( !HasRowid(pTab) ){ |
| 117047 | 117159 | testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) ); |
| 117048 | 117160 | x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol); |
| 117049 | | - op = OP_Column; |
| 117050 | 117161 | }else{ |
| 117051 | 117162 | x = sqlite3TableColumnToStorage(pTab,iCol); |
| 117052 | 117163 | testcase( x!=iCol ); |
| 117053 | | - op = OP_Column; |
| 117054 | 117164 | } |
| 117055 | | - sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); |
| 117165 | + sqlite3VdbeAddOp3(v, OP_Column, iTabCur, x, regOut); |
| 117056 | 117166 | sqlite3ColumnDefault(v, pTab, iCol, regOut); |
| 117057 | 117167 | } |
| 117058 | 117168 | } |
| 117059 | 117169 | |
| 117060 | 117170 | /* |
| | @@ -117433,10 +117543,15 @@ |
| 117433 | 117543 | addr = sqlite3VdbeAddOp1(v, OP_IfNullRow, p->iIdxCur); |
| 117434 | 117544 | } |
| 117435 | 117545 | ret = sqlite3ExprCodeTarget(pParse, p->pExpr, iTarget); |
| 117436 | 117546 | sqlite3VdbeAddOp4(pParse->pVdbe, OP_Affinity, ret, 1, 0, |
| 117437 | 117547 | (const char*)&p->aff, 1); |
| 117548 | + if( sqlite3ExprCanReturnSubtype(pParse, p->pExpr) ){ |
| 117549 | + /* If the expression value may have a sub-type, clear it. Values |
| 117550 | + ** read from columns do not have subtypes. */ |
| 117551 | + sqlite3VdbeAddOp1(pParse->pVdbe, OP_ClrSubtype, ret); |
| 117552 | + } |
| 117438 | 117553 | if( addr ){ |
| 117439 | 117554 | sqlite3VdbeJumpHere(v, addr); |
| 117440 | 117555 | sqlite3VdbeChangeP3(v, addr, ret); |
| 117441 | 117556 | } |
| 117442 | 117557 | return ret; |
| | @@ -119107,11 +119222,11 @@ |
| 119107 | 119222 | } |
| 119108 | 119223 | if( (pParse->db->flags & SQLITE_EnableQPSG)!=0 ) return 2; |
| 119109 | 119224 | sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR); |
| 119110 | 119225 | if( pR ){ |
| 119111 | 119226 | iVar = pVar->iColumn; |
| 119112 | | - sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); |
| 119227 | + sqlite3VdbeReprepareOnBind(pParse->pVdbe, iVar, 0); |
| 119113 | 119228 | pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB); |
| 119114 | 119229 | if( pL ){ |
| 119115 | 119230 | if( sqlite3_value_type(pL)==SQLITE_TEXT ){ |
| 119116 | 119231 | sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */ |
| 119117 | 119232 | } |
| | @@ -119363,11 +119478,11 @@ |
| 119363 | 119478 | static int sqlite3ExprIsNotTrue(Expr *pExpr){ |
| 119364 | 119479 | int v; |
| 119365 | 119480 | if( pExpr->op==TK_NULL ) return 1; |
| 119366 | 119481 | if( pExpr->op==TK_TRUEFALSE && sqlite3ExprTruthValue(pExpr)==0 ) return 1; |
| 119367 | 119482 | v = 1; |
| 119368 | | - if( sqlite3ExprIsInteger(pExpr, &v, 0) && v==0 ) return 1; |
| 119483 | + if( sqlite3ExprIsInteger(pExpr, &v, 0, 0) && v==0 ) return 1; |
| 119369 | 119484 | return 0; |
| 119370 | 119485 | } |
| 119371 | 119486 | |
| 119372 | 119487 | /* |
| 119373 | 119488 | ** Return true if the expression is one of the following: |
| | @@ -120613,10 +120728,34 @@ |
| 120613 | 120728 | sqlite3NestedParse(pParse, |
| 120614 | 120729 | "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"", |
| 120615 | 120730 | zErr, zDb, zTab |
| 120616 | 120731 | ); |
| 120617 | 120732 | } |
| 120733 | + |
| 120734 | +/* |
| 120735 | +** zCol is a column name used in an ALTER TABLE DROP, ADD or RENAME COLUMN |
| 120736 | +** operation. zOp identifies the specific operation - "drop", "add", "rename |
| 120737 | +** to" or "rename from". pTab is the table being altered. |
| 120738 | +** |
| 120739 | +** If pTab has a rowid and zCol is a rowid alias, then SQLITE_ERROR is |
| 120740 | +** returned and an error message left in pParse. Or, if zCol is not an alias |
| 120741 | +** for "rowid" or pTab is not an intkey table, then SQLITE_OK is returned. |
| 120742 | +*/ |
| 120743 | +static int isRowidAlias( |
| 120744 | + Parse *pParse, |
| 120745 | + Table *pTab, |
| 120746 | + const char *zCol, |
| 120747 | + const char *zOp |
| 120748 | +){ |
| 120749 | + if( HasRowid(pTab) && sqlite3IsRowid(zCol) ){ |
| 120750 | + sqlite3ErrorMsg(pParse, "cannot %s rowid alias: %s", zOp, zCol); |
| 120751 | + return SQLITE_ERROR; |
| 120752 | + } |
| 120753 | + return SQLITE_OK; |
| 120754 | +} |
| 120755 | + |
| 120756 | + |
| 120618 | 120757 | |
| 120619 | 120758 | /* |
| 120620 | 120759 | ** This function is called after an "ALTER TABLE ... ADD" statement |
| 120621 | 120760 | ** has been parsed. Argument pColDef contains the text of the new |
| 120622 | 120761 | ** column definition. |
| | @@ -120659,13 +120798,13 @@ |
| 120659 | 120798 | return; |
| 120660 | 120799 | } |
| 120661 | 120800 | #endif |
| 120662 | 120801 | |
| 120663 | 120802 | |
| 120664 | | - /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. |
| 120665 | | - ** If there is a NOT NULL constraint, then the default value for the |
| 120666 | | - ** column must not be NULL. |
| 120803 | + /* Check that the new column is not specified as PRIMARY KEY or UNIQUE, |
| 120804 | + ** or a rowid alias. If there is a NOT NULL constraint, then the default |
| 120805 | + ** value for the column must not be NULL. |
| 120667 | 120806 | */ |
| 120668 | 120807 | if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
| 120669 | 120808 | sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); |
| 120670 | 120809 | return; |
| 120671 | 120810 | } |
| | @@ -120672,10 +120811,11 @@ |
| 120672 | 120811 | if( pNew->pIndex ){ |
| 120673 | 120812 | sqlite3ErrorMsg(pParse, |
| 120674 | 120813 | "Cannot add a UNIQUE column"); |
| 120675 | 120814 | return; |
| 120676 | 120815 | } |
| 120816 | + if( isRowidAlias(pParse, pTab, pCol->zCnName, "add") ) return; |
| 120677 | 120817 | if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){ |
| 120678 | 120818 | /* If the default value for the new column was specified with a |
| 120679 | 120819 | ** literal NULL, then set pDflt to 0. This simplifies checking |
| 120680 | 120820 | ** for an SQL NULL default below. |
| 120681 | 120821 | */ |
| | @@ -120964,10 +121104,12 @@ |
| 120964 | 121104 | ** CREATE statement text for the sqlite_schema table. |
| 120965 | 121105 | */ |
| 120966 | 121106 | sqlite3MayAbort(pParse); |
| 120967 | 121107 | zNew = sqlite3NameFromToken(db, pNew); |
| 120968 | 121108 | if( !zNew ) goto exit_rename_column; |
| 121109 | + if( isRowidAlias(pParse, pTab, zOld, "rename from") ) goto exit_rename_column; |
| 121110 | + if( isRowidAlias(pParse, pTab, zNew, "rename to") ) goto exit_rename_column; |
| 120969 | 121111 | assert( pNew->n>0 ); |
| 120970 | 121112 | bQuote = sqlite3Isquote(pNew->z[0]); |
| 120971 | 121113 | sqlite3NestedParse(pParse, |
| 120972 | 121114 | "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " |
| 120973 | 121115 | "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) " |
| | @@ -122590,10 +122732,11 @@ |
| 122590 | 122732 | iCol = sqlite3ColumnIndex(pTab, zCol); |
| 122591 | 122733 | if( iCol<0 ){ |
| 122592 | 122734 | sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pName); |
| 122593 | 122735 | goto exit_drop_column; |
| 122594 | 122736 | } |
| 122737 | + if( isRowidAlias(pParse, pTab, zCol, "drop") ) goto exit_drop_column; |
| 122595 | 122738 | |
| 122596 | 122739 | /* Do not allow the user to drop a PRIMARY KEY column or a column |
| 122597 | 122740 | ** constrained by a UNIQUE constraint. */ |
| 122598 | 122741 | if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){ |
| 122599 | 122742 | sqlite3ErrorMsg(pParse, "cannot drop %s column: \"%s\"", |
| | @@ -127037,21 +127180,61 @@ |
| 127037 | 127180 | sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr); |
| 127038 | 127181 | pList->a[pCol->iDflt-1].pExpr = pExpr; |
| 127039 | 127182 | } |
| 127040 | 127183 | } |
| 127041 | 127184 | |
| 127185 | +#ifndef SQLITE_OMIT_AUTHORIZATION |
| 127186 | +/* |
| 127187 | +** If the expression contains any TK_FUNCTION, send it to the authorizer |
| 127188 | +** and if the authorization fails then raise an error. |
| 127189 | +*/ |
| 127190 | +static int authFunctions(Walker *pWalker, Expr *p){ |
| 127191 | + if( p->op==TK_FUNCTION ){ |
| 127192 | + Parse *pParse = pWalker->pParse; |
| 127193 | + int rc = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, p->u.zToken, 0); |
| 127194 | + if( rc ){ |
| 127195 | + sqlite3ErrorMsg(pParse, "not authorized to use function: %#T", p); |
| 127196 | + return WRC_Abort; |
| 127197 | + } |
| 127198 | + } |
| 127199 | + return WRC_Continue; |
| 127200 | +} |
| 127201 | +static void sqlite3FuncAuth(Parse *pParse, Expr *pExpr){ |
| 127202 | + Walker w; |
| 127203 | + memset(&w, 0, sizeof(w)); |
| 127204 | + w.xExprCallback = authFunctions; |
| 127205 | + w.pParse = pParse; |
| 127206 | + sqlite3WalkExpr(&w,pExpr); |
| 127207 | +} |
| 127208 | +#endif /* SQLITE_OMIT_AUTHORIZATION */ |
| 127209 | + |
| 127042 | 127210 | /* |
| 127043 | 127211 | ** Return the expression associated with a column. The expression might be |
| 127044 | 127212 | ** the DEFAULT clause or the AS clause of a generated column. |
| 127045 | 127213 | ** Return NULL if the column has no associated expression. |
| 127214 | +** |
| 127215 | +** There are two variants of this routine. sqlite3ColumnExpr() just |
| 127216 | +** returns the expression with no side effects. sqlite3ColumnExprAuth() |
| 127217 | +** takes the extra step of invoking the authorizer (if one is defined) |
| 127218 | +** and raising an error if the returned expression uses any unauthorized |
| 127219 | +** SQL function. |
| 127046 | 127220 | */ |
| 127047 | 127221 | SQLITE_PRIVATE Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){ |
| 127048 | 127222 | if( pCol->iDflt==0 ) return 0; |
| 127049 | 127223 | if( !IsOrdinaryTable(pTab) ) return 0; |
| 127050 | 127224 | if( NEVER(pTab->u.tab.pDfltList==0) ) return 0; |
| 127051 | 127225 | if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0; |
| 127052 | 127226 | return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; |
| 127227 | +} |
| 127228 | +SQLITE_PRIVATE Expr *sqlite3ColumnExprAuth(Table *pTab, Column *pCol, Parse *pParse){ |
| 127229 | + Expr *pExpr = sqlite3ColumnExpr(pTab,pCol); |
| 127230 | +#ifndef SQLITE_OMIT_AUTHORIZATION |
| 127231 | + if( pParse->db->xAuth!=0 ){ |
| 127232 | + sqlite3FuncAuth(pParse, pExpr); |
| 127233 | + } |
| 127234 | +#endif |
| 127235 | + return pExpr; |
| 127053 | 127236 | } |
| 127054 | 127237 | |
| 127055 | 127238 | /* |
| 127056 | 127239 | ** Suppress false-positive warning message generated with -O3 in GCC |
| 127057 | 127240 | ** on the second call to sqlite3Strlen30() in the sqlite3ColumnSetColl() |
| | @@ -128115,10 +128298,17 @@ |
| 128115 | 128298 | x.pLeft = pExpr; |
| 128116 | 128299 | x.flags = EP_Skip; |
| 128117 | 128300 | pDfltExpr = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); |
| 128118 | 128301 | sqlite3DbFree(db, x.u.zToken); |
| 128119 | 128302 | sqlite3ColumnSetExpr(pParse, p, pCol, pDfltExpr); |
| 128303 | +#ifndef SQLITE_OMIT_AUTHORIZATION |
| 128304 | + /* Reject unauthorized functions from DEFAULT clauses */ |
| 128305 | + if( db->init.busy==0 && db->xAuth!=0 ){ |
| 128306 | + sqlite3FuncAuth(pParse, pExpr); |
| 128307 | + } |
| 128308 | +#endif /* SQLITE_OMIT_AUTHORIZER */ |
| 128309 | + |
| 128120 | 128310 | } |
| 128121 | 128311 | } |
| 128122 | 128312 | if( IN_RENAME_OBJECT ){ |
| 128123 | 128313 | sqlite3RenameExprUnmap(pParse, pExpr); |
| 128124 | 128314 | } |
| | @@ -128980,11 +129170,10 @@ |
| 128980 | 129170 | } |
| 128981 | 129171 | } |
| 128982 | 129172 | #else |
| 128983 | 129173 | #define markExprListImmutable(X) /* no-op */ |
| 128984 | 129174 | #endif /* SQLITE_DEBUG */ |
| 128985 | | - |
| 128986 | 129175 | |
| 128987 | 129176 | /* |
| 128988 | 129177 | ** This routine is called to report the final ")" that terminates |
| 128989 | 129178 | ** a CREATE TABLE statement. |
| 128990 | 129179 | ** |
| | @@ -138593,11 +138782,11 @@ |
| 138593 | 138782 | if( pCol->colFlags & COLFLAG_GENERATED ){ |
| 138594 | 138783 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 138595 | 138784 | testcase( pCol->colFlags & COLFLAG_STORED ); |
| 138596 | 138785 | pDflt = 0; |
| 138597 | 138786 | }else{ |
| 138598 | | - pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol); |
| 138787 | + pDflt = sqlite3ColumnExprAuth(pFKey->pFrom, pCol, pParse); |
| 138599 | 138788 | } |
| 138600 | 138789 | if( pDflt ){ |
| 138601 | 138790 | pNew = sqlite3ExprDup(db, pDflt, 0); |
| 138602 | 138791 | }else{ |
| 138603 | 138792 | pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0); |
| | @@ -140165,11 +140354,11 @@ |
| 140165 | 140354 | continue; |
| 140166 | 140355 | }else if( pColumn==0 ){ |
| 140167 | 140356 | /* Hidden columns that are not explicitly named in the INSERT |
| 140168 | 140357 | ** get their default value */ |
| 140169 | 140358 | sqlite3ExprCodeFactorable(pParse, |
| 140170 | | - sqlite3ColumnExpr(pTab, &pTab->aCol[i]), |
| 140359 | + sqlite3ColumnExprAuth(pTab, &pTab->aCol[i], pParse), |
| 140171 | 140360 | iRegStore); |
| 140172 | 140361 | continue; |
| 140173 | 140362 | } |
| 140174 | 140363 | } |
| 140175 | 140364 | if( pColumn ){ |
| | @@ -140177,19 +140366,19 @@ |
| 140177 | 140366 | assert( j>=0 && j<=pColumn->nId ); |
| 140178 | 140367 | if( j==0 ){ |
| 140179 | 140368 | /* A column not named in the insert column list gets its |
| 140180 | 140369 | ** default value */ |
| 140181 | 140370 | sqlite3ExprCodeFactorable(pParse, |
| 140182 | | - sqlite3ColumnExpr(pTab, &pTab->aCol[i]), |
| 140371 | + sqlite3ColumnExprAuth(pTab, &pTab->aCol[i], pParse), |
| 140183 | 140372 | iRegStore); |
| 140184 | 140373 | continue; |
| 140185 | 140374 | } |
| 140186 | 140375 | k = j - 1; |
| 140187 | 140376 | }else if( nColumn==0 ){ |
| 140188 | 140377 | /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */ |
| 140189 | 140378 | sqlite3ExprCodeFactorable(pParse, |
| 140190 | | - sqlite3ColumnExpr(pTab, &pTab->aCol[i]), |
| 140379 | + sqlite3ColumnExprAuth(pTab, &pTab->aCol[i], pParse), |
| 140191 | 140380 | iRegStore); |
| 140192 | 140381 | continue; |
| 140193 | 140382 | }else{ |
| 140194 | 140383 | k = i - nHidden; |
| 140195 | 140384 | } |
| | @@ -140783,11 +140972,11 @@ |
| 140783 | 140972 | int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg); |
| 140784 | 140973 | VdbeCoverage(v); |
| 140785 | 140974 | assert( (pCol->colFlags & COLFLAG_GENERATED)==0 ); |
| 140786 | 140975 | nSeenReplace++; |
| 140787 | 140976 | sqlite3ExprCodeCopy(pParse, |
| 140788 | | - sqlite3ColumnExpr(pTab, pCol), iReg); |
| 140977 | + sqlite3ColumnExprAuth(pTab, pCol, pParse), iReg); |
| 140789 | 140978 | sqlite3VdbeJumpHere(v, addr1); |
| 140790 | 140979 | break; |
| 140791 | 140980 | } |
| 140792 | 140981 | case OE_Abort: |
| 140793 | 140982 | sqlite3MayAbort(pParse); |
| | @@ -141439,11 +141628,11 @@ |
| 141439 | 141628 | VdbeOp x; /* Conflict check opcode to copy */ |
| 141440 | 141629 | /* The sqlite3VdbeAddOp4() call might reallocate the opcode array. |
| 141441 | 141630 | ** Hence, make a complete copy of the opcode, rather than using |
| 141442 | 141631 | ** a pointer to the opcode. */ |
| 141443 | 141632 | x = *sqlite3VdbeGetOp(v, addrConflictCk); |
| 141444 | | - if( x.opcode!=OP_IdxRowid ){ |
| 141633 | + if( x.opcode!=OP_IdxRowid || isUpdate ){ |
| 141445 | 141634 | int p2; /* New P2 value for copied conflict check opcode */ |
| 141446 | 141635 | const char *zP4; |
| 141447 | 141636 | if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){ |
| 141448 | 141637 | p2 = lblRecheckOk; |
| 141449 | 141638 | }else{ |
| | @@ -146693,12 +146882,12 @@ |
| 146693 | 146882 | p1 = -1; |
| 146694 | 146883 | p3 = 3; |
| 146695 | 146884 | }else{ |
| 146696 | 146885 | if( pCol->iDflt ){ |
| 146697 | 146886 | sqlite3_value *pDfltValue = 0; |
| 146698 | | - sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db), |
| 146699 | | - pCol->affinity, &pDfltValue); |
| 146887 | + sqlite3ValueFromExpr(db, sqlite3ColumnExprAuth(pTab,pCol,pParse), |
| 146888 | + ENC(db), pCol->affinity, &pDfltValue); |
| 146700 | 146889 | if( pDfltValue ){ |
| 146701 | 146890 | p4 = sqlite3_value_type(pDfltValue); |
| 146702 | 146891 | sqlite3ValueFree(pDfltValue); |
| 146703 | 146892 | } |
| 146704 | 146893 | } |
| | @@ -151506,13 +151695,11 @@ |
| 151506 | 151695 | */ |
| 151507 | 151696 | SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){ |
| 151508 | 151697 | if( pParse->pVdbe ){ |
| 151509 | 151698 | return pParse->pVdbe; |
| 151510 | 151699 | } |
| 151511 | | - if( pParse->pToplevel==0 |
| 151512 | | - && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst) |
| 151513 | | - ){ |
| 151700 | + if( pParse->pToplevel==0 ){ |
| 151514 | 151701 | pParse->okConstFactor = 1; |
| 151515 | 151702 | } |
| 151516 | 151703 | return sqlite3VdbeCreate(pParse); |
| 151517 | 151704 | } |
| 151518 | 151705 | |
| | @@ -151559,11 +151746,11 @@ |
| 151559 | 151746 | assert( pLimit->op==TK_LIMIT ); |
| 151560 | 151747 | assert( pLimit->pLeft!=0 ); |
| 151561 | 151748 | p->iLimit = iLimit = ++pParse->nMem; |
| 151562 | 151749 | v = sqlite3GetVdbe(pParse); |
| 151563 | 151750 | assert( v!=0 ); |
| 151564 | | - if( sqlite3ExprIsInteger(pLimit->pLeft, &n, pParse) ){ |
| 151751 | + if( sqlite3ExprIsInteger(pLimit->pLeft, &n, pParse, 0) ){ |
| 151565 | 151752 | sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit); |
| 151566 | 151753 | VdbeComment((v, "LIMIT counter")); |
| 151567 | 151754 | if( n==0 ){ |
| 151568 | 151755 | sqlite3VdbeGoto(v, iBreak); |
| 151569 | 151756 | }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){ |
| | @@ -151926,15 +152113,37 @@ |
| 151926 | 152113 | while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; } |
| 151927 | 152114 | return p!=0; |
| 151928 | 152115 | } |
| 151929 | 152116 | |
| 151930 | 152117 | /* |
| 151931 | | -** This routine is called to process a compound query form from |
| 152118 | +** Return TRUE if p is a UNION with a LIMIT of exactly 1 and no OFFSET |
| 152119 | +** clause. This is a precondition for a couple of related optimizations. |
| 152120 | +** |
| 152121 | +** False negatives are harmless (apart from resulting in a slower query). |
| 152122 | +** False positives can result in incorrect answers, however. To provoke |
| 152123 | +** false negatives for testing purposes, disable the SQLITE_UnionLimit |
| 152124 | +** optimization. |
| 152125 | +*/ |
| 152126 | +static int unionWithLimitOne(sqlite3 *db, Select *p){ |
| 152127 | + int v; |
| 152128 | + if( p->op!=TK_UNION ) return 0; |
| 152129 | + if( p->pLimit==0 ) return 0; |
| 152130 | + if( p->pLimit->pRight ) return 0; /* No OFFSET */ |
| 152131 | + v = 0; |
| 152132 | + assert( p->pLimit->pLeft!=0 ); |
| 152133 | + if( sqlite3ExprIsInteger(p->pLimit->pLeft, &v, 0, 0)==0 ) return 0; |
| 152134 | + if( v!=1 ) return 0; |
| 152135 | + if( OptimizationDisabled(db, SQLITE_UnionLimit) ) return 0; |
| 152136 | + return 1; |
| 152137 | +} |
| 152138 | + |
| 152139 | +/* |
| 152140 | +** This routine is called to process a compound query formed from |
| 151932 | 152141 | ** two or more separate queries using UNION, UNION ALL, EXCEPT, or |
| 151933 | 152142 | ** INTERSECT |
| 151934 | 152143 | ** |
| 151935 | | -** "p" points to the right-most of the two queries. the query on the |
| 152144 | +** "p" points to the right-most of the two queries. The query on the |
| 151936 | 152145 | ** left is p->pPrior. The left query could also be a compound query |
| 151937 | 152146 | ** in which case this routine will be called recursively. |
| 151938 | 152147 | ** |
| 151939 | 152148 | ** The results of the total query are to be written into a destination |
| 151940 | 152149 | ** of type eDest with parameter iParm. |
| | @@ -152014,11 +152223,11 @@ |
| 152014 | 152223 | #endif |
| 152015 | 152224 | if( p->pOrderBy ){ |
| 152016 | 152225 | /* If the compound has an ORDER BY clause, then always use the merge |
| 152017 | 152226 | ** algorithm. */ |
| 152018 | 152227 | return multiSelectByMerge(pParse, p, pDest); |
| 152019 | | - }else if( p->op!=TK_ALL ){ |
| 152228 | + }else if( p->op!=TK_ALL && !unionWithLimitOne(db,p) ){ |
| 152020 | 152229 | /* If the compound is EXCEPT, INTERSECT, or UNION (anything other than |
| 152021 | 152230 | ** UNION ALL) then also always use the merge algorithm. However, the |
| 152022 | 152231 | ** multiSelectByMerge() routine requires that the compound have an |
| 152023 | 152232 | ** ORDER BY clause, and it doesn't right now. So invent one first. */ |
| 152024 | 152233 | Expr *pOne = sqlite3ExprInt32(db, 1); |
| | @@ -152026,11 +152235,12 @@ |
| 152026 | 152235 | if( pParse->nErr ) goto multi_select_end; |
| 152027 | 152236 | assert( p->pOrderBy!=0 ); |
| 152028 | 152237 | p->pOrderBy->a[0].u.x.iOrderByCol = 1; |
| 152029 | 152238 | return multiSelectByMerge(pParse, p, pDest); |
| 152030 | 152239 | }else{ |
| 152031 | | - /* For a UNION ALL compound without ORDER BY, simply run the left |
| 152240 | + /* For a UNION ALL compound without ORDER BY, or for a UNION with a |
| 152241 | + ** LIMIT of exactly 1 and no OFFSET and no ORDER BY, simply run the left |
| 152032 | 152242 | ** query, then run the right query */ |
| 152033 | 152243 | int addr = 0; |
| 152034 | 152244 | int nLimit = 0; /* Initialize to suppress harmless compiler warning */ |
| 152035 | 152245 | |
| 152036 | 152246 | #ifndef SQLITE_OMIT_EXPLAIN |
| | @@ -152067,11 +152277,11 @@ |
| 152067 | 152277 | testcase( rc!=SQLITE_OK ); |
| 152068 | 152278 | pDelete = p->pPrior; |
| 152069 | 152279 | p->pPrior = pPrior; |
| 152070 | 152280 | p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
| 152071 | 152281 | if( p->pLimit |
| 152072 | | - && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit, pParse) |
| 152282 | + && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit, pParse, 0) |
| 152073 | 152283 | && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) |
| 152074 | 152284 | ){ |
| 152075 | 152285 | p->nSelectRow = sqlite3LogEst((u64)nLimit); |
| 152076 | 152286 | } |
| 152077 | 152287 | if( addr ){ |
| | @@ -152577,11 +152787,11 @@ |
| 152577 | 152787 | sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER"); |
| 152578 | 152788 | sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER"); |
| 152579 | 152789 | |
| 152580 | 152790 | /* Compute the limit registers */ |
| 152581 | 152791 | computeLimitRegisters(pParse, p, labelEnd); |
| 152582 | | - if( p->iLimit && op==TK_ALL ){ |
| 152792 | + if( p->iLimit && (op==TK_ALL || unionWithLimitOne(db,p)) ){ |
| 152583 | 152793 | regLimitA = ++pParse->nMem; |
| 152584 | 152794 | regLimitB = ++pParse->nMem; |
| 152585 | 152795 | sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit, |
| 152586 | 152796 | regLimitA); |
| 152587 | 152797 | sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB); |
| | @@ -159864,30 +160074,47 @@ |
| 159864 | 160074 | ** If column as REAL affinity and the table is an ordinary b-tree table |
| 159865 | 160075 | ** (not a virtual table) then the value might have been stored as an |
| 159866 | 160076 | ** integer. In that case, add an OP_RealAffinity opcode to make sure |
| 159867 | 160077 | ** it has been converted into REAL. |
| 159868 | 160078 | */ |
| 159869 | | -SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){ |
| 160079 | +static SQLITE_NOINLINE void columnDefaultUncommonCase( |
| 160080 | + Vdbe *v, /* Byte code under construction */ |
| 160081 | + Table *pTab, /* The table */ |
| 160082 | + Column *pCol, /* Which column of the table */ |
| 160083 | + int iReg /* Register in which results are stored */ |
| 160084 | +){ |
| 160085 | + sqlite3_value *pValue = 0; |
| 160086 | + u8 enc = ENC(sqlite3VdbeDb(v)); |
| 160087 | + Parse *pParse = sqlite3VdbeParser(v); |
| 160088 | + assert( !IsView(pTab) ); |
| 160089 | + sqlite3ValueFromExpr(sqlite3VdbeDb(v), |
| 160090 | + sqlite3ColumnExprAuth(pTab,pCol,pParse), enc, |
| 160091 | + pCol->affinity, &pValue); |
| 160092 | + if( pValue ){ |
| 160093 | + sqlite3VdbeAppendP4(v, pValue, P4_MEM); |
| 160094 | + } |
| 160095 | +#ifndef SQLITE_OMIT_FLOATING_POINT |
| 160096 | + if( pCol->affinity==SQLITE_AFF_REAL ){ |
| 160097 | + sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
| 160098 | + } |
| 160099 | +#endif |
| 160100 | +} |
| 160101 | +SQLITE_PRIVATE void sqlite3ColumnDefault( |
| 160102 | + Vdbe *v, /* Byte code under construction */ |
| 160103 | + Table *pTab, /* The table */ |
| 160104 | + int i, /* Which column of the table */ |
| 160105 | + int iReg /* Register in which results are stored */ |
| 160106 | +){ |
| 159870 | 160107 | Column *pCol; |
| 159871 | 160108 | assert( pTab!=0 ); |
| 159872 | 160109 | assert( pTab->nCol>i ); |
| 159873 | 160110 | pCol = &pTab->aCol[i]; |
| 159874 | 160111 | if( pCol->iDflt ){ |
| 159875 | | - sqlite3_value *pValue = 0; |
| 159876 | | - u8 enc = ENC(sqlite3VdbeDb(v)); |
| 159877 | | - assert( !IsView(pTab) ); |
| 159878 | | - VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName)); |
| 159879 | | - assert( i<pTab->nCol ); |
| 159880 | | - sqlite3ValueFromExpr(sqlite3VdbeDb(v), |
| 159881 | | - sqlite3ColumnExpr(pTab,pCol), enc, |
| 159882 | | - pCol->affinity, &pValue); |
| 159883 | | - if( pValue ){ |
| 159884 | | - sqlite3VdbeAppendP4(v, pValue, P4_MEM); |
| 159885 | | - } |
| 160112 | + columnDefaultUncommonCase(v,pTab,pCol,iReg); |
| 159886 | 160113 | } |
| 159887 | 160114 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 159888 | | - if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){ |
| 160115 | + if( pCol->affinity==SQLITE_AFF_REAL ){ |
| 159889 | 160116 | sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
| 159890 | 160117 | } |
| 159891 | 160118 | #endif |
| 159892 | 160119 | } |
| 159893 | 160120 | |
| | @@ -164579,10 +164806,11 @@ |
| 164579 | 164806 | ExprList *pOrigRhs; /* Original unmodified RHS */ |
| 164580 | 164807 | ExprList *pOrigLhs = 0; /* Original unmodified LHS */ |
| 164581 | 164808 | ExprList *pRhs = 0; /* New RHS after modifications */ |
| 164582 | 164809 | ExprList *pLhs = 0; /* New LHS after mods */ |
| 164583 | 164810 | int i; /* Loop counter */ |
| 164811 | + int nRhs = 0; /* Number of RHS terms added so far */ |
| 164584 | 164812 | |
| 164585 | 164813 | assert( ExprUseXSelect(pNew) ); |
| 164586 | 164814 | pOrigRhs = pSelect->pEList; |
| 164587 | 164815 | assert( pNew->pLeft!=0 ); |
| 164588 | 164816 | assert( ExprUseXList(pNew->pLeft) ); |
| | @@ -164592,10 +164820,13 @@ |
| 164592 | 164820 | for(i=iEq; i<pLoop->nLTerm; i++){ |
| 164593 | 164821 | if( pLoop->aLTerm[i]->pExpr==pX ){ |
| 164594 | 164822 | int iField; |
| 164595 | 164823 | assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 ); |
| 164596 | 164824 | iField = pLoop->aLTerm[i]->u.x.iField - 1; |
| 164825 | + if( iField!=nRhs ){ |
| 164826 | + ExprClearProperty(pNew, EP_Subrtn); |
| 164827 | + } |
| 164597 | 164828 | if( NEVER(pOrigRhs->a[iField].pExpr==0) ){ |
| 164598 | 164829 | continue; /* Duplicate PK column */ |
| 164599 | 164830 | } |
| 164600 | 164831 | pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr); |
| 164601 | 164832 | pOrigRhs->a[iField].pExpr = 0; |
| | @@ -164603,10 +164834,11 @@ |
| 164603 | 164834 | if( pOrigLhs ){ |
| 164604 | 164835 | assert( pOrigLhs->a[iField].pExpr!=0 ); |
| 164605 | 164836 | pLhs = sqlite3ExprListAppend(pParse,pLhs,pOrigLhs->a[iField].pExpr); |
| 164606 | 164837 | pOrigLhs->a[iField].pExpr = 0; |
| 164607 | 164838 | } |
| 164839 | + nRhs++; |
| 164608 | 164840 | } |
| 164609 | 164841 | } |
| 164610 | 164842 | sqlite3ExprListDelete(db, pOrigRhs); |
| 164611 | 164843 | if( pOrigLhs ){ |
| 164612 | 164844 | sqlite3ExprListDelete(db, pOrigLhs); |
| | @@ -164714,11 +164946,12 @@ |
| 164714 | 164946 | ** in sorted order (requires checking asc/desc, and rejecting cases |
| 164715 | 164947 | ** where the indexed columns are not in the right order). */ |
| 164716 | 164948 | pLoop->wsFlags &= ~WHERE_IN_SEEKSCAN; |
| 164717 | 164949 | } |
| 164718 | 164950 | if( !db->mallocFailed ){ |
| 164719 | | - aiMap = (int*)sqlite3DbMallocZero(db, sizeof(int)*nEq); |
| 164951 | + int nCol = pX->x.pSelect->pEList->nExpr; |
| 164952 | + aiMap = (int*)sqlite3DbMallocZero(db, sizeof(int)*nCol); |
| 164720 | 164953 | eType = sqlite3FindInIndex(pParse, pXMod, IN_INDEX_LOOP, 0, aiMap, &iTab); |
| 164721 | 164954 | } |
| 164722 | 164955 | sqlite3ExprDelete(db, pXMod); |
| 164723 | 164956 | } |
| 164724 | 164957 | |
| | @@ -165468,10 +165701,53 @@ |
| 165468 | 165701 | } |
| 165469 | 165702 | return 1; |
| 165470 | 165703 | } |
| 165471 | 165704 | return 0; |
| 165472 | 165705 | } |
| 165706 | + |
| 165707 | +/* |
| 165708 | +** This is called while coding loop pLevel, which scans FROM clause item |
| 165709 | +** pTabItem. pE is an expression for which all the prerequisites are |
| 165710 | +** available. This function tests if pE can be coded as part of the current |
| 165711 | +** loop, or whether it needs to be deferred to ensure outer joins are |
| 165712 | +** processed correctly. This function returns non-zero if the expression |
| 165713 | +** can be coded as part of the loop, or 0 if it must be deferred. |
| 165714 | +** |
| 165715 | +** The expression must be deferred if: |
| 165716 | +** |
| 165717 | +** * There are any RIGHT joins in the FROM clause and the expression |
| 165718 | +** was not part of an ON clause, or was part of an ON clause to the |
| 165719 | +** right of the join. |
| 165720 | +** |
| 165721 | +** * The table is the RHS of a LEFT JOIN and the expression was not |
| 165722 | +** part of an ON clause on an OUTER join, or was part of an ON clause |
| 165723 | +** on an OUTER join to the right of the join. |
| 165724 | +*/ |
| 165725 | +static int whereExprIsReady( |
| 165726 | + WhereInfo *pWInfo, |
| 165727 | + WhereLevel *pLevel, |
| 165728 | + SrcItem *pTabItem, |
| 165729 | + Expr *pE |
| 165730 | +){ |
| 165731 | + u8 jtype = pTabItem->fg.jointype; |
| 165732 | + if( jtype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){ |
| 165733 | + if( !ExprHasProperty(pE,EP_OuterON|EP_InnerON) ){ |
| 165734 | + /* Defer processing WHERE clause constraints until after outer |
| 165735 | + ** join processing. tag-20220513a */ |
| 165736 | + return 0; |
| 165737 | + }else if( (jtype & JT_LEFT) && !ExprHasProperty(pE,EP_OuterON) ){ |
| 165738 | + return 0; |
| 165739 | + }else{ |
| 165740 | + Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin); |
| 165741 | + if( m & pLevel->notReady ){ |
| 165742 | + /* An ON clause that is not ripe */ |
| 165743 | + return 0; |
| 165744 | + } |
| 165745 | + } |
| 165746 | + } |
| 165747 | + return 1; |
| 165748 | +} |
| 165473 | 165749 | |
| 165474 | 165750 | /* |
| 165475 | 165751 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 165476 | 165752 | ** implementation described by pWInfo. |
| 165477 | 165753 | */ |
| | @@ -166399,10 +166675,11 @@ |
| 166399 | 166675 | if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){ |
| 166400 | 166676 | continue; |
| 166401 | 166677 | } |
| 166402 | 166678 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
| 166403 | 166679 | if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */ |
| 166680 | + if( whereExprIsReady(pWInfo, pLevel, pTabItem, pExpr)==0 ) continue; |
| 166404 | 166681 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 166405 | 166682 | pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr); |
| 166406 | 166683 | } |
| 166407 | 166684 | if( pAndExpr ){ |
| 166408 | 166685 | /* The extra 0x10000 bit on the opcode is masked off and does not |
| | @@ -166639,26 +166916,11 @@ |
| 166639 | 166916 | pWInfo->untestedTerms = 1; |
| 166640 | 166917 | continue; |
| 166641 | 166918 | } |
| 166642 | 166919 | pE = pTerm->pExpr; |
| 166643 | 166920 | assert( pE!=0 ); |
| 166644 | | - if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){ |
| 166645 | | - if( !ExprHasProperty(pE,EP_OuterON|EP_InnerON) ){ |
| 166646 | | - /* Defer processing WHERE clause constraints until after outer |
| 166647 | | - ** join processing. tag-20220513a */ |
| 166648 | | - continue; |
| 166649 | | - }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT |
| 166650 | | - && !ExprHasProperty(pE,EP_OuterON) ){ |
| 166651 | | - continue; |
| 166652 | | - }else{ |
| 166653 | | - Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin); |
| 166654 | | - if( m & pLevel->notReady ){ |
| 166655 | | - /* An ON clause that is not ripe */ |
| 166656 | | - continue; |
| 166657 | | - } |
| 166658 | | - } |
| 166659 | | - } |
| 166921 | + if( whereExprIsReady(pWInfo, pLevel, pTabItem, pE)==0 ) continue; |
| 166660 | 166922 | if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){ |
| 166661 | 166923 | iNext = 2; |
| 166662 | 166924 | continue; |
| 166663 | 166925 | } |
| 166664 | 166926 | if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){ |
| | @@ -166807,13 +167069,15 @@ |
| 166807 | 167069 | ** of the RIGHT JOIN. During normal operation, the subroutine will |
| 166808 | 167070 | ** be in-line with the rest of the code. But at the end, a separate |
| 166809 | 167071 | ** loop will run that invokes this subroutine for unmatched rows |
| 166810 | 167072 | ** of pTab, with all tables to left begin set to NULL. |
| 166811 | 167073 | */ |
| 166812 | | - WhereRightJoin *pRJ = pLevel->pRJ; |
| 166813 | | - sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn); |
| 166814 | | - pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v); |
| 167074 | + { |
| 167075 | + WhereRightJoin *pRJ = pLevel->pRJ; |
| 167076 | + sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn); |
| 167077 | + pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v); |
| 167078 | + } |
| 166815 | 167079 | assert( pParse->withinRJSubrtn < 255 ); |
| 166816 | 167080 | pParse->withinRJSubrtn++; |
| 166817 | 167081 | |
| 166818 | 167082 | /* WHERE clause constraints must be deferred until after outer join |
| 166819 | 167083 | ** row elimination has completed, since WHERE clause constraints apply |
| | @@ -167209,11 +167473,11 @@ |
| 167209 | 167473 | int iCol = pRight->iColumn; |
| 167210 | 167474 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB); |
| 167211 | 167475 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 167212 | 167476 | z = sqlite3_value_text(pVal); |
| 167213 | 167477 | } |
| 167214 | | - sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
| 167478 | + sqlite3VdbeReprepareOnBind(pParse->pVdbe, iCol, 0); |
| 167215 | 167479 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 167216 | 167480 | }else if( op==TK_STRING ){ |
| 167217 | 167481 | assert( !ExprHasProperty(pRight, EP_IntValue) ); |
| 167218 | 167482 | z = (u8*)pRight->u.zToken; |
| 167219 | 167483 | } |
| | @@ -167314,11 +167578,11 @@ |
| 167314 | 167578 | |
| 167315 | 167579 | /* If the RHS pattern is a bound parameter, make arrangements to |
| 167316 | 167580 | ** reprepare the statement when that parameter is rebound */ |
| 167317 | 167581 | if( op==TK_VARIABLE ){ |
| 167318 | 167582 | Vdbe *v = pParse->pVdbe; |
| 167319 | | - sqlite3VdbeSetVarmask(v, pRight->iColumn); |
| 167583 | + sqlite3VdbeReprepareOnBind(v, pRight->iColumn, 0); |
| 167320 | 167584 | assert( !ExprHasProperty(pRight, EP_IntValue) ); |
| 167321 | 167585 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 167322 | 167586 | /* If the rhs of the LIKE expression is a variable, and the current |
| 167323 | 167587 | ** value of the variable means there is no need to invoke the LIKE |
| 167324 | 167588 | ** function, then no OP_Variable will be added to the program. |
| | @@ -168665,11 +168929,11 @@ |
| 168665 | 168929 | Parse *pParse = pWC->pWInfo->pParse; |
| 168666 | 168930 | sqlite3 *db = pParse->db; |
| 168667 | 168931 | Expr *pNew; |
| 168668 | 168932 | int iVal = 0; |
| 168669 | 168933 | |
| 168670 | | - if( sqlite3ExprIsInteger(pExpr, &iVal, pParse) && iVal>=0 ){ |
| 168934 | + if( sqlite3ExprIsInteger(pExpr, &iVal, pParse, 0) && iVal>=0 ){ |
| 168671 | 168935 | Expr *pVal = sqlite3ExprInt32(db, iVal); |
| 168672 | 168936 | if( pVal==0 ) return; |
| 168673 | 168937 | pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); |
| 168674 | 168938 | }else{ |
| 168675 | 168939 | Expr *pVal = sqlite3ExprAlloc(db, TK_REGISTER, 0, 0); |
| | @@ -170248,13 +170512,11 @@ |
| 170248 | 170512 | /* |
| 170249 | 170513 | ** Generate bytecode that will initialize a Bloom filter that is appropriate |
| 170250 | 170514 | ** for pLevel. |
| 170251 | 170515 | ** |
| 170252 | 170516 | ** If there are inner loops within pLevel that have the WHERE_BLOOMFILTER |
| 170253 | | -** flag set, initialize a Bloomfilter for them as well. Except don't do |
| 170254 | | -** this recursive initialization if the SQLITE_BloomPulldown optimization has |
| 170255 | | -** been turned off. |
| 170517 | +** flag set, initialize a Bloomfilter for them as well. |
| 170256 | 170518 | ** |
| 170257 | 170519 | ** When the Bloom filter is initialized, the WHERE_BLOOMFILTER flag is cleared |
| 170258 | 170520 | ** from the loop, but the regFilter value is set to a register that implements |
| 170259 | 170521 | ** the Bloom filter. When regFilter is positive, the |
| 170260 | 170522 | ** sqlite3WhereCodeOneLoopStart() will generate code to test the Bloom filter |
| | @@ -170359,11 +170621,10 @@ |
| 170359 | 170621 | sqlite3VdbeResolveLabel(v, addrCont); |
| 170360 | 170622 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
| 170361 | 170623 | VdbeCoverage(v); |
| 170362 | 170624 | sqlite3VdbeJumpHere(v, addrTop); |
| 170363 | 170625 | pLoop->wsFlags &= ~WHERE_BLOOMFILTER; |
| 170364 | | - if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break; |
| 170365 | 170626 | while( ++iLevel < pWInfo->nLevel ){ |
| 170366 | 170627 | const SrcItem *pTabItem; |
| 170367 | 170628 | pLevel = &pWInfo->a[iLevel]; |
| 170368 | 170629 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 170369 | 170630 | if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue; |
| | @@ -172038,10 +172299,17 @@ |
| 172038 | 172299 | ){ |
| 172039 | 172300 | WhereTerm *pTerm, *pX; |
| 172040 | 172301 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
| 172041 | 172302 | int i, j; |
| 172042 | 172303 | LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */ |
| 172304 | + |
| 172305 | + /* Skip all this if the FROM clause of the query is a single table and |
| 172306 | + ** there is no ORDER BY. In this case it doesn't matter how accurate |
| 172307 | + ** the WhereLoop.nOut values are. */ |
| 172308 | + if( pWC->pWInfo->pTabList->nSrc<=1 && pWC->pWInfo->pOrderBy==0 ){ |
| 172309 | + return; |
| 172310 | + } |
| 172043 | 172311 | |
| 172044 | 172312 | assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
| 172045 | 172313 | for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){ |
| 172046 | 172314 | assert( pTerm!=0 ); |
| 172047 | 172315 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
| | @@ -172086,11 +172354,11 @@ |
| 172086 | 172354 | ){ |
| 172087 | 172355 | Expr *pRight = pOpExpr->pRight; |
| 172088 | 172356 | Parse *pParse = pWC->pWInfo->pParse; |
| 172089 | 172357 | int k = 0; |
| 172090 | 172358 | testcase( pOpExpr->op==TK_IS ); |
| 172091 | | - if( sqlite3ExprIsInteger(pRight, &k, pParse) && k>=(-1) && k<=1 ){ |
| 172359 | + if( sqlite3ExprIsInteger(pRight, &k, pParse, 1) && k>=(-1) && k<=1 ){ |
| 172092 | 172360 | k = 10; |
| 172093 | 172361 | }else{ |
| 172094 | 172362 | k = 20; |
| 172095 | 172363 | } |
| 172096 | 172364 | if( iReduce<k ){ |
| | @@ -174256,16 +174524,24 @@ |
| 174256 | 174524 | if( j>=pLoop->nLTerm ) continue; |
| 174257 | 174525 | } |
| 174258 | 174526 | if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){ |
| 174259 | 174527 | Parse *pParse = pWInfo->pParse; |
| 174260 | 174528 | CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr); |
| 174261 | | - CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr); |
| 174529 | + const Expr *pCExpr = pTerm->pExpr; |
| 174530 | + CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pCExpr); |
| 174531 | + char affRight; |
| 174262 | 174532 | assert( pColl1 ); |
| 174263 | 174533 | if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){ |
| 174264 | 174534 | continue; |
| 174265 | 174535 | } |
| 174266 | | - testcase( pTerm->pExpr->op==TK_IS ); |
| 174536 | + affRight = sqlite3ExprAffinity(pCExpr->pRight); |
| 174537 | + /* Left operand must be a column, hence always has affinity */ |
| 174538 | + assert( sqlite3ExprAffinity(pCExpr->pLeft)!=0 ); |
| 174539 | + if( affRight!=0 && affRight!=sqlite3ExprAffinity(pCExpr->pLeft) ){ |
| 174540 | + continue; /* An affinity conversion would be required */ |
| 174541 | + } |
| 174542 | + testcase( pCExpr->op==TK_IS ); |
| 174267 | 174543 | } |
| 174268 | 174544 | obSat |= MASKBIT(i); |
| 174269 | 174545 | } |
| 174270 | 174546 | |
| 174271 | 174547 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| | @@ -177803,11 +178079,11 @@ |
| 177803 | 178079 | } |
| 177804 | 178080 | if( bIntToNull ){ |
| 177805 | 178081 | int iDummy; |
| 177806 | 178082 | Expr *pSub; |
| 177807 | 178083 | pSub = sqlite3ExprSkipCollateAndLikely(pDup); |
| 177808 | | - if( sqlite3ExprIsInteger(pSub, &iDummy, 0) ){ |
| 178084 | + if( sqlite3ExprIsInteger(pSub, &iDummy, 0, 0) ){ |
| 177809 | 178085 | pSub->op = TK_NULL; |
| 177810 | 178086 | pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse); |
| 177811 | 178087 | pSub->u.zToken = 0; |
| 177812 | 178088 | } |
| 177813 | 178089 | } |
| | @@ -195326,11 +195602,14 @@ |
| 195326 | 195602 | /* Loop through the returned columns. Set nStr to the number of bytes of |
| 195327 | 195603 | ** space required to store a copy of each column name, including the |
| 195328 | 195604 | ** nul-terminator byte. */ |
| 195329 | 195605 | nCol = sqlite3_column_count(pStmt); |
| 195330 | 195606 | for(i=0; i<nCol; i++){ |
| 195607 | + /* This call to sqlite3_column_name() cannot fail, as no conversion |
| 195608 | + ** between encodings, and therefore no malloc() call, is required. */ |
| 195331 | 195609 | const char *zCol = sqlite3_column_name(pStmt, i); |
| 195610 | + assert( zCol ); |
| 195332 | 195611 | nStr += strlen(zCol) + 1; |
| 195333 | 195612 | } |
| 195334 | 195613 | |
| 195335 | 195614 | /* Allocate and populate the array to return. */ |
| 195336 | 195615 | azCol = (const char **)sqlite3_malloc64(sizeof(char *) * nCol + nStr); |
| | @@ -195339,10 +195618,11 @@ |
| 195339 | 195618 | }else{ |
| 195340 | 195619 | char *p = (char *)&azCol[nCol]; |
| 195341 | 195620 | for(i=0; i<nCol; i++){ |
| 195342 | 195621 | const char *zCol = sqlite3_column_name(pStmt, i); |
| 195343 | 195622 | int n = (int)strlen(zCol)+1; |
| 195623 | + assert( zCol ); /* no malloc() required, cannot fail */ |
| 195344 | 195624 | memcpy(p, zCol, n); |
| 195345 | 195625 | azCol[i] = p; |
| 195346 | 195626 | p += n; |
| 195347 | 195627 | } |
| 195348 | 195628 | } |
| | @@ -255110,10 +255390,11 @@ |
| 255110 | 255390 | ** against no columns at all). |
| 255111 | 255391 | */ |
| 255112 | 255392 | static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){ |
| 255113 | 255393 | UNUSED_PARAM(pSeg); |
| 255114 | 255394 | pIter->base.nData = 0; |
| 255395 | + pIter->base.bEof = 1; |
| 255115 | 255396 | } |
| 255116 | 255397 | |
| 255117 | 255398 | /* |
| 255118 | 255399 | ** xSetOutputs callback used by detail=col when there is a column filter |
| 255119 | 255400 | ** and there are 100 or more columns. Also called as a fallback from |
| | @@ -257019,11 +257300,10 @@ |
| 257019 | 257300 | && !fts5FlushSecureDelete(p, pStruct, zTerm, nTerm, iRowid) |
| 257020 | 257301 | ){ |
| 257021 | 257302 | iOff++; |
| 257022 | 257303 | if( iOff<nDoclist && pDoclist[iOff]==0x00 ){ |
| 257023 | 257304 | iOff++; |
| 257024 | | - nDoclist = 0; |
| 257025 | 257305 | }else{ |
| 257026 | 257306 | continue; |
| 257027 | 257307 | } |
| 257028 | 257308 | } |
| 257029 | 257309 | }else if( (pDoclist[iOff] & 0x01) |
| | @@ -257335,13 +257615,13 @@ |
| 257335 | 257615 | |
| 257336 | 257616 | assert( pIter->aPoslist || (p==0 && pIter->aPoslist==0) ); |
| 257337 | 257617 | if( p>=pIter->aEof ){ |
| 257338 | 257618 | pIter->aPoslist = 0; |
| 257339 | 257619 | }else{ |
| 257340 | | - i64 iDelta; |
| 257620 | + u64 iDelta; |
| 257341 | 257621 | |
| 257342 | | - p += fts5GetVarint(p, (u64*)&iDelta); |
| 257622 | + p += fts5GetVarint(p, &iDelta); |
| 257343 | 257623 | pIter->iRowid += iDelta; |
| 257344 | 257624 | |
| 257345 | 257625 | /* Read position list size */ |
| 257346 | 257626 | if( p[0] & 0x80 ){ |
| 257347 | 257627 | int nPos; |
| | @@ -264533,11 +264813,11 @@ |
| 264533 | 264813 | int nArg, /* Number of args */ |
| 264534 | 264814 | sqlite3_value **apUnused /* Function arguments */ |
| 264535 | 264815 | ){ |
| 264536 | 264816 | assert( nArg==0 ); |
| 264537 | 264817 | UNUSED_PARAM2(nArg, apUnused); |
| 264538 | | - sqlite3_result_text(pCtx, "fts5: 2026-08-20 13:25:12 3bd5456e50ef321f2cafcf67261d556a760624ce0caab430ed9454cebec233e7", -1, SQLITE_TRANSIENT); |
| 264818 | + sqlite3_result_text(pCtx, "fts5: 2026-08-27 10:31:42 4b30d1d7e22b9b74753a6fb4d9e1f884ddcee12273d6842954ed44de94e5f4b9", -1, SQLITE_TRANSIENT); |
| 264539 | 264819 | } |
| 264540 | 264820 | |
| 264541 | 264821 | /* |
| 264542 | 264822 | ** Implementation of fts5_locale(LOCALE, TEXT) function. |
| 264543 | 264823 | ** |
| | @@ -269637,13 +269917,22 @@ |
| 269637 | 269917 | sqlite3_value *pLe = 0; |
| 269638 | 269918 | |
| 269639 | 269919 | UNUSED_PARAM2(zUnused, nUnused); |
| 269640 | 269920 | |
| 269641 | 269921 | fts5VocabResetCursor(pCsr); |
| 269642 | | - if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++]; |
| 269643 | | - if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++]; |
| 269644 | | - if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++]; |
| 269922 | + if( idxNum & FTS5_VOCAB_TERM_EQ ){ |
| 269923 | + pEq = apVal[iVal++]; |
| 269924 | + if( sqlite3_value_type(pEq)!=SQLITE_TEXT ) pEq = 0; |
| 269925 | + } |
| 269926 | + if( idxNum & FTS5_VOCAB_TERM_GE ){ |
| 269927 | + pGe = apVal[iVal++]; |
| 269928 | + if( sqlite3_value_type(pGe)!=SQLITE_TEXT ) pGe = 0; |
| 269929 | + } |
| 269930 | + if( idxNum & FTS5_VOCAB_TERM_LE ){ |
| 269931 | + pLe = apVal[iVal++]; |
| 269932 | + if( sqlite3_value_type(pLe)!=SQLITE_TEXT ) pLe = 0; |
| 269933 | + } |
| 269645 | 269934 | pCsr->colUsed = (idxNum & FTS5_VOCAB_COLUSED_MASK); |
| 269646 | 269935 | |
| 269647 | 269936 | if( pEq ){ |
| 269648 | 269937 | zTerm = fts5VocabValueText(pEq, &nTerm); |
| 269649 | 269938 | f = FTS5INDEX_QUERY_NOTOKENDATA; |
| 269650 | 269939 | |