| | @@ -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 | | -** 76fb3a908f45230b956cb659c754e47603e8. |
| 21 | +** a49296de0061931badaf3db6b965131a78b1. |
| 22 | 22 | */ |
| 23 | 23 | #define SQLITE_CORE 1 |
| 24 | 24 | #define SQLITE_AMALGAMATION 1 |
| 25 | 25 | #ifndef SQLITE_PRIVATE |
| 26 | 26 | # define SQLITE_PRIVATE static |
| | @@ -459,11 +459,11 @@ |
| 459 | 459 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 460 | 460 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 461 | 461 | */ |
| 462 | 462 | #define SQLITE_VERSION "3.46.0" |
| 463 | 463 | #define SQLITE_VERSION_NUMBER 3046000 |
| 464 | | -#define SQLITE_SOURCE_ID "2024-03-18 18:03:17 76fb3a908f45230b956cb659c754e47603e80aa72a2aad50d479437a9c013f61" |
| 464 | +#define SQLITE_SOURCE_ID "2024-03-26 11:14:52 a49296de0061931badaf3db6b965131a78b1c6c21b1eeb62815ea7adf767d0b3" |
| 465 | 465 | |
| 466 | 466 | /* |
| 467 | 467 | ** CAPI3REF: Run-Time Library Version Numbers |
| 468 | 468 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 469 | 469 | ** |
| | @@ -2454,10 +2454,26 @@ |
| 2454 | 2454 | ** size can be adjusted up or down for individual databases using the |
| 2455 | 2455 | ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this |
| 2456 | 2456 | ** configuration setting is never used, then the default maximum is determined |
| 2457 | 2457 | ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that |
| 2458 | 2458 | ** compile-time option is not set, then the default maximum is 1073741824. |
| 2459 | +** |
| 2460 | +** [[SQLITE_CONFIG_ROWID_IN_VIEW]] |
| 2461 | +** <dt>SQLITE_CONFIG_ROWID_IN_VIEW |
| 2462 | +** <dd>The SQLITE_CONFIG_ROWID_IN_VIEW option enables or disables the ability |
| 2463 | +** for VIEWs to have a ROWID. The capability can only be enabled if SQLite is |
| 2464 | +** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability |
| 2465 | +** defaults to on. This configuration option queries the current setting or |
| 2466 | +** changes the setting to off or on. The argument is a pointer to an integer. |
| 2467 | +** If that integer initially holds a value of 1, then the ability for VIEWs to |
| 2468 | +** have ROWIDs is activated. If the integer initially holds zero, then the |
| 2469 | +** ability is deactivated. Any other initial value for the integer leaves the |
| 2470 | +** setting unchanged. After changes, if any, the integer is written with |
| 2471 | +** a 1 or 0, if the ability for VIEWs to have ROWIDs is on or off. If SQLite |
| 2472 | +** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and |
| 2473 | +** recommended case) then the integer is always filled with zero, regardless |
| 2474 | +** if its initial value. |
| 2459 | 2475 | ** </dl> |
| 2460 | 2476 | */ |
| 2461 | 2477 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 2462 | 2478 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 2463 | 2479 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| | @@ -2485,10 +2501,11 @@ |
| 2485 | 2501 | #define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */ |
| 2486 | 2502 | #define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */ |
| 2487 | 2503 | #define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */ |
| 2488 | 2504 | #define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */ |
| 2489 | 2505 | #define SQLITE_CONFIG_MEMDB_MAXSIZE 29 /* sqlite3_int64 */ |
| 2506 | +#define SQLITE_CONFIG_ROWID_IN_VIEW 30 /* int* */ |
| 2490 | 2507 | |
| 2491 | 2508 | /* |
| 2492 | 2509 | ** CAPI3REF: Database Connection Configuration Options |
| 2493 | 2510 | ** |
| 2494 | 2511 | ** These constants are the available integer configuration options that |
| | @@ -3599,12 +3616,12 @@ |
| 3599 | 3616 | #define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */ |
| 3600 | 3617 | #define SQLITE_COPY 0 /* No longer used */ |
| 3601 | 3618 | #define SQLITE_RECURSIVE 33 /* NULL NULL */ |
| 3602 | 3619 | |
| 3603 | 3620 | /* |
| 3604 | | -** CAPI3REF: Tracing And Profiling Functions |
| 3605 | | -** METHOD: sqlite3 |
| 3621 | +** CAPI3REF: Deprecated Tracing And Profiling Functions |
| 3622 | +** DEPRECATED |
| 3606 | 3623 | ** |
| 3607 | 3624 | ** These routines are deprecated. Use the [sqlite3_trace_v2()] interface |
| 3608 | 3625 | ** instead of the routines described here. |
| 3609 | 3626 | ** |
| 3610 | 3627 | ** These routines register callback functions that can be used for |
| | @@ -18438,10 +18455,19 @@ |
| 18438 | 18455 | |
| 18439 | 18456 | /* Does the table have a rowid */ |
| 18440 | 18457 | #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0) |
| 18441 | 18458 | #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0) |
| 18442 | 18459 | |
| 18460 | +/* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is |
| 18461 | +** available. By default, this macro is false |
| 18462 | +*/ |
| 18463 | +#ifndef SQLITE_ALLOW_ROWID_IN_VIEW |
| 18464 | +# define ViewCanHaveRowid 0 |
| 18465 | +#else |
| 18466 | +# define ViewCanHaveRowid (sqlite3Config.mNoVisibleRowid==0) |
| 18467 | +#endif |
| 18468 | + |
| 18443 | 18469 | /* |
| 18444 | 18470 | ** Each foreign key constraint is an instance of the following structure. |
| 18445 | 18471 | ** |
| 18446 | 18472 | ** A foreign key is associated with two tables. The "from" table is |
| 18447 | 18473 | ** the table that contains the REFERENCES clause that creates the foreign |
| | @@ -20157,10 +20183,15 @@ |
| 20157 | 20183 | sqlite3_int64 mxMemdbSize; /* Default max memdb size */ |
| 20158 | 20184 | #endif |
| 20159 | 20185 | #ifndef SQLITE_UNTESTABLE |
| 20160 | 20186 | int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */ |
| 20161 | 20187 | #endif |
| 20188 | +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 20189 | + u32 mNoVisibleRowid; /* TF_NoVisibleRowid if the ROWID_IN_VIEW |
| 20190 | + ** feature is disabled. 0 if rowids can |
| 20191 | + ** occur in views. */ |
| 20192 | +#endif |
| 20162 | 20193 | int bLocaltimeFault; /* True to fail localtime() calls */ |
| 20163 | 20194 | int (*xAltLocaltime)(const void*,void*); /* Alternative localtime() routine */ |
| 20164 | 20195 | int iOnceResetThreshold; /* When to reset OP_Once counters */ |
| 20165 | 20196 | u32 szSorterRef; /* Min size in bytes to use sorter-refs */ |
| 20166 | 20197 | unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */ |
| | @@ -20615,14 +20646,17 @@ |
| 20615 | 20646 | |
| 20616 | 20647 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 20617 | 20648 | # define EXP754 (((u64)0x7ff)<<52) |
| 20618 | 20649 | # define MAN754 ((((u64)1)<<52)-1) |
| 20619 | 20650 | # define IsNaN(X) (((X)&EXP754)==EXP754 && ((X)&MAN754)!=0) |
| 20651 | +# define IsOvfl(X) (((X)&EXP754)==EXP754) |
| 20620 | 20652 | SQLITE_PRIVATE int sqlite3IsNaN(double); |
| 20653 | +SQLITE_PRIVATE int sqlite3IsOverflow(double); |
| 20621 | 20654 | #else |
| 20622 | | -# define IsNaN(X) 0 |
| 20623 | | -# define sqlite3IsNaN(X) 0 |
| 20655 | +# define IsNaN(X) 0 |
| 20656 | +# define sqlite3IsNaN(X) 0 |
| 20657 | +# define sqlite3IsOVerflow(X) 0 |
| 20624 | 20658 | #endif |
| 20625 | 20659 | |
| 20626 | 20660 | /* |
| 20627 | 20661 | ** An instance of the following structure holds information about SQL |
| 20628 | 20662 | ** functions arguments that are the parameters to the printf() function. |
| | @@ -21853,10 +21887,13 @@ |
| 21853 | 21887 | #endif |
| 21854 | 21888 | #ifdef SQLITE_ALLOW_COVERING_INDEX_SCAN |
| 21855 | 21889 | # if SQLITE_ALLOW_COVERING_INDEX_SCAN != 1 |
| 21856 | 21890 | "ALLOW_COVERING_INDEX_SCAN=" CTIMEOPT_VAL(SQLITE_ALLOW_COVERING_INDEX_SCAN), |
| 21857 | 21891 | # endif |
| 21892 | +#endif |
| 21893 | +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 21894 | + "ALLOW_ROWID_IN_VIEW", |
| 21858 | 21895 | #endif |
| 21859 | 21896 | #ifdef SQLITE_ALLOW_URI_AUTHORITY |
| 21860 | 21897 | "ALLOW_URI_AUTHORITY", |
| 21861 | 21898 | #endif |
| 21862 | 21899 | #ifdef SQLITE_ATOMIC_INTRINSICS |
| | @@ -22874,10 +22911,13 @@ |
| 22874 | 22911 | SQLITE_MEMDB_DEFAULT_MAXSIZE, /* mxMemdbSize */ |
| 22875 | 22912 | #endif |
| 22876 | 22913 | #ifndef SQLITE_UNTESTABLE |
| 22877 | 22914 | 0, /* xTestCallback */ |
| 22878 | 22915 | #endif |
| 22916 | +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 22917 | + 0, /* mNoVisibleRowid. 0 == allow rowid-in-view */ |
| 22918 | +#endif |
| 22879 | 22919 | 0, /* bLocaltimeFault */ |
| 22880 | 22920 | 0, /* xAltLocaltime */ |
| 22881 | 22921 | 0x7ffffffe, /* iOnceResetThreshold */ |
| 22882 | 22922 | SQLITE_DEFAULT_SORTERREF_SIZE, /* szSorterRef */ |
| 22883 | 22923 | 0, /* iPrngSeed */ |
| | @@ -34877,10 +34917,23 @@ |
| 34877 | 34917 | testcase( rc ); |
| 34878 | 34918 | return rc; |
| 34879 | 34919 | } |
| 34880 | 34920 | #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 34881 | 34921 | |
| 34922 | +#ifndef SQLITE_OMIT_FLOATING_POINT |
| 34923 | +/* |
| 34924 | +** Return true if the floating point value is NaN or +Inf or -Inf. |
| 34925 | +*/ |
| 34926 | +SQLITE_PRIVATE int sqlite3IsOverflow(double x){ |
| 34927 | + int rc; /* The value return */ |
| 34928 | + u64 y; |
| 34929 | + memcpy(&y,&x,sizeof(y)); |
| 34930 | + rc = IsOvfl(y); |
| 34931 | + return rc; |
| 34932 | +} |
| 34933 | +#endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 34934 | + |
| 34882 | 34935 | /* |
| 34883 | 34936 | ** Compute a string length that is limited to what can be stored in |
| 34884 | 34937 | ** lower 30 bits of a 32-bit signed integer. |
| 34885 | 34938 | ** |
| 34886 | 34939 | ** The value returned will never be negative. Nor will it ever be greater |
| | @@ -44643,16 +44696,23 @@ |
| 44643 | 44696 | /* If unable to create a journal because the directory is not |
| 44644 | 44697 | ** writable, change the error code to indicate that. */ |
| 44645 | 44698 | rc = SQLITE_READONLY_DIRECTORY; |
| 44646 | 44699 | }else if( errno!=EISDIR && isReadWrite ){ |
| 44647 | 44700 | /* Failed to open the file for read/write access. Try read-only. */ |
| 44701 | + UnixUnusedFd *pReadonly = 0; |
| 44648 | 44702 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
| 44649 | 44703 | openFlags &= ~(O_RDWR|O_CREAT); |
| 44650 | 44704 | flags |= SQLITE_OPEN_READONLY; |
| 44651 | 44705 | openFlags |= O_RDONLY; |
| 44652 | 44706 | isReadonly = 1; |
| 44653 | | - fd = robust_open(zName, openFlags, openMode); |
| 44707 | + pReadonly = findReusableFd(zName, flags); |
| 44708 | + if( pReadonly ){ |
| 44709 | + fd = pReadonly->fd; |
| 44710 | + sqlite3_free(pReadonly); |
| 44711 | + }else{ |
| 44712 | + fd = robust_open(zName, openFlags, openMode); |
| 44713 | + } |
| 44654 | 44714 | } |
| 44655 | 44715 | } |
| 44656 | 44716 | if( fd<0 ){ |
| 44657 | 44717 | int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
| 44658 | 44718 | if( rc==SQLITE_OK ) rc = rc2; |
| | @@ -92645,11 +92705,10 @@ |
| 92645 | 92705 | } |
| 92646 | 92706 | return 1; |
| 92647 | 92707 | } |
| 92648 | 92708 | if( flags & SQLITE_SCANSTAT_COMPLEX ){ |
| 92649 | 92709 | idx = iScan; |
| 92650 | | - pScan = &p->aScan[idx]; |
| 92651 | 92710 | }else{ |
| 92652 | 92711 | /* If the COMPLEX flag is clear, then this function must ignore any |
| 92653 | 92712 | ** ScanStatus structures with ScanStatus.addrLoop set to 0. */ |
| 92654 | 92713 | for(idx=0; idx<p->nScan; idx++){ |
| 92655 | 92714 | pScan = &p->aScan[idx]; |
| | @@ -92658,10 +92717,12 @@ |
| 92658 | 92717 | if( iScan<0 ) break; |
| 92659 | 92718 | } |
| 92660 | 92719 | } |
| 92661 | 92720 | } |
| 92662 | 92721 | if( idx>=p->nScan ) return 1; |
| 92722 | + assert( pScan==0 || pScan==&p->aScan[idx] ); |
| 92723 | + pScan = &p->aScan[idx]; |
| 92663 | 92724 | |
| 92664 | 92725 | switch( iScanStatusOp ){ |
| 92665 | 92726 | case SQLITE_SCANSTAT_NLOOP: { |
| 92666 | 92727 | if( pScan->addrLoop>0 ){ |
| 92667 | 92728 | *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec; |
| | @@ -99169,11 +99230,12 @@ |
| 99169 | 99230 | |
| 99170 | 99231 | /* Opcode: IfSizeBetween P1 P2 P3 P4 * |
| 99171 | 99232 | ** |
| 99172 | 99233 | ** Let N be the approximate number of rows in the table or index |
| 99173 | 99234 | ** with cursor P1 and let X be 10*log2(N) if N is positive or -1 |
| 99174 | | -** if N is zero. Thus X will be within the range of -1 to 640, inclusive |
| 99235 | +** if N is zero. |
| 99236 | +** |
| 99175 | 99237 | ** Jump to P2 if X is in between P3 and P4, inclusive. |
| 99176 | 99238 | */ |
| 99177 | 99239 | case OP_IfSizeBetween: { /* jump */ |
| 99178 | 99240 | VdbeCursor *pC; |
| 99179 | 99241 | BtCursor *pCrsr; |
| | @@ -99180,12 +99242,12 @@ |
| 99180 | 99242 | int res; |
| 99181 | 99243 | i64 sz; |
| 99182 | 99244 | |
| 99183 | 99245 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 99184 | 99246 | assert( pOp->p4type==P4_INT32 ); |
| 99185 | | - assert( pOp->p3>=-1 && pOp->p3<=640 ); |
| 99186 | | - assert( pOp->p4.i>=-1 && pOp->p4.i<=640 ); |
| 99247 | + assert( pOp->p3>=-1 && pOp->p3<=640*2 ); |
| 99248 | + assert( pOp->p4.i>=-1 && pOp->p4.i<=640*2 ); |
| 99187 | 99249 | pC = p->apCsr[pOp->p1]; |
| 99188 | 99250 | assert( pC!=0 ); |
| 99189 | 99251 | pCrsr = pC->uc.pCursor; |
| 99190 | 99252 | assert( pCrsr ); |
| 99191 | 99253 | rc = sqlite3BtreeFirst(pCrsr, &res); |
| | @@ -107053,12 +107115,41 @@ |
| 107053 | 107115 | } |
| 107054 | 107116 | break; |
| 107055 | 107117 | } |
| 107056 | 107118 | } |
| 107057 | 107119 | if( 0==cnt && VisibleRowid(pTab) ){ |
| 107120 | + /* pTab is a potential ROWID match. Keep track of it and match |
| 107121 | + ** the ROWID later if that seems appropriate. (Search for "cntTab" |
| 107122 | + ** to find related code.) Only allow a ROWID match if there is |
| 107123 | + ** a single ROWID match candidate. |
| 107124 | + */ |
| 107125 | +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 107126 | + /* In SQLITE_ALLOW_ROWID_IN_VIEW mode, allow a ROWID match |
| 107127 | + ** if there is a single VIEW candidate or if there is a single |
| 107128 | + ** non-VIEW candidate plus multiple VIEW candidates. In other |
| 107129 | + ** words non-VIEW candidate terms take precedence over VIEWs. |
| 107130 | + */ |
| 107131 | + if( cntTab==0 |
| 107132 | + || (cntTab==1 |
| 107133 | + && ALWAYS(pMatch!=0) |
| 107134 | + && ALWAYS(pMatch->pTab!=0) |
| 107135 | + && (pMatch->pTab->tabFlags & TF_Ephemeral)!=0 |
| 107136 | + && (pTab->tabFlags & TF_Ephemeral)==0) |
| 107137 | + ){ |
| 107138 | + cntTab = 1; |
| 107139 | + pMatch = pItem; |
| 107140 | + }else{ |
| 107141 | + cntTab++; |
| 107142 | + } |
| 107143 | +#else |
| 107144 | + /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is |
| 107145 | + ** simpler since we require exactly one candidate, which will |
| 107146 | + ** always be a non-VIEW |
| 107147 | + */ |
| 107058 | 107148 | cntTab++; |
| 107059 | 107149 | pMatch = pItem; |
| 107150 | +#endif |
| 107060 | 107151 | } |
| 107061 | 107152 | } |
| 107062 | 107153 | if( pMatch ){ |
| 107063 | 107154 | pExpr->iTable = pMatch->iCursor; |
| 107064 | 107155 | assert( ExprUseYTab(pExpr) ); |
| | @@ -107180,17 +107271,17 @@ |
| 107180 | 107271 | |
| 107181 | 107272 | /* |
| 107182 | 107273 | ** Perhaps the name is a reference to the ROWID |
| 107183 | 107274 | */ |
| 107184 | 107275 | if( cnt==0 |
| 107185 | | - && cntTab==1 |
| 107276 | + && cntTab>=1 |
| 107186 | 107277 | && pMatch |
| 107187 | 107278 | && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0 |
| 107188 | 107279 | && sqlite3IsRowid(zCol) |
| 107189 | 107280 | && ALWAYS(VisibleRowid(pMatch->pTab) || pMatch->fg.isNestedFrom) |
| 107190 | 107281 | ){ |
| 107191 | | - cnt = 1; |
| 107282 | + cnt = cntTab; |
| 107192 | 107283 | if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1; |
| 107193 | 107284 | pExpr->affExpr = SQLITE_AFF_INTEGER; |
| 107194 | 107285 | } |
| 107195 | 107286 | |
| 107196 | 107287 | /* |
| | @@ -110701,19 +110792,20 @@ |
| 110701 | 110792 | pNewItem->addrFillSub = pOldItem->addrFillSub; |
| 110702 | 110793 | pNewItem->regReturn = pOldItem->regReturn; |
| 110703 | 110794 | pNewItem->regResult = pOldItem->regResult; |
| 110704 | 110795 | if( pNewItem->fg.isIndexedBy ){ |
| 110705 | 110796 | pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); |
| 110797 | + }else if( pNewItem->fg.isTabFunc ){ |
| 110798 | + pNewItem->u1.pFuncArg = |
| 110799 | + sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); |
| 110800 | + }else{ |
| 110801 | + pNewItem->u1.nRow = pOldItem->u1.nRow; |
| 110706 | 110802 | } |
| 110707 | 110803 | pNewItem->u2 = pOldItem->u2; |
| 110708 | 110804 | if( pNewItem->fg.isCte ){ |
| 110709 | 110805 | pNewItem->u2.pCteUse->nUse++; |
| 110710 | 110806 | } |
| 110711 | | - if( pNewItem->fg.isTabFunc ){ |
| 110712 | | - pNewItem->u1.pFuncArg = |
| 110713 | | - sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); |
| 110714 | | - } |
| 110715 | 110807 | pTab = pNewItem->pTab = pOldItem->pTab; |
| 110716 | 110808 | if( pTab ){ |
| 110717 | 110809 | pTab->nTabRef++; |
| 110718 | 110810 | } |
| 110719 | 110811 | pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); |
| | @@ -111301,13 +111393,15 @@ |
| 111301 | 111393 | } |
| 111302 | 111394 | /* no break */ deliberate_fall_through |
| 111303 | 111395 | case TK_IF_NULL_ROW: |
| 111304 | 111396 | case TK_REGISTER: |
| 111305 | 111397 | case TK_DOT: |
| 111398 | + case TK_RAISE: |
| 111306 | 111399 | testcase( pExpr->op==TK_REGISTER ); |
| 111307 | 111400 | testcase( pExpr->op==TK_IF_NULL_ROW ); |
| 111308 | 111401 | testcase( pExpr->op==TK_DOT ); |
| 111402 | + testcase( pExpr->op==TK_RAISE ); |
| 111309 | 111403 | pWalker->eCode = 0; |
| 111310 | 111404 | return WRC_Abort; |
| 111311 | 111405 | case TK_VARIABLE: |
| 111312 | 111406 | if( pWalker->eCode==5 ){ |
| 111313 | 111407 | /* Silently convert bound parameters that appear inside of CREATE |
| | @@ -111628,13 +111722,16 @@ |
| 111628 | 111722 | case TK_FLOAT: |
| 111629 | 111723 | case TK_BLOB: |
| 111630 | 111724 | return 0; |
| 111631 | 111725 | case TK_COLUMN: |
| 111632 | 111726 | assert( ExprUseYTab(p) ); |
| 111633 | | - return ExprHasProperty(p, EP_CanBeNull) || |
| 111634 | | - NEVER(p->y.pTab==0) || /* Reference to column of index on expr */ |
| 111635 | | - (p->iColumn>=0 |
| 111727 | + return ExprHasProperty(p, EP_CanBeNull) |
| 111728 | + || NEVER(p->y.pTab==0) /* Reference to column of index on expr */ |
| 111729 | +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 111730 | + || (p->iColumn==XN_ROWID && IsView(p->y.pTab)) |
| 111731 | +#endif |
| 111732 | + || (p->iColumn>=0 |
| 111636 | 111733 | && p->y.pTab->aCol!=0 /* Possible due to prior error */ |
| 111637 | 111734 | && ALWAYS(p->iColumn<p->y.pTab->nCol) |
| 111638 | 111735 | && p->y.pTab->aCol[p->iColumn].notNull==0); |
| 111639 | 111736 | default: |
| 111640 | 111737 | return 1; |
| | @@ -118166,11 +118263,16 @@ |
| 118166 | 118263 | regOut = reg+1+nField; |
| 118167 | 118264 | } |
| 118168 | 118265 | if( i==pTab->iPKey ){ |
| 118169 | 118266 | sqlite3VdbeAddOp2(v, OP_Null, 0, regOut); |
| 118170 | 118267 | }else{ |
| 118268 | + char aff = pTab->aCol[i].affinity; |
| 118269 | + if( aff==SQLITE_AFF_REAL ){ |
| 118270 | + pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC; |
| 118271 | + } |
| 118171 | 118272 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut); |
| 118273 | + pTab->aCol[i].affinity = aff; |
| 118172 | 118274 | } |
| 118173 | 118275 | nField++; |
| 118174 | 118276 | } |
| 118175 | 118277 | } |
| 118176 | 118278 | if( nField==0 ){ |
| | @@ -123952,24 +124054,24 @@ |
| 123952 | 124054 | int addrTop; /* Top of the co-routine */ |
| 123953 | 124055 | int regRec; /* A record to be insert into the new table */ |
| 123954 | 124056 | int regRowid; /* Rowid of the next row to insert */ |
| 123955 | 124057 | int addrInsLoop; /* Top of the loop for inserting rows */ |
| 123956 | 124058 | Table *pSelTab; /* A table that describes the SELECT results */ |
| 124059 | + int iCsr; /* Write cursor on the new table */ |
| 123957 | 124060 | |
| 123958 | 124061 | if( IN_SPECIAL_PARSE ){ |
| 123959 | 124062 | pParse->rc = SQLITE_ERROR; |
| 123960 | 124063 | pParse->nErr++; |
| 123961 | 124064 | return; |
| 123962 | 124065 | } |
| 124066 | + iCsr = pParse->nTab++; |
| 123963 | 124067 | regYield = ++pParse->nMem; |
| 123964 | 124068 | regRec = ++pParse->nMem; |
| 123965 | 124069 | regRowid = ++pParse->nMem; |
| 123966 | | - assert(pParse->nTab==1); |
| 123967 | 124070 | sqlite3MayAbort(pParse); |
| 123968 | | - sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); |
| 124071 | + sqlite3VdbeAddOp3(v, OP_OpenWrite, iCsr, pParse->regRoot, iDb); |
| 123969 | 124072 | sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); |
| 123970 | | - pParse->nTab = 2; |
| 123971 | 124073 | addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 123972 | 124074 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
| 123973 | 124075 | if( pParse->nErr ) return; |
| 123974 | 124076 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB); |
| 123975 | 124077 | if( pSelTab==0 ) return; |
| | @@ -123986,15 +124088,15 @@ |
| 123986 | 124088 | sqlite3VdbeJumpHere(v, addrTop - 1); |
| 123987 | 124089 | addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); |
| 123988 | 124090 | VdbeCoverage(v); |
| 123989 | 124091 | sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); |
| 123990 | 124092 | sqlite3TableAffinity(v, p, 0); |
| 123991 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid); |
| 123992 | | - sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid); |
| 124093 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iCsr, regRowid); |
| 124094 | + sqlite3VdbeAddOp3(v, OP_Insert, iCsr, regRec, regRowid); |
| 123993 | 124095 | sqlite3VdbeGoto(v, addrInsLoop); |
| 123994 | 124096 | sqlite3VdbeJumpHere(v, addrInsLoop); |
| 123995 | | - sqlite3VdbeAddOp1(v, OP_Close, 1); |
| 124097 | + sqlite3VdbeAddOp1(v, OP_Close, iCsr); |
| 123996 | 124098 | } |
| 123997 | 124099 | |
| 123998 | 124100 | /* Compute the complete text of the CREATE statement */ |
| 123999 | 124101 | if( pSelect ){ |
| 124000 | 124102 | zStmt = createTableStmt(db, p); |
| | @@ -124130,13 +124232,16 @@ |
| 124130 | 124232 | |
| 124131 | 124233 | /* Legacy versions of SQLite allowed the use of the magic "rowid" column |
| 124132 | 124234 | ** on a view, even though views do not have rowids. The following flag |
| 124133 | 124235 | ** setting fixes this problem. But the fix can be disabled by compiling |
| 124134 | 124236 | ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that |
| 124135 | | - ** depend upon the old buggy behavior. */ |
| 124136 | | -#ifndef SQLITE_ALLOW_ROWID_IN_VIEW |
| 124137 | | - p->tabFlags |= TF_NoVisibleRowid; |
| 124237 | + ** depend upon the old buggy behavior. The ability can also be toggled |
| 124238 | + ** using sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW,...) */ |
| 124239 | +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 124240 | + p->tabFlags |= sqlite3Config.mNoVisibleRowid; /* Optional. Allow by default */ |
| 124241 | +#else |
| 124242 | + p->tabFlags |= TF_NoVisibleRowid; /* Never allow rowid in view */ |
| 124138 | 124243 | #endif |
| 124139 | 124244 | |
| 124140 | 124245 | sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 124141 | 124246 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
| 124142 | 124247 | sqlite3FixInit(&sFix, pParse, iDb, "view", pName); |
| | @@ -130296,11 +130401,11 @@ |
| 130296 | 130401 | p = sqlite3_aggregate_context(context, 0); |
| 130297 | 130402 | if( p && p->cnt>0 ){ |
| 130298 | 130403 | if( p->approx ){ |
| 130299 | 130404 | if( p->ovrfl ){ |
| 130300 | 130405 | sqlite3_result_error(context,"integer overflow",-1); |
| 130301 | | - }else if( !sqlite3IsNaN(p->rErr) ){ |
| 130406 | + }else if( !sqlite3IsOverflow(p->rErr) ){ |
| 130302 | 130407 | sqlite3_result_double(context, p->rSum+p->rErr); |
| 130303 | 130408 | }else{ |
| 130304 | 130409 | sqlite3_result_double(context, p->rSum); |
| 130305 | 130410 | } |
| 130306 | 130411 | }else{ |
| | @@ -130313,11 +130418,11 @@ |
| 130313 | 130418 | p = sqlite3_aggregate_context(context, 0); |
| 130314 | 130419 | if( p && p->cnt>0 ){ |
| 130315 | 130420 | double r; |
| 130316 | 130421 | if( p->approx ){ |
| 130317 | 130422 | r = p->rSum; |
| 130318 | | - if( !sqlite3IsNaN(p->rErr) ) r += p->rErr; |
| 130423 | + if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr; |
| 130319 | 130424 | }else{ |
| 130320 | 130425 | r = (double)(p->iSum); |
| 130321 | 130426 | } |
| 130322 | 130427 | sqlite3_result_double(context, r/(double)p->cnt); |
| 130323 | 130428 | } |
| | @@ -130327,11 +130432,11 @@ |
| 130327 | 130432 | double r = 0.0; |
| 130328 | 130433 | p = sqlite3_aggregate_context(context, 0); |
| 130329 | 130434 | if( p ){ |
| 130330 | 130435 | if( p->approx ){ |
| 130331 | 130436 | r = p->rSum; |
| 130332 | | - if( !sqlite3IsNaN(p->rErr) ) r += p->rErr; |
| 130437 | + if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr; |
| 130333 | 130438 | }else{ |
| 130334 | 130439 | r = (double)(p->iSum); |
| 130335 | 130440 | } |
| 130336 | 130441 | } |
| 130337 | 130442 | sqlite3_result_double(context, r); |
| | @@ -133257,12 +133362,14 @@ |
| 133257 | 133362 | */ |
| 133258 | 133363 | static int exprListIsNoAffinity(Parse *pParse, ExprList *pRow){ |
| 133259 | 133364 | int ii; |
| 133260 | 133365 | if( exprListIsConstant(pParse,pRow)==0 ) return 0; |
| 133261 | 133366 | for(ii=0; ii<pRow->nExpr; ii++){ |
| 133262 | | - assert( pRow->a[ii].pExpr->affExpr==0 ); |
| 133263 | | - if( 0!=sqlite3ExprAffinity(pRow->a[ii].pExpr) ) return 0; |
| 133367 | + Expr *pExpr = pRow->a[ii].pExpr; |
| 133368 | + assert( pExpr->op!=TK_RAISE ); |
| 133369 | + assert( pExpr->affExpr==0 ); |
| 133370 | + if( 0!=sqlite3ExprAffinity(pExpr) ) return 0; |
| 133264 | 133371 | } |
| 133265 | 133372 | return 1; |
| 133266 | 133373 | |
| 133267 | 133374 | } |
| 133268 | 133375 | |
| | @@ -141342,13 +141449,13 @@ |
| 141342 | 141449 | if( pTab->nHidden==0 ){ return SQLITE_OK; } |
| 141343 | 141450 | pConstraint = pIdxInfo->aConstraint; |
| 141344 | 141451 | seen[0] = 0; |
| 141345 | 141452 | seen[1] = 0; |
| 141346 | 141453 | for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 141347 | | - if( pConstraint->usable==0 ) continue; |
| 141454 | + if( pConstraint->iColumn < pTab->iHidden ) continue; |
| 141348 | 141455 | if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 141349 | | - if( pConstraint->iColumn < pTab->iHidden ) continue; |
| 141456 | + if( pConstraint->usable==0 ) return SQLITE_CONSTRAINT; |
| 141350 | 141457 | j = pConstraint->iColumn - pTab->iHidden; |
| 141351 | 141458 | assert( j < 2 ); |
| 141352 | 141459 | seen[j] = i+1; |
| 141353 | 141460 | } |
| 141354 | 141461 | if( seen[0]==0 ){ |
| | @@ -141357,16 +141464,17 @@ |
| 141357 | 141464 | return SQLITE_OK; |
| 141358 | 141465 | } |
| 141359 | 141466 | j = seen[0]-1; |
| 141360 | 141467 | pIdxInfo->aConstraintUsage[j].argvIndex = 1; |
| 141361 | 141468 | pIdxInfo->aConstraintUsage[j].omit = 1; |
| 141362 | | - if( seen[1]==0 ) return SQLITE_OK; |
| 141363 | 141469 | pIdxInfo->estimatedCost = (double)20; |
| 141364 | 141470 | pIdxInfo->estimatedRows = 20; |
| 141365 | | - j = seen[1]-1; |
| 141366 | | - pIdxInfo->aConstraintUsage[j].argvIndex = 2; |
| 141367 | | - pIdxInfo->aConstraintUsage[j].omit = 1; |
| 141471 | + if( seen[1] ){ |
| 141472 | + j = seen[1]-1; |
| 141473 | + pIdxInfo->aConstraintUsage[j].argvIndex = 2; |
| 141474 | + pIdxInfo->aConstraintUsage[j].omit = 1; |
| 141475 | + } |
| 141368 | 141476 | return SQLITE_OK; |
| 141369 | 141477 | } |
| 141370 | 141478 | |
| 141371 | 141479 | /* Create a new cursor for the pragma virtual table */ |
| 141372 | 141480 | static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){ |
| | @@ -144304,13 +144412,20 @@ |
| 144304 | 144412 | struct ExprList_item *aOutEx = p->pEList->a; |
| 144305 | 144413 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 144306 | 144414 | int addrExplain; /* Address of OP_Explain instruction */ |
| 144307 | 144415 | #endif |
| 144308 | 144416 | |
| 144309 | | - ExplainQueryPlan2(addrExplain, (pParse, 0, |
| 144310 | | - "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat>0?"RIGHT PART OF ":"") |
| 144311 | | - ); |
| 144417 | + nKey = pOrderBy->nExpr - pSort->nOBSat; |
| 144418 | + if( pSort->nOBSat==0 || nKey==1 ){ |
| 144419 | + ExplainQueryPlan2(addrExplain, (pParse, 0, |
| 144420 | + "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat?"LAST TERM OF ":"" |
| 144421 | + )); |
| 144422 | + }else{ |
| 144423 | + ExplainQueryPlan2(addrExplain, (pParse, 0, |
| 144424 | + "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY", nKey |
| 144425 | + )); |
| 144426 | + } |
| 144312 | 144427 | sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd); |
| 144313 | 144428 | sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush); |
| 144314 | 144429 | |
| 144315 | 144430 | |
| 144316 | 144431 | assert( addrBreak<0 ); |
| | @@ -144344,11 +144459,10 @@ |
| 144344 | 144459 | nColumn = 0; |
| 144345 | 144460 | }else{ |
| 144346 | 144461 | regRow = sqlite3GetTempRange(pParse, nColumn); |
| 144347 | 144462 | } |
| 144348 | 144463 | } |
| 144349 | | - nKey = pOrderBy->nExpr - pSort->nOBSat; |
| 144350 | 144464 | if( pSort->sortFlags & SORTFLAG_UseSorter ){ |
| 144351 | 144465 | int regSortOut = ++pParse->nMem; |
| 144352 | 144466 | iSortTab = pParse->nTab++; |
| 144353 | 144467 | if( pSort->labelBkOut ){ |
| 144354 | 144468 | addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
| | @@ -144584,15 +144698,11 @@ |
| 144584 | 144698 | /* The "table" is actually a sub-select or a view in the FROM clause |
| 144585 | 144699 | ** of the SELECT statement. Return the declaration type and origin |
| 144586 | 144700 | ** data for the result-set column of the sub-select. |
| 144587 | 144701 | */ |
| 144588 | 144702 | if( iCol<pS->pEList->nExpr |
| 144589 | | -#ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 144590 | | - && iCol>=0 |
| 144591 | | -#else |
| 144592 | | - && ALWAYS(iCol>=0) |
| 144593 | | -#endif |
| 144703 | + && (!ViewCanHaveRowid || iCol>=0) |
| 144594 | 144704 | ){ |
| 144595 | 144705 | /* If iCol is less than zero, then the expression requests the |
| 144596 | 144706 | ** rowid of the sub-select or view. This expression is legal (see |
| 144597 | 144707 | ** test case misc2.2.2) - it always evaluates to NULL. |
| 144598 | 144708 | */ |
| | @@ -147762,10 +147872,14 @@ |
| 147762 | 147872 | ** also restriction (6) in sqlite3ExprIsSingleTableConstraint(). |
| 147763 | 147873 | ** |
| 147764 | 147874 | ** (10) The inner query is not the right-hand table of a RIGHT JOIN. |
| 147765 | 147875 | ** |
| 147766 | 147876 | ** (11) The subquery is not a VALUES clause |
| 147877 | +** |
| 147878 | +** (12) The WHERE clause is not "rowid ISNULL" or the equivalent. This |
| 147879 | +** case only comes up if SQLite is compiled using |
| 147880 | +** SQLITE_ALLOW_ROWID_IN_VIEW. |
| 147767 | 147881 | ** |
| 147768 | 147882 | ** Return 0 if no changes are made and non-zero if one or more WHERE clause |
| 147769 | 147883 | ** terms are duplicated into the subquery. |
| 147770 | 147884 | */ |
| 147771 | 147885 | static int pushDownWhereTerms( |
| | @@ -147870,10 +147984,22 @@ |
| 147870 | 147984 | } |
| 147871 | 147985 | if( ExprHasProperty(pWhere,EP_OuterON) |
| 147872 | 147986 | && pWhere->w.iJoin!=iCursor |
| 147873 | 147987 | ){ |
| 147874 | 147988 | return 0; /* restriction (5) */ |
| 147989 | + } |
| 147990 | +#endif |
| 147991 | + |
| 147992 | +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 147993 | + if( ViewCanHaveRowid && (pWhere->op==TK_ISNULL || pWhere->op==TK_NOTNULL) ){ |
| 147994 | + Expr *pLeft = pWhere->pLeft; |
| 147995 | + if( ALWAYS(pLeft) |
| 147996 | + && pLeft->op==TK_COLUMN |
| 147997 | + && pLeft->iColumn < 0 |
| 147998 | + ){ |
| 147999 | + return 0; /* Restriction (12) */ |
| 148000 | + } |
| 147875 | 148001 | } |
| 147876 | 148002 | #endif |
| 147877 | 148003 | |
| 147878 | 148004 | if( sqlite3ExprIsSingleTableConstraint(pWhere, pSrcList, iSrc) ){ |
| 147879 | 148005 | nChng++; |
| | @@ -148500,16 +148626,18 @@ |
| 148500 | 148626 | pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom); |
| 148501 | 148627 | } |
| 148502 | 148628 | while( pSel->pPrior ){ pSel = pSel->pPrior; } |
| 148503 | 148629 | sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol); |
| 148504 | 148630 | pTab->iPKey = -1; |
| 148631 | + pTab->eTabType = TABTYP_VIEW; |
| 148505 | 148632 | pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
| 148506 | 148633 | #ifndef SQLITE_ALLOW_ROWID_IN_VIEW |
| 148507 | 148634 | /* The usual case - do not allow ROWID on a subquery */ |
| 148508 | 148635 | pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid; |
| 148509 | 148636 | #else |
| 148510 | | - pTab->tabFlags |= TF_Ephemeral; /* Legacy compatibility mode */ |
| 148637 | + /* Legacy compatibility mode */ |
| 148638 | + pTab->tabFlags |= TF_Ephemeral | sqlite3Config.mNoVisibleRowid; |
| 148511 | 148639 | #endif |
| 148512 | 148640 | return pParse->nErr ? SQLITE_ERROR : SQLITE_OK; |
| 148513 | 148641 | } |
| 148514 | 148642 | |
| 148515 | 148643 | |
| | @@ -148773,11 +148901,11 @@ |
| 148773 | 148901 | if( pFrom->fg.isNestedFrom ){ |
| 148774 | 148902 | assert( pFrom->pSelect!=0 ); |
| 148775 | 148903 | pNestedFrom = pFrom->pSelect->pEList; |
| 148776 | 148904 | assert( pNestedFrom!=0 ); |
| 148777 | 148905 | assert( pNestedFrom->nExpr==pTab->nCol ); |
| 148778 | | - assert( VisibleRowid(pTab)==0 ); |
| 148906 | + assert( VisibleRowid(pTab)==0 || ViewCanHaveRowid ); |
| 148779 | 148907 | }else{ |
| 148780 | 148908 | if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){ |
| 148781 | 148909 | continue; |
| 148782 | 148910 | } |
| 148783 | 148911 | pNestedFrom = 0; |
| | @@ -148805,11 +148933,12 @@ |
| 148805 | 148933 | } |
| 148806 | 148934 | }else{ |
| 148807 | 148935 | pUsing = 0; |
| 148808 | 148936 | } |
| 148809 | 148937 | |
| 148810 | | - nAdd = pTab->nCol + (VisibleRowid(pTab) && (selFlags&SF_NestedFrom)); |
| 148938 | + nAdd = pTab->nCol; |
| 148939 | + if( VisibleRowid(pTab) && (selFlags & SF_NestedFrom)!=0 ) nAdd++; |
| 148811 | 148940 | for(j=0; j<nAdd; j++){ |
| 148812 | 148941 | const char *zName; |
| 148813 | 148942 | struct ExprList_item *pX; /* Newly added ExprList term */ |
| 148814 | 148943 | |
| 148815 | 148944 | if( j==pTab->nCol ){ |
| | @@ -148887,11 +149016,12 @@ |
| 148887 | 149016 | break; /* OOM */ |
| 148888 | 149017 | } |
| 148889 | 149018 | pX = &pNew->a[pNew->nExpr-1]; |
| 148890 | 149019 | assert( pX->zEName==0 ); |
| 148891 | 149020 | if( (selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT ){ |
| 148892 | | - if( pNestedFrom ){ |
| 149021 | + if( pNestedFrom && (!ViewCanHaveRowid || j<pNestedFrom->nExpr) ){ |
| 149022 | + assert( j<pNestedFrom->nExpr ); |
| 148893 | 149023 | pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName); |
| 148894 | 149024 | testcase( pX->zEName==0 ); |
| 148895 | 149025 | }else{ |
| 148896 | 149026 | pX->zEName = sqlite3MPrintf(db, "%s.%s.%s", |
| 148897 | 149027 | zSchemaName, zTabName, zName); |
| | @@ -155837,23 +155967,39 @@ |
| 155837 | 155967 | VtabCtx *pCtx; |
| 155838 | 155968 | int rc = SQLITE_OK; |
| 155839 | 155969 | Table *pTab; |
| 155840 | 155970 | Parse sParse; |
| 155841 | 155971 | int initBusy; |
| 155972 | + int i; |
| 155973 | + const unsigned char *z; |
| 155974 | + static const u8 aKeyword[] = { TK_CREATE, TK_TABLE, 0 }; |
| 155842 | 155975 | |
| 155843 | 155976 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 155844 | 155977 | if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){ |
| 155845 | 155978 | return SQLITE_MISUSE_BKPT; |
| 155846 | 155979 | } |
| 155847 | 155980 | #endif |
| 155981 | + |
| 155982 | + /* Verify that the first two keywords in the CREATE TABLE statement |
| 155983 | + ** really are "CREATE" and "TABLE". If this is not the case, then |
| 155984 | + ** sqlite3_declare_vtab() is being misused. |
| 155985 | + */ |
| 155986 | + z = (const unsigned char*)zCreateTable; |
| 155987 | + for(i=0; aKeyword[i]; i++){ |
| 155988 | + int tokenType = 0; |
| 155989 | + do{ z += sqlite3GetToken(z, &tokenType); }while( tokenType==TK_SPACE ); |
| 155990 | + if( tokenType!=aKeyword[i] ) return SQLITE_MISUSE_BKPT; |
| 155991 | + } |
| 155992 | + |
| 155848 | 155993 | sqlite3_mutex_enter(db->mutex); |
| 155849 | 155994 | pCtx = db->pVtabCtx; |
| 155850 | 155995 | if( !pCtx || pCtx->bDeclared ){ |
| 155851 | 155996 | sqlite3Error(db, SQLITE_MISUSE_BKPT); |
| 155852 | 155997 | sqlite3_mutex_leave(db->mutex); |
| 155853 | 155998 | return SQLITE_MISUSE_BKPT; |
| 155854 | 155999 | } |
| 156000 | + |
| 155855 | 156001 | pTab = pCtx->pTab; |
| 155856 | 156002 | assert( IsVirtual(pTab) ); |
| 155857 | 156003 | |
| 155858 | 156004 | sqlite3ParseObjectInit(&sParse, db); |
| 155859 | 156005 | sParse.eParseMode = PARSE_MODE_DECLARE_VTAB; |
| | @@ -155863,15 +156009,14 @@ |
| 155863 | 156009 | ** in case a bug arises. */ |
| 155864 | 156010 | assert( db->init.busy==0 ); |
| 155865 | 156011 | initBusy = db->init.busy; |
| 155866 | 156012 | db->init.busy = 0; |
| 155867 | 156013 | sParse.nQueryLoop = 1; |
| 155868 | | - if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable) |
| 155869 | | - && ALWAYS(sParse.pNewTable!=0) |
| 155870 | | - && ALWAYS(!db->mallocFailed) |
| 155871 | | - && IsOrdinaryTable(sParse.pNewTable) |
| 155872 | | - ){ |
| 156014 | + if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable) ){ |
| 156015 | + assert( sParse.pNewTable!=0 ); |
| 156016 | + assert( !db->mallocFailed ); |
| 156017 | + assert( IsOrdinaryTable(sParse.pNewTable) ); |
| 155873 | 156018 | assert( sParse.zErrMsg==0 ); |
| 155874 | 156019 | if( !pTab->aCol ){ |
| 155875 | 156020 | Table *pNew = sParse.pNewTable; |
| 155876 | 156021 | Index *pIdx; |
| 155877 | 156022 | pTab->aCol = pNew->aCol; |
| | @@ -162403,13 +162548,17 @@ |
| 162403 | 162548 | ** structure. Used for testing and debugging only. If neither |
| 162404 | 162549 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 162405 | 162550 | ** are no-ops. |
| 162406 | 162551 | */ |
| 162407 | 162552 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
| 162408 | | -static void whereTraceIndexInfoInputs(sqlite3_index_info *p){ |
| 162553 | +static void whereTraceIndexInfoInputs( |
| 162554 | + sqlite3_index_info *p, /* The IndexInfo object */ |
| 162555 | + Table *pTab /* The TABLE that is the virtual table */ |
| 162556 | +){ |
| 162409 | 162557 | int i; |
| 162410 | 162558 | if( (sqlite3WhereTrace & 0x10)==0 ) return; |
| 162559 | + sqlite3DebugPrintf("sqlite3_index_info inputs for %s:\n", pTab->zName); |
| 162411 | 162560 | for(i=0; i<p->nConstraint; i++){ |
| 162412 | 162561 | sqlite3DebugPrintf( |
| 162413 | 162562 | " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n", |
| 162414 | 162563 | i, |
| 162415 | 162564 | p->aConstraint[i].iColumn, |
| | @@ -162423,13 +162572,17 @@ |
| 162423 | 162572 | i, |
| 162424 | 162573 | p->aOrderBy[i].iColumn, |
| 162425 | 162574 | p->aOrderBy[i].desc); |
| 162426 | 162575 | } |
| 162427 | 162576 | } |
| 162428 | | -static void whereTraceIndexInfoOutputs(sqlite3_index_info *p){ |
| 162577 | +static void whereTraceIndexInfoOutputs( |
| 162578 | + sqlite3_index_info *p, /* The IndexInfo object */ |
| 162579 | + Table *pTab /* The TABLE that is the virtual table */ |
| 162580 | +){ |
| 162429 | 162581 | int i; |
| 162430 | 162582 | if( (sqlite3WhereTrace & 0x10)==0 ) return; |
| 162583 | + sqlite3DebugPrintf("sqlite3_index_info outputs for %s:\n", pTab->zName); |
| 162431 | 162584 | for(i=0; i<p->nConstraint; i++){ |
| 162432 | 162585 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 162433 | 162586 | i, |
| 162434 | 162587 | p->aConstraintUsage[i].argvIndex, |
| 162435 | 162588 | p->aConstraintUsage[i].omit); |
| | @@ -162439,12 +162592,12 @@ |
| 162439 | 162592 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 162440 | 162593 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
| 162441 | 162594 | sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows); |
| 162442 | 162595 | } |
| 162443 | 162596 | #else |
| 162444 | | -#define whereTraceIndexInfoInputs(A) |
| 162445 | | -#define whereTraceIndexInfoOutputs(A) |
| 162597 | +#define whereTraceIndexInfoInputs(A,B) |
| 162598 | +#define whereTraceIndexInfoOutputs(A,B) |
| 162446 | 162599 | #endif |
| 162447 | 162600 | |
| 162448 | 162601 | /* |
| 162449 | 162602 | ** We know that pSrc is an operand of an outer join. Return true if |
| 162450 | 162603 | ** pTerm is a constraint that is compatible with that join. |
| | @@ -162666,11 +162819,11 @@ |
| 162666 | 162819 | ** be a covering index because the index will not be updated if the |
| 162667 | 162820 | ** original table changes and the index and table cannot both be used |
| 162668 | 162821 | ** if they go out of sync. |
| 162669 | 162822 | */ |
| 162670 | 162823 | if( IsView(pTable) ){ |
| 162671 | | - extraCols = ALLBITS; |
| 162824 | + extraCols = ALLBITS & ~idxCols; |
| 162672 | 162825 | }else{ |
| 162673 | 162826 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
| 162674 | 162827 | } |
| 162675 | 162828 | mxBitCol = MIN(BMS-1,pTable->nCol); |
| 162676 | 162829 | testcase( pTable->nCol==BMS-1 ); |
| | @@ -163183,15 +163336,15 @@ |
| 163183 | 163336 | */ |
| 163184 | 163337 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
| 163185 | 163338 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
| 163186 | 163339 | int rc; |
| 163187 | 163340 | |
| 163188 | | - whereTraceIndexInfoInputs(p); |
| 163341 | + whereTraceIndexInfoInputs(p, pTab); |
| 163189 | 163342 | pParse->db->nSchemaLock++; |
| 163190 | 163343 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 163191 | 163344 | pParse->db->nSchemaLock--; |
| 163192 | | - whereTraceIndexInfoOutputs(p); |
| 163345 | + whereTraceIndexInfoOutputs(p, pTab); |
| 163193 | 163346 | |
| 163194 | 163347 | if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){ |
| 163195 | 163348 | if( rc==SQLITE_NOMEM ){ |
| 163196 | 163349 | sqlite3OomFault(pParse->db); |
| 163197 | 163350 | }else if( !pVtab->zErrMsg ){ |
| | @@ -179894,10 +180047,22 @@ |
| 179894 | 180047 | case SQLITE_CONFIG_MEMDB_MAXSIZE: { |
| 179895 | 180048 | sqlite3GlobalConfig.mxMemdbSize = va_arg(ap, sqlite3_int64); |
| 179896 | 180049 | break; |
| 179897 | 180050 | } |
| 179898 | 180051 | #endif /* SQLITE_OMIT_DESERIALIZE */ |
| 180052 | + |
| 180053 | + case SQLITE_CONFIG_ROWID_IN_VIEW: { |
| 180054 | + int *pVal = va_arg(ap,int*); |
| 180055 | +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 180056 | + if( 0==*pVal ) sqlite3GlobalConfig.mNoVisibleRowid = TF_NoVisibleRowid; |
| 180057 | + if( 1==*pVal ) sqlite3GlobalConfig.mNoVisibleRowid = 0; |
| 180058 | + *pVal = (sqlite3GlobalConfig.mNoVisibleRowid==0); |
| 180059 | +#else |
| 180060 | + *pVal = 0; |
| 180061 | +#endif |
| 180062 | + break; |
| 180063 | + } |
| 179899 | 180064 | |
| 179900 | 180065 | default: { |
| 179901 | 180066 | rc = SQLITE_ERROR; |
| 179902 | 180067 | break; |
| 179903 | 180068 | } |
| | @@ -217654,10 +217819,11 @@ |
| 217654 | 217819 | #if !defined(SQLITE_AMALGAMATION) |
| 217655 | 217820 | typedef unsigned int u32; |
| 217656 | 217821 | typedef unsigned short u16; |
| 217657 | 217822 | typedef unsigned char u8; |
| 217658 | 217823 | typedef sqlite3_int64 i64; |
| 217824 | +typedef sqlite3_uint64 u64; |
| 217659 | 217825 | #endif |
| 217660 | 217826 | |
| 217661 | 217827 | /* |
| 217662 | 217828 | ** These values must match the values defined in wal.c for the equivalent |
| 217663 | 217829 | ** locks. These are not magic numbers as they are part of the SQLite file |
| | @@ -218340,10 +218506,11 @@ |
| 218340 | 218506 | pIter->bCleanup = 0; |
| 218341 | 218507 | rc = sqlite3_step(pIter->pTblIter); |
| 218342 | 218508 | if( rc!=SQLITE_ROW ){ |
| 218343 | 218509 | rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg); |
| 218344 | 218510 | pIter->zTbl = 0; |
| 218511 | + pIter->zDataTbl = 0; |
| 218345 | 218512 | }else{ |
| 218346 | 218513 | pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0); |
| 218347 | 218514 | pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1); |
| 218348 | 218515 | rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM; |
| 218349 | 218516 | } |
| | @@ -220434,11 +220601,11 @@ |
| 220434 | 220601 | if( p->rc==SQLITE_OK ){ |
| 220435 | 220602 | sqlite3_file *pDb = p->pTargetFd->pReal; |
| 220436 | 220603 | u32 volatile *ptr; |
| 220437 | 220604 | p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr); |
| 220438 | 220605 | if( p->rc==SQLITE_OK ){ |
| 220439 | | - iRet = ((i64)ptr[10] << 32) + ptr[11]; |
| 220606 | + iRet = (i64)(((u64)ptr[10] << 32) + ptr[11]); |
| 220440 | 220607 | } |
| 220441 | 220608 | } |
| 220442 | 220609 | return iRet; |
| 220443 | 220610 | } |
| 220444 | 220611 | |
| | @@ -251802,11 +251969,11 @@ |
| 251802 | 251969 | int nArg, /* Number of args */ |
| 251803 | 251970 | sqlite3_value **apUnused /* Function arguments */ |
| 251804 | 251971 | ){ |
| 251805 | 251972 | assert( nArg==0 ); |
| 251806 | 251973 | UNUSED_PARAM2(nArg, apUnused); |
| 251807 | | - sqlite3_result_text(pCtx, "fts5: 2024-03-18 18:03:17 76fb3a908f45230b956cb659c754e47603e80aa72a2aad50d479437a9c013f61", -1, SQLITE_TRANSIENT); |
| 251974 | + sqlite3_result_text(pCtx, "fts5: 2024-03-26 11:14:52 a49296de0061931badaf3db6b965131a78b1c6c21b1eeb62815ea7adf767d0b3", -1, SQLITE_TRANSIENT); |
| 251808 | 251975 | } |
| 251809 | 251976 | |
| 251810 | 251977 | /* |
| 251811 | 251978 | ** Return true if zName is the extension on one of the shadow tables used |
| 251812 | 251979 | ** by this module. |
| 251813 | 251980 | |