| | @@ -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 | | -** 4733d351ec2376291f093ba8d2ba71d82c6f with changes in files: |
| 21 | +** 2610105a439e25c050b2deb32953861187c8 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.52.0" |
| 471 | 471 | #define SQLITE_VERSION_NUMBER 3052000 |
| 472 | | -#define SQLITE_SOURCE_ID "2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71" |
| 472 | +#define SQLITE_SOURCE_ID "2026-02-17 11:28:48 2610105a439e25c050b2deb32953861187c81b1d97407f41dc188e6627e0ac4d" |
| 473 | 473 | #define SQLITE_SCM_BRANCH "trunk" |
| 474 | 474 | #define SQLITE_SCM_TAGS "" |
| 475 | | -#define SQLITE_SCM_DATETIME "2026-01-26T10:53:24.426Z" |
| 475 | +#define SQLITE_SCM_DATETIME "2026-02-17T11:28:48.505Z" |
| 476 | 476 | |
| 477 | 477 | /* |
| 478 | 478 | ** CAPI3REF: Run-Time Library Version Numbers |
| 479 | 479 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 480 | 480 | ** |
| | @@ -11571,23 +11571,45 @@ |
| 11571 | 11571 | #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */ |
| 11572 | 11572 | |
| 11573 | 11573 | /* |
| 11574 | 11574 | ** CAPI3REF: Bind array values to the CARRAY table-valued function |
| 11575 | 11575 | ** |
| 11576 | | -** The sqlite3_carray_bind(S,I,P,N,F,X) interface binds an array value to |
| 11577 | | -** one of the first argument of the [carray() table-valued function]. The |
| 11578 | | -** S parameter is a pointer to the [prepared statement] that uses the carray() |
| 11579 | | -** functions. I is the parameter index to be bound. P is a pointer to the |
| 11580 | | -** array to be bound, and N is the number of eements in the array. The |
| 11581 | | -** F argument is one of constants [SQLITE_CARRAY_INT32], [SQLITE_CARRAY_INT64], |
| 11582 | | -** [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT], or [SQLITE_CARRAY_BLOB] to |
| 11583 | | -** indicate the datatype of the array being bound. The X argument is not a |
| 11584 | | -** NULL pointer, then SQLite will invoke the function X on the P parameter |
| 11585 | | -** after it has finished using P, even if the call to |
| 11586 | | -** sqlite3_carray_bind() fails. The special-case finalizer |
| 11587 | | -** SQLITE_TRANSIENT has no effect here. |
| 11576 | +** The sqlite3_carray_bind_v2(S,I,P,N,F,X,D) interface binds an array value to |
| 11577 | +** parameter that is the first argument of the [carray() table-valued function]. |
| 11578 | +** The S parameter is a pointer to the [prepared statement] that uses the carray() |
| 11579 | +** functions. I is the parameter index to be bound. I must be the index of the |
| 11580 | +** parameter that is the first argument to the carray() table-valued function. |
| 11581 | +** P is a pointer to the array to be bound, and N is the number of elements in |
| 11582 | +** the array. The F argument is one of constants [SQLITE_CARRAY_INT32], |
| 11583 | +** [SQLITE_CARRAY_INT64], [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT], |
| 11584 | +** or [SQLITE_CARRAY_BLOB] to indicate the datatype of the array P. |
| 11585 | +** |
| 11586 | +** If the X argument is not a NULL pointer or one of the special |
| 11587 | +** values [SQLITE_STATIC] or [SQLITE_TRANSIENT], then SQLite will invoke |
| 11588 | +** the function X with argument D when it is finished using the data in P. |
| 11589 | +** The call to X(D) is a destructor for the array P. The destructor X(D) |
| 11590 | +** is invoked even if the call to sqlite3_carray_bind() fails. If the X |
| 11591 | +** parameter is the special-case value [SQLITE_STATIC], then SQLite assumes |
| 11592 | +** that the data static and the destructor is never invoked. If the X |
| 11593 | +** parameter is the special-case value [SQLITE_TRANSIENT], then |
| 11594 | +** sqlite3_carray_bind_v2() makes its own private copy of the data prior |
| 11595 | +** to returning and never invokes the destructor X. |
| 11596 | +** |
| 11597 | +** The sqlite3_carray_bind() function works the same as sqlite_carray_bind_v2() |
| 11598 | +** with a D parameter set to P. In other words, |
| 11599 | +** sqlite3_carray_bind(S,I,P,N,F,X) is same as |
| 11600 | +** sqlite3_carray_bind(S,I,P,N,F,X,P). |
| 11588 | 11601 | */ |
| 11602 | +SQLITE_API int sqlite3_carray_bind_v2( |
| 11603 | + sqlite3_stmt *pStmt, /* Statement to be bound */ |
| 11604 | + int i, /* Parameter index */ |
| 11605 | + void *aData, /* Pointer to array data */ |
| 11606 | + int nData, /* Number of data elements */ |
| 11607 | + int mFlags, /* CARRAY flags */ |
| 11608 | + void (*xDel)(void*), /* Destructor for aData */ |
| 11609 | + void *pDel /* Optional argument to xDel() */ |
| 11610 | +); |
| 11589 | 11611 | SQLITE_API int sqlite3_carray_bind( |
| 11590 | 11612 | sqlite3_stmt *pStmt, /* Statement to be bound */ |
| 11591 | 11613 | int i, /* Parameter index */ |
| 11592 | 11614 | void *aData, /* Pointer to array data */ |
| 11593 | 11615 | int nData, /* Number of data elements */ |
| | @@ -15814,10 +15836,11 @@ |
| 15814 | 15836 | #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC |
| 15815 | 15837 | # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0) |
| 15816 | 15838 | #else |
| 15817 | 15839 | # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0) |
| 15818 | 15840 | #endif |
| 15841 | +#define TWO_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&1)==0) |
| 15819 | 15842 | |
| 15820 | 15843 | /* |
| 15821 | 15844 | ** Disable MMAP on platforms where it is known to not work |
| 15822 | 15845 | */ |
| 15823 | 15846 | #if defined(__OpenBSD__) || defined(__QNXNTO__) |
| | @@ -20337,11 +20360,11 @@ |
| 20337 | 20360 | #define SF_Distinct 0x0000001 /* Output should be DISTINCT */ |
| 20338 | 20361 | #define SF_All 0x0000002 /* Includes the ALL keyword */ |
| 20339 | 20362 | #define SF_Resolved 0x0000004 /* Identifiers have been resolved */ |
| 20340 | 20363 | #define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */ |
| 20341 | 20364 | #define SF_HasAgg 0x0000010 /* Contains aggregate functions */ |
| 20342 | | -/* 0x0000020 // available for reuse */ |
| 20365 | +#define SF_ClonedRhsIn 0x0000020 /* Cloned RHS of an IN operator */ |
| 20343 | 20366 | #define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */ |
| 20344 | 20367 | #define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */ |
| 20345 | 20368 | #define SF_Compound 0x0000100 /* Part of a compound query */ |
| 20346 | 20369 | #define SF_Values 0x0000200 /* Synthesized from VALUES clause */ |
| 20347 | 20370 | #define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */ |
| | @@ -20594,33 +20617,33 @@ |
| 20594 | 20617 | int rc; /* Return code from execution */ |
| 20595 | 20618 | LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ |
| 20596 | 20619 | u8 nested; /* Number of nested calls to the parser/code generator */ |
| 20597 | 20620 | u8 nTempReg; /* Number of temporary registers in aTempReg[] */ |
| 20598 | 20621 | u8 isMultiWrite; /* True if statement may modify/insert multiple rows */ |
| 20599 | | - u8 mayAbort; /* True if statement may throw an ABORT exception */ |
| 20600 | | - u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */ |
| 20601 | 20622 | u8 disableLookaside; /* Number of times lookaside has been disabled */ |
| 20602 | 20623 | u8 prepFlags; /* SQLITE_PREPARE_* flags */ |
| 20603 | 20624 | u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */ |
| 20604 | | - u8 bHasExists; /* Has a correlated "EXISTS (SELECT ....)" expression */ |
| 20605 | 20625 | u8 mSubrtnSig; /* mini Bloom filter on available SubrtnSig.selId */ |
| 20606 | 20626 | u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ |
| 20607 | | - u8 bReturning; /* Coding a RETURNING trigger */ |
| 20608 | 20627 | u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ |
| 20609 | | - u8 disableTriggers; /* True to disable triggers */ |
| 20610 | 20628 | #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) |
| 20611 | 20629 | u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */ |
| 20612 | 20630 | #endif |
| 20613 | 20631 | #ifdef SQLITE_DEBUG |
| 20614 | 20632 | u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */ |
| 20615 | 20633 | u8 isCreate; /* CREATE TABLE, INDEX, or VIEW (but not TRIGGER) |
| 20616 | 20634 | ** and ALTER TABLE ADD COLUMN. */ |
| 20617 | 20635 | #endif |
| 20618 | | - bft colNamesSet :1; /* TRUE after OP_ColumnName has been issued to pVdbe */ |
| 20619 | | - bft bHasWith :1; /* True if statement contains WITH */ |
| 20620 | | - bft okConstFactor :1; /* OK to factor out constants */ |
| 20621 | | - bft checkSchema :1; /* Causes schema cookie check after an error */ |
| 20636 | + bft disableTriggers:1; /* True to disable triggers */ |
| 20637 | + bft mayAbort :1; /* True if statement may throw an ABORT exception */ |
| 20638 | + bft hasCompound :1; /* Need to invoke convertCompoundSelectToSubquery() */ |
| 20639 | + bft bReturning :1; /* Coding a RETURNING trigger */ |
| 20640 | + bft bHasExists :1; /* Has a correlated "EXISTS (SELECT ....)" expression */ |
| 20641 | + bft colNamesSet :1; /* TRUE after OP_ColumnName has been issued to pVdbe */ |
| 20642 | + bft bHasWith :1; /* True if statement contains WITH */ |
| 20643 | + bft okConstFactor:1; /* OK to factor out constants */ |
| 20644 | + bft checkSchema :1; /* Causes schema cookie check after an error */ |
| 20622 | 20645 | int nRangeReg; /* Size of the temporary register block */ |
| 20623 | 20646 | int iRangeReg; /* First register in temporary register block */ |
| 20624 | 20647 | int nErr; /* Number of errors seen */ |
| 20625 | 20648 | int nTab; /* Number of previously allocated VDBE cursors */ |
| 20626 | 20649 | int nMem; /* Number of memory cells used so far */ |
| | @@ -21094,10 +21117,11 @@ |
| 21094 | 21117 | u16 mWFlags; /* Use-dependent flags */ |
| 21095 | 21118 | union { /* Extra data for callback */ |
| 21096 | 21119 | NameContext *pNC; /* Naming context */ |
| 21097 | 21120 | int n; /* A counter */ |
| 21098 | 21121 | int iCur; /* A cursor number */ |
| 21122 | + int sz; /* String literal length */ |
| 21099 | 21123 | SrcList *pSrcList; /* FROM clause */ |
| 21100 | 21124 | struct CCurHint *pCCurHint; /* Used by codeCursorHint() */ |
| 21101 | 21125 | struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */ |
| 21102 | 21126 | int *aiCol; /* array of column indexes */ |
| 21103 | 21127 | struct IdxCover *pIdxCover; /* Check for index coverage */ |
| | @@ -21780,10 +21804,11 @@ |
| 21780 | 21804 | SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*); |
| 21781 | 21805 | SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*, |
| 21782 | 21806 | Expr*,ExprList*,u32,Expr*); |
| 21783 | 21807 | SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*); |
| 21784 | 21808 | SQLITE_PRIVATE void sqlite3SelectDeleteGeneric(sqlite3*,void*); |
| 21809 | +SQLITE_PRIVATE void sqlite3SelectCheckOnClauses(Parse *pParse, Select *pSelect); |
| 21785 | 21810 | SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*); |
| 21786 | 21811 | SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, Trigger*); |
| 21787 | 21812 | SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int); |
| 21788 | 21813 | #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) |
| 21789 | 21814 | SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*); |
| | @@ -21877,10 +21902,11 @@ |
| 21877 | 21902 | SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*); |
| 21878 | 21903 | #endif |
| 21879 | 21904 | SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr*, int*, Parse*); |
| 21880 | 21905 | SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); |
| 21881 | 21906 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); |
| 21907 | +SQLITE_PRIVATE int sqlite3ExprIsLikeOperator(const Expr*); |
| 21882 | 21908 | SQLITE_PRIVATE int sqlite3IsRowid(const char*); |
| 21883 | 21909 | SQLITE_PRIVATE const char *sqlite3RowidAlias(Table *pTab); |
| 21884 | 21910 | SQLITE_PRIVATE void sqlite3GenerateRowDelete( |
| 21885 | 21911 | Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int); |
| 21886 | 21912 | SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int); |
| | @@ -22000,11 +22026,11 @@ |
| 22000 | 22026 | SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*); |
| 22001 | 22027 | |
| 22002 | 22028 | SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64); |
| 22003 | 22029 | SQLITE_PRIVATE i64 sqlite3RealToI64(double); |
| 22004 | 22030 | SQLITE_PRIVATE int sqlite3Int64ToText(i64,char*); |
| 22005 | | -SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8); |
| 22031 | +SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*); |
| 22006 | 22032 | SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*); |
| 22007 | 22033 | SQLITE_PRIVATE int sqlite3GetUInt32(const char*, u32*); |
| 22008 | 22034 | SQLITE_PRIVATE int sqlite3Atoi(const char*); |
| 22009 | 22035 | #ifndef SQLITE_OMIT_UTF16 |
| 22010 | 22036 | SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nByte, int nChar); |
| | @@ -22150,11 +22176,11 @@ |
| 22150 | 22176 | SQLITE_PRIVATE void sqlite3AlterAddConstraint(Parse*,SrcList*,Token*,Token*,const char*,int); |
| 22151 | 22177 | SQLITE_PRIVATE void sqlite3AlterSetNotNull(Parse*, SrcList*, Token*, Token*); |
| 22152 | 22178 | SQLITE_PRIVATE i64 sqlite3GetToken(const unsigned char *, int *); |
| 22153 | 22179 | SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...); |
| 22154 | 22180 | SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int); |
| 22155 | | -SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int); |
| 22181 | +SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int, int); |
| 22156 | 22182 | SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*); |
| 22157 | 22183 | SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*); |
| 22158 | 22184 | SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, SrcItem*); |
| 22159 | 22185 | SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p); |
| 22160 | 22186 | SQLITE_PRIVATE int sqlite3MatchEName( |
| | @@ -24584,17 +24610,18 @@ |
| 24584 | 24610 | #endif |
| 24585 | 24611 | #ifdef SQLITE_DEBUG |
| 24586 | 24612 | SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*); |
| 24587 | 24613 | #endif |
| 24588 | 24614 | SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*); |
| 24589 | | -SQLITE_PRIVATE void sqlite3VdbeMemZeroTerminateIfAble(Mem*); |
| 24615 | +SQLITE_PRIVATE int sqlite3VdbeMemZeroTerminateIfAble(Mem*); |
| 24590 | 24616 | SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*); |
| 24591 | 24617 | SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8); |
| 24592 | 24618 | SQLITE_PRIVATE int sqlite3IntFloatCompare(i64,double); |
| 24593 | 24619 | SQLITE_PRIVATE i64 sqlite3VdbeIntValue(const Mem*); |
| 24594 | 24620 | SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*); |
| 24595 | 24621 | SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*); |
| 24622 | +SQLITE_PRIVATE SQLITE_NOINLINE double sqlite3MemRealValueRC(Mem*, int*); |
| 24596 | 24623 | SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull); |
| 24597 | 24624 | SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*); |
| 24598 | 24625 | SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*); |
| 24599 | 24626 | SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*); |
| 24600 | 24627 | SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8); |
| | @@ -25550,11 +25577,11 @@ |
| 25550 | 25577 | return 0; |
| 25551 | 25578 | }else if( parseHhMmSs(zDate, p)==0 ){ |
| 25552 | 25579 | return 0; |
| 25553 | 25580 | }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){ |
| 25554 | 25581 | return setDateTimeToCurrent(context, p); |
| 25555 | | - }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8)>0 ){ |
| 25582 | + }else if( sqlite3AtoF(zDate, &r)>0 ){ |
| 25556 | 25583 | setRawDateNumber(p, r); |
| 25557 | 25584 | return 0; |
| 25558 | 25585 | }else if( (sqlite3StrICmp(zDate,"subsec")==0 |
| 25559 | 25586 | || sqlite3StrICmp(zDate,"subsecond")==0) |
| 25560 | 25587 | && sqlite3NotPureFunc(context) ){ |
| | @@ -25996,11 +26023,11 @@ |
| 25996 | 26023 | ** Move the date to the same time on the next occurrence of |
| 25997 | 26024 | ** weekday N where 0==Sunday, 1==Monday, and so forth. If the |
| 25998 | 26025 | ** date is already on the appropriate weekday, this is a no-op. |
| 25999 | 26026 | */ |
| 26000 | 26027 | if( sqlite3_strnicmp(z, "weekday ", 8)==0 |
| 26001 | | - && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)>0 |
| 26028 | + && sqlite3AtoF(&z[8], &r)>0 |
| 26002 | 26029 | && r>=0.0 && r<7.0 && (n=(int)r)==r ){ |
| 26003 | 26030 | sqlite3_int64 Z; |
| 26004 | 26031 | computeYMD_HMS(p); |
| 26005 | 26032 | p->tz = 0; |
| 26006 | 26033 | p->validJD = 0; |
| | @@ -26067,23 +26094,29 @@ |
| 26067 | 26094 | case '6': |
| 26068 | 26095 | case '7': |
| 26069 | 26096 | case '8': |
| 26070 | 26097 | case '9': { |
| 26071 | 26098 | double rRounder; |
| 26072 | | - int i; |
| 26099 | + int i, rx; |
| 26073 | 26100 | int Y,M,D,h,m,x; |
| 26074 | 26101 | const char *z2 = z; |
| 26102 | + char *zCopy; |
| 26103 | + sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 26075 | 26104 | char z0 = z[0]; |
| 26076 | 26105 | for(n=1; z[n]; n++){ |
| 26077 | 26106 | if( z[n]==':' ) break; |
| 26078 | 26107 | if( sqlite3Isspace(z[n]) ) break; |
| 26079 | 26108 | if( z[n]=='-' ){ |
| 26080 | 26109 | if( n==5 && getDigits(&z[1], "40f", &Y)==1 ) break; |
| 26081 | 26110 | if( n==6 && getDigits(&z[1], "50f", &Y)==1 ) break; |
| 26082 | 26111 | } |
| 26083 | 26112 | } |
| 26084 | | - if( sqlite3AtoF(z, &r, n, SQLITE_UTF8)<=0 ){ |
| 26113 | + zCopy = sqlite3DbStrNDup(db, z, n); |
| 26114 | + if( zCopy==0 ) break; |
| 26115 | + rx = sqlite3AtoF(zCopy, &r)<=0; |
| 26116 | + sqlite3DbFree(db, zCopy); |
| 26117 | + if( rx ){ |
| 26085 | 26118 | assert( rc==1 ); |
| 26086 | 26119 | break; |
| 26087 | 26120 | } |
| 26088 | 26121 | if( z[n]=='-' ){ |
| 26089 | 26122 | /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the |
| | @@ -34904,11 +34937,17 @@ |
| 34904 | 34937 | ** sqlite3ShowWhereTerm() in where.c |
| 34905 | 34938 | */ |
| 34906 | 34939 | SQLITE_PRIVATE void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); } |
| 34907 | 34940 | SQLITE_PRIVATE void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);} |
| 34908 | 34941 | SQLITE_PRIVATE void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); } |
| 34909 | | -SQLITE_PRIVATE void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); } |
| 34942 | +SQLITE_PRIVATE void sqlite3ShowSrcList(const SrcList *p){ |
| 34943 | + TreeView *pView = 0; |
| 34944 | + sqlite3TreeViewPush(&pView, 0); |
| 34945 | + sqlite3TreeViewLine(pView, "SRCLIST"); |
| 34946 | + sqlite3TreeViewSrcList(pView,p); |
| 34947 | + sqlite3TreeViewPop(&pView); |
| 34948 | +} |
| 34910 | 34949 | SQLITE_PRIVATE void sqlite3ShowSelect(const Select *p){ sqlite3TreeViewSelect(0,p,0); } |
| 34911 | 34950 | SQLITE_PRIVATE void sqlite3ShowWith(const With *p){ sqlite3TreeViewWith(0,p,0); } |
| 34912 | 34951 | SQLITE_PRIVATE void sqlite3ShowUpsert(const Upsert *p){ sqlite3TreeViewUpsert(0,p,0); } |
| 34913 | 34952 | #ifndef SQLITE_OMIT_TRIGGER |
| 34914 | 34953 | SQLITE_PRIVATE void sqlite3ShowTriggerStep(const TriggerStep *p){ |
| | @@ -36424,52 +36463,261 @@ |
| 36424 | 36463 | z++; |
| 36425 | 36464 | } |
| 36426 | 36465 | return h; |
| 36427 | 36466 | } |
| 36428 | 36467 | |
| 36429 | | -/* Double-Double multiplication. (x[0],x[1]) *= (y,yy) |
| 36430 | | -** |
| 36431 | | -** Reference: |
| 36432 | | -** T. J. Dekker, "A Floating-Point Technique for Extending the |
| 36433 | | -** Available Precision". 1971-07-26. |
| 36434 | | -*/ |
| 36435 | | -static void dekkerMul2(volatile double *x, double y, double yy){ |
| 36436 | | - /* |
| 36437 | | - ** The "volatile" keywords on parameter x[] and on local variables |
| 36438 | | - ** below are needed force intermediate results to be truncated to |
| 36439 | | - ** binary64 rather than be carried around in an extended-precision |
| 36440 | | - ** format. The truncation is necessary for the Dekker algorithm to |
| 36441 | | - ** work. Intel x86 floating point might omit the truncation without |
| 36442 | | - ** the use of volatile. |
| 36443 | | - */ |
| 36444 | | - volatile double tx, ty, p, q, c, cc; |
| 36445 | | - double hx, hy; |
| 36446 | | - u64 m; |
| 36447 | | - memcpy(&m, (void*)&x[0], 8); |
| 36448 | | - m &= 0xfffffffffc000000LL; |
| 36449 | | - memcpy(&hx, &m, 8); |
| 36450 | | - tx = x[0] - hx; |
| 36451 | | - memcpy(&m, &y, 8); |
| 36452 | | - m &= 0xfffffffffc000000LL; |
| 36453 | | - memcpy(&hy, &m, 8); |
| 36454 | | - ty = y - hy; |
| 36455 | | - p = hx*hy; |
| 36456 | | - q = hx*ty + tx*hy; |
| 36457 | | - c = p+q; |
| 36458 | | - cc = p - c + q + tx*ty; |
| 36459 | | - cc = x[0]*yy + x[1]*y + cc; |
| 36460 | | - x[0] = c + cc; |
| 36461 | | - x[1] = c - x[0]; |
| 36462 | | - x[1] += cc; |
| 36468 | +/* |
| 36469 | +** Two inputs are multiplied to get a 128-bit result. Return |
| 36470 | +** the high-order 64 bits of that result. |
| 36471 | +*/ |
| 36472 | +static u64 sqlite3Multiply128(u64 a, u64 b){ |
| 36473 | +#if (defined(__GNUC__) || defined(__clang__)) \ |
| 36474 | + && (defined(__x86_64__) || defined(__aarch64__) || defined(__riscv)) |
| 36475 | + return ((__uint128_t)a * b) >> 64; |
| 36476 | +#elif defined(_MSC_VER) && defined(_M_X64) |
| 36477 | + return __umulh(a, b); |
| 36478 | +#else |
| 36479 | + u64 a1 = (u32)a; |
| 36480 | + u64 a2 = a >> 32; |
| 36481 | + u64 b1 = (u32)b; |
| 36482 | + u64 b2 = b >> 32; |
| 36483 | + u64 p0 = a1 * b1; |
| 36484 | + u64 p1 = a1 * b2; |
| 36485 | + u64 p2 = a2 * b1; |
| 36486 | + u64 p3 = a2 * b2; |
| 36487 | + u64 carry = ((p0 >> 32) + (u32)p1 + (u32)p2) >> 32; |
| 36488 | + return p3 + (p1 >> 32) + (p2 >> 32) + carry; |
| 36489 | +#endif |
| 36490 | +} |
| 36491 | + |
| 36492 | +/* |
| 36493 | +** Return a u64 with the N-th bit set. |
| 36494 | +*/ |
| 36495 | +#define U64_BIT(N) (((u64)1)<<(N)) |
| 36496 | + |
| 36497 | +/* |
| 36498 | +** Range of powers of 10 that we need to deal with when converting |
| 36499 | +** IEEE754 doubles to and from decimal. |
| 36500 | +*/ |
| 36501 | +#define POWERSOF10_FIRST (-348) |
| 36502 | +#define POWERSOF10_LAST (+347) |
| 36503 | + |
| 36504 | +/* |
| 36505 | +** For any p between -348 and +347, return the integer part of |
| 36506 | +** |
| 36507 | +** pow(10,p) * pow(2,63-pow10to2(p)) |
| 36508 | +** |
| 36509 | +** Or, in other words, for any p in range, return the most significant |
| 36510 | +** 64 bits of pow(10,p). The pow(10,p) value is shifted left or right, |
| 36511 | +** as appropriate so the most significant 64 bits fit exactly into a |
| 36512 | +** 64-bit unsigned integer. |
| 36513 | +** |
| 36514 | +** Algorithm: |
| 36515 | +** |
| 36516 | +** (1) For p between 0 and 26, return the value directly from the aBase[] |
| 36517 | +** lookup table. |
| 36518 | +** |
| 36519 | +** (2) For p outside the range 0 to 26, use aScale[] for the initial value |
| 36520 | +** then refine that result (if necessary) by a single multiplication |
| 36521 | +** against aBase[]. |
| 36522 | +*/ |
| 36523 | +static u64 powerOfTen(int p){ |
| 36524 | + static const u64 aBase[] = { |
| 36525 | + 0x8000000000000000LLU, /* 0: 1.0e+0 << 63 */ |
| 36526 | + 0xa000000000000000LLU, /* 1: 1.0e+1 << 60 */ |
| 36527 | + 0xc800000000000000LLU, /* 2: 1.0e+2 << 57 */ |
| 36528 | + 0xfa00000000000000LLU, /* 3: 1.0e+3 << 54 */ |
| 36529 | + 0x9c40000000000000LLU, /* 4: 1.0e+4 << 50 */ |
| 36530 | + 0xc350000000000000LLU, /* 5: 1.0e+5 << 47 */ |
| 36531 | + 0xf424000000000000LLU, /* 6: 1.0e+6 << 44 */ |
| 36532 | + 0x9896800000000000LLU, /* 7: 1.0e+7 << 40 */ |
| 36533 | + 0xbebc200000000000LLU, /* 8: 1.0e+8 << 37 */ |
| 36534 | + 0xee6b280000000000LLU, /* 9: 1.0e+9 << 34 */ |
| 36535 | + 0x9502f90000000000LLU, /* 10: 1.0e+10 << 30 */ |
| 36536 | + 0xba43b74000000000LLU, /* 11: 1.0e+11 << 27 */ |
| 36537 | + 0xe8d4a51000000000LLU, /* 12: 1.0e+12 << 24 */ |
| 36538 | + 0x9184e72a00000000LLU, /* 13: 1.0e+13 << 20 */ |
| 36539 | + 0xb5e620f480000000LLU, /* 14: 1.0e+14 << 17 */ |
| 36540 | + 0xe35fa931a0000000LLU, /* 15: 1.0e+15 << 14 */ |
| 36541 | + 0x8e1bc9bf04000000LLU, /* 16: 1.0e+16 << 10 */ |
| 36542 | + 0xb1a2bc2ec5000000LLU, /* 17: 1.0e+17 << 7 */ |
| 36543 | + 0xde0b6b3a76400000LLU, /* 18: 1.0e+18 << 4 */ |
| 36544 | + 0x8ac7230489e80000LLU, /* 19: 1.0e+19 >> 0 */ |
| 36545 | + 0xad78ebc5ac620000LLU, /* 20: 1.0e+20 >> 3 */ |
| 36546 | + 0xd8d726b7177a8000LLU, /* 21: 1.0e+21 >> 6 */ |
| 36547 | + 0x878678326eac9000LLU, /* 22: 1.0e+22 >> 10 */ |
| 36548 | + 0xa968163f0a57b400LLU, /* 23: 1.0e+23 >> 13 */ |
| 36549 | + 0xd3c21bcecceda100LLU, /* 24: 1.0e+24 >> 16 */ |
| 36550 | + 0x84595161401484a0LLU, /* 25: 1.0e+25 >> 20 */ |
| 36551 | + 0xa56fa5b99019a5c8LLU, /* 26: 1.0e+26 >> 23 */ |
| 36552 | + }; |
| 36553 | + static const u64 aScale[] = { |
| 36554 | + 0x8049a4ac0c5811aeLLU, /* 0: 1.0e-351 << 1229 */ |
| 36555 | + 0xcf42894a5dce35eaLLU, /* 1: 1.0e-324 << 1140 */ |
| 36556 | + 0xa76c582338ed2621LLU, /* 2: 1.0e-297 << 1050 */ |
| 36557 | + 0x873e4f75e2224e68LLU, /* 3: 1.0e-270 << 960 */ |
| 36558 | + 0xda7f5bf590966848LLU, /* 4: 1.0e-243 << 871 */ |
| 36559 | + 0xb080392cc4349decLLU, /* 5: 1.0e-216 << 781 */ |
| 36560 | + 0x8e938662882af53eLLU, /* 6: 1.0e-189 << 691 */ |
| 36561 | + 0xe65829b3046b0afaLLU, /* 7: 1.0e-162 << 602 */ |
| 36562 | + 0xba121a4650e4ddebLLU, /* 8: 1.0e-135 << 512 */ |
| 36563 | + 0x964e858c91ba2655LLU, /* 9: 1.0e-108 << 422 */ |
| 36564 | + 0xf2d56790ab41c2a2LLU, /* 10: 1.0e-81 << 333 */ |
| 36565 | + 0xc428d05aa4751e4cLLU, /* 11: 1.0e-54 << 243 */ |
| 36566 | + 0x9e74d1b791e07e48LLU, /* 12: 1.0e-27 << 153 */ |
| 36567 | + 0x8000000000000000LLU, /* 13: 1.0e+0 << 63 */ |
| 36568 | + 0xcecb8f27f4200f3aLLU, /* 14: 1.0e+27 >> 26 */ |
| 36569 | + 0xa70c3c40a64e6c51LLU, /* 15: 1.0e+54 >> 116 */ |
| 36570 | + 0x86f0ac99b4e8dafdLLU, /* 16: 1.0e+81 >> 206 */ |
| 36571 | + 0xda01ee641a708de9LLU, /* 17: 1.0e+108 >> 295 */ |
| 36572 | + 0xb01ae745b101e9e4LLU, /* 18: 1.0e+135 >> 385 */ |
| 36573 | + 0x8e41ade9fbebc27dLLU, /* 19: 1.0e+162 >> 475 */ |
| 36574 | + 0xe5d3ef282a242e81LLU, /* 20: 1.0e+189 >> 564 */ |
| 36575 | + 0xb9a74a0637ce2ee1LLU, /* 21: 1.0e+216 >> 654 */ |
| 36576 | + 0x95f83d0a1fb69cd9LLU, /* 22: 1.0e+243 >> 744 */ |
| 36577 | + 0xf24a01a73cf2dccfLLU, /* 23: 1.0e+270 >> 833 */ |
| 36578 | + 0xc3b8358109e84f07LLU, /* 24: 1.0e+297 >> 923 */ |
| 36579 | + 0x9e19db92b4e31ba9LLU, /* 25: 1.0e+324 >> 1013 */ |
| 36580 | + }; |
| 36581 | + int g, n; |
| 36582 | + u64 x, y; |
| 36583 | + |
| 36584 | + assert( p>=POWERSOF10_FIRST && p<=POWERSOF10_LAST ); |
| 36585 | + if( p<0 ){ |
| 36586 | + g = p/27; |
| 36587 | + n = p%27; |
| 36588 | + if( n ){ |
| 36589 | + g--; |
| 36590 | + n += 27; |
| 36591 | + } |
| 36592 | + }else if( p<27 ){ |
| 36593 | + return aBase[p]; |
| 36594 | + }else{ |
| 36595 | + g = p/27; |
| 36596 | + n = p%27; |
| 36597 | + } |
| 36598 | + y = aScale[g+13]; |
| 36599 | + if( n==0 ){ |
| 36600 | + return y; |
| 36601 | + } |
| 36602 | + x = sqlite3Multiply128(aBase[n],y); |
| 36603 | + if( (U64_BIT(63) & x)==0 ){ |
| 36604 | + x <<= 1; |
| 36605 | + } |
| 36606 | + return x; |
| 36607 | +} |
| 36608 | + |
| 36609 | +/* |
| 36610 | +** pow10to2(x) computes floor(log2(pow(10,x))). |
| 36611 | +** pow2to10(y) computes floor(log10(pow(2,y))). |
| 36612 | +** |
| 36613 | +** Conceptually, pow10to2(p) converts a base-10 exponent p into |
| 36614 | +** a corresponding base-2 exponent, and pow2to10(e) converts a base-2 |
| 36615 | +** exponent into a base-10 exponent. |
| 36616 | +** |
| 36617 | +** The conversions are based on the observation that: |
| 36618 | +** |
| 36619 | +** ln(10.0)/ln(2.0) == 108853/32768 (approximately) |
| 36620 | +** ln(2.0)/ln(10.0) == 78913/262144 (approximately) |
| 36621 | +** |
| 36622 | +** These ratios are approximate, but they are accurate to 5 digits, |
| 36623 | +** which is close enough for the usage here. Right-shift is used |
| 36624 | +** for division so that rounding of negative numbers happens in the |
| 36625 | +** right direction. |
| 36626 | +*/ |
| 36627 | +static int pwr10to2(int p){ return (p*108853) >> 15; } |
| 36628 | +static int pwr2to10(int p){ return (p*78913) >> 18; } |
| 36629 | + |
| 36630 | +/* |
| 36631 | +** Count leading zeros for a 64-bit unsigned integer. |
| 36632 | +*/ |
| 36633 | +static int countLeadingZeros(u64 m){ |
| 36634 | +#if defined(__GNUC__) || defined(__clang__) |
| 36635 | + return __builtin_clzll(m); |
| 36636 | +#else |
| 36637 | + int n = 0; |
| 36638 | + if( m <= 0x00000000ffffffffULL) { n += 32; m <<= 32; } |
| 36639 | + if( m <= 0x0000ffffffffffffULL) { n += 16; m <<= 16; } |
| 36640 | + if( m <= 0x00ffffffffffffffULL) { n += 8; m <<= 8; } |
| 36641 | + if( m <= 0x0fffffffffffffffULL) { n += 4; m <<= 4; } |
| 36642 | + if( m <= 0x3fffffffffffffffULL) { n += 2; m <<= 2; } |
| 36643 | + if( m <= 0x7fffffffffffffffULL) { n += 1; } |
| 36644 | + return n; |
| 36645 | +#endif |
| 36646 | +} |
| 36647 | + |
| 36648 | +/* |
| 36649 | +** Given m and e, which represent a quantity r == m*pow(2,e), |
| 36650 | +** return values *pD and *pP such that r == (*pD)*pow(10,*pP), |
| 36651 | +** approximately. *pD should contain at least n significant digits. |
| 36652 | +** |
| 36653 | +** The input m is required to have its highest bit set. In other words, |
| 36654 | +** m should be left-shifted, and e decremented, to maximize the value of m. |
| 36655 | +*/ |
| 36656 | +static void sqlite3Fp2Convert10(u64 m, int e, int n, u64 *pD, int *pP){ |
| 36657 | + int p; |
| 36658 | + u64 h, out; |
| 36659 | + p = n - 1 - pwr2to10(e+63); |
| 36660 | + h = sqlite3Multiply128(m, powerOfTen(p)); |
| 36661 | + assert( -(e + pwr10to2(p) + 3) >=0 ); |
| 36662 | + assert( -(e + pwr10to2(p) + 3) <64 ); |
| 36663 | + out = h >> -(e + pwr10to2(p) + 3); |
| 36664 | + *pD = (out + 2 + ((out>>2)&1)) >> 2; |
| 36665 | + *pP = -p; |
| 36666 | +} |
| 36667 | + |
| 36668 | +/* |
| 36669 | +** Return an IEEE754 floating point value that approximates d*pow(10,p). |
| 36670 | +*/ |
| 36671 | +static double sqlite3Fp10Convert2(u64 d, int p){ |
| 36672 | + u64 out; |
| 36673 | + int b; |
| 36674 | + int e1; |
| 36675 | + int e2; |
| 36676 | + int lp; |
| 36677 | + u64 h; |
| 36678 | + double r; |
| 36679 | + assert( (d & U64_BIT(63))==0 ); |
| 36680 | + assert( d!=0 ); |
| 36681 | + if( p<POWERSOF10_FIRST ){ |
| 36682 | + return 0.0; |
| 36683 | + } |
| 36684 | + if( p>POWERSOF10_LAST ){ |
| 36685 | + return INFINITY; |
| 36686 | + } |
| 36687 | + b = 64 - countLeadingZeros(d); |
| 36688 | + lp = pwr10to2(p); |
| 36689 | + e1 = 53 - b - lp; |
| 36690 | + if( e1>1074 ){ |
| 36691 | + if( -(b + lp) >= 1077 ) return 0.0; |
| 36692 | + e1 = 1074; |
| 36693 | + } |
| 36694 | + e2 = e1 - (64-b); |
| 36695 | + h = sqlite3Multiply128(d<<(64-b), powerOfTen(p)); |
| 36696 | + assert( -(e2 + lp + 3) >=0 ); |
| 36697 | + assert( -(e2 + lp + 3) <64 ); |
| 36698 | + out = (h >> -(e2 + lp + 3)) | 1; |
| 36699 | + if( out >= U64_BIT(55)-2 ){ |
| 36700 | + out = (out>>1) | (out&1); |
| 36701 | + e1--; |
| 36702 | + } |
| 36703 | + if( e1<=(-972) ){ |
| 36704 | + return INFINITY; |
| 36705 | + } |
| 36706 | + out = (out + 1 + ((out>>2)&1)) >> 2; |
| 36707 | + if( (out & U64_BIT(52))!=0 ){ |
| 36708 | + out = (out & ~U64_BIT(52)) | ((u64)(1075-e1)<<52); |
| 36709 | + } |
| 36710 | + memcpy(&r, &out, 8); |
| 36711 | + return r; |
| 36463 | 36712 | } |
| 36464 | 36713 | |
| 36465 | 36714 | /* |
| 36466 | 36715 | ** The string z[] is an text representation of a real number. |
| 36467 | 36716 | ** Convert this string to a double and write it into *pResult. |
| 36468 | 36717 | ** |
| 36469 | | -** The string z[] is length bytes in length (bytes, not characters) and |
| 36470 | | -** uses the encoding enc. The string is not necessarily zero-terminated. |
| 36718 | +** z[] must be UTF-8 and zero-terminated. |
| 36471 | 36719 | ** |
| 36472 | 36720 | ** Return TRUE if the result is a valid real number (or integer) and FALSE |
| 36473 | 36721 | ** if the string is empty or contains extraneous text. More specifically |
| 36474 | 36722 | ** return |
| 36475 | 36723 | ** 1 => The input string is a pure integer |
| | @@ -36492,201 +36740,134 @@ |
| 36492 | 36740 | ** into *pResult. |
| 36493 | 36741 | */ |
| 36494 | 36742 | #if defined(_MSC_VER) |
| 36495 | 36743 | #pragma warning(disable : 4756) |
| 36496 | 36744 | #endif |
| 36497 | | -SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ |
| 36745 | +SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult){ |
| 36498 | 36746 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 36499 | | - int incr; |
| 36500 | | - const char *zEnd; |
| 36501 | 36747 | /* sign * significand * (10 ^ (esign * exponent)) */ |
| 36502 | | - int sign = 1; /* sign of significand */ |
| 36503 | | - u64 s = 0; /* significand */ |
| 36504 | | - int d = 0; /* adjust exponent for shifting decimal point */ |
| 36505 | | - int esign = 1; /* sign of exponent */ |
| 36506 | | - int e = 0; /* exponent */ |
| 36507 | | - int eValid = 1; /* True exponent is either not used or is well-formed */ |
| 36748 | + int neg = 0; /* True for a negative value */ |
| 36749 | + u64 s = 0; /* mantissa */ |
| 36750 | + int d = 0; /* Value is s * pow(10,d) */ |
| 36508 | 36751 | int nDigit = 0; /* Number of digits processed */ |
| 36509 | | - int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ |
| 36510 | | - u64 s2; /* round-tripped significand */ |
| 36511 | | - double rr[2]; |
| 36512 | | - |
| 36513 | | - assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
| 36514 | | - *pResult = 0.0; /* Default return value, in case of an error */ |
| 36515 | | - if( length==0 ) return 0; |
| 36516 | | - |
| 36517 | | - if( enc==SQLITE_UTF8 ){ |
| 36518 | | - incr = 1; |
| 36519 | | - zEnd = z + length; |
| 36520 | | - }else{ |
| 36521 | | - int i; |
| 36522 | | - incr = 2; |
| 36523 | | - length &= ~1; |
| 36524 | | - assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
| 36525 | | - testcase( enc==SQLITE_UTF16LE ); |
| 36526 | | - testcase( enc==SQLITE_UTF16BE ); |
| 36527 | | - for(i=3-enc; i<length && z[i]==0; i+=2){} |
| 36528 | | - if( i<length ) eType = -100; |
| 36529 | | - zEnd = &z[i^1]; |
| 36530 | | - z += (enc&1); |
| 36531 | | - } |
| 36752 | + int eType = 1; /* 1: pure integer, 2+: fractional */ |
| 36753 | + |
| 36754 | + *pResult = 0.0; /* Default return value, in case of an error */ |
| 36532 | 36755 | |
| 36533 | 36756 | /* skip leading spaces */ |
| 36534 | | - while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
| 36535 | | - if( z>=zEnd ) return 0; |
| 36757 | + while( sqlite3Isspace(*z) ) z++; |
| 36536 | 36758 | |
| 36537 | 36759 | /* get sign of significand */ |
| 36538 | 36760 | if( *z=='-' ){ |
| 36539 | | - sign = -1; |
| 36540 | | - z+=incr; |
| 36761 | + neg = 1; |
| 36762 | + z++; |
| 36541 | 36763 | }else if( *z=='+' ){ |
| 36542 | | - z+=incr; |
| 36764 | + z++; |
| 36543 | 36765 | } |
| 36544 | 36766 | |
| 36545 | 36767 | /* copy max significant digits to significand */ |
| 36546 | | - while( z<zEnd && sqlite3Isdigit(*z) ){ |
| 36768 | + while( sqlite3Isdigit(*z) ){ |
| 36547 | 36769 | s = s*10 + (*z - '0'); |
| 36548 | | - z+=incr; nDigit++; |
| 36549 | | - if( s>=((LARGEST_UINT64-9)/10) ){ |
| 36770 | + z++; nDigit++; |
| 36771 | + if( s>=((LARGEST_INT64-9)/10) ){ |
| 36550 | 36772 | /* skip non-significant significand digits |
| 36551 | 36773 | ** (increase exponent by d to shift decimal left) */ |
| 36552 | | - while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; } |
| 36774 | + while( sqlite3Isdigit(*z) ){ z++; d++; } |
| 36553 | 36775 | } |
| 36554 | 36776 | } |
| 36555 | | - if( z>=zEnd ) goto do_atof_calc; |
| 36556 | 36777 | |
| 36557 | 36778 | /* if decimal point is present */ |
| 36558 | 36779 | if( *z=='.' ){ |
| 36559 | | - z+=incr; |
| 36780 | + z++; |
| 36560 | 36781 | eType++; |
| 36561 | 36782 | /* copy digits from after decimal to significand |
| 36562 | 36783 | ** (decrease exponent by d to shift decimal right) */ |
| 36563 | | - while( z<zEnd && sqlite3Isdigit(*z) ){ |
| 36564 | | - if( s<((LARGEST_UINT64-9)/10) ){ |
| 36784 | + while( sqlite3Isdigit(*z) ){ |
| 36785 | + if( s<((LARGEST_INT64-9)/10) ){ |
| 36565 | 36786 | s = s*10 + (*z - '0'); |
| 36566 | 36787 | d--; |
| 36567 | 36788 | nDigit++; |
| 36568 | 36789 | } |
| 36569 | | - z+=incr; |
| 36790 | + z++; |
| 36570 | 36791 | } |
| 36571 | 36792 | } |
| 36572 | | - if( z>=zEnd ) goto do_atof_calc; |
| 36573 | 36793 | |
| 36574 | 36794 | /* if exponent is present */ |
| 36575 | 36795 | if( *z=='e' || *z=='E' ){ |
| 36576 | | - z+=incr; |
| 36577 | | - eValid = 0; |
| 36796 | + int esign = 1; /* sign of exponent */ |
| 36797 | + z++; |
| 36578 | 36798 | eType++; |
| 36579 | 36799 | |
| 36580 | | - /* This branch is needed to avoid a (harmless) buffer overread. The |
| 36581 | | - ** special comment alerts the mutation tester that the correct answer |
| 36582 | | - ** is obtained even if the branch is omitted */ |
| 36583 | | - if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ |
| 36584 | | - |
| 36585 | 36800 | /* get sign of exponent */ |
| 36586 | 36801 | if( *z=='-' ){ |
| 36587 | 36802 | esign = -1; |
| 36588 | | - z+=incr; |
| 36803 | + z++; |
| 36589 | 36804 | }else if( *z=='+' ){ |
| 36590 | | - z+=incr; |
| 36805 | + z++; |
| 36591 | 36806 | } |
| 36592 | 36807 | /* copy digits to exponent */ |
| 36593 | | - while( z<zEnd && sqlite3Isdigit(*z) ){ |
| 36594 | | - e = e<10000 ? (e*10 + (*z - '0')) : 10000; |
| 36595 | | - z+=incr; |
| 36596 | | - eValid = 1; |
| 36808 | + if( sqlite3Isdigit(*z) ){ |
| 36809 | + int exp = *z - '0'; |
| 36810 | + z++; |
| 36811 | + while( sqlite3Isdigit(*z) ){ |
| 36812 | + exp = exp<10000 ? (exp*10 + (*z - '0')) : 10000; |
| 36813 | + z++; |
| 36814 | + } |
| 36815 | + d += esign*exp; |
| 36816 | + }else{ |
| 36817 | + eType = -1; |
| 36597 | 36818 | } |
| 36598 | 36819 | } |
| 36599 | 36820 | |
| 36600 | 36821 | /* skip trailing spaces */ |
| 36601 | | - while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
| 36822 | + while( sqlite3Isspace(*z) ) z++; |
| 36602 | 36823 | |
| 36603 | | -do_atof_calc: |
| 36604 | 36824 | /* Zero is a special case */ |
| 36605 | 36825 | if( s==0 ){ |
| 36606 | | - *pResult = sign<0 ? -0.0 : +0.0; |
| 36607 | | - goto atof_return; |
| 36608 | | - } |
| 36609 | | - |
| 36610 | | - /* adjust exponent by d, and update sign */ |
| 36611 | | - e = (e*esign) + d; |
| 36612 | | - |
| 36613 | | - /* Try to adjust the exponent to make it smaller */ |
| 36614 | | - while( e>0 && s<((LARGEST_UINT64-0x7ff)/10) ){ |
| 36615 | | - s *= 10; |
| 36616 | | - e--; |
| 36617 | | - } |
| 36618 | | - while( e<0 && (s%10)==0 ){ |
| 36619 | | - s /= 10; |
| 36620 | | - e++; |
| 36621 | | - } |
| 36622 | | - |
| 36623 | | - rr[0] = (double)s; |
| 36624 | | - assert( sizeof(s2)==sizeof(rr[0]) ); |
| 36625 | | -#ifdef SQLITE_DEBUG |
| 36626 | | - rr[1] = 18446744073709549568.0; |
| 36627 | | - memcpy(&s2, &rr[1], sizeof(s2)); |
| 36628 | | - assert( s2==0x43efffffffffffffLL ); |
| 36629 | | -#endif |
| 36630 | | - /* Largest double that can be safely converted to u64 |
| 36631 | | - ** vvvvvvvvvvvvvvvvvvvvvv */ |
| 36632 | | - if( rr[0]<=18446744073709549568.0 ){ |
| 36633 | | - s2 = (u64)rr[0]; |
| 36634 | | - rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s); |
| 36635 | | - }else{ |
| 36636 | | - rr[1] = 0.0; |
| 36637 | | - } |
| 36638 | | - assert( rr[1]<=1.0e-10*rr[0] ); /* Equal only when rr[0]==0.0 */ |
| 36639 | | - |
| 36640 | | - if( e>0 ){ |
| 36641 | | - while( e>=100 ){ |
| 36642 | | - e -= 100; |
| 36643 | | - dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); |
| 36644 | | - } |
| 36645 | | - while( e>=10 ){ |
| 36646 | | - e -= 10; |
| 36647 | | - dekkerMul2(rr, 1.0e+10, 0.0); |
| 36648 | | - } |
| 36649 | | - while( e>=1 ){ |
| 36650 | | - e -= 1; |
| 36651 | | - dekkerMul2(rr, 1.0e+01, 0.0); |
| 36652 | | - } |
| 36653 | | - }else{ |
| 36654 | | - while( e<=-100 ){ |
| 36655 | | - e += 100; |
| 36656 | | - dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); |
| 36657 | | - } |
| 36658 | | - while( e<=-10 ){ |
| 36659 | | - e += 10; |
| 36660 | | - dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); |
| 36661 | | - } |
| 36662 | | - while( e<=-1 ){ |
| 36663 | | - e += 1; |
| 36664 | | - dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); |
| 36665 | | - } |
| 36666 | | - } |
| 36667 | | - *pResult = rr[0]+rr[1]; |
| 36668 | | - if( sqlite3IsNaN(*pResult) ) *pResult = 1e300*1e300; |
| 36669 | | - if( sign<0 ) *pResult = -*pResult; |
| 36670 | | - assert( !sqlite3IsNaN(*pResult) ); |
| 36671 | | - |
| 36672 | | -atof_return: |
| 36826 | + *pResult = neg ? -0.0 : +0.0; |
| 36827 | + }else{ |
| 36828 | + *pResult = sqlite3Fp10Convert2(s,d); |
| 36829 | + if( neg ) *pResult = -*pResult; |
| 36830 | + assert( !sqlite3IsNaN(*pResult) ); |
| 36831 | + } |
| 36832 | + |
| 36673 | 36833 | /* return true if number and no extra non-whitespace characters after */ |
| 36674 | | - if( z==zEnd && nDigit>0 && eValid && eType>0 ){ |
| 36834 | + if( z[0]==0 && nDigit>0 ){ |
| 36675 | 36835 | return eType; |
| 36676 | | - }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){ |
| 36836 | + }else if( eType>=2 && nDigit>0 ){ |
| 36677 | 36837 | return -1; |
| 36678 | 36838 | }else{ |
| 36679 | 36839 | return 0; |
| 36680 | 36840 | } |
| 36681 | 36841 | #else |
| 36682 | | - return !sqlite3Atoi64(z, pResult, length, enc); |
| 36842 | + return !sqlite3Atoi64(z, pResult, strlen(z), SQLITE_UTF8); |
| 36683 | 36843 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 36684 | 36844 | } |
| 36685 | 36845 | #if defined(_MSC_VER) |
| 36686 | 36846 | #pragma warning(default : 4756) |
| 36687 | 36847 | #endif |
| 36848 | + |
| 36849 | +/* |
| 36850 | +** Digit pairs used to convert a U64 or I64 into text, two digits |
| 36851 | +** at a time. |
| 36852 | +*/ |
| 36853 | +static const union { |
| 36854 | + char a[200]; |
| 36855 | + short int forceAlignment; |
| 36856 | +} sqlite3DigitPairs = { |
| 36857 | + "00010203040506070809" |
| 36858 | + "10111213141516171819" |
| 36859 | + "20212223242526272829" |
| 36860 | + "30313233343536373839" |
| 36861 | + "40414243444546474849" |
| 36862 | + "50515253545556575859" |
| 36863 | + "60616263646566676869" |
| 36864 | + "70717273747576777879" |
| 36865 | + "80818283848586878889" |
| 36866 | + "90919293949596979899" |
| 36867 | +}; |
| 36868 | + |
| 36688 | 36869 | |
| 36689 | 36870 | /* |
| 36690 | 36871 | ** Render an signed 64-bit integer as text. Store the result in zOut[] and |
| 36691 | 36872 | ** return the length of the string that was stored, in bytes. The value |
| 36692 | 36873 | ** returned does not include the zero terminator at the end of the output |
| | @@ -36695,27 +36876,39 @@ |
| 36695 | 36876 | ** The caller must ensure that zOut[] is at least 21 bytes in size. |
| 36696 | 36877 | */ |
| 36697 | 36878 | SQLITE_PRIVATE int sqlite3Int64ToText(i64 v, char *zOut){ |
| 36698 | 36879 | int i; |
| 36699 | 36880 | u64 x; |
| 36700 | | - char zTemp[22]; |
| 36701 | | - if( v<0 ){ |
| 36702 | | - x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; |
| 36703 | | - }else{ |
| 36881 | + union { |
| 36882 | + char a[23]; |
| 36883 | + u16 forceAlignment; |
| 36884 | + } u; |
| 36885 | + if( v>0 ){ |
| 36704 | 36886 | x = v; |
| 36705 | | - } |
| 36706 | | - i = sizeof(zTemp)-2; |
| 36707 | | - zTemp[sizeof(zTemp)-1] = 0; |
| 36708 | | - while( 1 /*exit-by-break*/ ){ |
| 36709 | | - zTemp[i] = (x%10) + '0'; |
| 36710 | | - x = x/10; |
| 36711 | | - if( x==0 ) break; |
| 36712 | | - i--; |
| 36713 | | - }; |
| 36714 | | - if( v<0 ) zTemp[--i] = '-'; |
| 36715 | | - memcpy(zOut, &zTemp[i], sizeof(zTemp)-i); |
| 36716 | | - return sizeof(zTemp)-1-i; |
| 36887 | + }else if( v==0 ){ |
| 36888 | + zOut[0] = '0'; |
| 36889 | + zOut[1] = 0; |
| 36890 | + return 1; |
| 36891 | + }else{ |
| 36892 | + x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; |
| 36893 | + } |
| 36894 | + i = sizeof(u.a)-1; |
| 36895 | + u.a[i] = 0; |
| 36896 | + while( x>=10 ){ |
| 36897 | + int kk = (x%100)*2; |
| 36898 | + assert( TWO_BYTE_ALIGNMENT(&sqlite3DigitPairs.a[kk]) ); |
| 36899 | + assert( TWO_BYTE_ALIGNMENT(&u.a[i-2]) ); |
| 36900 | + *(u16*)(&u.a[i-2]) = *(u16*)&sqlite3DigitPairs.a[kk]; |
| 36901 | + i -= 2; |
| 36902 | + x /= 100; |
| 36903 | + } |
| 36904 | + if( x ){ |
| 36905 | + u.a[--i] = x + '0'; |
| 36906 | + } |
| 36907 | + if( v<0 ) u.a[--i] = '-'; |
| 36908 | + memcpy(zOut, &u.a[i], sizeof(u.a)-i); |
| 36909 | + return sizeof(u.a)-1-i; |
| 36717 | 36910 | } |
| 36718 | 36911 | |
| 36719 | 36912 | /* |
| 36720 | 36913 | ** Compare the 19-character string zNum against the text representation |
| 36721 | 36914 | ** value 2^63: 9223372036854775808. Return negative, zero, or positive |
| | @@ -36984,11 +37177,10 @@ |
| 36984 | 37177 | */ |
| 36985 | 37178 | SQLITE_PRIVATE void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){ |
| 36986 | 37179 | int i; |
| 36987 | 37180 | u64 v; |
| 36988 | 37181 | int e, exp = 0; |
| 36989 | | - double rr[2]; |
| 36990 | 37182 | |
| 36991 | 37183 | p->isSpecial = 0; |
| 36992 | 37184 | p->z = p->zBuf; |
| 36993 | 37185 | assert( mxRound>0 ); |
| 36994 | 37186 | |
| | @@ -37005,64 +37197,43 @@ |
| 37005 | 37197 | return; |
| 37006 | 37198 | }else{ |
| 37007 | 37199 | p->sign = '+'; |
| 37008 | 37200 | } |
| 37009 | 37201 | memcpy(&v,&r,8); |
| 37010 | | - e = v>>52; |
| 37011 | | - if( (e&0x7ff)==0x7ff ){ |
| 37202 | + e = (v>>52)&0x7ff; |
| 37203 | + if( e==0x7ff ){ |
| 37012 | 37204 | p->isSpecial = 1 + (v!=0x7ff0000000000000LL); |
| 37013 | 37205 | p->n = 0; |
| 37014 | 37206 | p->iDP = 0; |
| 37015 | 37207 | return; |
| 37016 | 37208 | } |
| 37017 | | - |
| 37018 | | - /* Multiply r by powers of ten until it lands somewhere in between |
| 37019 | | - ** 1.0e+19 and 1.0e+17. |
| 37020 | | - ** |
| 37021 | | - ** Use Dekker-style double-double computation to increase the |
| 37022 | | - ** precision. |
| 37023 | | - ** |
| 37024 | | - ** The error terms on constants like 1.0e+100 computed using the |
| 37025 | | - ** decimal extension, for example as follows: |
| 37026 | | - ** |
| 37027 | | - ** SELECT decimal_exp(decimal_sub('1.0e+100',decimal(1.0e+100))); |
| 37028 | | - */ |
| 37029 | | - rr[0] = r; |
| 37030 | | - rr[1] = 0.0; |
| 37031 | | - if( rr[0]>9.223372036854774784e+18 ){ |
| 37032 | | - while( rr[0]>9.223372036854774784e+118 ){ |
| 37033 | | - exp += 100; |
| 37034 | | - dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); |
| 37035 | | - } |
| 37036 | | - while( rr[0]>9.223372036854774784e+28 ){ |
| 37037 | | - exp += 10; |
| 37038 | | - dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); |
| 37039 | | - } |
| 37040 | | - while( rr[0]>9.223372036854774784e+18 ){ |
| 37041 | | - exp += 1; |
| 37042 | | - dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); |
| 37043 | | - } |
| 37209 | + v &= 0x000fffffffffffffULL; |
| 37210 | + if( e==0 ){ |
| 37211 | + int n = countLeadingZeros(v); |
| 37212 | + v <<= n; |
| 37213 | + e = -1074 - n; |
| 37044 | 37214 | }else{ |
| 37045 | | - while( rr[0]<9.223372036854774784e-83 ){ |
| 37046 | | - exp -= 100; |
| 37047 | | - dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); |
| 37048 | | - } |
| 37049 | | - while( rr[0]<9.223372036854774784e+07 ){ |
| 37050 | | - exp -= 10; |
| 37051 | | - dekkerMul2(rr, 1.0e+10, 0.0); |
| 37052 | | - } |
| 37053 | | - while( rr[0]<9.22337203685477478e+17 ){ |
| 37054 | | - exp -= 1; |
| 37055 | | - dekkerMul2(rr, 1.0e+01, 0.0); |
| 37056 | | - } |
| 37057 | | - } |
| 37058 | | - v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1]; |
| 37215 | + v = (v<<11) | U64_BIT(63); |
| 37216 | + e -= 1086; |
| 37217 | + } |
| 37218 | + sqlite3Fp2Convert10(v, e, 17, &v, &exp); |
| 37059 | 37219 | |
| 37060 | 37220 | /* Extract significant digits. */ |
| 37061 | 37221 | i = sizeof(p->zBuf)-1; |
| 37062 | 37222 | assert( v>0 ); |
| 37063 | | - while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; } |
| 37223 | + while( v>=10 ){ |
| 37224 | + int kk = (v%100)*2; |
| 37225 | + assert( TWO_BYTE_ALIGNMENT(&sqlite3DigitPairs.a[kk]) ); |
| 37226 | + assert( TWO_BYTE_ALIGNMENT(&p->zBuf[i-1]) ); |
| 37227 | + *(u16*)(&p->zBuf[i-1]) = *(u16*)&sqlite3DigitPairs.a[kk]; |
| 37228 | + i -= 2; |
| 37229 | + v /= 100; |
| 37230 | + } |
| 37231 | + if( v ){ |
| 37232 | + assert( v<10 ); |
| 37233 | + p->zBuf[i--] = v + '0'; |
| 37234 | + } |
| 37064 | 37235 | assert( i>=0 && i<sizeof(p->zBuf)-1 ); |
| 37065 | 37236 | p->n = sizeof(p->zBuf) - 1 - i; |
| 37066 | 37237 | assert( p->n>0 ); |
| 37067 | 37238 | assert( p->n<sizeof(p->zBuf) ); |
| 37068 | 37239 | p->iDP = p->n + exp; |
| | @@ -84980,36 +85151,40 @@ |
| 84980 | 85151 | ** If pMem is already a string, detect if it is a zero-terminated |
| 84981 | 85152 | ** string, or make it into one if possible, and mark it as such. |
| 84982 | 85153 | ** |
| 84983 | 85154 | ** This is an optimization. Correct operation continues even if |
| 84984 | 85155 | ** this routine is a no-op. |
| 85156 | +** |
| 85157 | +** Return true if the strig is zero-terminated after this routine is |
| 85158 | +** called and false if it is not. |
| 84985 | 85159 | */ |
| 84986 | | -SQLITE_PRIVATE void sqlite3VdbeMemZeroTerminateIfAble(Mem *pMem){ |
| 85160 | +SQLITE_PRIVATE int sqlite3VdbeMemZeroTerminateIfAble(Mem *pMem){ |
| 84987 | 85161 | if( (pMem->flags & (MEM_Str|MEM_Term|MEM_Ephem|MEM_Static))!=MEM_Str ){ |
| 84988 | 85162 | /* pMem must be a string, and it cannot be an ephemeral or static string */ |
| 84989 | | - return; |
| 85163 | + return 0; |
| 84990 | 85164 | } |
| 84991 | | - if( pMem->enc!=SQLITE_UTF8 ) return; |
| 85165 | + if( pMem->enc!=SQLITE_UTF8 ) return 0; |
| 84992 | 85166 | assert( pMem->z!=0 ); |
| 84993 | 85167 | if( pMem->flags & MEM_Dyn ){ |
| 84994 | 85168 | if( pMem->xDel==sqlite3_free |
| 84995 | 85169 | && sqlite3_msize(pMem->z) >= (u64)(pMem->n+1) |
| 84996 | 85170 | ){ |
| 84997 | 85171 | pMem->z[pMem->n] = 0; |
| 84998 | 85172 | pMem->flags |= MEM_Term; |
| 84999 | | - return; |
| 85173 | + return 1; |
| 85000 | 85174 | } |
| 85001 | 85175 | if( pMem->xDel==sqlite3RCStrUnref ){ |
| 85002 | 85176 | /* Blindly assume that all RCStr objects are zero-terminated */ |
| 85003 | 85177 | pMem->flags |= MEM_Term; |
| 85004 | | - return; |
| 85178 | + return 1; |
| 85005 | 85179 | } |
| 85006 | 85180 | }else if( pMem->szMalloc >= pMem->n+1 ){ |
| 85007 | 85181 | pMem->z[pMem->n] = 0; |
| 85008 | 85182 | pMem->flags |= MEM_Term; |
| 85009 | | - return; |
| 85183 | + return 1; |
| 85010 | 85184 | } |
| 85185 | + return 0; |
| 85011 | 85186 | } |
| 85012 | 85187 | |
| 85013 | 85188 | /* |
| 85014 | 85189 | ** It is already known that pMem contains an unterminated string. |
| 85015 | 85190 | ** Add the zero terminator. |
| | @@ -85302,23 +85477,75 @@ |
| 85302 | 85477 | return memIntValue(pMem); |
| 85303 | 85478 | }else{ |
| 85304 | 85479 | return 0; |
| 85305 | 85480 | } |
| 85306 | 85481 | } |
| 85482 | + |
| 85483 | +/* |
| 85484 | +** Invoke sqlite3AtoF() on the text value of pMem and return the |
| 85485 | +** double result. If sqlite3AtoF() returns an error code, write |
| 85486 | +** that code into *pRC if (*pRC)!=NULL. |
| 85487 | +** |
| 85488 | +** The caller must ensure that pMem->db!=0 and that pMem is in |
| 85489 | +** mode MEM_Str or MEM_Blob. |
| 85490 | +*/ |
| 85491 | +SQLITE_PRIVATE SQLITE_NOINLINE double sqlite3MemRealValueRC(Mem *pMem, int *pRC){ |
| 85492 | + double val = (double)0; |
| 85493 | + int rc = 0; |
| 85494 | + assert( pMem->db!=0 ); |
| 85495 | + assert( pMem->flags & (MEM_Str|MEM_Blob) ); |
| 85496 | + if( pMem->z==0 ){ |
| 85497 | + /* no-op */ |
| 85498 | + }else if( pMem->enc==SQLITE_UTF8 |
| 85499 | + && ((pMem->flags & MEM_Term)!=0 || sqlite3VdbeMemZeroTerminateIfAble(pMem)) |
| 85500 | + ){ |
| 85501 | + rc = sqlite3AtoF(pMem->z, &val); |
| 85502 | + }else if( pMem->n==0 ){ |
| 85503 | + /* no-op */ |
| 85504 | + }else if( pMem->enc==SQLITE_UTF8 ){ |
| 85505 | + char *zCopy = sqlite3DbStrNDup(pMem->db, pMem->z, pMem->n); |
| 85506 | + if( zCopy ){ |
| 85507 | + rc = sqlite3AtoF(zCopy, &val); |
| 85508 | + sqlite3DbFree(pMem->db, zCopy); |
| 85509 | + } |
| 85510 | + }else{ |
| 85511 | + int n, i, j; |
| 85512 | + char *zCopy; |
| 85513 | + const char *z; |
| 85514 | + |
| 85515 | + n = pMem->n & ~1; |
| 85516 | + zCopy = sqlite3DbMallocRaw(pMem->db, n/2 + 2); |
| 85517 | + if( zCopy ){ |
| 85518 | + z = pMem->z; |
| 85519 | + if( pMem->enc==SQLITE_UTF16LE ){ |
| 85520 | + for(i=j=0; i<n-1; i+=2, j++){ |
| 85521 | + zCopy[j] = z[i]; |
| 85522 | + if( z[i+1]!=0 ) break; |
| 85523 | + } |
| 85524 | + }else{ |
| 85525 | + for(i=j=0; i<n-1; i+=2, j++){ |
| 85526 | + if( z[i]!=0 ) break; |
| 85527 | + zCopy[j] = z[i+1]; |
| 85528 | + } |
| 85529 | + } |
| 85530 | + assert( j<=n/2 ); |
| 85531 | + zCopy[j] = 0; |
| 85532 | + rc = sqlite3AtoF(zCopy, &val); |
| 85533 | + if( i<n ) rc = -100; |
| 85534 | + sqlite3DbFree(pMem->db, zCopy); |
| 85535 | + } |
| 85536 | + } |
| 85537 | + if( pRC ) *pRC = rc; |
| 85538 | + return val; |
| 85539 | +} |
| 85307 | 85540 | |
| 85308 | 85541 | /* |
| 85309 | 85542 | ** Return the best representation of pMem that we can get into a |
| 85310 | 85543 | ** double. If pMem is already a double or an integer, return its |
| 85311 | 85544 | ** value. If it is a string or blob, try to convert it to a double. |
| 85312 | 85545 | ** If it is a NULL, return 0.0. |
| 85313 | 85546 | */ |
| 85314 | | -static SQLITE_NOINLINE double memRealValue(Mem *pMem){ |
| 85315 | | - /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 85316 | | - double val = (double)0; |
| 85317 | | - sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc); |
| 85318 | | - return val; |
| 85319 | | -} |
| 85320 | 85547 | SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){ |
| 85321 | 85548 | assert( pMem!=0 ); |
| 85322 | 85549 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 85323 | 85550 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| 85324 | 85551 | if( pMem->flags & MEM_Real ){ |
| | @@ -85325,11 +85552,11 @@ |
| 85325 | 85552 | return pMem->u.r; |
| 85326 | 85553 | }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){ |
| 85327 | 85554 | testcase( pMem->flags & MEM_IntReal ); |
| 85328 | 85555 | return (double)pMem->u.i; |
| 85329 | 85556 | }else if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
| 85330 | | - return memRealValue(pMem); |
| 85557 | + return sqlite3MemRealValueRC(pMem, 0); |
| 85331 | 85558 | }else{ |
| 85332 | 85559 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 85333 | 85560 | return (double)0; |
| 85334 | 85561 | } |
| 85335 | 85562 | } |
| | @@ -85449,11 +85676,11 @@ |
| 85449 | 85676 | if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){ |
| 85450 | 85677 | int rc; |
| 85451 | 85678 | sqlite3_int64 ix; |
| 85452 | 85679 | assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); |
| 85453 | 85680 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 85454 | | - rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); |
| 85681 | + pMem->u.r = sqlite3MemRealValueRC(pMem, &rc); |
| 85455 | 85682 | if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1) |
| 85456 | 85683 | || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r))) |
| 85457 | 85684 | ){ |
| 85458 | 85685 | pMem->u.i = ix; |
| 85459 | 85686 | MemSetTypeFlag(pMem, MEM_Int); |
| | @@ -86420,11 +86647,11 @@ |
| 86420 | 86647 | } |
| 86421 | 86648 | } |
| 86422 | 86649 | if( affinity==SQLITE_AFF_BLOB ){ |
| 86423 | 86650 | if( op==TK_FLOAT ){ |
| 86424 | 86651 | assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) ); |
| 86425 | | - sqlite3AtoF(pVal->z, &pVal->u.r, pVal->n, SQLITE_UTF8); |
| 86652 | + sqlite3AtoF(pVal->z, &pVal->u.r); |
| 86426 | 86653 | pVal->flags = MEM_Real; |
| 86427 | 86654 | }else if( op==TK_INTEGER ){ |
| 86428 | 86655 | /* This case is required by -9223372036854775808 and other strings |
| 86429 | 86656 | ** that look like integers but cannot be handled by the |
| 86430 | 86657 | ** sqlite3DecOrHexToI64() call above. */ |
| | @@ -86688,10 +86915,15 @@ |
| 86688 | 86915 | ** the column value into *ppVal. If *ppVal is initially NULL then a new |
| 86689 | 86916 | ** sqlite3_value object is allocated. |
| 86690 | 86917 | ** |
| 86691 | 86918 | ** If *ppVal is initially NULL then the caller is responsible for |
| 86692 | 86919 | ** ensuring that the value written into *ppVal is eventually freed. |
| 86920 | +** |
| 86921 | +** If the buffer does not contain a well-formed record, this routine may |
| 86922 | +** read several bytes past the end of the buffer. Callers must therefore |
| 86923 | +** ensure that any buffer which may contain a corrupt record is padded |
| 86924 | +** with at least 8 bytes of addressable memory. |
| 86693 | 86925 | */ |
| 86694 | 86926 | SQLITE_PRIVATE int sqlite3Stat4Column( |
| 86695 | 86927 | sqlite3 *db, /* Database handle */ |
| 86696 | 86928 | const void *pRec, /* Pointer to buffer containing record */ |
| 86697 | 86929 | int nRec, /* Size of buffer pRec in bytes */ |
| | @@ -95661,14 +95893,13 @@ |
| 95661 | 95893 | ** point or exponential notation, the result is only MEM_Real, even |
| 95662 | 95894 | ** if there is an exact integer representation of the quantity. |
| 95663 | 95895 | */ |
| 95664 | 95896 | static void applyNumericAffinity(Mem *pRec, int bTryForInt){ |
| 95665 | 95897 | double rValue; |
| 95666 | | - u8 enc = pRec->enc; |
| 95667 | 95898 | int rc; |
| 95668 | 95899 | assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ); |
| 95669 | | - rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc); |
| 95900 | + rValue = sqlite3MemRealValueRC(pRec, &rc); |
| 95670 | 95901 | if( rc<=0 ) return; |
| 95671 | 95902 | if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){ |
| 95672 | 95903 | pRec->flags |= MEM_Int; |
| 95673 | 95904 | }else{ |
| 95674 | 95905 | pRec->u.r = rValue; |
| | @@ -95746,11 +95977,14 @@ |
| 95746 | 95977 | */ |
| 95747 | 95978 | SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
| 95748 | 95979 | int eType = sqlite3_value_type(pVal); |
| 95749 | 95980 | if( eType==SQLITE_TEXT ){ |
| 95750 | 95981 | Mem *pMem = (Mem*)pVal; |
| 95982 | + assert( pMem->db!=0 ); |
| 95983 | + sqlite3_mutex_enter(pMem->db->mutex); |
| 95751 | 95984 | applyNumericAffinity(pMem, 0); |
| 95985 | + sqlite3_mutex_leave(pMem->db->mutex); |
| 95752 | 95986 | eType = sqlite3_value_type(pVal); |
| 95753 | 95987 | } |
| 95754 | 95988 | return eType; |
| 95755 | 95989 | } |
| 95756 | 95990 | |
| | @@ -95779,11 +96013,11 @@ |
| 95779 | 96013 | assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); |
| 95780 | 96014 | if( ExpandBlob(pMem) ){ |
| 95781 | 96015 | pMem->u.i = 0; |
| 95782 | 96016 | return MEM_Int; |
| 95783 | 96017 | } |
| 95784 | | - rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); |
| 96018 | + pMem->u.r = sqlite3MemRealValueRC(pMem, &rc); |
| 95785 | 96019 | if( rc<=0 ){ |
| 95786 | 96020 | if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){ |
| 95787 | 96021 | pMem->u.i = ix; |
| 95788 | 96022 | return MEM_Int; |
| 95789 | 96023 | }else{ |
| | @@ -110056,11 +110290,11 @@ |
| 110056 | 110290 | */ |
| 110057 | 110291 | static int exprProbability(Expr *p){ |
| 110058 | 110292 | double r = -1.0; |
| 110059 | 110293 | if( p->op!=TK_FLOAT ) return -1; |
| 110060 | 110294 | assert( !ExprHasProperty(p, EP_IntValue) ); |
| 110061 | | - sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); |
| 110295 | + sqlite3AtoF(p->u.zToken, &r); |
| 110062 | 110296 | assert( r>=0.0 ); |
| 110063 | 110297 | if( r>1.0 ) return -1; |
| 110064 | 110298 | return (int)(r*134217728.0); |
| 110065 | 110299 | } |
| 110066 | 110300 | |
| | @@ -111192,10 +111426,18 @@ |
| 111192 | 111426 | ** number of expressions in the select list. */ |
| 111193 | 111427 | if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){ |
| 111194 | 111428 | sqlite3SelectWrongNumTermsError(pParse, p->pNext); |
| 111195 | 111429 | return WRC_Abort; |
| 111196 | 111430 | } |
| 111431 | + |
| 111432 | + /* If the SELECT statement contains ON clauses that were moved into |
| 111433 | + ** the WHERE clause, go through and verify that none of the terms |
| 111434 | + ** in the ON clauses reference tables to the right of the ON clause. */ |
| 111435 | + if( (p->selFlags & SF_OnToWhere) ){ |
| 111436 | + sqlite3SelectCheckOnClauses(pParse, p); |
| 111437 | + if( pParse->nErr ) return WRC_Abort; |
| 111438 | + } |
| 111197 | 111439 | |
| 111198 | 111440 | /* Advance to the next term of the compound |
| 111199 | 111441 | */ |
| 111200 | 111442 | p = p->pPrior; |
| 111201 | 111443 | nCompound++; |
| | @@ -114854,18 +115096,25 @@ |
| 114854 | 115096 | /* Could not find an existing table or index to use as the RHS b-tree. |
| 114855 | 115097 | ** We will have to generate an ephemeral table to do the job. |
| 114856 | 115098 | */ |
| 114857 | 115099 | u32 savedNQueryLoop = pParse->nQueryLoop; |
| 114858 | 115100 | int rMayHaveNull = 0; |
| 115101 | + int bloomOk = (inFlags & IN_INDEX_MEMBERSHIP)!=0; |
| 114859 | 115102 | eType = IN_INDEX_EPH; |
| 114860 | 115103 | if( inFlags & IN_INDEX_LOOP ){ |
| 114861 | 115104 | pParse->nQueryLoop = 0; |
| 114862 | 115105 | }else if( prRhsHasNull ){ |
| 114863 | 115106 | *prRhsHasNull = rMayHaveNull = ++pParse->nMem; |
| 114864 | 115107 | } |
| 114865 | 115108 | assert( pX->op==TK_IN ); |
| 114866 | | - sqlite3CodeRhsOfIN(pParse, pX, iTab); |
| 115109 | + if( !bloomOk |
| 115110 | + && ExprUseXSelect(pX) |
| 115111 | + && (pX->x.pSelect->selFlags & SF_ClonedRhsIn)!=0 |
| 115112 | + ){ |
| 115113 | + bloomOk = 1; |
| 115114 | + } |
| 115115 | + sqlite3CodeRhsOfIN(pParse, pX, iTab, bloomOk); |
| 114867 | 115116 | if( rMayHaveNull ){ |
| 114868 | 115117 | sqlite3SetHasNullFlag(v, iTab, rMayHaveNull); |
| 114869 | 115118 | } |
| 114870 | 115119 | pParse->nQueryLoop = savedNQueryLoop; |
| 114871 | 115120 | } |
| | @@ -115019,11 +115268,12 @@ |
| 115019 | 115268 | ** is used. |
| 115020 | 115269 | */ |
| 115021 | 115270 | SQLITE_PRIVATE void sqlite3CodeRhsOfIN( |
| 115022 | 115271 | Parse *pParse, /* Parsing context */ |
| 115023 | 115272 | Expr *pExpr, /* The IN operator */ |
| 115024 | | - int iTab /* Use this cursor number */ |
| 115273 | + int iTab, /* Use this cursor number */ |
| 115274 | + int allowBloom /* True to allow the use of a Bloom filter */ |
| 115025 | 115275 | ){ |
| 115026 | 115276 | int addrOnce = 0; /* Address of the OP_Once instruction at top */ |
| 115027 | 115277 | int addr; /* Address of OP_OpenEphemeral instruction */ |
| 115028 | 115278 | Expr *pLeft; /* the LHS of the IN operator */ |
| 115029 | 115279 | KeyInfo *pKeyInfo = 0; /* Key information */ |
| | @@ -115141,11 +115391,14 @@ |
| 115141 | 115391 | int rc; |
| 115142 | 115392 | int addrBloom = 0; |
| 115143 | 115393 | sqlite3SelectDestInit(&dest, SRT_Set, iTab); |
| 115144 | 115394 | dest.zAffSdst = exprINAffinity(pParse, pExpr); |
| 115145 | 115395 | pSelect->iLimit = 0; |
| 115146 | | - if( addrOnce && OptimizationEnabled(pParse->db, SQLITE_BloomFilter) ){ |
| 115396 | + if( addrOnce |
| 115397 | + && allowBloom |
| 115398 | + && OptimizationEnabled(pParse->db, SQLITE_BloomFilter) |
| 115399 | + ){ |
| 115147 | 115400 | int regBloom = ++pParse->nMem; |
| 115148 | 115401 | addrBloom = sqlite3VdbeAddOp2(v, OP_Blob, 10000, regBloom); |
| 115149 | 115402 | VdbeComment((v, "Bloom filter")); |
| 115150 | 115403 | dest.iSDParm2 = regBloom; |
| 115151 | 115404 | } |
| | @@ -115726,11 +115979,11 @@ |
| 115726 | 115979 | ** like the continuation of the number. |
| 115727 | 115980 | */ |
| 115728 | 115981 | static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ |
| 115729 | 115982 | if( ALWAYS(z!=0) ){ |
| 115730 | 115983 | double value; |
| 115731 | | - sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); |
| 115984 | + sqlite3AtoF(z, &value); |
| 115732 | 115985 | assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ |
| 115733 | 115986 | if( negateFlag ) value = -value; |
| 115734 | 115987 | sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); |
| 115735 | 115988 | } |
| 115736 | 115989 | } |
| | @@ -118827,11 +119080,14 @@ |
| 118827 | 119080 | if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break; |
| 118828 | 119081 | } |
| 118829 | 119082 | if( pIEpr==0 ) break; |
| 118830 | 119083 | if( NEVER(!ExprUseYTab(pExpr)) ) break; |
| 118831 | 119084 | for(i=0; i<pSrcList->nSrc; i++){ |
| 118832 | | - if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break; |
| 119085 | + if( pSrcList->a[i].iCursor==pIEpr->iDataCur ){ |
| 119086 | + testcase( i>0 ); |
| 119087 | + break; |
| 119088 | + } |
| 118833 | 119089 | } |
| 118834 | 119090 | if( i>=pSrcList->nSrc ) break; |
| 118835 | 119091 | if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */ |
| 118836 | 119092 | if( pParse->nErr ){ return WRC_Abort; } |
| 118837 | 119093 | |
| | @@ -124902,11 +125158,11 @@ |
| 124902 | 125158 | if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 124903 | 125159 | #endif |
| 124904 | 125160 | sqlite3_mutex_enter(db->mutex); |
| 124905 | 125161 | db->xAuth = (sqlite3_xauth)xAuth; |
| 124906 | 125162 | db->pAuthArg = pArg; |
| 124907 | | - if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1); |
| 125163 | + sqlite3ExpirePreparedStatements(db, 1); |
| 124908 | 125164 | sqlite3_mutex_leave(db->mutex); |
| 124909 | 125165 | return SQLITE_OK; |
| 124910 | 125166 | } |
| 124911 | 125167 | |
| 124912 | 125168 | /* |
| | @@ -125573,10 +125829,11 @@ |
| 125573 | 125829 | SrcItem *p |
| 125574 | 125830 | ){ |
| 125575 | 125831 | const char *zDb; |
| 125576 | 125832 | if( p->fg.fixedSchema ){ |
| 125577 | 125833 | int iDb = sqlite3SchemaToIndex(pParse->db, p->u4.pSchema); |
| 125834 | + assert( iDb>=0 && iDb<pParse->db->nDb ); |
| 125578 | 125835 | zDb = pParse->db->aDb[iDb].zDbSName; |
| 125579 | 125836 | }else{ |
| 125580 | 125837 | assert( !p->fg.isSubquery ); |
| 125581 | 125838 | zDb = p->u4.zDatabase; |
| 125582 | 125839 | } |
| | @@ -127657,17 +127914,18 @@ |
| 127657 | 127914 | ** |
| 127658 | 127915 | ** zName is temporarily modified while this routine is running, but is |
| 127659 | 127916 | ** restored to its original value prior to this routine returning. |
| 127660 | 127917 | */ |
| 127661 | 127918 | SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName){ |
| 127662 | | - char *zTail; /* Pointer to the last "_" in zName */ |
| 127919 | + const char *zTail; /* Pointer to the last "_" in zName */ |
| 127663 | 127920 | Table *pTab; /* Table that zName is a shadow of */ |
| 127921 | + char *zCopy; |
| 127664 | 127922 | zTail = strrchr(zName, '_'); |
| 127665 | 127923 | if( zTail==0 ) return 0; |
| 127666 | | - *zTail = 0; |
| 127667 | | - pTab = sqlite3FindTable(db, zName, 0); |
| 127668 | | - *zTail = '_'; |
| 127924 | + zCopy = sqlite3DbStrNDup(db, zName, (int)(zTail-zName)); |
| 127925 | + pTab = zCopy ? sqlite3FindTable(db, zCopy, 0) : 0; |
| 127926 | + sqlite3DbFree(db, zCopy); |
| 127669 | 127927 | if( pTab==0 ) return 0; |
| 127670 | 127928 | if( !IsVirtual(pTab) ) return 0; |
| 127671 | 127929 | return sqlite3IsShadowTableOf(db, pTab, zName); |
| 127672 | 127930 | } |
| 127673 | 127931 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| | @@ -127816,10 +128074,11 @@ |
| 127816 | 128074 | } |
| 127817 | 128075 | p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; |
| 127818 | 128076 | convertToWithoutRowidTable(pParse, p); |
| 127819 | 128077 | } |
| 127820 | 128078 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
| 128079 | + assert( iDb>=0 && iDb<=db->nDb ); |
| 127821 | 128080 | |
| 127822 | 128081 | #ifndef SQLITE_OMIT_CHECK |
| 127823 | 128082 | /* Resolve names in all CHECK constraint expressions. |
| 127824 | 128083 | */ |
| 127825 | 128084 | if( p->pCheck ){ |
| | @@ -128111,10 +128370,11 @@ |
| 128111 | 128370 | p->tabFlags |= TF_NoVisibleRowid; /* Never allow rowid in view */ |
| 128112 | 128371 | #endif |
| 128113 | 128372 | |
| 128114 | 128373 | sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 128115 | 128374 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
| 128375 | + assert( iDb>=0 && iDb<db->nDb ); |
| 128116 | 128376 | sqlite3FixInit(&sFix, pParse, iDb, "view", pName); |
| 128117 | 128377 | if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail; |
| 128118 | 128378 | |
| 128119 | 128379 | /* Make a copy of the entire SELECT statement that defines the view. |
| 128120 | 128380 | ** This will force all the Expr.token.z values to be dynamically |
| | @@ -129707,10 +129967,11 @@ |
| 129707 | 129967 | sqlite3ErrorMsg(pParse, "index associated with UNIQUE " |
| 129708 | 129968 | "or PRIMARY KEY constraint cannot be dropped", 0); |
| 129709 | 129969 | goto exit_drop_index; |
| 129710 | 129970 | } |
| 129711 | 129971 | iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
| 129972 | + assert( iDb>=0 && iDb<db->nDb ); |
| 129712 | 129973 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 129713 | 129974 | { |
| 129714 | 129975 | int code = SQLITE_DROP_INDEX; |
| 129715 | 129976 | Table *pTab = pIndex->pTable; |
| 129716 | 129977 | const char *zDb = db->aDb[iDb].zDbSName; |
| | @@ -132953,11 +133214,11 @@ |
| 132953 | 133214 | zBuf = sqlite3_mprintf("%!.*f",(int)n,r); |
| 132954 | 133215 | if( zBuf==0 ){ |
| 132955 | 133216 | sqlite3_result_error_nomem(context); |
| 132956 | 133217 | return; |
| 132957 | 133218 | } |
| 132958 | | - sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); |
| 133219 | + sqlite3AtoF(zBuf, &r); |
| 132959 | 133220 | sqlite3_free(zBuf); |
| 132960 | 133221 | } |
| 132961 | 133222 | sqlite3_result_double(context, r); |
| 132962 | 133223 | } |
| 132963 | 133224 | #endif |
| | @@ -133591,11 +133852,11 @@ |
| 133591 | 133852 | const char *zVal; |
| 133592 | 133853 | r1 = sqlite3_value_double(pValue); |
| 133593 | 133854 | sqlite3_str_appendf(pStr, "%!0.15g", r1); |
| 133594 | 133855 | zVal = sqlite3_str_value(pStr); |
| 133595 | 133856 | if( zVal ){ |
| 133596 | | - sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8); |
| 133857 | + sqlite3AtoF(zVal, &r2); |
| 133597 | 133858 | if( r1!=r2 ){ |
| 133598 | 133859 | sqlite3_str_reset(pStr); |
| 133599 | 133860 | sqlite3_str_appendf(pStr, "%!0.20e", r1); |
| 133600 | 133861 | } |
| 133601 | 133862 | } |
| | @@ -133688,11 +133949,11 @@ |
| 133688 | 133949 | sqlite3_result_error_nomem(context); |
| 133689 | 133950 | return; |
| 133690 | 133951 | } |
| 133691 | 133952 | i = j = 0; |
| 133692 | 133953 | while( i<nIn ){ |
| 133693 | | - char *z = strchr(&zIn[i],'\\'); |
| 133954 | + const char *z = strchr(&zIn[i],'\\'); |
| 133694 | 133955 | if( z==0 ){ |
| 133695 | 133956 | n = nIn - i; |
| 133696 | 133957 | memmove(&zOut[j], &zIn[i], n); |
| 133697 | 133958 | j += n; |
| 133698 | 133959 | break; |
| | @@ -136859,10 +137120,11 @@ |
| 136859 | 137120 | /* If foreign-keys are disabled, this function is a no-op. */ |
| 136860 | 137121 | if( (db->flags&SQLITE_ForeignKeys)==0 ) return; |
| 136861 | 137122 | if( !IsOrdinaryTable(pTab) ) return; |
| 136862 | 137123 | |
| 136863 | 137124 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 137125 | + assert( iDb>=00 && iDb<db->nDb ); |
| 136864 | 137126 | zDb = db->aDb[iDb].zDbSName; |
| 136865 | 137127 | |
| 136866 | 137128 | /* Loop through all the foreign key constraints for which pTab is the |
| 136867 | 137129 | ** child table (the table that the foreign key definition is part of). */ |
| 136868 | 137130 | for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| | @@ -141374,10 +141636,11 @@ |
| 141374 | 141636 | int (*db_status64)(sqlite3*,int,sqlite3_int64*,sqlite3_int64*,int); |
| 141375 | 141637 | /* Version 3.52.0 and later */ |
| 141376 | 141638 | void (*str_truncate)(sqlite3_str*,int); |
| 141377 | 141639 | void (*str_free)(sqlite3_str*); |
| 141378 | 141640 | int (*carray_bind)(sqlite3_stmt*,int,void*,int,int,void(*)(void*)); |
| 141641 | + int (*carray_bind_v2)(sqlite3_stmt*,int,void*,int,int,void(*)(void*),void*); |
| 141379 | 141642 | }; |
| 141380 | 141643 | |
| 141381 | 141644 | /* |
| 141382 | 141645 | ** This is the function signature used for all extension entry points. It |
| 141383 | 141646 | ** is also defined in the file "loadext.c". |
| | @@ -141716,10 +141979,11 @@ |
| 141716 | 141979 | #define sqlite3_db_status64 sqlite3_api->db_status64 |
| 141717 | 141980 | /* Version 3.52.0 and later */ |
| 141718 | 141981 | #define sqlite3_str_truncate sqlite3_api->str_truncate |
| 141719 | 141982 | #define sqlite3_str_free sqlite3_api->str_free |
| 141720 | 141983 | #define sqlite3_carray_bind sqlite3_api->carray_bind |
| 141984 | +#define sqlite3_carray_bind_v2 sqlite3_api->carray_bind_v2 |
| 141721 | 141985 | #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 141722 | 141986 | |
| 141723 | 141987 | #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 141724 | 141988 | /* This case when the file really is being compiled as a loadable |
| 141725 | 141989 | ** extension */ |
| | @@ -142247,12 +142511,14 @@ |
| 142247 | 142511 | sqlite3_db_status64, |
| 142248 | 142512 | /* Version 3.52.0 and later */ |
| 142249 | 142513 | sqlite3_str_truncate, |
| 142250 | 142514 | sqlite3_str_free, |
| 142251 | 142515 | #ifdef SQLITE_ENABLE_CARRAY |
| 142252 | | - sqlite3_carray_bind |
| 142516 | + sqlite3_carray_bind, |
| 142517 | + sqlite3_carray_bind_v2 |
| 142253 | 142518 | #else |
| 142519 | + 0, |
| 142254 | 142520 | 0 |
| 142255 | 142521 | #endif |
| 142256 | 142522 | }; |
| 142257 | 142523 | |
| 142258 | 142524 | /* True if x is the directory separator character |
| | @@ -148181,10 +148447,14 @@ |
| 148181 | 148447 | p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn); |
| 148182 | 148448 | pRight->u3.pOn = 0; |
| 148183 | 148449 | pRight->fg.isOn = 1; |
| 148184 | 148450 | p->selFlags |= SF_OnToWhere; |
| 148185 | 148451 | } |
| 148452 | + |
| 148453 | + if( IsVirtual(pRightTab) && joinType==EP_OuterON && pRight->u1.pFuncArg ){ |
| 148454 | + p->selFlags |= SF_OnToWhere; |
| 148455 | + } |
| 148186 | 148456 | } |
| 148187 | 148457 | return 0; |
| 148188 | 148458 | } |
| 148189 | 148459 | |
| 148190 | 148460 | /* |
| | @@ -150108,11 +150378,11 @@ |
| 150108 | 150378 | ** function is responsible for ensuring that this structure is eventually |
| 150109 | 150379 | ** freed. |
| 150110 | 150380 | */ |
| 150111 | 150381 | static KeyInfo *multiSelectByMergeKeyInfo(Parse *pParse, Select *p, int nExtra){ |
| 150112 | 150382 | ExprList *pOrderBy = p->pOrderBy; |
| 150113 | | - int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0; |
| 150383 | + int nOrderBy = (pOrderBy!=0) ? pOrderBy->nExpr : 0; |
| 150114 | 150384 | sqlite3 *db = pParse->db; |
| 150115 | 150385 | KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1); |
| 150116 | 150386 | if( pRet ){ |
| 150117 | 150387 | int i; |
| 150118 | 150388 | for(i=0; i<nOrderBy; i++){ |
| | @@ -150911,14 +151181,12 @@ |
| 150911 | 151181 | ** EofB: ... |
| 150912 | 151182 | ** AltB: ... |
| 150913 | 151183 | ** AeqB: ... |
| 150914 | 151184 | ** AgtB: ... |
| 150915 | 151185 | ** Init: initialize coroutine registers |
| 150916 | | -** yield coA |
| 150917 | | -** if eof(A) goto EofA |
| 150918 | | -** yield coB |
| 150919 | | -** if eof(B) goto EofB |
| 151186 | +** yield coA, on eof goto EofA |
| 151187 | +** yield coB, on eof goto EofB |
| 150920 | 151188 | ** Cmpr: Compare A, B |
| 150921 | 151189 | ** Jump AltB, AeqB, AgtB |
| 150922 | 151190 | ** End: ... |
| 150923 | 151191 | ** |
| 150924 | 151192 | ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not |
| | @@ -151006,30 +151274,33 @@ |
| 151006 | 151274 | } |
| 151007 | 151275 | } |
| 151008 | 151276 | } |
| 151009 | 151277 | |
| 151010 | 151278 | /* Compute the comparison permutation and keyinfo that is used with |
| 151011 | | - ** the permutation used to determine if the next |
| 151012 | | - ** row of results comes from selectA or selectB. Also add explicit |
| 151013 | | - ** collations to the ORDER BY clause terms so that when the subqueries |
| 151014 | | - ** to the right and the left are evaluated, they use the correct |
| 151015 | | - ** collation. |
| 151279 | + ** the permutation to determine if the next row of results comes |
| 151280 | + ** from selectA or selectB. Also add literal collations to the |
| 151281 | + ** ORDER BY clause terms so that when selectA and selectB are |
| 151282 | + ** evaluated, they use the correct collation. |
| 151016 | 151283 | */ |
| 151017 | 151284 | aPermute = sqlite3DbMallocRawNN(db, sizeof(u32)*(nOrderBy + 1)); |
| 151018 | 151285 | if( aPermute ){ |
| 151019 | 151286 | struct ExprList_item *pItem; |
| 151287 | + int bKeep = 0; |
| 151020 | 151288 | aPermute[0] = nOrderBy; |
| 151021 | 151289 | for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){ |
| 151022 | 151290 | assert( pItem!=0 ); |
| 151023 | 151291 | assert( pItem->u.x.iOrderByCol>0 ); |
| 151024 | 151292 | assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ); |
| 151025 | 151293 | aPermute[i] = pItem->u.x.iOrderByCol - 1; |
| 151294 | + if( aPermute[i]!=(u32)i-1 ) bKeep = 1; |
| 151026 | 151295 | } |
| 151027 | | - pKeyMerge = multiSelectByMergeKeyInfo(pParse, p, 1); |
| 151028 | | - }else{ |
| 151029 | | - pKeyMerge = 0; |
| 151296 | + if( bKeep==0 ){ |
| 151297 | + sqlite3DbFreeNN(db, aPermute); |
| 151298 | + aPermute = 0; |
| 151299 | + } |
| 151030 | 151300 | } |
| 151301 | + pKeyMerge = multiSelectByMergeKeyInfo(pParse, p, 1); |
| 151031 | 151302 | |
| 151032 | 151303 | /* Allocate a range of temporary registers and the KeyInfo needed |
| 151033 | 151304 | ** for the logic that removes duplicate result rows when the |
| 151034 | 151305 | ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL). |
| 151035 | 151306 | */ |
| | @@ -151104,11 +151375,11 @@ |
| 151104 | 151375 | /* Generate a coroutine to evaluate the SELECT statement to the |
| 151105 | 151376 | ** left of the compound operator - the "A" select. |
| 151106 | 151377 | */ |
| 151107 | 151378 | addrSelectA = sqlite3VdbeCurrentAddr(v) + 1; |
| 151108 | 151379 | addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA); |
| 151109 | | - VdbeComment((v, "left SELECT")); |
| 151380 | + VdbeComment((v, "SUBR: next-A")); |
| 151110 | 151381 | pPrior->iLimit = regLimitA; |
| 151111 | 151382 | ExplainQueryPlan((pParse, 1, "LEFT")); |
| 151112 | 151383 | sqlite3Select(pParse, pPrior, &destA); |
| 151113 | 151384 | sqlite3VdbeEndCoroutine(v, regAddrA); |
| 151114 | 151385 | sqlite3VdbeJumpHere(v, addr1); |
| | @@ -151116,11 +151387,11 @@ |
| 151116 | 151387 | /* Generate a coroutine to evaluate the SELECT statement on |
| 151117 | 151388 | ** the right - the "B" select |
| 151118 | 151389 | */ |
| 151119 | 151390 | addrSelectB = sqlite3VdbeCurrentAddr(v) + 1; |
| 151120 | 151391 | addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB); |
| 151121 | | - VdbeComment((v, "right SELECT")); |
| 151392 | + VdbeComment((v, "SUBR: next-B")); |
| 151122 | 151393 | savedLimit = p->iLimit; |
| 151123 | 151394 | savedOffset = p->iOffset; |
| 151124 | 151395 | p->iLimit = regLimitB; |
| 151125 | 151396 | p->iOffset = 0; |
| 151126 | 151397 | ExplainQueryPlan((pParse, 1, "RIGHT")); |
| | @@ -151130,20 +151401,20 @@ |
| 151130 | 151401 | sqlite3VdbeEndCoroutine(v, regAddrB); |
| 151131 | 151402 | |
| 151132 | 151403 | /* Generate a subroutine that outputs the current row of the A |
| 151133 | 151404 | ** select as the next output row of the compound select. |
| 151134 | 151405 | */ |
| 151135 | | - VdbeNoopComment((v, "Output routine for A")); |
| 151406 | + VdbeNoopComment((v, "SUBR: out-A")); |
| 151136 | 151407 | addrOutA = generateOutputSubroutine(pParse, |
| 151137 | 151408 | p, &destA, pDest, regOutA, |
| 151138 | 151409 | regPrev, pKeyDup, labelEnd); |
| 151139 | 151410 | |
| 151140 | 151411 | /* Generate a subroutine that outputs the current row of the B |
| 151141 | 151412 | ** select as the next output row of the compound select. |
| 151142 | 151413 | */ |
| 151143 | 151414 | if( op==TK_ALL || op==TK_UNION ){ |
| 151144 | | - VdbeNoopComment((v, "Output routine for B")); |
| 151415 | + VdbeNoopComment((v, "SUBR: out-B")); |
| 151145 | 151416 | addrOutB = generateOutputSubroutine(pParse, |
| 151146 | 151417 | p, &destB, pDest, regOutB, |
| 151147 | 151418 | regPrev, pKeyDup, labelEnd); |
| 151148 | 151419 | } |
| 151149 | 151420 | sqlite3KeyInfoUnref(pKeyDup); |
| | @@ -151152,14 +151423,16 @@ |
| 151152 | 151423 | ** are exhausted and only data in select B remains. |
| 151153 | 151424 | */ |
| 151154 | 151425 | if( op==TK_EXCEPT || op==TK_INTERSECT ){ |
| 151155 | 151426 | addrEofA_noB = addrEofA = labelEnd; |
| 151156 | 151427 | }else{ |
| 151157 | | - VdbeNoopComment((v, "eof-A subroutine")); |
| 151428 | + VdbeNoopComment((v, "SUBR: eof-A")); |
| 151158 | 151429 | addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); |
| 151430 | + VdbeComment((v, "out-B")); |
| 151159 | 151431 | addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd); |
| 151160 | 151432 | VdbeCoverage(v); |
| 151433 | + VdbeComment((v, "next-B")); |
| 151161 | 151434 | sqlite3VdbeGoto(v, addrEofA); |
| 151162 | 151435 | p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
| 151163 | 151436 | } |
| 151164 | 151437 | |
| 151165 | 151438 | /* Generate a subroutine to run when the results from select B |
| | @@ -151167,21 +151440,24 @@ |
| 151167 | 151440 | */ |
| 151168 | 151441 | if( op==TK_INTERSECT ){ |
| 151169 | 151442 | addrEofB = addrEofA; |
| 151170 | 151443 | if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; |
| 151171 | 151444 | }else{ |
| 151172 | | - VdbeNoopComment((v, "eof-B subroutine")); |
| 151445 | + VdbeNoopComment((v, "SUBR: eof-B")); |
| 151173 | 151446 | addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA); |
| 151447 | + VdbeComment((v, "out-A")); |
| 151174 | 151448 | sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v); |
| 151449 | + VdbeComment((v, "next-A")); |
| 151175 | 151450 | sqlite3VdbeGoto(v, addrEofB); |
| 151176 | 151451 | } |
| 151177 | 151452 | |
| 151178 | 151453 | /* Generate code to handle the case of A<B |
| 151179 | 151454 | */ |
| 151180 | | - VdbeNoopComment((v, "A-lt-B subroutine")); |
| 151181 | 151455 | addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA); |
| 151456 | + VdbeComment((v, "out-A")); |
| 151182 | 151457 | sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v); |
| 151458 | + VdbeComment((v, "next-A")); |
| 151183 | 151459 | sqlite3VdbeGoto(v, labelCmpr); |
| 151184 | 151460 | |
| 151185 | 151461 | /* Generate code to handle the case of A==B |
| 151186 | 151462 | */ |
| 151187 | 151463 | if( op==TK_ALL ){ |
| | @@ -151188,40 +151464,52 @@ |
| 151188 | 151464 | addrAeqB = addrAltB; |
| 151189 | 151465 | }else if( op==TK_INTERSECT ){ |
| 151190 | 151466 | addrAeqB = addrAltB; |
| 151191 | 151467 | addrAltB++; |
| 151192 | 151468 | }else{ |
| 151193 | | - VdbeNoopComment((v, "A-eq-B subroutine")); |
| 151194 | | - addrAeqB = |
| 151195 | | - sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v); |
| 151196 | | - sqlite3VdbeGoto(v, labelCmpr); |
| 151469 | + addrAeqB = addrAltB + 1; |
| 151197 | 151470 | } |
| 151198 | 151471 | |
| 151199 | 151472 | /* Generate code to handle the case of A>B |
| 151200 | 151473 | */ |
| 151201 | | - VdbeNoopComment((v, "A-gt-B subroutine")); |
| 151202 | 151474 | addrAgtB = sqlite3VdbeCurrentAddr(v); |
| 151203 | 151475 | if( op==TK_ALL || op==TK_UNION ){ |
| 151204 | 151476 | sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); |
| 151477 | + VdbeComment((v, "out-B")); |
| 151478 | + sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v); |
| 151479 | + VdbeComment((v, "next-B")); |
| 151480 | + sqlite3VdbeGoto(v, labelCmpr); |
| 151481 | + }else{ |
| 151482 | + addrAgtB++; /* Just do next-B. Might as well use the next-B call |
| 151483 | + ** in the next code block */ |
| 151205 | 151484 | } |
| 151206 | | - sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v); |
| 151207 | | - sqlite3VdbeGoto(v, labelCmpr); |
| 151208 | 151485 | |
| 151209 | 151486 | /* This code runs once to initialize everything. |
| 151210 | 151487 | */ |
| 151211 | 151488 | sqlite3VdbeJumpHere(v, addr1); |
| 151212 | 151489 | sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v); |
| 151490 | + VdbeComment((v, "next-A")); |
| 151491 | + /* v--- Also the A>B case for EXCEPT and INTERSECT */ |
| 151213 | 151492 | sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v); |
| 151493 | + VdbeComment((v, "next-B")); |
| 151214 | 151494 | |
| 151215 | 151495 | /* Implement the main merge loop |
| 151216 | 151496 | */ |
| 151497 | + if( aPermute!=0 ){ |
| 151498 | + sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY); |
| 151499 | + } |
| 151217 | 151500 | sqlite3VdbeResolveLabel(v, labelCmpr); |
| 151218 | | - sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY); |
| 151219 | 151501 | sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy, |
| 151220 | 151502 | (char*)pKeyMerge, P4_KEYINFO); |
| 151221 | | - sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE); |
| 151222 | | - sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v); |
| 151503 | + if( aPermute!=0 ){ |
| 151504 | + sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE); |
| 151505 | + } |
| 151506 | + sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); |
| 151507 | + VdbeCoverageIf(v, op==TK_ALL); |
| 151508 | + VdbeCoverageIf(v, op==TK_UNION); |
| 151509 | + VdbeCoverageIf(v, op==TK_EXCEPT); |
| 151510 | + VdbeCoverageIf(v, op==TK_INTERSECT); |
| 151223 | 151511 | |
| 151224 | 151512 | /* Jump to the this point in order to terminate the query. |
| 151225 | 151513 | */ |
| 151226 | 151514 | sqlite3VdbeResolveLabel(v, labelEnd); |
| 151227 | 151515 | |
| | @@ -152774,10 +153062,20 @@ |
| 152774 | 153062 | x.isOuterJoin = 0; |
| 152775 | 153063 | x.nSelDepth = 0; |
| 152776 | 153064 | x.pEList = pSubq->pEList; |
| 152777 | 153065 | x.pCList = findLeftmostExprlist(pSubq); |
| 152778 | 153066 | pNew = substExpr(&x, pNew); |
| 153067 | + assert( pNew!=0 || pParse->nErr!=0 ); |
| 153068 | + if( pParse->nErr==0 && pNew->op==TK_IN && ExprUseXSelect(pNew) ){ |
| 153069 | + assert( pNew->x.pSelect!=0 ); |
| 153070 | + pNew->x.pSelect->selFlags |= SF_ClonedRhsIn; |
| 153071 | + assert( pWhere!=0 ); |
| 153072 | + assert( pWhere->op==TK_IN ); |
| 153073 | + assert( ExprUseXSelect(pWhere) ); |
| 153074 | + assert( pWhere->x.pSelect!=0 ); |
| 153075 | + pWhere->x.pSelect->selFlags |= SF_ClonedRhsIn; |
| 153076 | + } |
| 152779 | 153077 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 152780 | 153078 | if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){ |
| 152781 | 153079 | /* Restriction 6c has prevented push-down in this case */ |
| 152782 | 153080 | sqlite3ExprDelete(pParse->db, pNew); |
| 152783 | 153081 | nChng--; |
| | @@ -154892,10 +155190,11 @@ |
| 154892 | 155190 | */ |
| 154893 | 155191 | typedef struct CheckOnCtx CheckOnCtx; |
| 154894 | 155192 | struct CheckOnCtx { |
| 154895 | 155193 | SrcList *pSrc; /* SrcList for this context */ |
| 154896 | 155194 | int iJoin; /* Cursor numbers must be =< than this */ |
| 155195 | + int bFuncArg; /* True for table-function arg */ |
| 154897 | 155196 | CheckOnCtx *pParent; /* Parent context */ |
| 154898 | 155197 | }; |
| 154899 | 155198 | |
| 154900 | 155199 | /* |
| 154901 | 155200 | ** True if the SrcList passed as the only argument contains at least |
| | @@ -154943,11 +155242,13 @@ |
| 154943 | 155242 | SrcList *pSrc = pCtx->pSrc; |
| 154944 | 155243 | int iTab = pExpr->iTable; |
| 154945 | 155244 | if( iTab>=pSrc->a[0].iCursor && iTab<=pSrc->a[pSrc->nSrc-1].iCursor ){ |
| 154946 | 155245 | if( pCtx->iJoin && iTab>pCtx->iJoin ){ |
| 154947 | 155246 | sqlite3ErrorMsg(pWalker->pParse, |
| 154948 | | - "ON clause references tables to its right"); |
| 155247 | + "%s references tables to its right", |
| 155248 | + (pCtx->bFuncArg ? "table-function argument" : "ON clause") |
| 155249 | + ); |
| 154949 | 155250 | return WRC_Abort; |
| 154950 | 155251 | } |
| 154951 | 155252 | break; |
| 154952 | 155253 | } |
| 154953 | 155254 | pCtx = pCtx->pParent; |
| | @@ -154978,24 +155279,39 @@ |
| 154978 | 155279 | |
| 154979 | 155280 | /* |
| 154980 | 155281 | ** Check all ON clauses in pSelect to verify that they do not reference |
| 154981 | 155282 | ** columns to the right. |
| 154982 | 155283 | */ |
| 154983 | | -static void selectCheckOnClauses(Parse *pParse, Select *pSelect){ |
| 155284 | +SQLITE_PRIVATE void sqlite3SelectCheckOnClauses(Parse *pParse, Select *pSelect){ |
| 154984 | 155285 | Walker w; |
| 154985 | 155286 | CheckOnCtx sCtx; |
| 155287 | + int ii; |
| 154986 | 155288 | assert( pSelect->selFlags & SF_OnToWhere ); |
| 154987 | 155289 | assert( pSelect->pSrc!=0 && pSelect->pSrc->nSrc>=2 ); |
| 154988 | 155290 | memset(&w, 0, sizeof(w)); |
| 154989 | 155291 | w.pParse = pParse; |
| 154990 | 155292 | w.xExprCallback = selectCheckOnClausesExpr; |
| 154991 | 155293 | w.xSelectCallback = selectCheckOnClausesSelect; |
| 154992 | 155294 | w.u.pCheckOnCtx = &sCtx; |
| 154993 | 155295 | memset(&sCtx, 0, sizeof(sCtx)); |
| 154994 | 155296 | sCtx.pSrc = pSelect->pSrc; |
| 154995 | | - sqlite3WalkExprNN(&w, pSelect->pWhere); |
| 155297 | + sqlite3WalkExpr(&w, pSelect->pWhere); |
| 154996 | 155298 | pSelect->selFlags &= ~SF_OnToWhere; |
| 155299 | + |
| 155300 | + /* Check for any table-function args that are attached to virtual tables |
| 155301 | + ** on the RHS of an outer join. They are subject to the same constraints |
| 155302 | + ** as ON clauses. */ |
| 155303 | + sCtx.bFuncArg = 1; |
| 155304 | + for(ii=0; ii<pSelect->pSrc->nSrc; ii++){ |
| 155305 | + SrcItem *pItem = &pSelect->pSrc->a[ii]; |
| 155306 | + if( pItem->fg.isTabFunc |
| 155307 | + && (pItem->fg.jointype & JT_OUTER) |
| 155308 | + ){ |
| 155309 | + sCtx.iJoin = pItem->iCursor; |
| 155310 | + sqlite3WalkExprList(&w, pItem->u1.pFuncArg); |
| 155311 | + } |
| 155312 | + } |
| 154997 | 155313 | } |
| 154998 | 155314 | |
| 154999 | 155315 | /* |
| 155000 | 155316 | ** If p2 exists and p1 and p2 have the same number of terms, then change |
| 155001 | 155317 | ** every term of p1 to have the same sort order as p2 and return true. |
| | @@ -155143,22 +155459,10 @@ |
| 155143 | 155459 | TREETRACE(0x10,pParse,p, ("after name resolution:\n")); |
| 155144 | 155460 | sqlite3TreeViewSelect(0, p, 0); |
| 155145 | 155461 | } |
| 155146 | 155462 | #endif |
| 155147 | 155463 | |
| 155148 | | - /* If the SELECT statement contains ON clauses that were moved into |
| 155149 | | - ** the WHERE clause, go through and verify that none of the terms |
| 155150 | | - ** in the ON clauses reference tables to the right of the ON clause. |
| 155151 | | - ** Do this now, after name resolution, but before query flattening |
| 155152 | | - */ |
| 155153 | | - if( p->selFlags & SF_OnToWhere ){ |
| 155154 | | - selectCheckOnClauses(pParse, p); |
| 155155 | | - if( pParse->nErr ){ |
| 155156 | | - goto select_end; |
| 155157 | | - } |
| 155158 | | - } |
| 155159 | | - |
| 155160 | 155464 | /* If the SF_UFSrcCheck flag is set, then this function is being called |
| 155161 | 155465 | ** as part of populating the temp table for an UPDATE...FROM statement. |
| 155162 | 155466 | ** In this case, it is an error if the target object (pSrc->a[0]) name |
| 155163 | 155467 | ** or alias is duplicated within FROM clause (pSrc->a[1..n]). |
| 155164 | 155468 | ** |
| | @@ -157006,10 +157310,11 @@ |
| 157006 | 157310 | |
| 157007 | 157311 | pParse->pNewTrigger = 0; |
| 157008 | 157312 | if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup; |
| 157009 | 157313 | zName = pTrig->zName; |
| 157010 | 157314 | iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); |
| 157315 | + assert( iDb>=00 && iDb<db->nDb ); |
| 157011 | 157316 | pTrig->step_list = pStepList; |
| 157012 | 157317 | while( pStepList ){ |
| 157013 | 157318 | pStepList->pTrig = pTrig; |
| 157014 | 157319 | pStepList = pStepList->pNext; |
| 157015 | 157320 | } |
| | @@ -163989,11 +164294,11 @@ |
| 163989 | 164294 | if( NEVER(pTerm==0) ) continue; |
| 163990 | 164295 | if( pTerm->eOperator & WO_IN ){ |
| 163991 | 164296 | if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){ |
| 163992 | 164297 | int iTab = pParse->nTab++; |
| 163993 | 164298 | int iCache = ++pParse->nMem; |
| 163994 | | - sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab); |
| 164299 | + sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab, 0); |
| 163995 | 164300 | sqlite3VdbeAddOp3(v, OP_VInitIn, iTab, iTarget, iCache); |
| 163996 | 164301 | }else{ |
| 163997 | 164302 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 163998 | 164303 | addrNotFound = pLevel->addrNxt; |
| 163999 | 164304 | } |
| | @@ -165648,17 +165953,18 @@ |
| 165648 | 165953 | && ALWAYS(pLeft->y.pTab) |
| 165649 | 165954 | && IsVirtual(pLeft->y.pTab)) /* Might be numeric */ |
| 165650 | 165955 | ){ |
| 165651 | 165956 | int isNum; |
| 165652 | 165957 | double rDummy; |
| 165653 | | - isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8); |
| 165958 | + assert( zNew[iTo]==0 ); |
| 165959 | + isNum = sqlite3AtoF(zNew, &rDummy); |
| 165654 | 165960 | if( isNum<=0 ){ |
| 165655 | 165961 | if( iTo==1 && zNew[0]=='-' ){ |
| 165656 | 165962 | isNum = +1; |
| 165657 | 165963 | }else{ |
| 165658 | 165964 | zNew[iTo-1]++; |
| 165659 | | - isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8); |
| 165965 | + isNum = sqlite3AtoF(zNew, &rDummy); |
| 165660 | 165966 | zNew[iTo-1]--; |
| 165661 | 165967 | } |
| 165662 | 165968 | } |
| 165663 | 165969 | if( isNum>0 ){ |
| 165664 | 165970 | sqlite3ExprDelete(db, pPrefix); |
| | @@ -165697,10 +166003,38 @@ |
| 165697 | 166003 | sqlite3ValueFree(pVal); |
| 165698 | 166004 | return rc; |
| 165699 | 166005 | } |
| 165700 | 166006 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 165701 | 166007 | |
| 166008 | +/* |
| 166009 | +** If pExpr is one of "like", "glob", "match", or "regexp", then |
| 166010 | +** return the corresponding SQLITE_INDEX_CONSTRAINT_xxxx value. |
| 166011 | +** If not, return 0. |
| 166012 | +** |
| 166013 | +** pExpr is guaranteed to be a TK_FUNCTION. |
| 166014 | +*/ |
| 166015 | +SQLITE_PRIVATE int sqlite3ExprIsLikeOperator(const Expr *pExpr){ |
| 166016 | + static const struct { |
| 166017 | + const char *zOp; |
| 166018 | + unsigned char eOp; |
| 166019 | + } aOp[] = { |
| 166020 | + { "match", SQLITE_INDEX_CONSTRAINT_MATCH }, |
| 166021 | + { "glob", SQLITE_INDEX_CONSTRAINT_GLOB }, |
| 166022 | + { "like", SQLITE_INDEX_CONSTRAINT_LIKE }, |
| 166023 | + { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP } |
| 166024 | + }; |
| 166025 | + int i; |
| 166026 | + assert( pExpr->op==TK_FUNCTION ); |
| 166027 | + assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 166028 | + for(i=0; i<ArraySize(aOp); i++){ |
| 166029 | + if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){ |
| 166030 | + return aOp[i].eOp; |
| 166031 | + } |
| 166032 | + } |
| 166033 | + return 0; |
| 166034 | +} |
| 166035 | + |
| 165702 | 166036 | |
| 165703 | 166037 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 165704 | 166038 | /* |
| 165705 | 166039 | ** Check to see if the pExpr expression is a form that needs to be passed |
| 165706 | 166040 | ** to the xBestIndex method of virtual tables. Forms of interest include: |
| | @@ -165733,19 +166067,10 @@ |
| 165733 | 166067 | unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */ |
| 165734 | 166068 | Expr **ppLeft, /* Column expression to left of MATCH/op2 */ |
| 165735 | 166069 | Expr **ppRight /* Expression to left of MATCH/op2 */ |
| 165736 | 166070 | ){ |
| 165737 | 166071 | if( pExpr->op==TK_FUNCTION ){ |
| 165738 | | - static const struct Op2 { |
| 165739 | | - const char *zOp; |
| 165740 | | - unsigned char eOp2; |
| 165741 | | - } aOp[] = { |
| 165742 | | - { "match", SQLITE_INDEX_CONSTRAINT_MATCH }, |
| 165743 | | - { "glob", SQLITE_INDEX_CONSTRAINT_GLOB }, |
| 165744 | | - { "like", SQLITE_INDEX_CONSTRAINT_LIKE }, |
| 165745 | | - { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP } |
| 165746 | | - }; |
| 165747 | 166072 | ExprList *pList; |
| 165748 | 166073 | Expr *pCol; /* Column reference */ |
| 165749 | 166074 | int i; |
| 165750 | 166075 | |
| 165751 | 166076 | assert( ExprUseXList(pExpr) ); |
| | @@ -165761,20 +166086,15 @@ |
| 165761 | 166086 | ** vtab_column MATCH expression |
| 165762 | 166087 | ** MATCH(expression,vtab_column) |
| 165763 | 166088 | */ |
| 165764 | 166089 | pCol = pList->a[1].pExpr; |
| 165765 | 166090 | assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) ); |
| 165766 | | - if( ExprIsVtab(pCol) ){ |
| 165767 | | - for(i=0; i<ArraySize(aOp); i++){ |
| 165768 | | - assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 165769 | | - if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){ |
| 165770 | | - *peOp2 = aOp[i].eOp2; |
| 165771 | | - *ppRight = pList->a[0].pExpr; |
| 165772 | | - *ppLeft = pCol; |
| 165773 | | - return 1; |
| 165774 | | - } |
| 165775 | | - } |
| 166091 | + if( ExprIsVtab(pCol) && (i = sqlite3ExprIsLikeOperator(pExpr))!=0 ){ |
| 166092 | + *peOp2 = i; |
| 166093 | + *ppRight = pList->a[0].pExpr; |
| 166094 | + *ppLeft = pCol; |
| 166095 | + return 1; |
| 165776 | 166096 | } |
| 165777 | 166097 | |
| 165778 | 166098 | /* We can also match against the first column of overloaded |
| 165779 | 166099 | ** functions where xFindFunction returns a value of at least |
| 165780 | 166100 | ** SQLITE_INDEX_CONSTRAINT_FUNCTION. |
| | @@ -170218,10 +170538,71 @@ |
| 170218 | 170538 | p->u.btree.pIndex = 0; |
| 170219 | 170539 | } |
| 170220 | 170540 | } |
| 170221 | 170541 | return rc; |
| 170222 | 170542 | } |
| 170543 | + |
| 170544 | +/* |
| 170545 | +** Callback for estLikePatternLength(). |
| 170546 | +** |
| 170547 | +** If this node is a string literal that is longer pWalker->sz, then set |
| 170548 | +** pWalker->sz to the byte length of that string literal. |
| 170549 | +** |
| 170550 | +** pWalker->eCode indicates how to count characters: |
| 170551 | +** |
| 170552 | +** eCode==0 Count as a GLOB pattern |
| 170553 | +** eCode==1 Count as a LIKE pattern |
| 170554 | +*/ |
| 170555 | +static int exprNodePatternLengthEst(Walker *pWalker, Expr *pExpr){ |
| 170556 | + if( pExpr->op==TK_STRING ){ |
| 170557 | + int sz = 0; /* Pattern size in bytes */ |
| 170558 | + u8 *z = (u8*)pExpr->u.zToken; /* The pattern */ |
| 170559 | + u8 c; /* Next character of the pattern */ |
| 170560 | + u8 c1, c2, c3; /* Wildcards */ |
| 170561 | + if( pWalker->eCode ){ |
| 170562 | + c1 = '%'; |
| 170563 | + c2 = '_'; |
| 170564 | + c3 = 0; |
| 170565 | + }else{ |
| 170566 | + c1 = '*'; |
| 170567 | + c2 = '?'; |
| 170568 | + c3 = '['; |
| 170569 | + } |
| 170570 | + while( (c = *(z++))!=0 ){ |
| 170571 | + if( c==c3 ){ |
| 170572 | + if( *z ) z++; |
| 170573 | + while( *z && *z!=']' ) z++; |
| 170574 | + }else if( c!=c1 && c!=c2 ){ |
| 170575 | + sz++; |
| 170576 | + } |
| 170577 | + } |
| 170578 | + if( sz>pWalker->u.sz ) pWalker->u.sz = sz; |
| 170579 | + } |
| 170580 | + return WRC_Continue; |
| 170581 | +} |
| 170582 | + |
| 170583 | +/* |
| 170584 | +** Return the length of the longest string literal in the given |
| 170585 | +** expression. |
| 170586 | +** |
| 170587 | +** eCode indicates how to count characters: |
| 170588 | +** |
| 170589 | +** eCode==0 Count as a GLOB pattern |
| 170590 | +** eCode==1 Count as a LIKE pattern |
| 170591 | +*/ |
| 170592 | +static int estLikePatternLength(Expr *p, u16 eCode){ |
| 170593 | + Walker w; |
| 170594 | + w.u.sz = 0; |
| 170595 | + w.eCode = eCode; |
| 170596 | + w.xExprCallback = exprNodePatternLengthEst; |
| 170597 | + w.xSelectCallback = sqlite3SelectWalkFail; |
| 170598 | +#ifdef SQLITE_DEBUG |
| 170599 | + w.xSelectCallback2 = sqlite3SelectWalkAssert2; |
| 170600 | +#endif |
| 170601 | + sqlite3WalkExpr(&w, p); |
| 170602 | + return w.u.sz; |
| 170603 | +} |
| 170223 | 170604 | |
| 170224 | 170605 | /* |
| 170225 | 170606 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 170226 | 170607 | ** WHERE clause that reference the loop but which are not used by an |
| 170227 | 170608 | ** index. |
| | @@ -170247,10 +170628,17 @@ |
| 170247 | 170628 | ** of rows in the table. In other words, assume that x==EXPR will filter |
| 170248 | 170629 | ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the |
| 170249 | 170630 | ** "x" column is boolean or else -1 or 0 or 1 is a common default value |
| 170250 | 170631 | ** on the "x" column and so in that case only cap the output row estimate |
| 170251 | 170632 | ** at 1/2 instead of 1/4. |
| 170633 | +** |
| 170634 | +** Heuristic 3: If there is a LIKE or GLOB (or REGEXP or MATCH) operator |
| 170635 | +** with a large constant pattern, then reduce the size of the search |
| 170636 | +** space according to the length of the pattern, under the theory that |
| 170637 | +** longer patterns are less likely to match. This heuristic was added |
| 170638 | +** to give better output-row count estimates when preparing queries for |
| 170639 | +** the Join-Order Benchmarks. See forum thread 2026-01-30T09:57:54z |
| 170252 | 170640 | */ |
| 170253 | 170641 | static void whereLoopOutputAdjust( |
| 170254 | 170642 | WhereClause *pWC, /* The WHERE clause */ |
| 170255 | 170643 | WhereLoop *pLoop, /* The loop to adjust downward */ |
| 170256 | 170644 | LogEst nRow /* Number of rows in the entire table */ |
| | @@ -170296,25 +170684,43 @@ |
| 170296 | 170684 | ** then use the probability provided by the application. */ |
| 170297 | 170685 | pLoop->nOut += pTerm->truthProb; |
| 170298 | 170686 | }else{ |
| 170299 | 170687 | /* In the absence of explicit truth probabilities, use heuristics to |
| 170300 | 170688 | ** guess a reasonable truth probability. */ |
| 170689 | + Expr *pOpExpr = pTerm->pExpr; |
| 170301 | 170690 | pLoop->nOut--; |
| 170302 | 170691 | if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 |
| 170303 | 170692 | && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */ |
| 170304 | 170693 | ){ |
| 170305 | | - Expr *pRight = pTerm->pExpr->pRight; |
| 170694 | + Expr *pRight = pOpExpr->pRight; |
| 170306 | 170695 | int k = 0; |
| 170307 | | - testcase( pTerm->pExpr->op==TK_IS ); |
| 170696 | + testcase( pOpExpr->op==TK_IS ); |
| 170308 | 170697 | if( sqlite3ExprIsInteger(pRight, &k, 0) && k>=(-1) && k<=1 ){ |
| 170309 | 170698 | k = 10; |
| 170310 | 170699 | }else{ |
| 170311 | 170700 | k = 20; |
| 170312 | 170701 | } |
| 170313 | 170702 | if( iReduce<k ){ |
| 170314 | 170703 | pTerm->wtFlags |= TERM_HEURTRUTH; |
| 170315 | 170704 | iReduce = k; |
| 170705 | + } |
| 170706 | + }else |
| 170707 | + if( ExprHasProperty(pOpExpr, EP_InfixFunc) |
| 170708 | + && pOpExpr->op==TK_FUNCTION |
| 170709 | + ){ |
| 170710 | + int eOp; |
| 170711 | + assert( ExprUseXList(pOpExpr) ); |
| 170712 | + assert( pOpExpr->x.pList->nExpr>=2 ); |
| 170713 | + eOp = sqlite3ExprIsLikeOperator(pOpExpr); |
| 170714 | + if( ALWAYS(eOp>0) ){ |
| 170715 | + int szPattern; |
| 170716 | + Expr *pRHS = pOpExpr->x.pList->a[0].pExpr; |
| 170717 | + eOp = eOp==SQLITE_INDEX_CONSTRAINT_LIKE; |
| 170718 | + szPattern = estLikePatternLength(pRHS, eOp); |
| 170719 | + if( szPattern>0 ){ |
| 170720 | + pLoop->nOut -= szPattern*2; |
| 170721 | + } |
| 170316 | 170722 | } |
| 170317 | 170723 | } |
| 170318 | 170724 | } |
| 170319 | 170725 | } |
| 170320 | 170726 | } |
| | @@ -172136,11 +172542,11 @@ |
| 172136 | 172542 | SrcItem *pItem; |
| 172137 | 172543 | SrcItem *pEnd = &pTabList->a[pWInfo->nLevel]; |
| 172138 | 172544 | sqlite3 *db = pWInfo->pParse->db; |
| 172139 | 172545 | int rc = SQLITE_OK; |
| 172140 | 172546 | int bFirstPastRJ = 0; |
| 172141 | | - int hasRightJoin = 0; |
| 172547 | + int hasRightCrossJoin = 0; |
| 172142 | 172548 | WhereLoop *pNew; |
| 172143 | 172549 | |
| 172144 | 172550 | |
| 172145 | 172551 | /* Loop over the tables in the join, from left to right */ |
| 172146 | 172552 | pNew = pBuilder->pNew; |
| | @@ -172163,16 +172569,24 @@ |
| 172163 | 172569 | /* Add prerequisites to prevent reordering of FROM clause terms |
| 172164 | 172570 | ** across CROSS joins and outer joins. The bFirstPastRJ boolean |
| 172165 | 172571 | ** prevents the right operand of a RIGHT JOIN from being swapped with |
| 172166 | 172572 | ** other elements even further to the right. |
| 172167 | 172573 | ** |
| 172168 | | - ** The JT_LTORJ case and the hasRightJoin flag work together to |
| 172169 | | - ** prevent FROM-clause terms from moving from the right side of |
| 172170 | | - ** a LEFT JOIN over to the left side of that join if the LEFT JOIN |
| 172171 | | - ** is itself on the left side of a RIGHT JOIN. |
| 172574 | + ** The hasRightCrossJoin flag prevent FROM-clause terms from moving |
| 172575 | + ** from the right side of a LEFT JOIN or CROSS JOIN over to the |
| 172576 | + ** left side of that same join. This is a required restriction in |
| 172577 | + ** the case of LEFT JOIN - an incorrect answer may results if it is |
| 172578 | + ** not enforced. This restriction is not required for CROSS JOIN. |
| 172579 | + ** It is provided merely as a means of controlling join order, under |
| 172580 | + ** the theory that no real-world queries that care about performance |
| 172581 | + ** actually use the CROSS JOIN syntax. |
| 172172 | 172582 | */ |
| 172173 | | - if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1; |
| 172583 | + if( pItem->fg.jointype & (JT_LTORJ|JT_CROSS) ){ |
| 172584 | + testcase( pItem->fg.jointype & JT_LTORJ ); |
| 172585 | + testcase( pItem->fg.jointype & JT_CROSS ); |
| 172586 | + hasRightCrossJoin = 1; |
| 172587 | + } |
| 172174 | 172588 | mPrereq |= mPrior; |
| 172175 | 172589 | bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0; |
| 172176 | 172590 | }else if( pItem->fg.fromExists ){ |
| 172177 | 172591 | /* joins that result from the EXISTS-to-JOIN optimization should not |
| 172178 | 172592 | ** be moved to the left of any of their dependencies */ |
| | @@ -172182,11 +172596,11 @@ |
| 172182 | 172596 | for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){ |
| 172183 | 172597 | if( (pNew->maskSelf & pTerm->prereqAll)!=0 ){ |
| 172184 | 172598 | mPrereq |= (pTerm->prereqAll & (pNew->maskSelf-1)); |
| 172185 | 172599 | } |
| 172186 | 172600 | } |
| 172187 | | - }else if( !hasRightJoin ){ |
| 172601 | + }else if( !hasRightCrossJoin ){ |
| 172188 | 172602 | mPrereq = 0; |
| 172189 | 172603 | } |
| 172190 | 172604 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 172191 | 172605 | if( IsVirtual(pItem->pSTab) ){ |
| 172192 | 172606 | SrcItem *p; |
| | @@ -172781,16 +173195,25 @@ |
| 172781 | 173195 | ** |
| 172782 | 173196 | ** 18 for star queries |
| 172783 | 173197 | ** 12 otherwise |
| 172784 | 173198 | ** |
| 172785 | 173199 | ** For the purposes of this heuristic, a star-query is defined as a query |
| 172786 | | -** with a large central table that is joined using an INNER JOIN, |
| 172787 | | -** not CROSS or OUTER JOINs, against four or more smaller tables. |
| 172788 | | -** The central table is called the "fact" table. The smaller tables |
| 172789 | | -** that get joined are "dimension tables". Also, any table that is |
| 172790 | | -** self-joined cannot be a dimension table; we assume that dimension |
| 172791 | | -** tables may only be joined against fact tables. |
| 173200 | +** with a central "fact" table that is joined against multiple |
| 173201 | +** "dimension" tables, subject to the following constraints: |
| 173202 | +** |
| 173203 | +** (aa) Only a five-way or larger join is considered for this |
| 173204 | +** optimization. If there are fewer than four terms in the FROM |
| 173205 | +** clause, this heuristic does not apply. |
| 173206 | +** |
| 173207 | +** (bb) The join between the fact table and the dimension tables must |
| 173208 | +** be an INNER join. CROSS and OUTER JOINs do not qualify. |
| 173209 | +** |
| 173210 | +** (cc) A table must have 3 or more dimension tables in order to be |
| 173211 | +** considered a fact table. (Was 4 prior to 2026-02-10.) |
| 173212 | +** |
| 173213 | +** (dd) A table that is a self-join cannot be a dimension table. |
| 173214 | +** Dimension tables are joined against fact tables. |
| 172792 | 173215 | ** |
| 172793 | 173216 | ** SIDE EFFECT: (and really the whole point of this subroutine) |
| 172794 | 173217 | ** |
| 172795 | 173218 | ** If pWInfo describes a star-query, then the cost for SCANs of dimension |
| 172796 | 173219 | ** WhereLoops is increased to be slightly larger than the cost of a SCAN |
| | @@ -172839,11 +173262,11 @@ |
| 172839 | 173262 | assert( pWLoop->maskSelf==MASKBIT(pWLoop->iTab) ); |
| 172840 | 173263 | assert( pWLoop->pNextLoop==0 || pWLoop->iTab<=pWLoop->pNextLoop->iTab ); |
| 172841 | 173264 | } |
| 172842 | 173265 | #endif /* SQLITE_DEBUG */ |
| 172843 | 173266 | |
| 172844 | | - if( nLoop>=5 |
| 173267 | + if( nLoop>=4 /* Constraint (aa) */ |
| 172845 | 173268 | && !pWInfo->bStarDone |
| 172846 | 173269 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_StarQuery) |
| 172847 | 173270 | ){ |
| 172848 | 173271 | SrcItem *aFromTabs; /* All terms of the FROM clause */ |
| 172849 | 173272 | int iFromIdx; /* Term of FROM clause is the candidate fact-table */ |
| | @@ -172851,11 +173274,11 @@ |
| 172851 | 173274 | Bitmask mSelfJoin = 0; /* Tables that cannot be dimension tables */ |
| 172852 | 173275 | WhereLoop *pStart; /* Where to start searching for dimension-tables */ |
| 172853 | 173276 | |
| 172854 | 173277 | pWInfo->bStarDone = 1; /* Only do this computation once */ |
| 172855 | 173278 | |
| 172856 | | - /* Look for fact tables with four or more dimensions where the |
| 173279 | + /* Look for fact tables with three or more dimensions where the |
| 172857 | 173280 | ** dimension tables are not separately from the fact tables by an outer |
| 172858 | 173281 | ** or cross join. Adjust cost weights if found. |
| 172859 | 173282 | */ |
| 172860 | 173283 | assert( !pWInfo->bStarUsed ); |
| 172861 | 173284 | aFromTabs = pWInfo->pTabList->a; |
| | @@ -172868,22 +173291,21 @@ |
| 172868 | 173291 | |
| 172869 | 173292 | pFactTab = aFromTabs + iFromIdx; |
| 172870 | 173293 | if( (pFactTab->fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){ |
| 172871 | 173294 | /* If the candidate fact-table is the right table of an outer join |
| 172872 | 173295 | ** restrict the search for dimension-tables to be tables to the right |
| 172873 | | - ** of the fact-table. */ |
| 172874 | | - if( iFromIdx+4 > nLoop ) break; /* Impossible to reach nDep>=4 */ |
| 173296 | + ** of the fact-table. Constraint (bb) */ |
| 173297 | + if( iFromIdx+3 > nLoop ){ |
| 173298 | + break; /* ^-- Impossible to reach nDep>=2 - Constraint (cc) */ |
| 173299 | + } |
| 172875 | 173300 | while( pStart && pStart->iTab<=iFromIdx ){ |
| 172876 | 173301 | pStart = pStart->pNextLoop; |
| 172877 | 173302 | } |
| 172878 | 173303 | } |
| 172879 | 173304 | for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 172880 | 173305 | if( (aFromTabs[pWLoop->iTab].fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){ |
| 172881 | | - /* Fact-tables and dimension-tables cannot be separated by an |
| 172882 | | - ** outer join (at least for the definition of fact- and dimension- |
| 172883 | | - ** used by this heuristic). */ |
| 172884 | | - break; |
| 173306 | + break; /* Constraint (bb) */ |
| 172885 | 173307 | } |
| 172886 | 173308 | if( (pWLoop->prereq & m)!=0 /* pWInfo depends on iFromIdx */ |
| 172887 | 173309 | && (pWLoop->maskSelf & mSeen)==0 /* pWInfo not already a dependency */ |
| 172888 | 173310 | && (pWLoop->maskSelf & mSelfJoin)==0 /* Not a self-join */ |
| 172889 | 173311 | ){ |
| | @@ -172893,11 +173315,13 @@ |
| 172893 | 173315 | nDep++; |
| 172894 | 173316 | mSeen |= pWLoop->maskSelf; |
| 172895 | 173317 | } |
| 172896 | 173318 | } |
| 172897 | 173319 | } |
| 172898 | | - if( nDep<=3 ) continue; |
| 173320 | + if( nDep<=2 ){ |
| 173321 | + continue; /* Constraint (cc) */ |
| 173322 | + } |
| 172899 | 173323 | |
| 172900 | 173324 | /* If we reach this point, it means that pFactTab is a fact table |
| 172901 | 173325 | ** with four or more dimensions connected by inner joins. Proceed |
| 172902 | 173326 | ** to make cost adjustments. */ |
| 172903 | 173327 | |
| | @@ -172906,10 +173330,27 @@ |
| 172906 | 173330 | if( !pWInfo->bStarUsed ){ |
| 172907 | 173331 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 172908 | 173332 | pWLoop->rStarDelta = 0; |
| 172909 | 173333 | } |
| 172910 | 173334 | } |
| 173335 | +#endif |
| 173336 | +#ifdef WHERETRACE_ENABLED /* 0x80000 */ |
| 173337 | + if( sqlite3WhereTrace & 0x80000 ){ |
| 173338 | + Bitmask mShow = mSeen; |
| 173339 | + sqlite3DebugPrintf("Fact table %s(%d), dimensions:", |
| 173340 | + pFactTab->zAlias ? pFactTab->zAlias : pFactTab->pSTab->zName, |
| 173341 | + iFromIdx); |
| 173342 | + for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 173343 | + if( mShow & pWLoop->maskSelf ){ |
| 173344 | + SrcItem *pDim = aFromTabs + pWLoop->iTab; |
| 173345 | + mShow &= ~pWLoop->maskSelf; |
| 173346 | + sqlite3DebugPrintf(" %s(%d)", |
| 173347 | + pDim->zAlias ? pDim->zAlias: pDim->pSTab->zName, pWLoop->iTab); |
| 173348 | + } |
| 173349 | + } |
| 173350 | + sqlite3DebugPrintf("\n"); |
| 173351 | + } |
| 172911 | 173352 | #endif |
| 172912 | 173353 | pWInfo->bStarUsed = 1; |
| 172913 | 173354 | |
| 172914 | 173355 | /* Compute the maximum cost of any WhereLoop for the |
| 172915 | 173356 | ** fact table plus one epsilon */ |
| | @@ -172929,14 +173370,12 @@ |
| 172929 | 173370 | if( pWLoop->rRun<mxRun ){ |
| 172930 | 173371 | #ifdef WHERETRACE_ENABLED /* 0x80000 */ |
| 172931 | 173372 | if( sqlite3WhereTrace & 0x80000 ){ |
| 172932 | 173373 | SrcItem *pDim = aFromTabs + pWLoop->iTab; |
| 172933 | 173374 | sqlite3DebugPrintf( |
| 172934 | | - "Increase SCAN cost of dimension %s(%d) of fact %s(%d) to %d\n", |
| 172935 | | - pDim->zAlias ? pDim->zAlias: pDim->pSTab->zName, pWLoop->iTab, |
| 172936 | | - pFactTab->zAlias ? pFactTab->zAlias : pFactTab->pSTab->zName, |
| 172937 | | - iFromIdx, mxRun |
| 173375 | + "Increase SCAN cost of %s to %d\n", |
| 173376 | + pDim->zAlias ? pDim->zAlias: pDim->pSTab->zName, mxRun |
| 172938 | 173377 | ); |
| 172939 | 173378 | } |
| 172940 | 173379 | pWLoop->rStarDelta = mxRun - pWLoop->rRun; |
| 172941 | 173380 | #endif /* WHERETRACE_ENABLED */ |
| 172942 | 173381 | pWLoop->rRun = mxRun; |
| | @@ -214034,32 +214473,36 @@ |
| 214034 | 214473 | jsonParseReset(&v); |
| 214035 | 214474 | jsonParseReset(&ix); |
| 214036 | 214475 | return rc; |
| 214037 | 214476 | } |
| 214038 | 214477 | }else if( zPath[0]=='[' ){ |
| 214478 | + u64 kk = 0; |
| 214039 | 214479 | x = pParse->aBlob[iRoot] & 0x0f; |
| 214040 | 214480 | if( x!=JSONB_ARRAY ) return JSON_LOOKUP_NOTFOUND; |
| 214041 | 214481 | n = jsonbPayloadSize(pParse, iRoot, &sz); |
| 214042 | | - k = 0; |
| 214043 | 214482 | i = 1; |
| 214044 | 214483 | while( sqlite3Isdigit(zPath[i]) ){ |
| 214045 | | - k = k*10 + zPath[i] - '0'; |
| 214484 | + if( kk<0xffffffff ) kk = kk*10 + zPath[i] - '0'; |
| 214485 | + /* ^^^^^^^^^^--- Allow kk to be bigger than any JSON array so that |
| 214486 | + ** we get NOTFOUND instead of PATHERROR, without overflowing kk. */ |
| 214046 | 214487 | i++; |
| 214047 | 214488 | } |
| 214048 | 214489 | if( i<2 || zPath[i]!=']' ){ |
| 214049 | 214490 | if( zPath[1]=='#' ){ |
| 214050 | | - k = jsonbArrayCount(pParse, iRoot); |
| 214491 | + kk = jsonbArrayCount(pParse, iRoot); |
| 214051 | 214492 | i = 2; |
| 214052 | 214493 | if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){ |
| 214053 | | - unsigned int nn = 0; |
| 214494 | + u64 nn = 0; |
| 214054 | 214495 | i = 3; |
| 214055 | 214496 | do{ |
| 214056 | | - nn = nn*10 + zPath[i] - '0'; |
| 214497 | + if( nn<0xffffffff ) nn = nn*10 + zPath[i] - '0'; |
| 214498 | + /* ^^^^^^^^^^--- Allow nn to be bigger than any JSON array to |
| 214499 | + ** get NOTFOUND instead of PATHERROR, without overflowing nn. */ |
| 214057 | 214500 | i++; |
| 214058 | 214501 | }while( sqlite3Isdigit(zPath[i]) ); |
| 214059 | | - if( nn>k ) return JSON_LOOKUP_NOTFOUND; |
| 214060 | | - k -= nn; |
| 214502 | + if( nn>kk ) return JSON_LOOKUP_NOTFOUND; |
| 214503 | + kk -= nn; |
| 214061 | 214504 | } |
| 214062 | 214505 | if( zPath[i]!=']' ){ |
| 214063 | 214506 | return JSON_LOOKUP_PATHERROR; |
| 214064 | 214507 | } |
| 214065 | 214508 | }else{ |
| | @@ -214067,22 +214510,22 @@ |
| 214067 | 214510 | } |
| 214068 | 214511 | } |
| 214069 | 214512 | j = iRoot+n; |
| 214070 | 214513 | iEnd = j+sz; |
| 214071 | 214514 | while( j<iEnd ){ |
| 214072 | | - if( k==0 ){ |
| 214515 | + if( kk==0 ){ |
| 214073 | 214516 | rc = jsonLookupStep(pParse, j, &zPath[i+1], 0); |
| 214074 | 214517 | if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot); |
| 214075 | 214518 | return rc; |
| 214076 | 214519 | } |
| 214077 | | - k--; |
| 214520 | + kk--; |
| 214078 | 214521 | n = jsonbPayloadSize(pParse, j, &sz); |
| 214079 | 214522 | if( n==0 ) return JSON_LOOKUP_ERROR; |
| 214080 | 214523 | j += n+sz; |
| 214081 | 214524 | } |
| 214082 | 214525 | if( j>iEnd ) return JSON_LOOKUP_ERROR; |
| 214083 | | - if( k>0 ) return JSON_LOOKUP_NOTFOUND; |
| 214526 | + if( kk>0 ) return JSON_LOOKUP_NOTFOUND; |
| 214084 | 214527 | if( pParse->eEdit>=JEDIT_INS ){ |
| 214085 | 214528 | JsonParse v; |
| 214086 | 214529 | testcase( pParse->eEdit==JEDIT_INS ); |
| 214087 | 214530 | testcase( pParse->eEdit==JEDIT_AINS ); |
| 214088 | 214531 | testcase( pParse->eEdit==JEDIT_SET ); |
| | @@ -214220,11 +214663,11 @@ |
| 214220 | 214663 | char *z; |
| 214221 | 214664 | if( sz==0 ) goto returnfromblob_malformed; |
| 214222 | 214665 | to_double: |
| 214223 | 214666 | z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz); |
| 214224 | 214667 | if( z==0 ) goto returnfromblob_oom; |
| 214225 | | - rc = sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8); |
| 214668 | + rc = sqlite3AtoF(z, &r); |
| 214226 | 214669 | sqlite3DbFree(db, z); |
| 214227 | 214670 | if( rc<=0 ) goto returnfromblob_malformed; |
| 214228 | 214671 | sqlite3_result_double(pCtx, r); |
| 214229 | 214672 | break; |
| 214230 | 214673 | } |
| | @@ -221099,11 +221542,11 @@ |
| 221099 | 221542 | if( pVal ){ |
| 221100 | 221543 | #ifdef SQLITE_AMALGAMATION |
| 221101 | 221544 | /* The sqlite3AtoF() routine is much much faster than atof(), if it |
| 221102 | 221545 | ** is available */ |
| 221103 | 221546 | double r; |
| 221104 | | - (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF8); |
| 221547 | + (void)sqlite3AtoF((const char*)p->z, &r); |
| 221105 | 221548 | *pVal = r; |
| 221106 | 221549 | #else |
| 221107 | 221550 | *pVal = (GeoCoord)atof((const char*)p->z); |
| 221108 | 221551 | #endif |
| 221109 | 221552 | } |
| | @@ -231358,10 +231801,11 @@ |
| 231358 | 231801 | struct carray_bind { |
| 231359 | 231802 | void *aData; /* The data */ |
| 231360 | 231803 | int nData; /* Number of elements */ |
| 231361 | 231804 | int mFlags; /* Control flags */ |
| 231362 | 231805 | void (*xDel)(void*); /* Destructor for aData */ |
| 231806 | + void *pDel; /* Alternative argument to xDel() */ |
| 231363 | 231807 | }; |
| 231364 | 231808 | |
| 231365 | 231809 | |
| 231366 | 231810 | /* carray_cursor is a subclass of sqlite3_vtab_cursor which will |
| 231367 | 231811 | ** serve as the underlying representation of a cursor that scans |
| | @@ -231690,26 +232134,38 @@ |
| 231690 | 232134 | ** Destructor for the carray_bind object |
| 231691 | 232135 | */ |
| 231692 | 232136 | static void carrayBindDel(void *pPtr){ |
| 231693 | 232137 | carray_bind *p = (carray_bind*)pPtr; |
| 231694 | 232138 | if( p->xDel!=SQLITE_STATIC ){ |
| 231695 | | - p->xDel(p->aData); |
| 232139 | + p->xDel(p->pDel); |
| 231696 | 232140 | } |
| 231697 | 232141 | sqlite3_free(p); |
| 231698 | 232142 | } |
| 231699 | 232143 | |
| 231700 | 232144 | /* |
| 231701 | 232145 | ** Invoke this interface in order to bind to the single-argument |
| 231702 | 232146 | ** version of CARRAY(). |
| 232147 | +** |
| 232148 | +** pStmt The prepared statement to which to bind |
| 232149 | +** idx The index of the parameter of pStmt to which to bind |
| 232150 | +** aData The data to be bound |
| 232151 | +** nData The number of elements in aData |
| 232152 | +** mFlags One of SQLITE_CARRAY_xxxx indicating datatype of aData |
| 232153 | +** xDestroy Destructor for pDestroy or aData if pDestroy==NULL. |
| 232154 | +** pDestroy Invoke xDestroy on this pointer if not NULL |
| 232155 | +** |
| 232156 | +** The destructor is called pDestroy if pDestroy!=NULL, or against |
| 232157 | +** aData if pDestroy==NULL. |
| 231703 | 232158 | */ |
| 231704 | | -SQLITE_API int sqlite3_carray_bind( |
| 232159 | +SQLITE_API int sqlite3_carray_bind_v2( |
| 231705 | 232160 | sqlite3_stmt *pStmt, |
| 231706 | 232161 | int idx, |
| 231707 | 232162 | void *aData, |
| 231708 | 232163 | int nData, |
| 231709 | 232164 | int mFlags, |
| 231710 | | - void (*xDestroy)(void*) |
| 232165 | + void (*xDestroy)(void*), |
| 232166 | + void *pDestroy |
| 231711 | 232167 | ){ |
| 231712 | 232168 | carray_bind *pNew = 0; |
| 231713 | 232169 | int i; |
| 231714 | 232170 | int rc = SQLITE_OK; |
| 231715 | 232171 | |
| | @@ -231782,23 +232238,41 @@ |
| 231782 | 232238 | } |
| 231783 | 232239 | }else{ |
| 231784 | 232240 | memcpy(pNew->aData, aData, sz); |
| 231785 | 232241 | } |
| 231786 | 232242 | pNew->xDel = sqlite3_free; |
| 232243 | + pNew->pDel = pNew->aData; |
| 231787 | 232244 | }else{ |
| 231788 | 232245 | pNew->aData = aData; |
| 231789 | 232246 | pNew->xDel = xDestroy; |
| 232247 | + pNew->pDel = pDestroy; |
| 231790 | 232248 | } |
| 231791 | 232249 | return sqlite3_bind_pointer(pStmt, idx, pNew, "carray-bind", carrayBindDel); |
| 231792 | 232250 | |
| 231793 | 232251 | carray_bind_error: |
| 231794 | 232252 | if( xDestroy!=SQLITE_STATIC && xDestroy!=SQLITE_TRANSIENT ){ |
| 231795 | | - xDestroy(aData); |
| 232253 | + xDestroy(pDestroy); |
| 231796 | 232254 | } |
| 231797 | 232255 | sqlite3_free(pNew); |
| 231798 | 232256 | return rc; |
| 231799 | 232257 | } |
| 232258 | + |
| 232259 | +/* |
| 232260 | +** Invoke this interface in order to bind to the single-argument |
| 232261 | +** version of CARRAY(). Same as sqlite3_carray_bind_v2() with the |
| 232262 | +** pDestroy parameter set to NULL. |
| 232263 | +*/ |
| 232264 | +SQLITE_API int sqlite3_carray_bind( |
| 232265 | + sqlite3_stmt *pStmt, |
| 232266 | + int idx, |
| 232267 | + void *aData, |
| 232268 | + int nData, |
| 232269 | + int mFlags, |
| 232270 | + void (*xDestroy)(void*) |
| 232271 | +){ |
| 232272 | + return sqlite3_carray_bind_v2(pStmt,idx,aData,nData,mFlags,xDestroy,aData); |
| 232273 | +} |
| 231800 | 232274 | |
| 231801 | 232275 | /* |
| 231802 | 232276 | ** Invoke this routine to register the carray() function. |
| 231803 | 232277 | */ |
| 231804 | 232278 | SQLITE_PRIVATE Module *sqlite3CarrayRegister(sqlite3 *db){ |
| | @@ -232157,10 +232631,24 @@ |
| 232157 | 232631 | ** bytes read. |
| 232158 | 232632 | */ |
| 232159 | 232633 | static int sessionVarintGet(const u8 *aBuf, int *piVal){ |
| 232160 | 232634 | return getVarint32(aBuf, *piVal); |
| 232161 | 232635 | } |
| 232636 | + |
| 232637 | +/* |
| 232638 | +** Read a varint value from buffer aBuf[], size nBuf bytes, into *piVal. |
| 232639 | +** Return the number of bytes read. |
| 232640 | +*/ |
| 232641 | +static int sessionVarintGetSafe(const u8 *aBuf, int nBuf, int *piVal){ |
| 232642 | + u8 aCopy[5]; |
| 232643 | + const u8 *aRead = aBuf; |
| 232644 | + if( nBuf<5 ){ |
| 232645 | + memcpy(aCopy, aBuf, nBuf); |
| 232646 | + aRead = aCopy; |
| 232647 | + } |
| 232648 | + return getVarint32(aRead, *piVal); |
| 232649 | +} |
| 232162 | 232650 | |
| 232163 | 232651 | /* Load an unaligned and unsigned 32-bit integer */ |
| 232164 | 232652 | #define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) |
| 232165 | 232653 | |
| 232166 | 232654 | /* |
| | @@ -235370,11 +235858,12 @@ |
| 235370 | 235858 | |
| 235371 | 235859 | if( rc==SQLITE_OK ){ |
| 235372 | 235860 | u8 *aVal = &pIn->aData[pIn->iNext]; |
| 235373 | 235861 | if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ |
| 235374 | 235862 | int nByte; |
| 235375 | | - pIn->iNext += sessionVarintGet(aVal, &nByte); |
| 235863 | + int nRem = pIn->nData - pIn->iNext; |
| 235864 | + pIn->iNext += sessionVarintGetSafe(aVal, nRem, &nByte); |
| 235376 | 235865 | rc = sessionInputBuffer(pIn, nByte); |
| 235377 | 235866 | if( rc==SQLITE_OK ){ |
| 235378 | 235867 | if( nByte<0 || nByte>pIn->nData-pIn->iNext ){ |
| 235379 | 235868 | rc = SQLITE_CORRUPT_BKPT; |
| 235380 | 235869 | }else{ |
| | @@ -235423,11 +235912,12 @@ |
| 235423 | 235912 | int nCol = 0; |
| 235424 | 235913 | int nRead = 0; |
| 235425 | 235914 | |
| 235426 | 235915 | rc = sessionInputBuffer(pIn, 9); |
| 235427 | 235916 | if( rc==SQLITE_OK ){ |
| 235428 | | - nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol); |
| 235917 | + int nBuf = pIn->nData - pIn->iNext; |
| 235918 | + nRead += sessionVarintGetSafe(&pIn->aData[pIn->iNext], nBuf, &nCol); |
| 235429 | 235919 | /* The hard upper limit for the number of columns in an SQLite |
| 235430 | 235920 | ** database table is, according to sqliteLimit.h, 32676. So |
| 235431 | 235921 | ** consider any table-header that purports to have more than 65536 |
| 235432 | 235922 | ** columns to be corrupt. This is convenient because otherwise, |
| 235433 | 235923 | ** if the (nCol>65536) condition below were omitted, a sufficiently |
| | @@ -235583,14 +236073,14 @@ |
| 235583 | 236073 | sqlite3ValueFree(p->apValue[i]); |
| 235584 | 236074 | } |
| 235585 | 236075 | memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2); |
| 235586 | 236076 | } |
| 235587 | 236077 | |
| 235588 | | - /* Make sure the buffer contains at least 10 bytes of input data, or all |
| 235589 | | - ** remaining data if there are less than 10 bytes available. This is |
| 235590 | | - ** sufficient either for the 'T' or 'P' byte and the varint that follows |
| 235591 | | - ** it, or for the two single byte values otherwise. */ |
| 236078 | + /* Make sure the buffer contains at least 2 bytes of input data, or all |
| 236079 | + ** remaining data if there are less than 2 bytes available. This is |
| 236080 | + ** sufficient either for the 'T' or 'P' byte that begins a new table, |
| 236081 | + ** or for the "op" and "bIndirect" single bytes otherwise. */ |
| 235592 | 236082 | p->rc = sessionInputBuffer(&p->in, 2); |
| 235593 | 236083 | if( p->rc!=SQLITE_OK ) return p->rc; |
| 235594 | 236084 | |
| 235595 | 236085 | p->in.iCurrent = p->in.iNext; |
| 235596 | 236086 | sessionDiscardData(&p->in); |
| | @@ -235616,15 +236106,17 @@ |
| 235616 | 236106 | ** corrupt changeset. */ |
| 235617 | 236107 | assert( p->in.iNext==1 || p->zTab ); |
| 235618 | 236108 | return (p->rc = SQLITE_CORRUPT_BKPT); |
| 235619 | 236109 | } |
| 235620 | 236110 | |
| 235621 | | - p->op = op; |
| 235622 | | - p->bIndirect = p->in.aData[p->in.iNext++]; |
| 235623 | | - if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){ |
| 236111 | + if( (op!=SQLITE_UPDATE && op!=SQLITE_DELETE && op!=SQLITE_INSERT) |
| 236112 | + || (p->in.iNext>=p->in.nData) |
| 236113 | + ){ |
| 235624 | 236114 | return (p->rc = SQLITE_CORRUPT_BKPT); |
| 235625 | 236115 | } |
| 236116 | + p->op = op; |
| 236117 | + p->bIndirect = p->in.aData[p->in.iNext++]; |
| 235626 | 236118 | |
| 235627 | 236119 | if( paRec ){ |
| 235628 | 236120 | int nVal; /* Number of values to buffer */ |
| 235629 | 236121 | if( p->bPatchset==0 && op==SQLITE_UPDATE ){ |
| 235630 | 236122 | nVal = p->nCol * 2; |
| | @@ -261258,11 +261750,11 @@ |
| 261258 | 261750 | int nArg, /* Number of args */ |
| 261259 | 261751 | sqlite3_value **apUnused /* Function arguments */ |
| 261260 | 261752 | ){ |
| 261261 | 261753 | assert( nArg==0 ); |
| 261262 | 261754 | UNUSED_PARAM2(nArg, apUnused); |
| 261263 | | - sqlite3_result_text(pCtx, "fts5: 2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71", -1, SQLITE_TRANSIENT); |
| 261755 | + sqlite3_result_text(pCtx, "fts5: 2026-02-17 01:04:23 dd5af703e1082951a4295a3453611db12b23cfbcfee4258ec3985abe96ab54ba", -1, SQLITE_TRANSIENT); |
| 261264 | 261756 | } |
| 261265 | 261757 | |
| 261266 | 261758 | /* |
| 261267 | 261759 | ** Implementation of fts5_locale(LOCALE, TEXT) function. |
| 261268 | 261760 | ** |
| 261269 | 261761 | |