| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.47.0. By combining all the individual C code files into this |
| 3 | +** version 3.48.0. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -16,12 +16,15 @@ |
| 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 | | -** 03a9703e27c44437c39363d0baf82db4ebc9. |
| 21 | +** 2b17bc49655c577029919c2d409de994b0d2 with changes in files: |
| 22 | +** |
| 23 | +** |
| 22 | 24 | */ |
| 25 | +#ifndef SQLITE_AMALGAMATION |
| 23 | 26 | #define SQLITE_CORE 1 |
| 24 | 27 | #define SQLITE_AMALGAMATION 1 |
| 25 | 28 | #ifndef SQLITE_PRIVATE |
| 26 | 29 | # define SQLITE_PRIVATE static |
| 27 | 30 | #endif |
| | @@ -460,13 +463,13 @@ |
| 460 | 463 | ** |
| 461 | 464 | ** See also: [sqlite3_libversion()], |
| 462 | 465 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 463 | 466 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 464 | 467 | */ |
| 465 | | -#define SQLITE_VERSION "3.47.0" |
| 466 | | -#define SQLITE_VERSION_NUMBER 3047000 |
| 467 | | -#define SQLITE_SOURCE_ID "2024-10-21 16:30:22 03a9703e27c44437c39363d0baf82db4ebc94538a0f28411c85dda156f82636e" |
| 468 | +#define SQLITE_VERSION "3.48.0" |
| 469 | +#define SQLITE_VERSION_NUMBER 3048000 |
| 470 | +#define SQLITE_SOURCE_ID "2024-12-30 21:23:53 2b17bc49655c577029919c2d409de994b0d252f8efb5da1ba0913f2c96bee552" |
| 468 | 471 | |
| 469 | 472 | /* |
| 470 | 473 | ** CAPI3REF: Run-Time Library Version Numbers |
| 471 | 474 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 472 | 475 | ** |
| | @@ -966,10 +969,17 @@ |
| 966 | 969 | ** |
| 967 | 970 | ** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying |
| 968 | 971 | ** filesystem supports doing multiple write operations atomically when those |
| 969 | 972 | ** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and |
| 970 | 973 | ** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. |
| 974 | +** |
| 975 | +** The SQLITE_IOCAP_SUBPAGE_READ property means that it is ok to read |
| 976 | +** from the database file in amounts that are not a multiple of the |
| 977 | +** page size and that do not begin at a page boundary. Without this |
| 978 | +** property, SQLite is careful to only do full-page reads and write |
| 979 | +** on aligned pages, with the one exception that it will do a sub-page |
| 980 | +** read of the first page to access the database header. |
| 971 | 981 | */ |
| 972 | 982 | #define SQLITE_IOCAP_ATOMIC 0x00000001 |
| 973 | 983 | #define SQLITE_IOCAP_ATOMIC512 0x00000002 |
| 974 | 984 | #define SQLITE_IOCAP_ATOMIC1K 0x00000004 |
| 975 | 985 | #define SQLITE_IOCAP_ATOMIC2K 0x00000008 |
| | @@ -982,10 +992,11 @@ |
| 982 | 992 | #define SQLITE_IOCAP_SEQUENTIAL 0x00000400 |
| 983 | 993 | #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800 |
| 984 | 994 | #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000 |
| 985 | 995 | #define SQLITE_IOCAP_IMMUTABLE 0x00002000 |
| 986 | 996 | #define SQLITE_IOCAP_BATCH_ATOMIC 0x00004000 |
| 997 | +#define SQLITE_IOCAP_SUBPAGE_READ 0x00008000 |
| 987 | 998 | |
| 988 | 999 | /* |
| 989 | 1000 | ** CAPI3REF: File Locking Levels |
| 990 | 1001 | ** |
| 991 | 1002 | ** SQLite uses one of these integer values as the second |
| | @@ -1128,10 +1139,11 @@ |
| 1128 | 1139 | ** <li> [SQLITE_IOCAP_SEQUENTIAL] |
| 1129 | 1140 | ** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN] |
| 1130 | 1141 | ** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE] |
| 1131 | 1142 | ** <li> [SQLITE_IOCAP_IMMUTABLE] |
| 1132 | 1143 | ** <li> [SQLITE_IOCAP_BATCH_ATOMIC] |
| 1144 | +** <li> [SQLITE_IOCAP_SUBPAGE_READ] |
| 1133 | 1145 | ** </ul> |
| 1134 | 1146 | ** |
| 1135 | 1147 | ** The SQLITE_IOCAP_ATOMIC property means that all writes of |
| 1136 | 1148 | ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values |
| 1137 | 1149 | ** mean that writes of blocks that are nnn bytes in size and |
| | @@ -1405,10 +1417,15 @@ |
| 1405 | 1417 | ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This |
| 1406 | 1418 | ** opcode causes the xFileControl method to swap the file handle with the one |
| 1407 | 1419 | ** pointed to by the pArg argument. This capability is used during testing |
| 1408 | 1420 | ** and only needs to be supported when SQLITE_TEST is defined. |
| 1409 | 1421 | ** |
| 1422 | +** <li>[[SQLITE_FCNTL_NULL_IO]] |
| 1423 | +** The [SQLITE_FCNTL_NULL_IO] opcode sets the low-level file descriptor |
| 1424 | +** or file handle for the [sqlite3_file] object such that it will no longer |
| 1425 | +** read or write to the database file. |
| 1426 | +** |
| 1410 | 1427 | ** <li>[[SQLITE_FCNTL_WAL_BLOCK]] |
| 1411 | 1428 | ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might |
| 1412 | 1429 | ** be advantageous to block on the next WAL lock if the lock is not immediately |
| 1413 | 1430 | ** available. The WAL subsystem issues this signal during rare |
| 1414 | 1431 | ** circumstances in order to fix a problem with priority inversion. |
| | @@ -1558,10 +1575,11 @@ |
| 1558 | 1575 | #define SQLITE_FCNTL_RESERVE_BYTES 38 |
| 1559 | 1576 | #define SQLITE_FCNTL_CKPT_START 39 |
| 1560 | 1577 | #define SQLITE_FCNTL_EXTERNAL_READER 40 |
| 1561 | 1578 | #define SQLITE_FCNTL_CKSM_FILE 41 |
| 1562 | 1579 | #define SQLITE_FCNTL_RESET_CACHE 42 |
| 1580 | +#define SQLITE_FCNTL_NULL_IO 43 |
| 1563 | 1581 | |
| 1564 | 1582 | /* deprecated names */ |
| 1565 | 1583 | #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE |
| 1566 | 1584 | #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE |
| 1567 | 1585 | #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO |
| | @@ -2936,14 +2954,18 @@ |
| 2936 | 2954 | ** |
| 2937 | 2955 | ** ^These functions return the number of rows modified, inserted or |
| 2938 | 2956 | ** deleted by the most recently completed INSERT, UPDATE or DELETE |
| 2939 | 2957 | ** statement on the database connection specified by the only parameter. |
| 2940 | 2958 | ** The two functions are identical except for the type of the return value |
| 2941 | | -** and that if the number of rows modified by the most recent INSERT, UPDATE |
| 2959 | +** and that if the number of rows modified by the most recent INSERT, UPDATE, |
| 2942 | 2960 | ** or DELETE is greater than the maximum value supported by type "int", then |
| 2943 | 2961 | ** the return value of sqlite3_changes() is undefined. ^Executing any other |
| 2944 | 2962 | ** type of SQL statement does not modify the value returned by these functions. |
| 2963 | +** For the purposes of this interface, a CREATE TABLE AS SELECT statement |
| 2964 | +** does not count as an INSERT, UPDATE or DELETE statement and hence the rows |
| 2965 | +** added to the new table by the CREATE TABLE AS SELECT statement are not |
| 2966 | +** counted. |
| 2945 | 2967 | ** |
| 2946 | 2968 | ** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are |
| 2947 | 2969 | ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], |
| 2948 | 2970 | ** [foreign key actions] or [REPLACE] constraint resolution are not counted. |
| 2949 | 2971 | ** |
| | @@ -4499,15 +4521,26 @@ |
| 4499 | 4521 | ** |
| 4500 | 4522 | ** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt> |
| 4501 | 4523 | ** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler |
| 4502 | 4524 | ** to return an error (error code SQLITE_ERROR) if the statement uses |
| 4503 | 4525 | ** any virtual tables. |
| 4526 | +** |
| 4527 | +** [[SQLITE_PREPARE_DONT_LOG]] <dt>SQLITE_PREPARE_DONT_LOG</dt> |
| 4528 | +** <dd>The SQLITE_PREPARE_DONT_LOG flag prevents SQL compiler |
| 4529 | +** errors from being sent to the error log defined by |
| 4530 | +** [SQLITE_CONFIG_LOG]. This can be used, for example, to do test |
| 4531 | +** compiles to see if some SQL syntax is well-formed, without generating |
| 4532 | +** messages on the global error log when it is not. If the test compile |
| 4533 | +** fails, the sqlite3_prepare_v3() call returns the same error indications |
| 4534 | +** with or without this flag; it just omits the call to [sqlite3_log()] that |
| 4535 | +** logs the error. |
| 4504 | 4536 | ** </dl> |
| 4505 | 4537 | */ |
| 4506 | 4538 | #define SQLITE_PREPARE_PERSISTENT 0x01 |
| 4507 | 4539 | #define SQLITE_PREPARE_NORMALIZE 0x02 |
| 4508 | 4540 | #define SQLITE_PREPARE_NO_VTAB 0x04 |
| 4541 | +#define SQLITE_PREPARE_DONT_LOG 0x10 |
| 4509 | 4542 | |
| 4510 | 4543 | /* |
| 4511 | 4544 | ** CAPI3REF: Compiling An SQL Statement |
| 4512 | 4545 | ** KEYWORDS: {SQL statement compiler} |
| 4513 | 4546 | ** METHOD: sqlite3 |
| | @@ -11194,11 +11227,11 @@ |
| 11194 | 11227 | #endif |
| 11195 | 11228 | |
| 11196 | 11229 | #if 0 |
| 11197 | 11230 | } /* End of the 'extern "C"' block */ |
| 11198 | 11231 | #endif |
| 11199 | | -#endif /* SQLITE3_H */ |
| 11232 | +/* #endif for SQLITE3_H will be added by mksqlite3.tcl */ |
| 11200 | 11233 | |
| 11201 | 11234 | /******** Begin file sqlite3rtree.h *********/ |
| 11202 | 11235 | /* |
| 11203 | 11236 | ** 2010 August 30 |
| 11204 | 11237 | ** |
| | @@ -13445,17 +13478,32 @@ |
| 13445 | 13478 | ** This is used to access token iToken of phrase hit iIdx within the |
| 13446 | 13479 | ** current row. If iIdx is less than zero or greater than or equal to the |
| 13447 | 13480 | ** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise, |
| 13448 | 13481 | ** output variable (*ppToken) is set to point to a buffer containing the |
| 13449 | 13482 | ** matching document token, and (*pnToken) to the size of that buffer in |
| 13450 | | -** bytes. This API is not available if the specified token matches a |
| 13451 | | -** prefix query term. In that case both output variables are always set |
| 13452 | | -** to 0. |
| 13483 | +** bytes. |
| 13453 | 13484 | ** |
| 13454 | 13485 | ** The output text is not a copy of the document text that was tokenized. |
| 13455 | 13486 | ** It is the output of the tokenizer module. For tokendata=1 tables, this |
| 13456 | 13487 | ** includes any embedded 0x00 and trailing data. |
| 13488 | +** |
| 13489 | +** This API may be slow in some cases if the token identified by parameters |
| 13490 | +** iIdx and iToken matched a prefix token in the query. In most cases, the |
| 13491 | +** first call to this API for each prefix token in the query is forced |
| 13492 | +** to scan the portion of the full-text index that matches the prefix |
| 13493 | +** token to collect the extra data required by this API. If the prefix |
| 13494 | +** token matches a large number of token instances in the document set, |
| 13495 | +** this may be a performance problem. |
| 13496 | +** |
| 13497 | +** If the user knows in advance that a query may use this API for a |
| 13498 | +** prefix token, FTS5 may be configured to collect all required data as part |
| 13499 | +** of the initial querying of the full-text index, avoiding the second scan |
| 13500 | +** entirely. This also causes prefix queries that do not use this API to |
| 13501 | +** run more slowly and use more memory. FTS5 may be configured in this way |
| 13502 | +** either on a per-table basis using the [FTS5 insttoken | 'insttoken'] |
| 13503 | +** option, or on a per-query basis using the |
| 13504 | +** [fts5_insttoken | fts5_insttoken()] user function. |
| 13457 | 13505 | ** |
| 13458 | 13506 | ** This API can be quite slow if used with an FTS5 table created with the |
| 13459 | 13507 | ** "detail=none" or "detail=column" option. |
| 13460 | 13508 | ** |
| 13461 | 13509 | ** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale) |
| | @@ -13886,10 +13934,11 @@ |
| 13886 | 13934 | #endif |
| 13887 | 13935 | |
| 13888 | 13936 | #endif /* _FTS5_H */ |
| 13889 | 13937 | |
| 13890 | 13938 | /******** End of fts5.h *********/ |
| 13939 | +#endif /* SQLITE3_H */ |
| 13891 | 13940 | |
| 13892 | 13941 | /************** End of sqlite3.h *********************************************/ |
| 13893 | 13942 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 13894 | 13943 | |
| 13895 | 13944 | /* |
| | @@ -13931,10 +13980,11 @@ |
| 13931 | 13980 | ** to count the size: 2^31-1 or 2147483647. |
| 13932 | 13981 | */ |
| 13933 | 13982 | #ifndef SQLITE_MAX_LENGTH |
| 13934 | 13983 | # define SQLITE_MAX_LENGTH 1000000000 |
| 13935 | 13984 | #endif |
| 13985 | +#define SQLITE_MIN_LENGTH 30 /* Minimum value for the length limit */ |
| 13936 | 13986 | |
| 13937 | 13987 | /* |
| 13938 | 13988 | ** This is the maximum number of |
| 13939 | 13989 | ** |
| 13940 | 13990 | ** * Columns in a table |
| | @@ -13996,13 +14046,17 @@ |
| 13996 | 14046 | # define SQLITE_MAX_VDBE_OP 250000000 |
| 13997 | 14047 | #endif |
| 13998 | 14048 | |
| 13999 | 14049 | /* |
| 14000 | 14050 | ** The maximum number of arguments to an SQL function. |
| 14051 | +** |
| 14052 | +** This value has a hard upper limit of 32767 due to storage |
| 14053 | +** constraints (it needs to fit inside a i16). We keep it |
| 14054 | +** lower than that to prevent abuse. |
| 14001 | 14055 | */ |
| 14002 | 14056 | #ifndef SQLITE_MAX_FUNCTION_ARG |
| 14003 | | -# define SQLITE_MAX_FUNCTION_ARG 127 |
| 14057 | +# define SQLITE_MAX_FUNCTION_ARG 1000 |
| 14004 | 14058 | #endif |
| 14005 | 14059 | |
| 14006 | 14060 | /* |
| 14007 | 14061 | ** The suggested maximum number of in-memory pages to use for |
| 14008 | 14062 | ** the main database table and for temporary tables. |
| | @@ -16000,10 +16054,26 @@ |
| 16000 | 16054 | #define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */ |
| 16001 | 16055 | #define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */ |
| 16002 | 16056 | #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */ |
| 16003 | 16057 | #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */ |
| 16004 | 16058 | |
| 16059 | +#define isWalMode(x) ((x)==PAGER_JOURNALMODE_WAL) |
| 16060 | + |
| 16061 | +/* |
| 16062 | +** The argument to this macro is a file descriptor (type sqlite3_file*). |
| 16063 | +** Return 0 if it is not open, or non-zero (but not 1) if it is. |
| 16064 | +** |
| 16065 | +** This is so that expressions can be written as: |
| 16066 | +** |
| 16067 | +** if( isOpen(pPager->jfd) ){ ... |
| 16068 | +** |
| 16069 | +** instead of |
| 16070 | +** |
| 16071 | +** if( pPager->jfd->pMethods ){ ... |
| 16072 | +*/ |
| 16073 | +#define isOpen(pFd) ((pFd)->pMethods!=0) |
| 16074 | + |
| 16005 | 16075 | /* |
| 16006 | 16076 | ** Flags that make up the mask passed to sqlite3PagerGet(). |
| 16007 | 16077 | */ |
| 16008 | 16078 | #define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */ |
| 16009 | 16079 | #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */ |
| | @@ -17025,11 +17095,11 @@ |
| 17025 | 17095 | |
| 17026 | 17096 | /* |
| 17027 | 17097 | ** Additional non-public SQLITE_PREPARE_* flags |
| 17028 | 17098 | */ |
| 17029 | 17099 | #define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */ |
| 17030 | | -#define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */ |
| 17100 | +#define SQLITE_PREPARE_MASK 0x1f /* Mask of public flags */ |
| 17031 | 17101 | |
| 17032 | 17102 | /* |
| 17033 | 17103 | ** Prototypes for the VDBE interface. See comments on the implementation |
| 17034 | 17104 | ** for a description of what each of these routines does. |
| 17035 | 17105 | */ |
| | @@ -17740,51 +17810,15 @@ |
| 17740 | 17810 | struct FuncDefHash { |
| 17741 | 17811 | FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */ |
| 17742 | 17812 | }; |
| 17743 | 17813 | #define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ) |
| 17744 | 17814 | |
| 17745 | | -#if defined(SQLITE_USER_AUTHENTICATION) |
| 17746 | | -# warning "The SQLITE_USER_AUTHENTICATION extension is deprecated. \ |
| 17747 | | - See ext/userauth/user-auth.txt for details." |
| 17748 | | -#endif |
| 17749 | | -#ifdef SQLITE_USER_AUTHENTICATION |
| 17750 | | -/* |
| 17751 | | -** Information held in the "sqlite3" database connection object and used |
| 17752 | | -** to manage user authentication. |
| 17753 | | -*/ |
| 17754 | | -typedef struct sqlite3_userauth sqlite3_userauth; |
| 17755 | | -struct sqlite3_userauth { |
| 17756 | | - u8 authLevel; /* Current authentication level */ |
| 17757 | | - int nAuthPW; /* Size of the zAuthPW in bytes */ |
| 17758 | | - char *zAuthPW; /* Password used to authenticate */ |
| 17759 | | - char *zAuthUser; /* User name used to authenticate */ |
| 17760 | | -}; |
| 17761 | | - |
| 17762 | | -/* Allowed values for sqlite3_userauth.authLevel */ |
| 17763 | | -#define UAUTH_Unknown 0 /* Authentication not yet checked */ |
| 17764 | | -#define UAUTH_Fail 1 /* User authentication failed */ |
| 17765 | | -#define UAUTH_User 2 /* Authenticated as a normal user */ |
| 17766 | | -#define UAUTH_Admin 3 /* Authenticated as an administrator */ |
| 17767 | | - |
| 17768 | | -/* Functions used only by user authorization logic */ |
| 17769 | | -SQLITE_PRIVATE int sqlite3UserAuthTable(const char*); |
| 17770 | | -SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*); |
| 17771 | | -SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*); |
| 17772 | | -SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**); |
| 17773 | | - |
| 17774 | | -#endif /* SQLITE_USER_AUTHENTICATION */ |
| 17775 | | - |
| 17776 | 17815 | /* |
| 17777 | 17816 | ** typedef for the authorization callback function. |
| 17778 | 17817 | */ |
| 17779 | | -#ifdef SQLITE_USER_AUTHENTICATION |
| 17780 | | - typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*, |
| 17781 | | - const char*, const char*); |
| 17782 | | -#else |
| 17783 | | - typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*, |
| 17784 | | - const char*); |
| 17785 | | -#endif |
| 17818 | +typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*, |
| 17819 | + const char*); |
| 17786 | 17820 | |
| 17787 | 17821 | #ifndef SQLITE_OMIT_DEPRECATED |
| 17788 | 17822 | /* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing |
| 17789 | 17823 | ** in the style of sqlite3_trace() |
| 17790 | 17824 | */ |
| | @@ -17941,13 +17975,10 @@ |
| 17941 | 17975 | sqlite3 *pUnlockConnection; /* Connection to watch for unlock */ |
| 17942 | 17976 | void *pUnlockArg; /* Argument to xUnlockNotify */ |
| 17943 | 17977 | void (*xUnlockNotify)(void **, int); /* Unlock notify callback */ |
| 17944 | 17978 | sqlite3 *pNextBlocked; /* Next in list of all blocked connections */ |
| 17945 | 17979 | #endif |
| 17946 | | -#ifdef SQLITE_USER_AUTHENTICATION |
| 17947 | | - sqlite3_userauth auth; /* User authentication information */ |
| 17948 | | -#endif |
| 17949 | 17980 | }; |
| 17950 | 17981 | |
| 17951 | 17982 | /* |
| 17952 | 17983 | ** A macro to discover the encoding of a database. |
| 17953 | 17984 | */ |
| | @@ -18102,11 +18133,11 @@ |
| 18102 | 18133 | ** |
| 18103 | 18134 | ** The u.pHash field is used by the global built-ins. The u.pDestructor |
| 18104 | 18135 | ** field is used by per-connection app-def functions. |
| 18105 | 18136 | */ |
| 18106 | 18137 | struct FuncDef { |
| 18107 | | - i8 nArg; /* Number of arguments. -1 means unlimited */ |
| 18138 | + i16 nArg; /* Number of arguments. -1 means unlimited */ |
| 18108 | 18139 | u32 funcFlags; /* Some combination of SQLITE_FUNC_* */ |
| 18109 | 18140 | void *pUserData; /* User data parameter */ |
| 18110 | 18141 | FuncDef *pNext; /* Next function with same name */ |
| 18111 | 18142 | void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */ |
| 18112 | 18143 | void (*xFinalize)(sqlite3_context*); /* Agg finalizer */ |
| | @@ -22850,13 +22881,10 @@ |
| 22850 | 22881 | "UNLINK_AFTER_CLOSE", |
| 22851 | 22882 | #endif |
| 22852 | 22883 | #ifdef SQLITE_UNTESTABLE |
| 22853 | 22884 | "UNTESTABLE", |
| 22854 | 22885 | #endif |
| 22855 | | -#ifdef SQLITE_USER_AUTHENTICATION |
| 22856 | | - "USER_AUTHENTICATION", |
| 22857 | | -#endif |
| 22858 | 22886 | #ifdef SQLITE_USE_ALLOCA |
| 22859 | 22887 | "USE_ALLOCA", |
| 22860 | 22888 | #endif |
| 22861 | 22889 | #ifdef SQLITE_USE_FCNTL_TRACE |
| 22862 | 22890 | "USE_FCNTL_TRACE", |
| | @@ -23700,11 +23728,11 @@ |
| 23700 | 23728 | Vdbe *pVdbe; /* The VM that owns this context */ |
| 23701 | 23729 | int iOp; /* Instruction number of OP_Function */ |
| 23702 | 23730 | int isError; /* Error code returned by the function. */ |
| 23703 | 23731 | u8 enc; /* Encoding to use for results */ |
| 23704 | 23732 | u8 skipFlag; /* Skip accumulator loading if true */ |
| 23705 | | - u8 argc; /* Number of arguments */ |
| 23733 | + u16 argc; /* Number of arguments */ |
| 23706 | 23734 | sqlite3_value *argv[1]; /* Argument set */ |
| 23707 | 23735 | }; |
| 23708 | 23736 | |
| 23709 | 23737 | /* A bitfield type for use inside of structures. Always follow with :N where |
| 23710 | 23738 | ** N is the number of bits. |
| | @@ -23847,10 +23875,11 @@ |
| 23847 | 23875 | UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */ |
| 23848 | 23876 | int iNewReg; /* Register for new.* values */ |
| 23849 | 23877 | int iBlobWrite; /* Value returned by preupdate_blobwrite() */ |
| 23850 | 23878 | i64 iKey1; /* First key value passed to hook */ |
| 23851 | 23879 | i64 iKey2; /* Second key value passed to hook */ |
| 23880 | + Mem oldipk; /* Memory cell holding "old" IPK value */ |
| 23852 | 23881 | Mem *aNew; /* Array of new.* values */ |
| 23853 | 23882 | Table *pTab; /* Schema object being updated */ |
| 23854 | 23883 | Index *pPk; /* PK index if pTab is WITHOUT ROWID */ |
| 23855 | 23884 | sqlite3_value **apDflt; /* Array of default values, if required */ |
| 23856 | 23885 | }; |
| | @@ -32296,10 +32325,11 @@ |
| 32296 | 32325 | && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0) |
| 32297 | 32326 | ){ |
| 32298 | 32327 | pExpr = pExpr->pLeft; |
| 32299 | 32328 | } |
| 32300 | 32329 | if( pExpr==0 ) return; |
| 32330 | + if( ExprHasProperty(pExpr, EP_FromDDL) ) return; |
| 32301 | 32331 | db->errByteOffset = pExpr->w.iOfst; |
| 32302 | 32332 | } |
| 32303 | 32333 | |
| 32304 | 32334 | /* |
| 32305 | 32335 | ** Enlarge the memory allocation on a StrAccum object so that it is |
| | @@ -33025,11 +33055,11 @@ |
| 33025 | 33055 | } |
| 33026 | 33056 | if( pItem->fg.isCte ){ |
| 33027 | 33057 | sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse); |
| 33028 | 33058 | } |
| 33029 | 33059 | if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){ |
| 33030 | | - sqlite3_str_appendf(&x, " ON"); |
| 33060 | + sqlite3_str_appendf(&x, " isOn"); |
| 33031 | 33061 | } |
| 33032 | 33062 | if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc"); |
| 33033 | 33063 | if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated"); |
| 33034 | 33064 | if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized"); |
| 33035 | 33065 | if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine"); |
| | @@ -34109,10 +34139,14 @@ |
| 34109 | 34139 | ** |
| 34110 | 34140 | ** This routines are given external linkage so that they will always be |
| 34111 | 34141 | ** accessible to the debugging, and to avoid warnings about unused |
| 34112 | 34142 | ** functions. But these routines only exist in debugging builds, so they |
| 34113 | 34143 | ** do not contaminate the interface. |
| 34144 | +** |
| 34145 | +** See Also: |
| 34146 | +** |
| 34147 | +** sqlite3ShowWhereTerm() in where.c |
| 34114 | 34148 | */ |
| 34115 | 34149 | SQLITE_PRIVATE void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); } |
| 34116 | 34150 | SQLITE_PRIVATE void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);} |
| 34117 | 34151 | SQLITE_PRIVATE void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); } |
| 34118 | 34152 | SQLITE_PRIVATE void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); } |
| | @@ -35685,12 +35719,12 @@ |
| 35685 | 35719 | int esign = 1; /* sign of exponent */ |
| 35686 | 35720 | int e = 0; /* exponent */ |
| 35687 | 35721 | int eValid = 1; /* True exponent is either not used or is well-formed */ |
| 35688 | 35722 | int nDigit = 0; /* Number of digits processed */ |
| 35689 | 35723 | int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ |
| 35724 | + u64 s2; /* round-tripped significand */ |
| 35690 | 35725 | double rr[2]; |
| 35691 | | - u64 s2; |
| 35692 | 35726 | |
| 35693 | 35727 | assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
| 35694 | 35728 | *pResult = 0.0; /* Default return value, in case of an error */ |
| 35695 | 35729 | if( length==0 ) return 0; |
| 35696 | 35730 | |
| | @@ -35789,25 +35823,36 @@ |
| 35789 | 35823 | |
| 35790 | 35824 | /* adjust exponent by d, and update sign */ |
| 35791 | 35825 | e = (e*esign) + d; |
| 35792 | 35826 | |
| 35793 | 35827 | /* Try to adjust the exponent to make it smaller */ |
| 35794 | | - while( e>0 && s<(LARGEST_UINT64/10) ){ |
| 35828 | + while( e>0 && s<((LARGEST_UINT64-0x7ff)/10) ){ |
| 35795 | 35829 | s *= 10; |
| 35796 | 35830 | e--; |
| 35797 | 35831 | } |
| 35798 | 35832 | while( e<0 && (s%10)==0 ){ |
| 35799 | 35833 | s /= 10; |
| 35800 | 35834 | e++; |
| 35801 | 35835 | } |
| 35802 | 35836 | |
| 35803 | 35837 | rr[0] = (double)s; |
| 35804 | | - s2 = (u64)rr[0]; |
| 35805 | | -#if defined(_MSC_VER) && _MSC_VER<1700 |
| 35806 | | - if( s2==0x8000000000000000LL ){ s2 = 2*(u64)(0.5*rr[0]); } |
| 35838 | + assert( sizeof(s2)==sizeof(rr[0]) ); |
| 35839 | +#ifdef SQLITE_DEBUG |
| 35840 | + rr[1] = 18446744073709549568.0; |
| 35841 | + memcpy(&s2, &rr[1], sizeof(s2)); |
| 35842 | + assert( s2==0x43efffffffffffffLL ); |
| 35807 | 35843 | #endif |
| 35808 | | - rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s); |
| 35844 | + /* Largest double that can be safely converted to u64 |
| 35845 | + ** vvvvvvvvvvvvvvvvvvvvvv */ |
| 35846 | + if( rr[0]<=18446744073709549568.0 ){ |
| 35847 | + s2 = (u64)rr[0]; |
| 35848 | + rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s); |
| 35849 | + }else{ |
| 35850 | + rr[1] = 0.0; |
| 35851 | + } |
| 35852 | + assert( rr[1]<=1.0e-10*rr[0] ); /* Equal only when rr[0]==0.0 */ |
| 35853 | + |
| 35809 | 35854 | if( e>0 ){ |
| 35810 | 35855 | while( e>=100 ){ |
| 35811 | 35856 | e -= 100; |
| 35812 | 35857 | dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); |
| 35813 | 35858 | } |
| | @@ -38674,11 +38719,11 @@ |
| 38674 | 38719 | # define F_SETLKW 7 |
| 38675 | 38720 | # endif |
| 38676 | 38721 | # endif |
| 38677 | 38722 | #else /* !SQLITE_WASI */ |
| 38678 | 38723 | # ifndef HAVE_FCHMOD |
| 38679 | | -# define HAVE_FCHMOD |
| 38724 | +# define HAVE_FCHMOD 1 |
| 38680 | 38725 | # endif |
| 38681 | 38726 | #endif /* SQLITE_WASI */ |
| 38682 | 38727 | |
| 38683 | 38728 | #ifdef SQLITE_WASI |
| 38684 | 38729 | # define osGetpid(X) (pid_t)1 |
| | @@ -42448,10 +42493,15 @@ |
| 42448 | 42493 | int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE); |
| 42449 | 42494 | return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK; |
| 42450 | 42495 | } |
| 42451 | 42496 | #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ |
| 42452 | 42497 | |
| 42498 | + case SQLITE_FCNTL_NULL_IO: { |
| 42499 | + osClose(pFile->h); |
| 42500 | + pFile->h = -1; |
| 42501 | + return SQLITE_OK; |
| 42502 | + } |
| 42453 | 42503 | case SQLITE_FCNTL_LOCKSTATE: { |
| 42454 | 42504 | *(int*)pArg = pFile->eFileLock; |
| 42455 | 42505 | return SQLITE_OK; |
| 42456 | 42506 | } |
| 42457 | 42507 | case SQLITE_FCNTL_LAST_ERRNO: { |
| | @@ -42589,10 +42639,11 @@ |
| 42589 | 42639 | |
| 42590 | 42640 | /* Set the POWERSAFE_OVERWRITE flag if requested. */ |
| 42591 | 42641 | if( pFd->ctrlFlags & UNIXFILE_PSOW ){ |
| 42592 | 42642 | pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 42593 | 42643 | } |
| 42644 | + pFd->deviceCharacteristics |= SQLITE_IOCAP_SUBPAGE_READ; |
| 42594 | 42645 | |
| 42595 | 42646 | pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 42596 | 42647 | } |
| 42597 | 42648 | } |
| 42598 | 42649 | #else |
| | @@ -50328,10 +50379,15 @@ |
| 50328 | 50379 | OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n", |
| 50329 | 50380 | hOldFile, pFile->h)); |
| 50330 | 50381 | return SQLITE_OK; |
| 50331 | 50382 | } |
| 50332 | 50383 | #endif |
| 50384 | + case SQLITE_FCNTL_NULL_IO: { |
| 50385 | + (void)osCloseHandle(pFile->h); |
| 50386 | + pFile->h = NULL; |
| 50387 | + return SQLITE_OK; |
| 50388 | + } |
| 50333 | 50389 | case SQLITE_FCNTL_TEMPFILENAME: { |
| 50334 | 50390 | char *zTFile = 0; |
| 50335 | 50391 | int rc = winGetTempname(pFile->pVfs, &zTFile); |
| 50336 | 50392 | if( rc==SQLITE_OK ){ |
| 50337 | 50393 | *(char**)pArg = zTFile; |
| | @@ -50389,11 +50445,11 @@ |
| 50389 | 50445 | /* |
| 50390 | 50446 | ** Return a vector of device characteristics. |
| 50391 | 50447 | */ |
| 50392 | 50448 | static int winDeviceCharacteristics(sqlite3_file *id){ |
| 50393 | 50449 | winFile *p = (winFile*)id; |
| 50394 | | - return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | |
| 50450 | + return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | SQLITE_IOCAP_SUBPAGE_READ | |
| 50395 | 50451 | ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0); |
| 50396 | 50452 | } |
| 50397 | 50453 | |
| 50398 | 50454 | /* |
| 50399 | 50455 | ** Windows will only let you create file view mappings |
| | @@ -51777,11 +51833,11 @@ |
| 51777 | 51833 | */ |
| 51778 | 51834 | char *zTmpname = 0; /* For temporary filename, if necessary. */ |
| 51779 | 51835 | |
| 51780 | 51836 | int rc = SQLITE_OK; /* Function Return Code */ |
| 51781 | 51837 | #if !defined(NDEBUG) || SQLITE_OS_WINCE |
| 51782 | | - int eType = flags&0xFFFFFF00; /* Type of file to open */ |
| 51838 | + int eType = flags&0x0FFF00; /* Type of file to open */ |
| 51783 | 51839 | #endif |
| 51784 | 51840 | |
| 51785 | 51841 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 51786 | 51842 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 51787 | 51843 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| | @@ -57978,43 +58034,37 @@ |
| 57978 | 58034 | # define USEFETCH(x) ((x)->bUseFetch) |
| 57979 | 58035 | #else |
| 57980 | 58036 | # define USEFETCH(x) 0 |
| 57981 | 58037 | #endif |
| 57982 | 58038 | |
| 57983 | | -/* |
| 57984 | | -** The argument to this macro is a file descriptor (type sqlite3_file*). |
| 57985 | | -** Return 0 if it is not open, or non-zero (but not 1) if it is. |
| 57986 | | -** |
| 57987 | | -** This is so that expressions can be written as: |
| 57988 | | -** |
| 57989 | | -** if( isOpen(pPager->jfd) ){ ... |
| 57990 | | -** |
| 57991 | | -** instead of |
| 57992 | | -** |
| 57993 | | -** if( pPager->jfd->pMethods ){ ... |
| 57994 | | -*/ |
| 57995 | | -#define isOpen(pFd) ((pFd)->pMethods!=0) |
| 57996 | | - |
| 57997 | 58039 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 57998 | 58040 | /* |
| 57999 | 58041 | ** Return true if page pgno can be read directly from the database file |
| 58000 | 58042 | ** by the b-tree layer. This is the case if: |
| 58001 | 58043 | ** |
| 58002 | | -** * the database file is open, |
| 58003 | | -** * there are no dirty pages in the cache, and |
| 58004 | | -** * the desired page is not currently in the wal file. |
| 58044 | +** (1) the database file is open |
| 58045 | +** (2) the VFS for the database is able to do unaligned sub-page reads |
| 58046 | +** (3) there are no dirty pages in the cache, and |
| 58047 | +** (4) the desired page is not currently in the wal file. |
| 58005 | 58048 | */ |
| 58006 | 58049 | SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){ |
| 58007 | | - if( pPager->fd->pMethods==0 ) return 0; |
| 58008 | | - if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; |
| 58050 | + assert( pPager!=0 ); |
| 58051 | + assert( pPager->fd!=0 ); |
| 58052 | + if( pPager->fd->pMethods==0 ) return 0; /* Case (1) */ |
| 58053 | + if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; /* Failed (3) */ |
| 58009 | 58054 | #ifndef SQLITE_OMIT_WAL |
| 58010 | 58055 | if( pPager->pWal ){ |
| 58011 | 58056 | u32 iRead = 0; |
| 58012 | 58057 | (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); |
| 58013 | | - return iRead==0; |
| 58058 | + return iRead==0; /* Condition (4) */ |
| 58014 | 58059 | } |
| 58015 | 58060 | #endif |
| 58061 | + assert( pPager->fd->pMethods->xDeviceCharacteristics!=0 ); |
| 58062 | + if( (pPager->fd->pMethods->xDeviceCharacteristics(pPager->fd) |
| 58063 | + & SQLITE_IOCAP_SUBPAGE_READ)==0 ){ |
| 58064 | + return 0; /* Case (2) */ |
| 58065 | + } |
| 58016 | 58066 | return 1; |
| 58017 | 58067 | } |
| 58018 | 58068 | #endif |
| 58019 | 58069 | |
| 58020 | 58070 | #ifndef SQLITE_OMIT_WAL |
| | @@ -59269,11 +59319,11 @@ |
| 59269 | 59319 | rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags); |
| 59270 | 59320 | } |
| 59271 | 59321 | } |
| 59272 | 59322 | pPager->journalOff = 0; |
| 59273 | 59323 | }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST |
| 59274 | | - || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL) |
| 59324 | + || (pPager->exclusiveMode && pPager->journalMode<PAGER_JOURNALMODE_WAL) |
| 59275 | 59325 | ){ |
| 59276 | 59326 | rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile); |
| 59277 | 59327 | pPager->journalOff = 0; |
| 59278 | 59328 | }else{ |
| 59279 | 59329 | /* This branch may be executed with Pager.journalMode==MEMORY if |
| | @@ -67979,15 +68029,11 @@ |
| 67979 | 68029 | ** so it takes care to hold an exclusive lock on the corresponding |
| 67980 | 68030 | ** WAL_READ_LOCK() while changing values. |
| 67981 | 68031 | */ |
| 67982 | 68032 | static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){ |
| 67983 | 68033 | volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */ |
| 67984 | | - u32 mxReadMark; /* Largest aReadMark[] value */ |
| 67985 | | - int mxI; /* Index of largest aReadMark[] value */ |
| 67986 | | - int i; /* Loop counter */ |
| 67987 | 68034 | int rc = SQLITE_OK; /* Return code */ |
| 67988 | | - u32 mxFrame; /* Wal frame to lock to */ |
| 67989 | 68035 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67990 | 68036 | int nBlockTmout = 0; |
| 67991 | 68037 | #endif |
| 67992 | 68038 | |
| 67993 | 68039 | assert( pWal->readLock<0 ); /* Not currently locked */ |
| | @@ -68089,145 +68135,151 @@ |
| 68089 | 68135 | |
| 68090 | 68136 | assert( pWal->nWiData>0 ); |
| 68091 | 68137 | assert( pWal->apWiData[0]!=0 ); |
| 68092 | 68138 | pInfo = walCkptInfo(pWal); |
| 68093 | 68139 | SEH_INJECT_FAULT; |
| 68094 | | - if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame |
| 68095 | | -#ifdef SQLITE_ENABLE_SNAPSHOT |
| 68096 | | - && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0) |
| 68097 | | -#endif |
| 68098 | | - ){ |
| 68099 | | - /* The WAL has been completely backfilled (or it is empty). |
| 68100 | | - ** and can be safely ignored. |
| 68101 | | - */ |
| 68102 | | - rc = walLockShared(pWal, WAL_READ_LOCK(0)); |
| 68103 | | - walShmBarrier(pWal); |
| 68104 | | - if( rc==SQLITE_OK ){ |
| 68105 | | - if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){ |
| 68106 | | - /* It is not safe to allow the reader to continue here if frames |
| 68107 | | - ** may have been appended to the log before READ_LOCK(0) was obtained. |
| 68108 | | - ** When holding READ_LOCK(0), the reader ignores the entire log file, |
| 68109 | | - ** which implies that the database file contains a trustworthy |
| 68110 | | - ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from |
| 68111 | | - ** happening, this is usually correct. |
| 68112 | | - ** |
| 68113 | | - ** However, if frames have been appended to the log (or if the log |
| 68114 | | - ** is wrapped and written for that matter) before the READ_LOCK(0) |
| 68115 | | - ** is obtained, that is not necessarily true. A checkpointer may |
| 68116 | | - ** have started to backfill the appended frames but crashed before |
| 68117 | | - ** it finished. Leaving a corrupt image in the database file. |
| 68118 | | - */ |
| 68119 | | - walUnlockShared(pWal, WAL_READ_LOCK(0)); |
| 68120 | | - return WAL_RETRY; |
| 68121 | | - } |
| 68122 | | - pWal->readLock = 0; |
| 68123 | | - return SQLITE_OK; |
| 68124 | | - }else if( rc!=SQLITE_BUSY ){ |
| 68125 | | - return rc; |
| 68126 | | - } |
| 68127 | | - } |
| 68128 | | - |
| 68129 | | - /* If we get this far, it means that the reader will want to use |
| 68130 | | - ** the WAL to get at content from recent commits. The job now is |
| 68131 | | - ** to select one of the aReadMark[] entries that is closest to |
| 68132 | | - ** but not exceeding pWal->hdr.mxFrame and lock that entry. |
| 68133 | | - */ |
| 68134 | | - mxReadMark = 0; |
| 68135 | | - mxI = 0; |
| 68136 | | - mxFrame = pWal->hdr.mxFrame; |
| 68137 | | -#ifdef SQLITE_ENABLE_SNAPSHOT |
| 68138 | | - if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){ |
| 68139 | | - mxFrame = pWal->pSnapshot->mxFrame; |
| 68140 | | - } |
| 68141 | | -#endif |
| 68142 | | - for(i=1; i<WAL_NREADER; i++){ |
| 68143 | | - u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT; |
| 68144 | | - if( mxReadMark<=thisMark && thisMark<=mxFrame ){ |
| 68145 | | - assert( thisMark!=READMARK_NOT_USED ); |
| 68146 | | - mxReadMark = thisMark; |
| 68147 | | - mxI = i; |
| 68148 | | - } |
| 68149 | | - } |
| 68150 | | - if( (pWal->readOnly & WAL_SHM_RDONLY)==0 |
| 68151 | | - && (mxReadMark<mxFrame || mxI==0) |
| 68152 | | - ){ |
| 68153 | | - for(i=1; i<WAL_NREADER; i++){ |
| 68154 | | - rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 68155 | | - if( rc==SQLITE_OK ){ |
| 68156 | | - AtomicStore(pInfo->aReadMark+i,mxFrame); |
| 68157 | | - mxReadMark = mxFrame; |
| 68158 | | - mxI = i; |
| 68159 | | - walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 68160 | | - break; |
| 68161 | | - }else if( rc!=SQLITE_BUSY ){ |
| 68162 | | - return rc; |
| 68163 | | - } |
| 68164 | | - } |
| 68165 | | - } |
| 68166 | | - if( mxI==0 ){ |
| 68167 | | - assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); |
| 68168 | | - return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT; |
| 68169 | | - } |
| 68170 | | - |
| 68171 | | - (void)walEnableBlockingMs(pWal, nBlockTmout); |
| 68172 | | - rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); |
| 68173 | | - walDisableBlocking(pWal); |
| 68174 | | - if( rc ){ |
| 68175 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 68176 | | - if( rc==SQLITE_BUSY_TIMEOUT ){ |
| 68177 | | - *pCnt |= WAL_RETRY_BLOCKED_MASK; |
| 68178 | | - } |
| 68179 | | -#else |
| 68180 | | - assert( rc!=SQLITE_BUSY_TIMEOUT ); |
| 68181 | | -#endif |
| 68182 | | - assert( (rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT ); |
| 68183 | | - return (rc&0xFF)==SQLITE_BUSY ? WAL_RETRY : rc; |
| 68184 | | - } |
| 68185 | | - /* Now that the read-lock has been obtained, check that neither the |
| 68186 | | - ** value in the aReadMark[] array or the contents of the wal-index |
| 68187 | | - ** header have changed. |
| 68188 | | - ** |
| 68189 | | - ** It is necessary to check that the wal-index header did not change |
| 68190 | | - ** between the time it was read and when the shared-lock was obtained |
| 68191 | | - ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility |
| 68192 | | - ** that the log file may have been wrapped by a writer, or that frames |
| 68193 | | - ** that occur later in the log than pWal->hdr.mxFrame may have been |
| 68194 | | - ** copied into the database by a checkpointer. If either of these things |
| 68195 | | - ** happened, then reading the database with the current value of |
| 68196 | | - ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry |
| 68197 | | - ** instead. |
| 68198 | | - ** |
| 68199 | | - ** Before checking that the live wal-index header has not changed |
| 68200 | | - ** since it was read, set Wal.minFrame to the first frame in the wal |
| 68201 | | - ** file that has not yet been checkpointed. This client will not need |
| 68202 | | - ** to read any frames earlier than minFrame from the wal file - they |
| 68203 | | - ** can be safely read directly from the database file. |
| 68204 | | - ** |
| 68205 | | - ** Because a ShmBarrier() call is made between taking the copy of |
| 68206 | | - ** nBackfill and checking that the wal-header in shared-memory still |
| 68207 | | - ** matches the one cached in pWal->hdr, it is guaranteed that the |
| 68208 | | - ** checkpointer that set nBackfill was not working with a wal-index |
| 68209 | | - ** header newer than that cached in pWal->hdr. If it were, that could |
| 68210 | | - ** cause a problem. The checkpointer could omit to checkpoint |
| 68211 | | - ** a version of page X that lies before pWal->minFrame (call that version |
| 68212 | | - ** A) on the basis that there is a newer version (version B) of the same |
| 68213 | | - ** page later in the wal file. But if version B happens to like past |
| 68214 | | - ** frame pWal->hdr.mxFrame - then the client would incorrectly assume |
| 68215 | | - ** that it can read version A from the database file. However, since |
| 68216 | | - ** we can guarantee that the checkpointer that set nBackfill could not |
| 68217 | | - ** see any pages past pWal->hdr.mxFrame, this problem does not come up. |
| 68218 | | - */ |
| 68219 | | - pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT; |
| 68220 | | - walShmBarrier(pWal); |
| 68221 | | - if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark |
| 68222 | | - || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) |
| 68223 | | - ){ |
| 68224 | | - walUnlockShared(pWal, WAL_READ_LOCK(mxI)); |
| 68225 | | - return WAL_RETRY; |
| 68226 | | - }else{ |
| 68227 | | - assert( mxReadMark<=pWal->hdr.mxFrame ); |
| 68228 | | - pWal->readLock = (i16)mxI; |
| 68140 | + { |
| 68141 | + u32 mxReadMark; /* Largest aReadMark[] value */ |
| 68142 | + int mxI; /* Index of largest aReadMark[] value */ |
| 68143 | + int i; /* Loop counter */ |
| 68144 | + u32 mxFrame; /* Wal frame to lock to */ |
| 68145 | + if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame |
| 68146 | +#ifdef SQLITE_ENABLE_SNAPSHOT |
| 68147 | + && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0) |
| 68148 | +#endif |
| 68149 | + ){ |
| 68150 | + /* The WAL has been completely backfilled (or it is empty). |
| 68151 | + ** and can be safely ignored. |
| 68152 | + */ |
| 68153 | + rc = walLockShared(pWal, WAL_READ_LOCK(0)); |
| 68154 | + walShmBarrier(pWal); |
| 68155 | + if( rc==SQLITE_OK ){ |
| 68156 | + if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr,sizeof(WalIndexHdr)) ){ |
| 68157 | + /* It is not safe to allow the reader to continue here if frames |
| 68158 | + ** may have been appended to the log before READ_LOCK(0) was obtained. |
| 68159 | + ** When holding READ_LOCK(0), the reader ignores the entire log file, |
| 68160 | + ** which implies that the database file contains a trustworthy |
| 68161 | + ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from |
| 68162 | + ** happening, this is usually correct. |
| 68163 | + ** |
| 68164 | + ** However, if frames have been appended to the log (or if the log |
| 68165 | + ** is wrapped and written for that matter) before the READ_LOCK(0) |
| 68166 | + ** is obtained, that is not necessarily true. A checkpointer may |
| 68167 | + ** have started to backfill the appended frames but crashed before |
| 68168 | + ** it finished. Leaving a corrupt image in the database file. |
| 68169 | + */ |
| 68170 | + walUnlockShared(pWal, WAL_READ_LOCK(0)); |
| 68171 | + return WAL_RETRY; |
| 68172 | + } |
| 68173 | + pWal->readLock = 0; |
| 68174 | + return SQLITE_OK; |
| 68175 | + }else if( rc!=SQLITE_BUSY ){ |
| 68176 | + return rc; |
| 68177 | + } |
| 68178 | + } |
| 68179 | + |
| 68180 | + /* If we get this far, it means that the reader will want to use |
| 68181 | + ** the WAL to get at content from recent commits. The job now is |
| 68182 | + ** to select one of the aReadMark[] entries that is closest to |
| 68183 | + ** but not exceeding pWal->hdr.mxFrame and lock that entry. |
| 68184 | + */ |
| 68185 | + mxReadMark = 0; |
| 68186 | + mxI = 0; |
| 68187 | + mxFrame = pWal->hdr.mxFrame; |
| 68188 | +#ifdef SQLITE_ENABLE_SNAPSHOT |
| 68189 | + if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){ |
| 68190 | + mxFrame = pWal->pSnapshot->mxFrame; |
| 68191 | + } |
| 68192 | +#endif |
| 68193 | + for(i=1; i<WAL_NREADER; i++){ |
| 68194 | + u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT; |
| 68195 | + if( mxReadMark<=thisMark && thisMark<=mxFrame ){ |
| 68196 | + assert( thisMark!=READMARK_NOT_USED ); |
| 68197 | + mxReadMark = thisMark; |
| 68198 | + mxI = i; |
| 68199 | + } |
| 68200 | + } |
| 68201 | + if( (pWal->readOnly & WAL_SHM_RDONLY)==0 |
| 68202 | + && (mxReadMark<mxFrame || mxI==0) |
| 68203 | + ){ |
| 68204 | + for(i=1; i<WAL_NREADER; i++){ |
| 68205 | + rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 68206 | + if( rc==SQLITE_OK ){ |
| 68207 | + AtomicStore(pInfo->aReadMark+i,mxFrame); |
| 68208 | + mxReadMark = mxFrame; |
| 68209 | + mxI = i; |
| 68210 | + walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 68211 | + break; |
| 68212 | + }else if( rc!=SQLITE_BUSY ){ |
| 68213 | + return rc; |
| 68214 | + } |
| 68215 | + } |
| 68216 | + } |
| 68217 | + if( mxI==0 ){ |
| 68218 | + assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); |
| 68219 | + return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT; |
| 68220 | + } |
| 68221 | + |
| 68222 | + (void)walEnableBlockingMs(pWal, nBlockTmout); |
| 68223 | + rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); |
| 68224 | + walDisableBlocking(pWal); |
| 68225 | + if( rc ){ |
| 68226 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 68227 | + if( rc==SQLITE_BUSY_TIMEOUT ){ |
| 68228 | + *pCnt |= WAL_RETRY_BLOCKED_MASK; |
| 68229 | + } |
| 68230 | +#else |
| 68231 | + assert( rc!=SQLITE_BUSY_TIMEOUT ); |
| 68232 | +#endif |
| 68233 | + assert((rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT); |
| 68234 | + return (rc&0xFF)==SQLITE_BUSY ? WAL_RETRY : rc; |
| 68235 | + } |
| 68236 | + /* Now that the read-lock has been obtained, check that neither the |
| 68237 | + ** value in the aReadMark[] array or the contents of the wal-index |
| 68238 | + ** header have changed. |
| 68239 | + ** |
| 68240 | + ** It is necessary to check that the wal-index header did not change |
| 68241 | + ** between the time it was read and when the shared-lock was obtained |
| 68242 | + ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility |
| 68243 | + ** that the log file may have been wrapped by a writer, or that frames |
| 68244 | + ** that occur later in the log than pWal->hdr.mxFrame may have been |
| 68245 | + ** copied into the database by a checkpointer. If either of these things |
| 68246 | + ** happened, then reading the database with the current value of |
| 68247 | + ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry |
| 68248 | + ** instead. |
| 68249 | + ** |
| 68250 | + ** Before checking that the live wal-index header has not changed |
| 68251 | + ** since it was read, set Wal.minFrame to the first frame in the wal |
| 68252 | + ** file that has not yet been checkpointed. This client will not need |
| 68253 | + ** to read any frames earlier than minFrame from the wal file - they |
| 68254 | + ** can be safely read directly from the database file. |
| 68255 | + ** |
| 68256 | + ** Because a ShmBarrier() call is made between taking the copy of |
| 68257 | + ** nBackfill and checking that the wal-header in shared-memory still |
| 68258 | + ** matches the one cached in pWal->hdr, it is guaranteed that the |
| 68259 | + ** checkpointer that set nBackfill was not working with a wal-index |
| 68260 | + ** header newer than that cached in pWal->hdr. If it were, that could |
| 68261 | + ** cause a problem. The checkpointer could omit to checkpoint |
| 68262 | + ** a version of page X that lies before pWal->minFrame (call that version |
| 68263 | + ** A) on the basis that there is a newer version (version B) of the same |
| 68264 | + ** page later in the wal file. But if version B happens to like past |
| 68265 | + ** frame pWal->hdr.mxFrame - then the client would incorrectly assume |
| 68266 | + ** that it can read version A from the database file. However, since |
| 68267 | + ** we can guarantee that the checkpointer that set nBackfill could not |
| 68268 | + ** see any pages past pWal->hdr.mxFrame, this problem does not come up. |
| 68269 | + */ |
| 68270 | + pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT; |
| 68271 | + walShmBarrier(pWal); |
| 68272 | + if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark |
| 68273 | + || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) |
| 68274 | + ){ |
| 68275 | + walUnlockShared(pWal, WAL_READ_LOCK(mxI)); |
| 68276 | + return WAL_RETRY; |
| 68277 | + }else{ |
| 68278 | + assert( mxReadMark<=pWal->hdr.mxFrame ); |
| 68279 | + pWal->readLock = (i16)mxI; |
| 68280 | + } |
| 68229 | 68281 | } |
| 68230 | 68282 | return rc; |
| 68231 | 68283 | } |
| 68232 | 68284 | |
| 68233 | 68285 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| | @@ -87103,10 +87155,11 @@ |
| 87103 | 87155 | ** |
| 87104 | 87156 | ** All other fields of Mem can safely remain uninitialized for now. They |
| 87105 | 87157 | ** will be initialized before use. |
| 87106 | 87158 | */ |
| 87107 | 87159 | static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){ |
| 87160 | + assert( db!=0 ); |
| 87108 | 87161 | if( N>0 ){ |
| 87109 | 87162 | do{ |
| 87110 | 87163 | p->flags = flags; |
| 87111 | 87164 | p->db = db; |
| 87112 | 87165 | p->szMalloc = 0; |
| | @@ -87128,10 +87181,11 @@ |
| 87128 | 87181 | */ |
| 87129 | 87182 | static void releaseMemArray(Mem *p, int N){ |
| 87130 | 87183 | if( p && N ){ |
| 87131 | 87184 | Mem *pEnd = &p[N]; |
| 87132 | 87185 | sqlite3 *db = p->db; |
| 87186 | + assert( db!=0 ); |
| 87133 | 87187 | if( db->pnBytesFreed ){ |
| 87134 | 87188 | do{ |
| 87135 | 87189 | if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); |
| 87136 | 87190 | }while( (++p)<pEnd ); |
| 87137 | 87191 | return; |
| | @@ -87608,10 +87662,11 @@ |
| 87608 | 87662 | assert( p!=0 ); |
| 87609 | 87663 | assert( p->nOp>0 ); |
| 87610 | 87664 | assert( pParse!=0 ); |
| 87611 | 87665 | assert( p->eVdbeState==VDBE_INIT_STATE ); |
| 87612 | 87666 | assert( pParse==p->pParse ); |
| 87667 | + assert( pParse->db==p->db ); |
| 87613 | 87668 | p->pVList = pParse->pVList; |
| 87614 | 87669 | pParse->pVList = 0; |
| 87615 | 87670 | db = p->db; |
| 87616 | 87671 | assert( db->mallocFailed==0 ); |
| 87617 | 87672 | nVar = pParse->nVar; |
| | @@ -90488,10 +90543,11 @@ |
| 90488 | 90543 | db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2); |
| 90489 | 90544 | db->pPreUpdate = 0; |
| 90490 | 90545 | sqlite3DbFree(db, preupdate.aRecord); |
| 90491 | 90546 | vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked); |
| 90492 | 90547 | vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked); |
| 90548 | + sqlite3VdbeMemRelease(&preupdate.oldipk); |
| 90493 | 90549 | if( preupdate.aNew ){ |
| 90494 | 90550 | int i; |
| 90495 | 90551 | for(i=0; i<pCsr->nField; i++){ |
| 90496 | 90552 | sqlite3VdbeMemRelease(&preupdate.aNew[i]); |
| 90497 | 90553 | } |
| | @@ -91844,11 +91900,11 @@ |
| 91844 | 91900 | ** |
| 91845 | 91901 | ** sqlite3_column_int() |
| 91846 | 91902 | ** sqlite3_column_int64() |
| 91847 | 91903 | ** sqlite3_column_text() |
| 91848 | 91904 | ** sqlite3_column_text16() |
| 91849 | | -** sqlite3_column_real() |
| 91905 | +** sqlite3_column_double() |
| 91850 | 91906 | ** sqlite3_column_bytes() |
| 91851 | 91907 | ** sqlite3_column_bytes16() |
| 91852 | 91908 | ** sqlite3_column_blob() |
| 91853 | 91909 | */ |
| 91854 | 91910 | static void columnMallocFailure(sqlite3_stmt *pStmt) |
| | @@ -92706,64 +92762,68 @@ |
| 92706 | 92762 | if( iIdx>=p->pCsr->nField || iIdx<0 ){ |
| 92707 | 92763 | rc = SQLITE_RANGE; |
| 92708 | 92764 | goto preupdate_old_out; |
| 92709 | 92765 | } |
| 92710 | 92766 | |
| 92711 | | - /* If the old.* record has not yet been loaded into memory, do so now. */ |
| 92712 | | - if( p->pUnpacked==0 ){ |
| 92713 | | - u32 nRec; |
| 92714 | | - u8 *aRec; |
| 92715 | | - |
| 92716 | | - assert( p->pCsr->eCurType==CURTYPE_BTREE ); |
| 92717 | | - nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); |
| 92718 | | - aRec = sqlite3DbMallocRaw(db, nRec); |
| 92719 | | - if( !aRec ) goto preupdate_old_out; |
| 92720 | | - rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); |
| 92721 | | - if( rc==SQLITE_OK ){ |
| 92722 | | - p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec); |
| 92723 | | - if( !p->pUnpacked ) rc = SQLITE_NOMEM; |
| 92724 | | - } |
| 92725 | | - if( rc!=SQLITE_OK ){ |
| 92726 | | - sqlite3DbFree(db, aRec); |
| 92727 | | - goto preupdate_old_out; |
| 92728 | | - } |
| 92729 | | - p->aRecord = aRec; |
| 92730 | | - } |
| 92731 | | - |
| 92732 | | - pMem = *ppValue = &p->pUnpacked->aMem[iIdx]; |
| 92733 | 92767 | if( iIdx==p->pTab->iPKey ){ |
| 92768 | + *ppValue = pMem = &p->oldipk; |
| 92734 | 92769 | sqlite3VdbeMemSetInt64(pMem, p->iKey1); |
| 92735 | | - }else if( iIdx>=p->pUnpacked->nField ){ |
| 92736 | | - /* This occurs when the table has been extended using ALTER TABLE |
| 92737 | | - ** ADD COLUMN. The value to return is the default value of the column. */ |
| 92738 | | - Column *pCol = &p->pTab->aCol[iIdx]; |
| 92739 | | - if( pCol->iDflt>0 ){ |
| 92740 | | - if( p->apDflt==0 ){ |
| 92741 | | - int nByte = sizeof(sqlite3_value*)*p->pTab->nCol; |
| 92742 | | - p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte); |
| 92743 | | - if( p->apDflt==0 ) goto preupdate_old_out; |
| 92744 | | - } |
| 92745 | | - if( p->apDflt[iIdx]==0 ){ |
| 92746 | | - sqlite3_value *pVal = 0; |
| 92747 | | - Expr *pDflt; |
| 92748 | | - assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) ); |
| 92749 | | - pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; |
| 92750 | | - rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal); |
| 92751 | | - if( rc==SQLITE_OK && pVal==0 ){ |
| 92752 | | - rc = SQLITE_CORRUPT_BKPT; |
| 92753 | | - } |
| 92754 | | - p->apDflt[iIdx] = pVal; |
| 92755 | | - } |
| 92756 | | - *ppValue = p->apDflt[iIdx]; |
| 92757 | | - }else{ |
| 92758 | | - *ppValue = (sqlite3_value *)columnNullValue(); |
| 92759 | | - } |
| 92760 | | - }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ |
| 92761 | | - if( pMem->flags & (MEM_Int|MEM_IntReal) ){ |
| 92762 | | - testcase( pMem->flags & MEM_Int ); |
| 92763 | | - testcase( pMem->flags & MEM_IntReal ); |
| 92764 | | - sqlite3VdbeMemRealify(pMem); |
| 92770 | + }else{ |
| 92771 | + |
| 92772 | + /* If the old.* record has not yet been loaded into memory, do so now. */ |
| 92773 | + if( p->pUnpacked==0 ){ |
| 92774 | + u32 nRec; |
| 92775 | + u8 *aRec; |
| 92776 | + |
| 92777 | + assert( p->pCsr->eCurType==CURTYPE_BTREE ); |
| 92778 | + nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); |
| 92779 | + aRec = sqlite3DbMallocRaw(db, nRec); |
| 92780 | + if( !aRec ) goto preupdate_old_out; |
| 92781 | + rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); |
| 92782 | + if( rc==SQLITE_OK ){ |
| 92783 | + p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec); |
| 92784 | + if( !p->pUnpacked ) rc = SQLITE_NOMEM; |
| 92785 | + } |
| 92786 | + if( rc!=SQLITE_OK ){ |
| 92787 | + sqlite3DbFree(db, aRec); |
| 92788 | + goto preupdate_old_out; |
| 92789 | + } |
| 92790 | + p->aRecord = aRec; |
| 92791 | + } |
| 92792 | + |
| 92793 | + pMem = *ppValue = &p->pUnpacked->aMem[iIdx]; |
| 92794 | + if( iIdx>=p->pUnpacked->nField ){ |
| 92795 | + /* This occurs when the table has been extended using ALTER TABLE |
| 92796 | + ** ADD COLUMN. The value to return is the default value of the column. */ |
| 92797 | + Column *pCol = &p->pTab->aCol[iIdx]; |
| 92798 | + if( pCol->iDflt>0 ){ |
| 92799 | + if( p->apDflt==0 ){ |
| 92800 | + int nByte = sizeof(sqlite3_value*)*p->pTab->nCol; |
| 92801 | + p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte); |
| 92802 | + if( p->apDflt==0 ) goto preupdate_old_out; |
| 92803 | + } |
| 92804 | + if( p->apDflt[iIdx]==0 ){ |
| 92805 | + sqlite3_value *pVal = 0; |
| 92806 | + Expr *pDflt; |
| 92807 | + assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) ); |
| 92808 | + pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; |
| 92809 | + rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal); |
| 92810 | + if( rc==SQLITE_OK && pVal==0 ){ |
| 92811 | + rc = SQLITE_CORRUPT_BKPT; |
| 92812 | + } |
| 92813 | + p->apDflt[iIdx] = pVal; |
| 92814 | + } |
| 92815 | + *ppValue = p->apDflt[iIdx]; |
| 92816 | + }else{ |
| 92817 | + *ppValue = (sqlite3_value *)columnNullValue(); |
| 92818 | + } |
| 92819 | + }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ |
| 92820 | + if( pMem->flags & (MEM_Int|MEM_IntReal) ){ |
| 92821 | + testcase( pMem->flags & MEM_Int ); |
| 92822 | + testcase( pMem->flags & MEM_IntReal ); |
| 92823 | + sqlite3VdbeMemRealify(pMem); |
| 92824 | + } |
| 92765 | 92825 | } |
| 92766 | 92826 | } |
| 92767 | 92827 | |
| 92768 | 92828 | preupdate_old_out: |
| 92769 | 92829 | sqlite3Error(db, rc); |
| | @@ -97913,13 +97973,15 @@ |
| 97913 | 97973 | 0, pCx->uc.pCursor); |
| 97914 | 97974 | pCx->isTable = 1; |
| 97915 | 97975 | } |
| 97916 | 97976 | } |
| 97917 | 97977 | pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
| 97978 | + assert( p->apCsr[pOp->p1]==pCx ); |
| 97918 | 97979 | if( rc ){ |
| 97919 | 97980 | assert( !sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) ); |
| 97920 | 97981 | sqlite3BtreeClose(pCx->ub.pBtx); |
| 97982 | + p->apCsr[pOp->p1] = 0; /* Not required; helps with static analysis */ |
| 97921 | 97983 | }else{ |
| 97922 | 97984 | assert( sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) ); |
| 97923 | 97985 | } |
| 97924 | 97986 | } |
| 97925 | 97987 | } |
| | @@ -109845,11 +109907,11 @@ |
| 109845 | 109907 | p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); |
| 109846 | 109908 | } |
| 109847 | 109909 | p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); |
| 109848 | 109910 | addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, |
| 109849 | 109911 | (void*)p4, P4_COLLSEQ); |
| 109850 | | - sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5); |
| 109912 | + sqlite3VdbeChangeP5(pParse->pVdbe, (u16)p5); |
| 109851 | 109913 | return addr; |
| 109852 | 109914 | } |
| 109853 | 109915 | |
| 109854 | 109916 | /* |
| 109855 | 109917 | ** Return true if expression pExpr is a vector, or false otherwise. |
| | @@ -112012,11 +112074,11 @@ |
| 112012 | 112074 | ** |
| 112013 | 112075 | ** (4) If pSrc is the right operand of a LEFT JOIN, then... |
| 112014 | 112076 | ** (4a) pExpr must come from an ON clause.. |
| 112015 | 112077 | ** (4b) and specifically the ON clause associated with the LEFT JOIN. |
| 112016 | 112078 | ** |
| 112017 | | -** (5) If pSrc is not the right operand of a LEFT JOIN or the left |
| 112079 | +** (5) If pSrc is the right operand of a LEFT JOIN or the left |
| 112018 | 112080 | ** operand of a RIGHT JOIN, then pExpr must be from the WHERE |
| 112019 | 112081 | ** clause, not an ON clause. |
| 112020 | 112082 | ** |
| 112021 | 112083 | ** (6) Either: |
| 112022 | 112084 | ** |
| | @@ -115546,35 +115608,41 @@ |
| 115546 | 115608 | ** |
| 115547 | 115609 | ** Additionally, if pExpr is a simple SQL value and the value is the |
| 115548 | 115610 | ** same as that currently bound to variable pVar, non-zero is returned. |
| 115549 | 115611 | ** Otherwise, if the values are not the same or if pExpr is not a simple |
| 115550 | 115612 | ** SQL value, zero is returned. |
| 115613 | +** |
| 115614 | +** If the SQLITE_EnableQPSG flag is set on the database connection, then |
| 115615 | +** this routine always returns false. |
| 115551 | 115616 | */ |
| 115552 | | -static int exprCompareVariable( |
| 115617 | +static SQLITE_NOINLINE int exprCompareVariable( |
| 115553 | 115618 | const Parse *pParse, |
| 115554 | 115619 | const Expr *pVar, |
| 115555 | 115620 | const Expr *pExpr |
| 115556 | 115621 | ){ |
| 115557 | | - int res = 0; |
| 115622 | + int res = 2; |
| 115558 | 115623 | int iVar; |
| 115559 | 115624 | sqlite3_value *pL, *pR = 0; |
| 115560 | 115625 | |
| 115626 | + if( pExpr->op==TK_VARIABLE && pVar->iColumn==pExpr->iColumn ){ |
| 115627 | + return 0; |
| 115628 | + } |
| 115629 | + if( (pParse->db->flags & SQLITE_EnableQPSG)!=0 ) return 2; |
| 115561 | 115630 | sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR); |
| 115562 | 115631 | if( pR ){ |
| 115563 | 115632 | iVar = pVar->iColumn; |
| 115564 | 115633 | sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); |
| 115565 | 115634 | pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB); |
| 115566 | 115635 | if( pL ){ |
| 115567 | 115636 | if( sqlite3_value_type(pL)==SQLITE_TEXT ){ |
| 115568 | 115637 | sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */ |
| 115569 | 115638 | } |
| 115570 | | - res = 0==sqlite3MemCompare(pL, pR, 0); |
| 115639 | + res = sqlite3MemCompare(pL, pR, 0) ? 2 : 0; |
| 115571 | 115640 | } |
| 115572 | 115641 | sqlite3ValueFree(pR); |
| 115573 | 115642 | sqlite3ValueFree(pL); |
| 115574 | 115643 | } |
| 115575 | | - |
| 115576 | 115644 | return res; |
| 115577 | 115645 | } |
| 115578 | 115646 | |
| 115579 | 115647 | /* |
| 115580 | 115648 | ** Do a deep comparison of two expression trees. Return 0 if the two |
| | @@ -115596,16 +115664,14 @@ |
| 115596 | 115664 | ** can be sure the expressions are the same. In the places where |
| 115597 | 115665 | ** this routine is used, it does not hurt to get an extra 2 - that |
| 115598 | 115666 | ** just might result in some slightly slower code. But returning |
| 115599 | 115667 | ** an incorrect 0 or 1 could lead to a malfunction. |
| 115600 | 115668 | ** |
| 115601 | | -** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in |
| 115602 | | -** pParse->pReprepare can be matched against literals in pB. The |
| 115603 | | -** pParse->pVdbe->expmask bitmask is updated for each variable referenced. |
| 115604 | | -** If pParse is NULL (the normal case) then any TK_VARIABLE term in |
| 115605 | | -** Argument pParse should normally be NULL. If it is not NULL and pA or |
| 115606 | | -** pB causes a return value of 2. |
| 115669 | +** If pParse is not NULL and SQLITE_EnableQPSG is off then TK_VARIABLE |
| 115670 | +** terms in pA with bindings in pParse->pReprepare can be matched against |
| 115671 | +** literals in pB. The pParse->pVdbe->expmask bitmask is updated for |
| 115672 | +** each variable referenced. |
| 115607 | 115673 | */ |
| 115608 | 115674 | SQLITE_PRIVATE int sqlite3ExprCompare( |
| 115609 | 115675 | const Parse *pParse, |
| 115610 | 115676 | const Expr *pA, |
| 115611 | 115677 | const Expr *pB, |
| | @@ -115613,12 +115679,12 @@ |
| 115613 | 115679 | ){ |
| 115614 | 115680 | u32 combinedFlags; |
| 115615 | 115681 | if( pA==0 || pB==0 ){ |
| 115616 | 115682 | return pB==pA ? 0 : 2; |
| 115617 | 115683 | } |
| 115618 | | - if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){ |
| 115619 | | - return 0; |
| 115684 | + if( pParse && pA->op==TK_VARIABLE ){ |
| 115685 | + return exprCompareVariable(pParse, pA, pB); |
| 115620 | 115686 | } |
| 115621 | 115687 | combinedFlags = pA->flags | pB->flags; |
| 115622 | 115688 | if( combinedFlags & EP_IntValue ){ |
| 115623 | 115689 | if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ |
| 115624 | 115690 | return 0; |
| | @@ -115808,23 +115874,75 @@ |
| 115808 | 115874 | return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1); |
| 115809 | 115875 | } |
| 115810 | 115876 | } |
| 115811 | 115877 | return 0; |
| 115812 | 115878 | } |
| 115879 | + |
| 115880 | +/* |
| 115881 | +** Return true if the boolean value of the expression is always either |
| 115882 | +** FALSE or NULL. |
| 115883 | +*/ |
| 115884 | +static int sqlite3ExprIsNotTrue(Expr *pExpr){ |
| 115885 | + int v; |
| 115886 | + if( pExpr->op==TK_NULL ) return 1; |
| 115887 | + if( pExpr->op==TK_TRUEFALSE && sqlite3ExprTruthValue(pExpr)==0 ) return 1; |
| 115888 | + v = 1; |
| 115889 | + if( sqlite3ExprIsInteger(pExpr, &v, 0) && v==0 ) return 1; |
| 115890 | + return 0; |
| 115891 | +} |
| 115892 | + |
| 115893 | +/* |
| 115894 | +** Return true if the expression is one of the following: |
| 115895 | +** |
| 115896 | +** CASE WHEN x THEN y END |
| 115897 | +** CASE WHEN x THEN y ELSE NULL END |
| 115898 | +** CASE WHEN x THEN y ELSE false END |
| 115899 | +** iif(x,y) |
| 115900 | +** iif(x,y,NULL) |
| 115901 | +** iif(x,y,false) |
| 115902 | +*/ |
| 115903 | +static int sqlite3ExprIsIIF(sqlite3 *db, const Expr *pExpr){ |
| 115904 | + ExprList *pList; |
| 115905 | + if( pExpr->op==TK_FUNCTION ){ |
| 115906 | + const char *z = pExpr->u.zToken; |
| 115907 | + FuncDef *pDef; |
| 115908 | + if( (z[0]!='i' && z[0]!='I') ) return 0; |
| 115909 | + if( pExpr->x.pList==0 ) return 0; |
| 115910 | + pDef = sqlite3FindFunction(db, z, pExpr->x.pList->nExpr, ENC(db), 0); |
| 115911 | +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
| 115912 | + if( pDef==0 ) return 0; |
| 115913 | +#else |
| 115914 | + if( NEVER(pDef==0) ) return 0; |
| 115915 | +#endif |
| 115916 | + if( (pDef->funcFlags & SQLITE_FUNC_INLINE)==0 ) return 0; |
| 115917 | + if( SQLITE_PTR_TO_INT(pDef->pUserData)!=INLINEFUNC_iif ) return 0; |
| 115918 | + }else if( pExpr->op==TK_CASE ){ |
| 115919 | + if( pExpr->pLeft!=0 ) return 0; |
| 115920 | + }else{ |
| 115921 | + return 0; |
| 115922 | + } |
| 115923 | + pList = pExpr->x.pList; |
| 115924 | + assert( pList!=0 ); |
| 115925 | + if( pList->nExpr==2 ) return 1; |
| 115926 | + if( pList->nExpr==3 && sqlite3ExprIsNotTrue(pList->a[2].pExpr) ) return 1; |
| 115927 | + return 0; |
| 115928 | +} |
| 115813 | 115929 | |
| 115814 | 115930 | /* |
| 115815 | 115931 | ** Return true if we can prove the pE2 will always be true if pE1 is |
| 115816 | 115932 | ** true. Return false if we cannot complete the proof or if pE2 might |
| 115817 | 115933 | ** be false. Examples: |
| 115818 | 115934 | ** |
| 115819 | | -** pE1: x==5 pE2: x==5 Result: true |
| 115820 | | -** pE1: x>0 pE2: x==5 Result: false |
| 115821 | | -** pE1: x=21 pE2: x=21 OR y=43 Result: true |
| 115822 | | -** pE1: x!=123 pE2: x IS NOT NULL Result: true |
| 115823 | | -** pE1: x!=?1 pE2: x IS NOT NULL Result: true |
| 115824 | | -** pE1: x IS NULL pE2: x IS NOT NULL Result: false |
| 115825 | | -** pE1: x IS ?2 pE2: x IS NOT NULL Result: false |
| 115935 | +** pE1: x==5 pE2: x==5 Result: true |
| 115936 | +** pE1: x>0 pE2: x==5 Result: false |
| 115937 | +** pE1: x=21 pE2: x=21 OR y=43 Result: true |
| 115938 | +** pE1: x!=123 pE2: x IS NOT NULL Result: true |
| 115939 | +** pE1: x!=?1 pE2: x IS NOT NULL Result: true |
| 115940 | +** pE1: x IS NULL pE2: x IS NOT NULL Result: false |
| 115941 | +** pE1: x IS ?2 pE2: x IS NOT NULL Result: false |
| 115942 | +** pE1: iif(x,y) pE2: x Result: true |
| 115943 | +** PE1: iif(x,y,0) pE2: x Result: true |
| 115826 | 115944 | ** |
| 115827 | 115945 | ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has |
| 115828 | 115946 | ** Expr.iTable<0 then assume a table number given by iTab. |
| 115829 | 115947 | ** |
| 115830 | 115948 | ** If pParse is not NULL, then the values of bound variables in pE1 are |
| | @@ -115854,10 +115972,13 @@ |
| 115854 | 115972 | if( pE2->op==TK_NOTNULL |
| 115855 | 115973 | && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0) |
| 115856 | 115974 | ){ |
| 115857 | 115975 | return 1; |
| 115858 | 115976 | } |
| 115977 | + if( sqlite3ExprIsIIF(pParse->db, pE1) ){ |
| 115978 | + return sqlite3ExprImpliesExpr(pParse,pE1->x.pList->a[0].pExpr,pE2,iTab); |
| 115979 | + } |
| 115859 | 115980 | return 0; |
| 115860 | 115981 | } |
| 115861 | 115982 | |
| 115862 | 115983 | /* This is a helper function to impliesNotNullRow(). In this routine, |
| 115863 | 115984 | ** set pWalker->eCode to one only if *both* of the input expressions |
| | @@ -121265,19 +121386,10 @@ |
| 121265 | 121386 | rc = sqlite3Init(db, &zErrDyn); |
| 121266 | 121387 | } |
| 121267 | 121388 | sqlite3BtreeLeaveAll(db); |
| 121268 | 121389 | assert( zErrDyn==0 || rc!=SQLITE_OK ); |
| 121269 | 121390 | } |
| 121270 | | -#ifdef SQLITE_USER_AUTHENTICATION |
| 121271 | | - if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){ |
| 121272 | | - u8 newAuth = 0; |
| 121273 | | - rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth); |
| 121274 | | - if( newAuth<db->auth.authLevel ){ |
| 121275 | | - rc = SQLITE_AUTH_USER; |
| 121276 | | - } |
| 121277 | | - } |
| 121278 | | -#endif |
| 121279 | 121391 | if( rc ){ |
| 121280 | 121392 | if( ALWAYS(!REOPEN_AS_MEMDB(db)) ){ |
| 121281 | 121393 | int iDb = db->nDb - 1; |
| 121282 | 121394 | assert( iDb>=2 ); |
| 121283 | 121395 | if( db->aDb[iDb].pBt ){ |
| | @@ -121771,15 +121883,11 @@ |
| 121771 | 121883 | sqlite3 *db = pParse->db; /* Database handle */ |
| 121772 | 121884 | char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */ |
| 121773 | 121885 | int rc; /* Auth callback return code */ |
| 121774 | 121886 | |
| 121775 | 121887 | if( db->init.busy ) return SQLITE_OK; |
| 121776 | | - rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext |
| 121777 | | -#ifdef SQLITE_USER_AUTHENTICATION |
| 121778 | | - ,db->auth.zAuthUser |
| 121779 | | -#endif |
| 121780 | | - ); |
| 121888 | + rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext); |
| 121781 | 121889 | if( rc==SQLITE_DENY ){ |
| 121782 | 121890 | char *z = sqlite3_mprintf("%s.%s", zTab, zCol); |
| 121783 | 121891 | if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z); |
| 121784 | 121892 | sqlite3ErrorMsg(pParse, "access to %z is prohibited", z); |
| 121785 | 121893 | pParse->rc = SQLITE_AUTH; |
| | @@ -121882,15 +121990,11 @@ |
| 121882 | 121990 | testcase( zArg1==0 ); |
| 121883 | 121991 | testcase( zArg2==0 ); |
| 121884 | 121992 | testcase( zArg3==0 ); |
| 121885 | 121993 | testcase( pParse->zAuthContext==0 ); |
| 121886 | 121994 | |
| 121887 | | - rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext |
| 121888 | | -#ifdef SQLITE_USER_AUTHENTICATION |
| 121889 | | - ,db->auth.zAuthUser |
| 121890 | | -#endif |
| 121891 | | - ); |
| 121995 | + rc = db->xAuth(db->pAuthArg,code,zArg1,zArg2,zArg3,pParse->zAuthContext); |
| 121892 | 121996 | if( rc==SQLITE_DENY ){ |
| 121893 | 121997 | sqlite3ErrorMsg(pParse, "not authorized"); |
| 121894 | 121998 | pParse->rc = SQLITE_AUTH; |
| 121895 | 121999 | }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){ |
| 121896 | 122000 | rc = SQLITE_DENY; |
| | @@ -122119,21 +122223,10 @@ |
| 122119 | 122223 | sqlite3VdbeJumpHere(v, addrRewind); |
| 122120 | 122224 | } |
| 122121 | 122225 | } |
| 122122 | 122226 | sqlite3VdbeAddOp0(v, OP_Halt); |
| 122123 | 122227 | |
| 122124 | | -#if SQLITE_USER_AUTHENTICATION && !defined(SQLITE_OMIT_SHARED_CACHE) |
| 122125 | | - if( pParse->nTableLock>0 && db->init.busy==0 ){ |
| 122126 | | - sqlite3UserAuthInit(db); |
| 122127 | | - if( db->auth.authLevel<UAUTH_User ){ |
| 122128 | | - sqlite3ErrorMsg(pParse, "user not authenticated"); |
| 122129 | | - pParse->rc = SQLITE_AUTH_USER; |
| 122130 | | - return; |
| 122131 | | - } |
| 122132 | | - } |
| 122133 | | -#endif |
| 122134 | | - |
| 122135 | 122228 | /* The cookie mask contains one bit for each database file open. |
| 122136 | 122229 | ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are |
| 122137 | 122230 | ** set for each database that is used. Generate code to start a |
| 122138 | 122231 | ** transaction on each used database and to verify the schema cookie |
| 122139 | 122232 | ** on each used database. |
| | @@ -122258,20 +122351,10 @@ |
| 122258 | 122351 | sqlite3DbFree(db, zSql); |
| 122259 | 122352 | memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); |
| 122260 | 122353 | pParse->nested--; |
| 122261 | 122354 | } |
| 122262 | 122355 | |
| 122263 | | -#if SQLITE_USER_AUTHENTICATION |
| 122264 | | -/* |
| 122265 | | -** Return TRUE if zTable is the name of the system table that stores the |
| 122266 | | -** list of users and their access credentials. |
| 122267 | | -*/ |
| 122268 | | -SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){ |
| 122269 | | - return sqlite3_stricmp(zTable, "sqlite_user")==0; |
| 122270 | | -} |
| 122271 | | -#endif |
| 122272 | | - |
| 122273 | 122356 | /* |
| 122274 | 122357 | ** Locate the in-memory structure that describes a particular database |
| 122275 | 122358 | ** table given the name of that table and (optionally) the name of the |
| 122276 | 122359 | ** database containing the table. Return NULL if not found. |
| 122277 | 122360 | ** |
| | @@ -122286,17 +122369,10 @@ |
| 122286 | 122369 | Table *p = 0; |
| 122287 | 122370 | int i; |
| 122288 | 122371 | |
| 122289 | 122372 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 122290 | 122373 | assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
| 122291 | | -#if SQLITE_USER_AUTHENTICATION |
| 122292 | | - /* Only the admin user is allowed to know that the sqlite_user table |
| 122293 | | - ** exists */ |
| 122294 | | - if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ |
| 122295 | | - return 0; |
| 122296 | | - } |
| 122297 | | -#endif |
| 122298 | 122374 | if( zDatabase ){ |
| 122299 | 122375 | for(i=0; i<db->nDb; i++){ |
| 122300 | 122376 | if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break; |
| 122301 | 122377 | } |
| 122302 | 122378 | if( i>=db->nDb ){ |
| | @@ -125951,13 +126027,10 @@ |
| 125951 | 126027 | |
| 125952 | 126028 | assert( pTab!=0 ); |
| 125953 | 126029 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
| 125954 | 126030 | && db->init.busy==0 |
| 125955 | 126031 | && pTblName!=0 |
| 125956 | | -#if SQLITE_USER_AUTHENTICATION |
| 125957 | | - && sqlite3UserAuthTable(pTab->zName)==0 |
| 125958 | | -#endif |
| 125959 | 126032 | ){ |
| 125960 | 126033 | sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
| 125961 | 126034 | goto exit_create_index; |
| 125962 | 126035 | } |
| 125963 | 126036 | #ifndef SQLITE_OMIT_VIEW |
| | @@ -129661,20 +129734,19 @@ |
| 129661 | 129734 | const unsigned char *z; |
| 129662 | 129735 | const unsigned char *z2; |
| 129663 | 129736 | int len; |
| 129664 | 129737 | int p0type; |
| 129665 | 129738 | i64 p1, p2; |
| 129666 | | - int negP2 = 0; |
| 129667 | 129739 | |
| 129668 | 129740 | assert( argc==3 || argc==2 ); |
| 129669 | 129741 | if( sqlite3_value_type(argv[1])==SQLITE_NULL |
| 129670 | 129742 | || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL) |
| 129671 | 129743 | ){ |
| 129672 | 129744 | return; |
| 129673 | 129745 | } |
| 129674 | 129746 | p0type = sqlite3_value_type(argv[0]); |
| 129675 | | - p1 = sqlite3_value_int(argv[1]); |
| 129747 | + p1 = sqlite3_value_int64(argv[1]); |
| 129676 | 129748 | if( p0type==SQLITE_BLOB ){ |
| 129677 | 129749 | len = sqlite3_value_bytes(argv[0]); |
| 129678 | 129750 | z = sqlite3_value_blob(argv[0]); |
| 129679 | 129751 | if( z==0 ) return; |
| 129680 | 129752 | assert( len==sqlite3_value_bytes(argv[0]) ); |
| | @@ -129695,36 +129767,36 @@ |
| 129695 | 129767 | ** from 2009-02-02 for compatibility of applications that exploited the |
| 129696 | 129768 | ** old buggy behavior. */ |
| 129697 | 129769 | if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */ |
| 129698 | 129770 | #endif |
| 129699 | 129771 | if( argc==3 ){ |
| 129700 | | - p2 = sqlite3_value_int(argv[2]); |
| 129701 | | - if( p2<0 ){ |
| 129702 | | - p2 = -p2; |
| 129703 | | - negP2 = 1; |
| 129704 | | - } |
| 129772 | + p2 = sqlite3_value_int64(argv[2]); |
| 129705 | 129773 | }else{ |
| 129706 | 129774 | p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; |
| 129707 | 129775 | } |
| 129708 | 129776 | if( p1<0 ){ |
| 129709 | 129777 | p1 += len; |
| 129710 | 129778 | if( p1<0 ){ |
| 129711 | | - p2 += p1; |
| 129712 | | - if( p2<0 ) p2 = 0; |
| 129779 | + if( p2<0 ){ |
| 129780 | + p2 = 0; |
| 129781 | + }else{ |
| 129782 | + p2 += p1; |
| 129783 | + } |
| 129713 | 129784 | p1 = 0; |
| 129714 | 129785 | } |
| 129715 | 129786 | }else if( p1>0 ){ |
| 129716 | 129787 | p1--; |
| 129717 | 129788 | }else if( p2>0 ){ |
| 129718 | 129789 | p2--; |
| 129719 | 129790 | } |
| 129720 | | - if( negP2 ){ |
| 129791 | + if( p2<0 ){ |
| 129792 | + if( p2<-p1 ){ |
| 129793 | + p2 = p1; |
| 129794 | + }else{ |
| 129795 | + p2 = -p2; |
| 129796 | + } |
| 129721 | 129797 | p1 -= p2; |
| 129722 | | - if( p1<0 ){ |
| 129723 | | - p2 += p1; |
| 129724 | | - p1 = 0; |
| 129725 | | - } |
| 129726 | 129798 | } |
| 129727 | 129799 | assert( p1>=0 && p2>=0 ); |
| 129728 | 129800 | if( p0type!=SQLITE_BLOB ){ |
| 129729 | 129801 | while( *z && p1 ){ |
| 129730 | 129802 | SQLITE_SKIP_UTF8(z); |
| | @@ -129734,13 +129806,15 @@ |
| 129734 | 129806 | SQLITE_SKIP_UTF8(z2); |
| 129735 | 129807 | } |
| 129736 | 129808 | sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT, |
| 129737 | 129809 | SQLITE_UTF8); |
| 129738 | 129810 | }else{ |
| 129739 | | - if( p1+p2>len ){ |
| 129811 | + if( p1>=len ){ |
| 129812 | + p1 = p2 = 0; |
| 129813 | + }else if( p2>len-p1 ){ |
| 129740 | 129814 | p2 = len-p1; |
| 129741 | | - if( p2<0 ) p2 = 0; |
| 129815 | + assert( p2>0 ); |
| 129742 | 129816 | } |
| 129743 | 129817 | sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT); |
| 129744 | 129818 | } |
| 129745 | 129819 | } |
| 129746 | 129820 | |
| | @@ -129747,17 +129821,17 @@ |
| 129747 | 129821 | /* |
| 129748 | 129822 | ** Implementation of the round() function |
| 129749 | 129823 | */ |
| 129750 | 129824 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 129751 | 129825 | static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 129752 | | - int n = 0; |
| 129826 | + i64 n = 0; |
| 129753 | 129827 | double r; |
| 129754 | 129828 | char *zBuf; |
| 129755 | 129829 | assert( argc==1 || argc==2 ); |
| 129756 | 129830 | if( argc==2 ){ |
| 129757 | 129831 | if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; |
| 129758 | | - n = sqlite3_value_int(argv[1]); |
| 129832 | + n = sqlite3_value_int64(argv[1]); |
| 129759 | 129833 | if( n>30 ) n = 30; |
| 129760 | 129834 | if( n<0 ) n = 0; |
| 129761 | 129835 | } |
| 129762 | 129836 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 129763 | 129837 | r = sqlite3_value_double(argv[0]); |
| | @@ -129768,11 +129842,11 @@ |
| 129768 | 129842 | if( r<-4503599627370496.0 || r>+4503599627370496.0 ){ |
| 129769 | 129843 | /* The value has no fractional part so there is nothing to round */ |
| 129770 | 129844 | }else if( n==0 ){ |
| 129771 | 129845 | r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5))); |
| 129772 | 129846 | }else{ |
| 129773 | | - zBuf = sqlite3_mprintf("%!.*f",n,r); |
| 129847 | + zBuf = sqlite3_mprintf("%!.*f",(int)n,r); |
| 129774 | 129848 | if( zBuf==0 ){ |
| 129775 | 129849 | sqlite3_result_error_nomem(context); |
| 129776 | 129850 | return; |
| 129777 | 129851 | } |
| 129778 | 129852 | sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); |
| | @@ -131985,13 +132059,10 @@ |
| 131985 | 132059 | #endif |
| 131986 | 132060 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 131987 | 132061 | SFUNCTION(load_extension, 1, 0, 0, loadExt ), |
| 131988 | 132062 | SFUNCTION(load_extension, 2, 0, 0, loadExt ), |
| 131989 | 132063 | #endif |
| 131990 | | -#if SQLITE_USER_AUTHENTICATION |
| 131991 | | - FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ), |
| 131992 | | -#endif |
| 131993 | 132064 | #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 131994 | 132065 | DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), |
| 131995 | 132066 | DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), |
| 131996 | 132067 | #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 131997 | 132068 | INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY), |
| | @@ -132124,11 +132195,14 @@ |
| 132124 | 132195 | MFUNCTION(degrees, 1, radToDeg, math1Func ), |
| 132125 | 132196 | MFUNCTION(pi, 0, 0, piFunc ), |
| 132126 | 132197 | #endif /* SQLITE_ENABLE_MATH_FUNCTIONS */ |
| 132127 | 132198 | FUNCTION(sign, 1, 0, 0, signFunc ), |
| 132128 | 132199 | INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ), |
| 132200 | + INLINE_FUNC(iif, 2, INLINEFUNC_iif, 0 ), |
| 132129 | 132201 | INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ), |
| 132202 | + INLINE_FUNC(if, 2, INLINEFUNC_iif, 0 ), |
| 132203 | + INLINE_FUNC(if, 3, INLINEFUNC_iif, 0 ), |
| 132130 | 132204 | }; |
| 132131 | 132205 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 132132 | 132206 | sqlite3AlterFunctions(); |
| 132133 | 132207 | #endif |
| 132134 | 132208 | sqlite3WindowFunctions(); |
| | @@ -140637,16 +140711,10 @@ |
| 140637 | 140711 | if( db->autoCommit==0 ){ |
| 140638 | 140712 | /* Foreign key support may not be enabled or disabled while not |
| 140639 | 140713 | ** in auto-commit mode. */ |
| 140640 | 140714 | mask &= ~(SQLITE_ForeignKeys); |
| 140641 | 140715 | } |
| 140642 | | -#if SQLITE_USER_AUTHENTICATION |
| 140643 | | - if( db->auth.authLevel==UAUTH_User ){ |
| 140644 | | - /* Do not allow non-admin users to modify the schema arbitrarily */ |
| 140645 | | - mask &= ~(SQLITE_WriteSchema); |
| 140646 | | - } |
| 140647 | | -#endif |
| 140648 | 140716 | |
| 140649 | 140717 | if( sqlite3GetBoolean(zRight, 0) ){ |
| 140650 | 140718 | if( (mask & SQLITE_WriteSchema)==0 |
| 140651 | 140719 | || (db->flags & SQLITE_Defensive)==0 |
| 140652 | 140720 | ){ |
| | @@ -140778,11 +140846,12 @@ |
| 140778 | 140846 | pTab = sqliteHashData(k); |
| 140779 | 140847 | if( pTab->nCol==0 ){ |
| 140780 | 140848 | char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName); |
| 140781 | 140849 | if( zSql ){ |
| 140782 | 140850 | sqlite3_stmt *pDummy = 0; |
| 140783 | | - (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0); |
| 140851 | + (void)sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_DONT_LOG, |
| 140852 | + &pDummy, 0); |
| 140784 | 140853 | (void)sqlite3_finalize(pDummy); |
| 140785 | 140854 | sqlite3DbFree(db, zSql); |
| 140786 | 140855 | } |
| 140787 | 140856 | if( db->mallocFailed ){ |
| 140788 | 140857 | sqlite3ErrorMsg(db->pParse, "out of memory"); |
| | @@ -141259,11 +141328,11 @@ |
| 141259 | 141328 | sqlite3VdbeAddOp3(v, OP_Null, 0, 8, 8+cnt); |
| 141260 | 141329 | sqlite3ClearTempRegCache(pParse); |
| 141261 | 141330 | |
| 141262 | 141331 | /* Do the b-tree integrity checks */ |
| 141263 | 141332 | sqlite3VdbeAddOp4(v, OP_IntegrityCk, 1, cnt, 8, (char*)aRoot,P4_INTARRAY); |
| 141264 | | - sqlite3VdbeChangeP5(v, (u8)i); |
| 141333 | + sqlite3VdbeChangeP5(v, (u16)i); |
| 141265 | 141334 | addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v); |
| 141266 | 141335 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, |
| 141267 | 141336 | sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName), |
| 141268 | 141337 | P4_DYNAMIC); |
| 141269 | 141338 | sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3); |
| | @@ -142879,18 +142948,11 @@ |
| 142879 | 142948 | encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3; |
| 142880 | 142949 | if( encoding==0 ) encoding = SQLITE_UTF8; |
| 142881 | 142950 | #else |
| 142882 | 142951 | encoding = SQLITE_UTF8; |
| 142883 | 142952 | #endif |
| 142884 | | - if( db->nVdbeActive>0 && encoding!=ENC(db) |
| 142885 | | - && (db->mDbFlags & DBFLAG_Vacuum)==0 |
| 142886 | | - ){ |
| 142887 | | - rc = SQLITE_LOCKED; |
| 142888 | | - goto initone_error_out; |
| 142889 | | - }else{ |
| 142890 | | - sqlite3SetTextEncoding(db, encoding); |
| 142891 | | - } |
| 142953 | + sqlite3SetTextEncoding(db, encoding); |
| 142892 | 142954 | }else{ |
| 142893 | 142955 | /* If opening an attached database, the encoding much match ENC(db) */ |
| 142894 | 142956 | if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){ |
| 142895 | 142957 | sqlite3SetString(pzErrMsg, db, "attached databases must use the same" |
| 142896 | 142958 | " text encoding as main database"); |
| | @@ -147584,36 +147646,36 @@ |
| 147584 | 147646 | return pExpr; |
| 147585 | 147647 | } |
| 147586 | 147648 | if( pSubst->isOuterJoin ){ |
| 147587 | 147649 | ExprSetProperty(pNew, EP_CanBeNull); |
| 147588 | 147650 | } |
| 147651 | + if( pNew->op==TK_TRUEFALSE ){ |
| 147652 | + pNew->u.iValue = sqlite3ExprTruthValue(pNew); |
| 147653 | + pNew->op = TK_INTEGER; |
| 147654 | + ExprSetProperty(pNew, EP_IntValue); |
| 147655 | + } |
| 147656 | + |
| 147657 | + /* Ensure that the expression now has an implicit collation sequence, |
| 147658 | + ** just as it did when it was a column of a view or sub-query. */ |
| 147659 | + { |
| 147660 | + CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pNew); |
| 147661 | + CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, |
| 147662 | + pSubst->pCList->a[iColumn].pExpr |
| 147663 | + ); |
| 147664 | + if( pNat!=pColl || (pNew->op!=TK_COLUMN && pNew->op!=TK_COLLATE) ){ |
| 147665 | + pNew = sqlite3ExprAddCollateString(pSubst->pParse, pNew, |
| 147666 | + (pColl ? pColl->zName : "BINARY") |
| 147667 | + ); |
| 147668 | + } |
| 147669 | + } |
| 147670 | + ExprClearProperty(pNew, EP_Collate); |
| 147589 | 147671 | if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) ){ |
| 147590 | 147672 | sqlite3SetJoinExpr(pNew, pExpr->w.iJoin, |
| 147591 | 147673 | pExpr->flags & (EP_OuterON|EP_InnerON)); |
| 147592 | 147674 | } |
| 147593 | 147675 | sqlite3ExprDelete(db, pExpr); |
| 147594 | 147676 | pExpr = pNew; |
| 147595 | | - if( pExpr->op==TK_TRUEFALSE ){ |
| 147596 | | - pExpr->u.iValue = sqlite3ExprTruthValue(pExpr); |
| 147597 | | - pExpr->op = TK_INTEGER; |
| 147598 | | - ExprSetProperty(pExpr, EP_IntValue); |
| 147599 | | - } |
| 147600 | | - |
| 147601 | | - /* Ensure that the expression now has an implicit collation sequence, |
| 147602 | | - ** just as it did when it was a column of a view or sub-query. */ |
| 147603 | | - { |
| 147604 | | - CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr); |
| 147605 | | - CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, |
| 147606 | | - pSubst->pCList->a[iColumn].pExpr |
| 147607 | | - ); |
| 147608 | | - if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){ |
| 147609 | | - pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr, |
| 147610 | | - (pColl ? pColl->zName : "BINARY") |
| 147611 | | - ); |
| 147612 | | - } |
| 147613 | | - } |
| 147614 | | - ExprClearProperty(pExpr, EP_Collate); |
| 147615 | 147677 | } |
| 147616 | 147678 | } |
| 147617 | 147679 | }else{ |
| 147618 | 147680 | if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){ |
| 147619 | 147681 | pExpr->iTable = pSubst->iNewTable; |
| | @@ -148346,20 +148408,20 @@ |
| 148346 | 148408 | } |
| 148347 | 148409 | |
| 148348 | 148410 | /* Transfer the FROM clause terms from the subquery into the |
| 148349 | 148411 | ** outer query. |
| 148350 | 148412 | */ |
| 148413 | + iNewParent = pSubSrc->a[0].iCursor; |
| 148351 | 148414 | for(i=0; i<nSubSrc; i++){ |
| 148352 | 148415 | SrcItem *pItem = &pSrc->a[i+iFrom]; |
| 148353 | 148416 | assert( pItem->fg.isTabFunc==0 ); |
| 148354 | 148417 | assert( pItem->fg.isSubquery |
| 148355 | 148418 | || pItem->fg.fixedSchema |
| 148356 | 148419 | || pItem->u4.zDatabase==0 ); |
| 148357 | 148420 | if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing); |
| 148358 | 148421 | *pItem = pSubSrc->a[i]; |
| 148359 | 148422 | pItem->fg.jointype |= ltorj; |
| 148360 | | - iNewParent = pSubSrc->a[i].iCursor; |
| 148361 | 148423 | memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); |
| 148362 | 148424 | } |
| 148363 | 148425 | pSrc->a[iFrom].fg.jointype &= JT_LTORJ; |
| 148364 | 148426 | pSrc->a[iFrom].fg.jointype |= jointype | ltorj; |
| 148365 | 148427 | |
| | @@ -148395,10 +148457,11 @@ |
| 148395 | 148457 | pSub->pOrderBy = 0; |
| 148396 | 148458 | } |
| 148397 | 148459 | pWhere = pSub->pWhere; |
| 148398 | 148460 | pSub->pWhere = 0; |
| 148399 | 148461 | if( isOuterJoin>0 ){ |
| 148462 | + assert( pSubSrc->nSrc==1 ); |
| 148400 | 148463 | sqlite3SetJoinExpr(pWhere, iNewParent, EP_OuterON); |
| 148401 | 148464 | } |
| 148402 | 148465 | if( pWhere ){ |
| 148403 | 148466 | if( pParent->pWhere ){ |
| 148404 | 148467 | pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere); |
| | @@ -150498,11 +150561,11 @@ |
| 150498 | 150561 | } |
| 150499 | 150562 | sqlite3ReleaseTempReg(pParse, regSubtype); |
| 150500 | 150563 | } |
| 150501 | 150564 | sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i)); |
| 150502 | 150565 | sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF); |
| 150503 | | - sqlite3VdbeChangeP5(v, (u8)nArg); |
| 150566 | + sqlite3VdbeChangeP5(v, (u16)nArg); |
| 150504 | 150567 | sqlite3VdbeAddOp2(v, OP_Next, pF->iOBTab, iTop+1); VdbeCoverage(v); |
| 150505 | 150568 | sqlite3VdbeJumpHere(v, iTop); |
| 150506 | 150569 | sqlite3ReleaseTempRange(pParse, regAgg, nArg); |
| 150507 | 150570 | } |
| 150508 | 150571 | sqlite3VdbeAddOp2(v, OP_AggFinal, AggInfoFuncReg(pAggInfo,i), |
| | @@ -150661,11 +150724,11 @@ |
| 150661 | 150724 | sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, |
| 150662 | 150725 | (char *)pColl, P4_COLLSEQ); |
| 150663 | 150726 | } |
| 150664 | 150727 | sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i)); |
| 150665 | 150728 | sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF); |
| 150666 | | - sqlite3VdbeChangeP5(v, (u8)nArg); |
| 150729 | + sqlite3VdbeChangeP5(v, (u16)nArg); |
| 150667 | 150730 | sqlite3ReleaseTempRange(pParse, regAgg, nArg); |
| 150668 | 150731 | } |
| 150669 | 150732 | if( addrNext ){ |
| 150670 | 150733 | sqlite3VdbeResolveLabel(v, addrNext); |
| 150671 | 150734 | } |
| | @@ -151494,11 +151557,11 @@ |
| 151494 | 151557 | sqlite3TreeViewSelect(0, p, 0); |
| 151495 | 151558 | } |
| 151496 | 151559 | #endif |
| 151497 | 151560 | assert( pSubq->pSelect && (pSub->selFlags & SF_PushDown)!=0 ); |
| 151498 | 151561 | }else{ |
| 151499 | | - TREETRACE(0x4000,pParse,p,("WHERE-lcause push-down not possible\n")); |
| 151562 | + TREETRACE(0x4000,pParse,p,("WHERE-clause push-down not possible\n")); |
| 151500 | 151563 | } |
| 151501 | 151564 | |
| 151502 | 151565 | /* Convert unused result columns of the subquery into simple NULL |
| 151503 | 151566 | ** expressions, to avoid unneeded searching and computation. |
| 151504 | 151567 | ** tag-select-0440 |
| | @@ -154055,11 +154118,11 @@ |
| 154055 | 154118 | /* Set the P5 operand of the OP_Program instruction to non-zero if |
| 154056 | 154119 | ** recursive invocation of this trigger program is disallowed. Recursive |
| 154057 | 154120 | ** invocation is disallowed if (a) the sub-program is really a trigger, |
| 154058 | 154121 | ** not a foreign key action, and (b) the flag to enable recursive triggers |
| 154059 | 154122 | ** is clear. */ |
| 154060 | | - sqlite3VdbeChangeP5(v, (u8)bRecursive); |
| 154123 | + sqlite3VdbeChangeP5(v, (u16)bRecursive); |
| 154061 | 154124 | } |
| 154062 | 154125 | } |
| 154063 | 154126 | |
| 154064 | 154127 | /* |
| 154065 | 154128 | ** This is called to code the required FOR EACH ROW triggers for an operation |
| | @@ -158268,13 +158331,21 @@ |
| 158268 | 158331 | SQLITE_PRIVATE int sqlite3WhereExplainBloomFilter( |
| 158269 | 158332 | const Parse *pParse, /* Parse context */ |
| 158270 | 158333 | const WhereInfo *pWInfo, /* WHERE clause */ |
| 158271 | 158334 | const WhereLevel *pLevel /* Bloom filter on this level */ |
| 158272 | 158335 | ); |
| 158336 | +SQLITE_PRIVATE void sqlite3WhereAddExplainText( |
| 158337 | + Parse *pParse, /* Parse context */ |
| 158338 | + int addr, |
| 158339 | + SrcList *pTabList, /* Table list this loop refers to */ |
| 158340 | + WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 158341 | + u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 158342 | +); |
| 158273 | 158343 | #else |
| 158274 | 158344 | # define sqlite3WhereExplainOneScan(u,v,w,x) 0 |
| 158275 | 158345 | # define sqlite3WhereExplainBloomFilter(u,v,w) 0 |
| 158346 | +# define sqlite3WhereAddExplainText(u,v,w,x,y) |
| 158276 | 158347 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 158277 | 158348 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 158278 | 158349 | SQLITE_PRIVATE void sqlite3WhereAddScanStatus( |
| 158279 | 158350 | Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 158280 | 158351 | SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| | @@ -158472,42 +158543,42 @@ |
| 158472 | 158543 | } |
| 158473 | 158544 | sqlite3_str_append(pStr, ")", 1); |
| 158474 | 158545 | } |
| 158475 | 158546 | |
| 158476 | 158547 | /* |
| 158477 | | -** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 158478 | | -** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG |
| 158479 | | -** was defined at compile-time. If it is not a no-op, a single OP_Explain |
| 158480 | | -** opcode is added to the output to describe the table scan strategy in pLevel. |
| 158481 | | -** |
| 158482 | | -** If an OP_Explain opcode is added to the VM, its address is returned. |
| 158483 | | -** Otherwise, if no OP_Explain is coded, zero is returned. |
| 158548 | +** This function sets the P4 value of an existing OP_Explain opcode to |
| 158549 | +** text describing the loop in pLevel. If the OP_Explain opcode already has |
| 158550 | +** a P4 value, it is freed before it is overwritten. |
| 158484 | 158551 | */ |
| 158485 | | -SQLITE_PRIVATE int sqlite3WhereExplainOneScan( |
| 158552 | +SQLITE_PRIVATE void sqlite3WhereAddExplainText( |
| 158486 | 158553 | Parse *pParse, /* Parse context */ |
| 158554 | + int addr, /* Address of OP_Explain opcode */ |
| 158487 | 158555 | SrcList *pTabList, /* Table list this loop refers to */ |
| 158488 | 158556 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 158489 | 158557 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 158490 | 158558 | ){ |
| 158491 | | - int ret = 0; |
| 158492 | 158559 | #if !defined(SQLITE_DEBUG) |
| 158493 | 158560 | if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) ) |
| 158494 | 158561 | #endif |
| 158495 | 158562 | { |
| 158563 | + VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe, addr); |
| 158564 | + |
| 158496 | 158565 | SrcItem *pItem = &pTabList->a[pLevel->iFrom]; |
| 158497 | | - Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 158498 | 158566 | sqlite3 *db = pParse->db; /* Database handle */ |
| 158499 | 158567 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 158500 | 158568 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 158501 | 158569 | u32 flags; /* Flags that describe this loop */ |
| 158570 | +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN) |
| 158502 | 158571 | char *zMsg; /* Text to add to EQP output */ |
| 158572 | +#endif |
| 158503 | 158573 | StrAccum str; /* EQP output string */ |
| 158504 | 158574 | char zBuf[100]; /* Initial space for EQP output string */ |
| 158505 | 158575 | |
| 158576 | + if( db->mallocFailed ) return; |
| 158577 | + |
| 158506 | 158578 | pLoop = pLevel->pWLoop; |
| 158507 | 158579 | flags = pLoop->wsFlags; |
| 158508 | | - if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0; |
| 158509 | 158580 | |
| 158510 | 158581 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 158511 | 158582 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 158512 | 158583 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 158513 | 158584 | |
| | @@ -158527,11 +158598,11 @@ |
| 158527 | 158598 | } |
| 158528 | 158599 | }else if( flags & WHERE_PARTIALIDX ){ |
| 158529 | 158600 | zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; |
| 158530 | 158601 | }else if( flags & WHERE_AUTO_INDEX ){ |
| 158531 | 158602 | zFmt = "AUTOMATIC COVERING INDEX"; |
| 158532 | | - }else if( flags & WHERE_IDX_ONLY ){ |
| 158603 | + }else if( flags & (WHERE_IDX_ONLY|WHERE_EXPRIDX) ){ |
| 158533 | 158604 | zFmt = "COVERING INDEX %s"; |
| 158534 | 158605 | }else{ |
| 158535 | 158606 | zFmt = "INDEX %s"; |
| 158536 | 158607 | } |
| 158537 | 158608 | if( zFmt ){ |
| | @@ -158579,15 +158650,54 @@ |
| 158579 | 158650 | sqlite3LogEstToInt(pLoop->nOut)); |
| 158580 | 158651 | }else{ |
| 158581 | 158652 | sqlite3_str_append(&str, " (~1 row)", 9); |
| 158582 | 158653 | } |
| 158583 | 158654 | #endif |
| 158655 | +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN) |
| 158584 | 158656 | zMsg = sqlite3StrAccumFinish(&str); |
| 158585 | 158657 | sqlite3ExplainBreakpoint("",zMsg); |
| 158586 | | - ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v), |
| 158587 | | - pParse->addrExplain, pLoop->rRun, |
| 158588 | | - zMsg, P4_DYNAMIC); |
| 158658 | +#endif |
| 158659 | + |
| 158660 | + assert( pOp->opcode==OP_Explain ); |
| 158661 | + assert( pOp->p4type==P4_DYNAMIC || pOp->p4.z==0 ); |
| 158662 | + sqlite3DbFree(db, pOp->p4.z); |
| 158663 | + pOp->p4type = P4_DYNAMIC; |
| 158664 | + pOp->p4.z = sqlite3StrAccumFinish(&str); |
| 158665 | + } |
| 158666 | +} |
| 158667 | + |
| 158668 | + |
| 158669 | +/* |
| 158670 | +** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 158671 | +** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG |
| 158672 | +** was defined at compile-time. If it is not a no-op, a single OP_Explain |
| 158673 | +** opcode is added to the output to describe the table scan strategy in pLevel. |
| 158674 | +** |
| 158675 | +** If an OP_Explain opcode is added to the VM, its address is returned. |
| 158676 | +** Otherwise, if no OP_Explain is coded, zero is returned. |
| 158677 | +*/ |
| 158678 | +SQLITE_PRIVATE int sqlite3WhereExplainOneScan( |
| 158679 | + Parse *pParse, /* Parse context */ |
| 158680 | + SrcList *pTabList, /* Table list this loop refers to */ |
| 158681 | + WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 158682 | + u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 158683 | +){ |
| 158684 | + int ret = 0; |
| 158685 | +#if !defined(SQLITE_DEBUG) |
| 158686 | + if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) ) |
| 158687 | +#endif |
| 158688 | + { |
| 158689 | + if( (pLevel->pWLoop->wsFlags & WHERE_MULTI_OR)==0 |
| 158690 | + && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 |
| 158691 | + ){ |
| 158692 | + Vdbe *v = pParse->pVdbe; |
| 158693 | + int addr = sqlite3VdbeCurrentAddr(v); |
| 158694 | + ret = sqlite3VdbeAddOp3( |
| 158695 | + v, OP_Explain, addr, pParse->addrExplain, pLevel->pWLoop->rRun |
| 158696 | + ); |
| 158697 | + sqlite3WhereAddExplainText(pParse, addr, pTabList, pLevel, wctrlFlags); |
| 158698 | + } |
| 158589 | 158699 | } |
| 158590 | 158700 | return ret; |
| 158591 | 158701 | } |
| 158592 | 158702 | |
| 158593 | 158703 | /* |
| | @@ -158682,13 +158792,14 @@ |
| 158682 | 158792 | if( wsFlags & WHERE_INDEXED ){ |
| 158683 | 158793 | sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur); |
| 158684 | 158794 | } |
| 158685 | 158795 | }else{ |
| 158686 | 158796 | int addr; |
| 158797 | + VdbeOp *pOp; |
| 158687 | 158798 | assert( pSrclist->a[pLvl->iFrom].fg.isSubquery ); |
| 158688 | 158799 | addr = pSrclist->a[pLvl->iFrom].u4.pSubq->addrFillSub; |
| 158689 | | - VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-1); |
| 158800 | + pOp = sqlite3VdbeGetOp(v, addr-1); |
| 158690 | 158801 | assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine ); |
| 158691 | 158802 | assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr ); |
| 158692 | 158803 | sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1); |
| 158693 | 158804 | } |
| 158694 | 158805 | } |
| | @@ -158937,10 +159048,11 @@ |
| 158937 | 159048 | if( pOrigLhs ){ |
| 158938 | 159049 | sqlite3ExprListDelete(db, pOrigLhs); |
| 158939 | 159050 | pNew->pLeft->x.pList = pLhs; |
| 158940 | 159051 | } |
| 158941 | 159052 | pSelect->pEList = pRhs; |
| 159053 | + pSelect->selId = ++pParse->nSelect; /* Req'd for SubrtnSig validity */ |
| 158942 | 159054 | if( pLhs && pLhs->nExpr==1 ){ |
| 158943 | 159055 | /* Take care here not to generate a TK_VECTOR containing only a |
| 158944 | 159056 | ** single value. Since the parser never creates such a vector, some |
| 158945 | 159057 | ** of the subroutines do not handle this case. */ |
| 158946 | 159058 | Expr *p = pLhs->a[0].pExpr; |
| | @@ -164009,11 +164121,11 @@ |
| 164009 | 164121 | || pTerm->pExpr->w.iJoin != pSrc->iCursor |
| 164010 | 164122 | ){ |
| 164011 | 164123 | return 0; |
| 164012 | 164124 | } |
| 164013 | 164125 | if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0 |
| 164014 | | - && ExprHasProperty(pTerm->pExpr, EP_InnerON) |
| 164126 | + && NEVER(ExprHasProperty(pTerm->pExpr, EP_InnerON)) |
| 164015 | 164127 | ){ |
| 164016 | 164128 | return 0; |
| 164017 | 164129 | } |
| 164018 | 164130 | return 1; |
| 164019 | 164131 | } |
| | @@ -165502,11 +165614,11 @@ |
| 165502 | 165614 | return rc; |
| 165503 | 165615 | } |
| 165504 | 165616 | #endif /* SQLITE_ENABLE_STAT4 */ |
| 165505 | 165617 | |
| 165506 | 165618 | |
| 165507 | | -#ifdef WHERETRACE_ENABLED |
| 165619 | +#if defined(WHERETRACE_ENABLED) || defined(SQLITE_DEBUG) |
| 165508 | 165620 | /* |
| 165509 | 165621 | ** Print the content of a WhereTerm object |
| 165510 | 165622 | */ |
| 165511 | 165623 | SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){ |
| 165512 | 165624 | if( pTerm==0 ){ |
| | @@ -165545,10 +165657,13 @@ |
| 165545 | 165657 | sqlite3DebugPrintf(" iParent=%d", pTerm->iParent); |
| 165546 | 165658 | } |
| 165547 | 165659 | sqlite3DebugPrintf("\n"); |
| 165548 | 165660 | sqlite3TreeViewExpr(0, pTerm->pExpr, 0); |
| 165549 | 165661 | } |
| 165662 | +} |
| 165663 | +SQLITE_PRIVATE void sqlite3ShowWhereTerm(WhereTerm *pTerm){ |
| 165664 | + sqlite3WhereTermPrint(pTerm, 0); |
| 165550 | 165665 | } |
| 165551 | 165666 | #endif |
| 165552 | 165667 | |
| 165553 | 165668 | #ifdef WHERETRACE_ENABLED |
| 165554 | 165669 | /* |
| | @@ -166731,11 +166846,10 @@ |
| 166731 | 166846 | pParse = pWC->pWInfo->pParse; |
| 166732 | 166847 | while( pWhere->op==TK_AND ){ |
| 166733 | 166848 | if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0; |
| 166734 | 166849 | pWhere = pWhere->pRight; |
| 166735 | 166850 | } |
| 166736 | | - if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0; |
| 166737 | 166851 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 166738 | 166852 | Expr *pExpr; |
| 166739 | 166853 | pExpr = pTerm->pExpr; |
| 166740 | 166854 | if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab) |
| 166741 | 166855 | && ((jointype & JT_OUTER)==0 || ExprHasProperty(pExpr, EP_OuterON)) |
| | @@ -169392,11 +169506,11 @@ |
| 169392 | 169506 | break; |
| 169393 | 169507 | } |
| 169394 | 169508 | } |
| 169395 | 169509 | if( hasRightJoin |
| 169396 | 169510 | && ExprHasProperty(pTerm->pExpr, EP_InnerON) |
| 169397 | | - && pTerm->pExpr->w.iJoin==pItem->iCursor |
| 169511 | + && NEVER(pTerm->pExpr->w.iJoin==pItem->iCursor) |
| 169398 | 169512 | ){ |
| 169399 | 169513 | break; /* restriction (5) */ |
| 169400 | 169514 | } |
| 169401 | 169515 | } |
| 169402 | 169516 | if( pTerm<pEnd ) continue; |
| | @@ -170312,10 +170426,11 @@ |
| 170312 | 170426 | int pc, |
| 170313 | 170427 | VdbeOp *pOp |
| 170314 | 170428 | ){ |
| 170315 | 170429 | if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return; |
| 170316 | 170430 | sqlite3VdbePrintOp(0, pc, pOp); |
| 170431 | + sqlite3ShowWhereTerm(0); /* So compiler won't complain about unused func */ |
| 170317 | 170432 | } |
| 170318 | 170433 | #endif |
| 170319 | 170434 | |
| 170320 | 170435 | /* |
| 170321 | 170436 | ** Generate the end of the WHERE loop. See comments on |
| | @@ -170611,18 +170726,32 @@ |
| 170611 | 170726 | x = sqlite3TableColumnToIndex(pIdx, x); |
| 170612 | 170727 | if( x>=0 ){ |
| 170613 | 170728 | pOp->p2 = x; |
| 170614 | 170729 | pOp->p1 = pLevel->iIdxCur; |
| 170615 | 170730 | OpcodeRewriteTrace(db, k, pOp); |
| 170616 | | - }else{ |
| 170617 | | - /* Unable to translate the table reference into an index |
| 170618 | | - ** reference. Verify that this is harmless - that the |
| 170619 | | - ** table being referenced really is open. |
| 170620 | | - */ |
| 170731 | + }else if( pLoop->wsFlags & (WHERE_IDX_ONLY|WHERE_EXPRIDX) ){ |
| 170621 | 170732 | if( pLoop->wsFlags & WHERE_IDX_ONLY ){ |
| 170733 | + /* An error. pLoop is supposed to be a covering index loop, |
| 170734 | + ** and yet the VM code refers to a column of the table that |
| 170735 | + ** is not part of the index. */ |
| 170622 | 170736 | sqlite3ErrorMsg(pParse, "internal query planner error"); |
| 170623 | 170737 | pParse->rc = SQLITE_INTERNAL; |
| 170738 | + }else{ |
| 170739 | + /* The WHERE_EXPRIDX flag is set by the planner when it is likely |
| 170740 | + ** that pLoop is a covering index loop, but it is not possible |
| 170741 | + ** to be 100% sure. In this case, any OP_Explain opcode |
| 170742 | + ** corresponding to this loop describes the index as a "COVERING |
| 170743 | + ** INDEX". But, pOp proves that pLoop is not actually a covering |
| 170744 | + ** index loop. So clear the WHERE_EXPRIDX flag and rewrite the |
| 170745 | + ** text that accompanies the OP_Explain opcode, if any. */ |
| 170746 | + pLoop->wsFlags &= ~WHERE_EXPRIDX; |
| 170747 | + sqlite3WhereAddExplainText(pParse, |
| 170748 | + pLevel->addrBody-1, |
| 170749 | + pTabList, |
| 170750 | + pLevel, |
| 170751 | + pWInfo->wctrlFlags |
| 170752 | + ); |
| 170624 | 170753 | } |
| 170625 | 170754 | } |
| 170626 | 170755 | }else if( pOp->opcode==OP_Rowid ){ |
| 170627 | 170756 | pOp->p1 = pLevel->iIdxCur; |
| 170628 | 170757 | pOp->opcode = OP_IdxRowid; |
| | @@ -172326,10 +172455,11 @@ |
| 172326 | 172455 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 172327 | 172456 | FuncDef *pFunc = pWin->pWFunc; |
| 172328 | 172457 | int regArg; |
| 172329 | 172458 | int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin); |
| 172330 | 172459 | int i; |
| 172460 | + int addrIf = 0; |
| 172331 | 172461 | |
| 172332 | 172462 | assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED ); |
| 172333 | 172463 | |
| 172334 | 172464 | /* All OVER clauses in the same window function aggregate step must |
| 172335 | 172465 | ** be the same. */ |
| | @@ -172341,10 +172471,22 @@ |
| 172341 | 172471 | }else{ |
| 172342 | 172472 | sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i); |
| 172343 | 172473 | } |
| 172344 | 172474 | } |
| 172345 | 172475 | regArg = reg; |
| 172476 | + |
| 172477 | + if( pWin->pFilter ){ |
| 172478 | + int regTmp; |
| 172479 | + assert( ExprUseXList(pWin->pOwner) ); |
| 172480 | + assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr ); |
| 172481 | + assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 ); |
| 172482 | + regTmp = sqlite3GetTempReg(pParse); |
| 172483 | + sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp); |
| 172484 | + addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1); |
| 172485 | + VdbeCoverage(v); |
| 172486 | + sqlite3ReleaseTempReg(pParse, regTmp); |
| 172487 | + } |
| 172346 | 172488 | |
| 172347 | 172489 | if( pMWin->regStartRowid==0 |
| 172348 | 172490 | && (pFunc->funcFlags & SQLITE_FUNC_MINMAX) |
| 172349 | 172491 | && (pWin->eStart!=TK_UNBOUNDED) |
| 172350 | 172492 | ){ |
| | @@ -172361,29 +172503,17 @@ |
| 172361 | 172503 | sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp); |
| 172362 | 172504 | sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); |
| 172363 | 172505 | } |
| 172364 | 172506 | sqlite3VdbeJumpHere(v, addrIsNull); |
| 172365 | 172507 | }else if( pWin->regApp ){ |
| 172508 | + assert( pWin->pFilter==0 ); |
| 172366 | 172509 | assert( pFunc->zName==nth_valueName |
| 172367 | 172510 | || pFunc->zName==first_valueName |
| 172368 | 172511 | ); |
| 172369 | 172512 | assert( bInverse==0 || bInverse==1 ); |
| 172370 | 172513 | sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1); |
| 172371 | 172514 | }else if( pFunc->xSFunc!=noopStepFunc ){ |
| 172372 | | - int addrIf = 0; |
| 172373 | | - if( pWin->pFilter ){ |
| 172374 | | - int regTmp; |
| 172375 | | - assert( ExprUseXList(pWin->pOwner) ); |
| 172376 | | - assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr ); |
| 172377 | | - assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 ); |
| 172378 | | - regTmp = sqlite3GetTempReg(pParse); |
| 172379 | | - sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp); |
| 172380 | | - addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1); |
| 172381 | | - VdbeCoverage(v); |
| 172382 | | - sqlite3ReleaseTempReg(pParse, regTmp); |
| 172383 | | - } |
| 172384 | | - |
| 172385 | 172515 | if( pWin->bExprArgs ){ |
| 172386 | 172516 | int iOp = sqlite3VdbeCurrentAddr(v); |
| 172387 | 172517 | int iEnd; |
| 172388 | 172518 | |
| 172389 | 172519 | assert( ExprUseXList(pWin->pOwner) ); |
| | @@ -172406,16 +172536,17 @@ |
| 172406 | 172536 | sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ); |
| 172407 | 172537 | } |
| 172408 | 172538 | sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep, |
| 172409 | 172539 | bInverse, regArg, pWin->regAccum); |
| 172410 | 172540 | sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF); |
| 172411 | | - sqlite3VdbeChangeP5(v, (u8)nArg); |
| 172541 | + sqlite3VdbeChangeP5(v, (u16)nArg); |
| 172412 | 172542 | if( pWin->bExprArgs ){ |
| 172413 | 172543 | sqlite3ReleaseTempRange(pParse, regArg, nArg); |
| 172414 | 172544 | } |
| 172415 | | - if( addrIf ) sqlite3VdbeJumpHere(v, addrIf); |
| 172416 | 172545 | } |
| 172546 | + |
| 172547 | + if( addrIf ) sqlite3VdbeJumpHere(v, addrIf); |
| 172417 | 172548 | } |
| 172418 | 172549 | } |
| 172419 | 172550 | |
| 172420 | 172551 | /* |
| 172421 | 172552 | ** Values that may be passed as the second argument to windowCodeOp(). |
| | @@ -173837,10 +173968,17 @@ |
| 173837 | 173968 | ** Then the "b" IdList records the list "a,b,c". |
| 173838 | 173969 | */ |
| 173839 | 173970 | struct TrigEvent { int a; IdList * b; }; |
| 173840 | 173971 | |
| 173841 | 173972 | struct FrameBound { int eType; Expr *pExpr; }; |
| 173973 | + |
| 173974 | +/* |
| 173975 | +** Generate a syntax error |
| 173976 | +*/ |
| 173977 | +static void parserSyntaxError(Parse *pParse, Token *p){ |
| 173978 | + sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", p); |
| 173979 | +} |
| 173842 | 173980 | |
| 173843 | 173981 | /* |
| 173844 | 173982 | ** Disable lookaside memory allocation for objects that might be |
| 173845 | 173983 | ** shared across database connections. |
| 173846 | 173984 | */ |
| | @@ -177730,11 +177868,15 @@ |
| 177730 | 177868 | } |
| 177731 | 177869 | break; |
| 177732 | 177870 | case 84: /* cmd ::= select */ |
| 177733 | 177871 | { |
| 177734 | 177872 | SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0}; |
| 177735 | | - sqlite3Select(pParse, yymsp[0].minor.yy555, &dest); |
| 177873 | + if( (pParse->db->mDbFlags & DBFLAG_EncodingFixed)!=0 |
| 177874 | + || sqlite3ReadSchema(pParse)==SQLITE_OK |
| 177875 | + ){ |
| 177876 | + sqlite3Select(pParse, yymsp[0].minor.yy555, &dest); |
| 177877 | + } |
| 177736 | 177878 | sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy555); |
| 177737 | 177879 | } |
| 177738 | 177880 | break; |
| 177739 | 177881 | case 85: /* select ::= WITH wqlist selectnowith */ |
| 177740 | 177882 | {yymsp[-2].minor.yy555 = attachWithToSelect(pParse,yymsp[0].minor.yy555,yymsp[-1].minor.yy59);} |
| | @@ -178201,11 +178343,11 @@ |
| 178201 | 178343 | ** that look like this: #1 #2 ... These terms refer to registers |
| 178202 | 178344 | ** in the virtual machine. #N is the N-th register. */ |
| 178203 | 178345 | Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/ |
| 178204 | 178346 | assert( t.n>=2 ); |
| 178205 | 178347 | if( pParse->nested==0 ){ |
| 178206 | | - sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t); |
| 178348 | + parserSyntaxError(pParse, &t); |
| 178207 | 178349 | yymsp[0].minor.yy454 = 0; |
| 178208 | 178350 | }else{ |
| 178209 | 178351 | yymsp[0].minor.yy454 = sqlite3PExpr(pParse, TK_REGISTER, 0, 0); |
| 178210 | 178352 | if( yymsp[0].minor.yy454 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy454->iTable); |
| 178211 | 178353 | } |
| | @@ -179049,11 +179191,11 @@ |
| 179049 | 179191 | #define TOKEN yyminor |
| 179050 | 179192 | /************ Begin %syntax_error code ****************************************/ |
| 179051 | 179193 | |
| 179052 | 179194 | UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ |
| 179053 | 179195 | if( TOKEN.z[0] ){ |
| 179054 | | - sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); |
| 179196 | + parserSyntaxError(pParse, &TOKEN); |
| 179055 | 179197 | }else{ |
| 179056 | 179198 | sqlite3ErrorMsg(pParse, "incomplete input"); |
| 179057 | 179199 | } |
| 179058 | 179200 | /************ End %syntax_error code ******************************************/ |
| 179059 | 179201 | sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */ |
| | @@ -180540,11 +180682,13 @@ |
| 180540 | 180682 | } |
| 180541 | 180683 | if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){ |
| 180542 | 180684 | if( pParse->zErrMsg==0 ){ |
| 180543 | 180685 | pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); |
| 180544 | 180686 | } |
| 180545 | | - sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail); |
| 180687 | + if( (pParse->prepFlags & SQLITE_PREPARE_DONT_LOG)==0 ){ |
| 180688 | + sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail); |
| 180689 | + } |
| 180546 | 180690 | nErr++; |
| 180547 | 180691 | } |
| 180548 | 180692 | pParse->zTail = zSql; |
| 180549 | 180693 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 180550 | 180694 | sqlite3_free(pParse->apVtabLock); |
| | @@ -182513,14 +182657,10 @@ |
| 182513 | 182657 | #endif |
| 182514 | 182658 | |
| 182515 | 182659 | sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */ |
| 182516 | 182660 | sqlite3ValueFree(db->pErr); |
| 182517 | 182661 | sqlite3CloseExtensions(db); |
| 182518 | | -#if SQLITE_USER_AUTHENTICATION |
| 182519 | | - sqlite3_free(db->auth.zAuthUser); |
| 182520 | | - sqlite3_free(db->auth.zAuthPW); |
| 182521 | | -#endif |
| 182522 | 182662 | |
| 182523 | 182663 | db->eOpenState = SQLITE_STATE_ERROR; |
| 182524 | 182664 | |
| 182525 | 182665 | /* The temp-database schema is allocated differently from the other schema |
| 182526 | 182666 | ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). |
| | @@ -183951,12 +184091,12 @@ |
| 183951 | 184091 | # error SQLITE_MAX_COMPOUND_SELECT must be at least 2 |
| 183952 | 184092 | #endif |
| 183953 | 184093 | #if SQLITE_MAX_VDBE_OP<40 |
| 183954 | 184094 | # error SQLITE_MAX_VDBE_OP must be at least 40 |
| 183955 | 184095 | #endif |
| 183956 | | -#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127 |
| 183957 | | -# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127 |
| 184096 | +#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>32767 |
| 184097 | +# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 32767 |
| 183958 | 184098 | #endif |
| 183959 | 184099 | #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125 |
| 183960 | 184100 | # error SQLITE_MAX_ATTACHED must be between 0 and 125 |
| 183961 | 184101 | #endif |
| 183962 | 184102 | #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1 |
| | @@ -184019,12 +184159,12 @@ |
| 184019 | 184159 | } |
| 184020 | 184160 | oldLimit = db->aLimit[limitId]; |
| 184021 | 184161 | if( newLimit>=0 ){ /* IMP: R-52476-28732 */ |
| 184022 | 184162 | if( newLimit>aHardLimit[limitId] ){ |
| 184023 | 184163 | newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ |
| 184024 | | - }else if( newLimit<1 && limitId==SQLITE_LIMIT_LENGTH ){ |
| 184025 | | - newLimit = 1; |
| 184164 | + }else if( newLimit<SQLITE_MIN_LENGTH && limitId==SQLITE_LIMIT_LENGTH ){ |
| 184165 | + newLimit = SQLITE_MIN_LENGTH; |
| 184026 | 184166 | } |
| 184027 | 184167 | db->aLimit[limitId] = newLimit; |
| 184028 | 184168 | } |
| 184029 | 184169 | return oldLimit; /* IMP: R-53341-35419 */ |
| 184030 | 184170 | } |
| | @@ -185364,11 +185504,10 @@ |
| 185364 | 185504 | rc = x; |
| 185365 | 185505 | #if defined(SQLITE_DEBUG) |
| 185366 | 185506 | /* Invoke these debugging routines so that the compiler does not |
| 185367 | 185507 | ** issue "defined but not used" warnings. */ |
| 185368 | 185508 | if( x==9999 ){ |
| 185369 | | - sqlite3ShowExpr(0); |
| 185370 | 185509 | sqlite3ShowExpr(0); |
| 185371 | 185510 | sqlite3ShowExprList(0); |
| 185372 | 185511 | sqlite3ShowIdList(0); |
| 185373 | 185512 | sqlite3ShowSrcList(0); |
| 185374 | 185513 | sqlite3ShowWith(0); |
| | @@ -189796,14 +189935,19 @@ |
| 189796 | 189935 | |
| 189797 | 189936 | assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 ); |
| 189798 | 189937 | if( *p1==POS_COLUMN ){ |
| 189799 | 189938 | p1++; |
| 189800 | 189939 | p1 += fts3GetVarint32(p1, &iCol1); |
| 189940 | + /* iCol1==0 indicates corruption. Column 0 does not have a POS_COLUMN |
| 189941 | + ** entry, so this is actually end-of-doclist. */ |
| 189942 | + if( iCol1==0 ) return 0; |
| 189801 | 189943 | } |
| 189802 | 189944 | if( *p2==POS_COLUMN ){ |
| 189803 | 189945 | p2++; |
| 189804 | 189946 | p2 += fts3GetVarint32(p2, &iCol2); |
| 189947 | + /* As above, iCol2==0 indicates corruption. */ |
| 189948 | + if( iCol2==0 ) return 0; |
| 189805 | 189949 | } |
| 189806 | 189950 | |
| 189807 | 189951 | while( 1 ){ |
| 189808 | 189952 | if( iCol1==iCol2 ){ |
| 189809 | 189953 | char *pSave = p; |
| | @@ -192970,11 +193114,11 @@ |
| 192970 | 193114 | for(p=pExpr; p->pLeft; p=p->pLeft){ |
| 192971 | 193115 | assert( p->pRight->pPhrase->doclist.nList>0 ); |
| 192972 | 193116 | nTmp += p->pRight->pPhrase->doclist.nList; |
| 192973 | 193117 | } |
| 192974 | 193118 | nTmp += p->pPhrase->doclist.nList; |
| 192975 | | - aTmp = sqlite3_malloc64(nTmp*2); |
| 193119 | + aTmp = sqlite3_malloc64(nTmp*2 + FTS3_VARINT_MAX); |
| 192976 | 193120 | if( !aTmp ){ |
| 192977 | 193121 | *pRc = SQLITE_NOMEM; |
| 192978 | 193122 | res = 0; |
| 192979 | 193123 | }else{ |
| 192980 | 193124 | char *aPoslist = p->pPhrase->doclist.pList; |
| | @@ -193621,11 +193765,11 @@ |
| 193621 | 193765 | SQLITE_PRIVATE int sqlite3Fts3Corrupt(){ |
| 193622 | 193766 | return SQLITE_CORRUPT_VTAB; |
| 193623 | 193767 | } |
| 193624 | 193768 | #endif |
| 193625 | 193769 | |
| 193626 | | -#if !SQLITE_CORE |
| 193770 | +#if !defined(SQLITE_CORE) |
| 193627 | 193771 | /* |
| 193628 | 193772 | ** Initialize API pointer table, if required. |
| 193629 | 193773 | */ |
| 193630 | 193774 | #ifdef _WIN32 |
| 193631 | 193775 | __declspec(dllexport) |
| | @@ -194523,14 +194667,15 @@ |
| 194523 | 194667 | rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos); |
| 194524 | 194668 | if( rc==SQLITE_OK ){ |
| 194525 | 194669 | Fts3PhraseToken *pToken; |
| 194526 | 194670 | |
| 194527 | 194671 | p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken)); |
| 194528 | | - if( !p ) goto no_mem; |
| 194529 | | - |
| 194530 | 194672 | zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte); |
| 194531 | | - if( !zTemp ) goto no_mem; |
| 194673 | + if( !zTemp || !p ){ |
| 194674 | + rc = SQLITE_NOMEM; |
| 194675 | + goto getnextstring_out; |
| 194676 | + } |
| 194532 | 194677 | |
| 194533 | 194678 | assert( nToken==ii ); |
| 194534 | 194679 | pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii]; |
| 194535 | 194680 | memset(pToken, 0, sizeof(Fts3PhraseToken)); |
| 194536 | 194681 | |
| | @@ -194541,53 +194686,51 @@ |
| 194541 | 194686 | pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*'); |
| 194542 | 194687 | pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^'); |
| 194543 | 194688 | nToken = ii+1; |
| 194544 | 194689 | } |
| 194545 | 194690 | } |
| 194546 | | - |
| 194547 | | - pModule->xClose(pCursor); |
| 194548 | | - pCursor = 0; |
| 194549 | 194691 | } |
| 194550 | 194692 | |
| 194551 | 194693 | if( rc==SQLITE_DONE ){ |
| 194552 | 194694 | int jj; |
| 194553 | 194695 | char *zBuf = 0; |
| 194554 | 194696 | |
| 194555 | 194697 | p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp); |
| 194556 | | - if( !p ) goto no_mem; |
| 194698 | + if( !p ){ |
| 194699 | + rc = SQLITE_NOMEM; |
| 194700 | + goto getnextstring_out; |
| 194701 | + } |
| 194557 | 194702 | memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p); |
| 194558 | 194703 | p->eType = FTSQUERY_PHRASE; |
| 194559 | 194704 | p->pPhrase = (Fts3Phrase *)&p[1]; |
| 194560 | 194705 | p->pPhrase->iColumn = pParse->iDefaultCol; |
| 194561 | 194706 | p->pPhrase->nToken = nToken; |
| 194562 | 194707 | |
| 194563 | 194708 | zBuf = (char *)&p->pPhrase->aToken[nToken]; |
| 194709 | + assert( nTemp==0 || zTemp ); |
| 194564 | 194710 | if( zTemp ){ |
| 194565 | 194711 | memcpy(zBuf, zTemp, nTemp); |
| 194566 | | - sqlite3_free(zTemp); |
| 194567 | | - }else{ |
| 194568 | | - assert( nTemp==0 ); |
| 194569 | 194712 | } |
| 194570 | 194713 | |
| 194571 | 194714 | for(jj=0; jj<p->pPhrase->nToken; jj++){ |
| 194572 | 194715 | p->pPhrase->aToken[jj].z = zBuf; |
| 194573 | 194716 | zBuf += p->pPhrase->aToken[jj].n; |
| 194574 | 194717 | } |
| 194575 | 194718 | rc = SQLITE_OK; |
| 194576 | 194719 | } |
| 194577 | 194720 | |
| 194578 | | - *ppExpr = p; |
| 194579 | | - return rc; |
| 194580 | | -no_mem: |
| 194581 | | - |
| 194721 | + getnextstring_out: |
| 194582 | 194722 | if( pCursor ){ |
| 194583 | 194723 | pModule->xClose(pCursor); |
| 194584 | 194724 | } |
| 194585 | 194725 | sqlite3_free(zTemp); |
| 194586 | | - sqlite3_free(p); |
| 194587 | | - *ppExpr = 0; |
| 194588 | | - return SQLITE_NOMEM; |
| 194726 | + if( rc!=SQLITE_OK ){ |
| 194727 | + sqlite3_free(p); |
| 194728 | + p = 0; |
| 194729 | + } |
| 194730 | + *ppExpr = p; |
| 194731 | + return rc; |
| 194589 | 194732 | } |
| 194590 | 194733 | |
| 194591 | 194734 | /* |
| 194592 | 194735 | ** The output variable *ppExpr is populated with an allocated Fts3Expr |
| 194593 | 194736 | ** structure, or set to 0 if the end of the input buffer is reached. |
| | @@ -215395,12 +215538,12 @@ |
| 215395 | 215538 | #endif |
| 215396 | 215539 | } |
| 215397 | 215540 | sqlite3_str_append(pOut, "}", 1); |
| 215398 | 215541 | } |
| 215399 | 215542 | errCode = sqlite3_str_errcode(pOut); |
| 215400 | | - sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free); |
| 215401 | 215543 | sqlite3_result_error_code(ctx, errCode); |
| 215544 | + sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free); |
| 215402 | 215545 | } |
| 215403 | 215546 | |
| 215404 | 215547 | /* This routine implements an SQL function that returns the "depth" parameter |
| 215405 | 215548 | ** from the front of a blob that is an r-tree node. For example: |
| 215406 | 215549 | ** |
| | @@ -217912,11 +218055,11 @@ |
| 217912 | 218055 | return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY, |
| 217913 | 218056 | (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback |
| 217914 | 218057 | ); |
| 217915 | 218058 | } |
| 217916 | 218059 | |
| 217917 | | -#if !SQLITE_CORE |
| 218060 | +#ifndef SQLITE_CORE |
| 217918 | 218061 | #ifdef _WIN32 |
| 217919 | 218062 | __declspec(dllexport) |
| 217920 | 218063 | #endif |
| 217921 | 218064 | SQLITE_API int sqlite3_rtree_init( |
| 217922 | 218065 | sqlite3 *db, |
| | @@ -218503,11 +218646,11 @@ |
| 218503 | 218646 | } |
| 218504 | 218647 | |
| 218505 | 218648 | return rc; |
| 218506 | 218649 | } |
| 218507 | 218650 | |
| 218508 | | -#if !SQLITE_CORE |
| 218651 | +#ifndef SQLITE_CORE |
| 218509 | 218652 | #ifdef _WIN32 |
| 218510 | 218653 | __declspec(dllexport) |
| 218511 | 218654 | #endif |
| 218512 | 218655 | SQLITE_API int sqlite3_icu_init( |
| 218513 | 218656 | sqlite3 *db, |
| | @@ -226177,10 +226320,12 @@ |
| 226177 | 226320 | if( (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK && pData ){ |
| 226178 | 226321 | unsigned char *aPage = sqlite3PagerGetData(pDbPage); |
| 226179 | 226322 | memcpy(aPage, pData, szPage); |
| 226180 | 226323 | pTab->pgnoTrunc = 0; |
| 226181 | 226324 | } |
| 226325 | + }else{ |
| 226326 | + pTab->pgnoTrunc = 0; |
| 226182 | 226327 | } |
| 226183 | 226328 | sqlite3PagerUnref(pDbPage); |
| 226184 | 226329 | return rc; |
| 226185 | 226330 | |
| 226186 | 226331 | update_fail: |
| | @@ -226210,11 +226355,15 @@ |
| 226210 | 226355 | static int dbpageSync(sqlite3_vtab *pVtab){ |
| 226211 | 226356 | DbpageTable *pTab = (DbpageTable *)pVtab; |
| 226212 | 226357 | if( pTab->pgnoTrunc>0 ){ |
| 226213 | 226358 | Btree *pBt = pTab->db->aDb[pTab->iDbTrunc].pBt; |
| 226214 | 226359 | Pager *pPager = sqlite3BtreePager(pBt); |
| 226215 | | - sqlite3PagerTruncateImage(pPager, pTab->pgnoTrunc); |
| 226360 | + sqlite3BtreeEnter(pBt); |
| 226361 | + if( pTab->pgnoTrunc<sqlite3BtreeLastPage(pBt) ){ |
| 226362 | + sqlite3PagerTruncateImage(pPager, pTab->pgnoTrunc); |
| 226363 | + } |
| 226364 | + sqlite3BtreeLeave(pBt); |
| 226216 | 226365 | } |
| 226217 | 226366 | pTab->pgnoTrunc = 0; |
| 226218 | 226367 | return SQLITE_OK; |
| 226219 | 226368 | } |
| 226220 | 226369 | |
| | @@ -232804,20 +232953,46 @@ |
| 232804 | 232953 | #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */ |
| 232805 | 232954 | |
| 232806 | 232955 | /************** End of sqlite3session.c **************************************/ |
| 232807 | 232956 | /************** Begin file fts5.c ********************************************/ |
| 232808 | 232957 | |
| 232809 | | - |
| 232958 | +/* |
| 232959 | +** This, the "fts5.c" source file, is a composite file that is itself |
| 232960 | +** assembled from the following files: |
| 232961 | +** |
| 232962 | +** fts5.h |
| 232963 | +** fts5Int.h |
| 232964 | +** fts5parse.h <--- Generated from fts5parse.y by Lemon |
| 232965 | +** fts5parse.c <--- Generated from fts5parse.y by Lemon |
| 232966 | +** fts5_aux.c |
| 232967 | +** fts5_buffer.c |
| 232968 | +** fts5_config.c |
| 232969 | +** fts5_expr.c |
| 232970 | +** fts5_hash.c |
| 232971 | +** fts5_index.c |
| 232972 | +** fts5_main.c |
| 232973 | +** fts5_storage.c |
| 232974 | +** fts5_tokenize.c |
| 232975 | +** fts5_unicode2.c |
| 232976 | +** fts5_varint.c |
| 232977 | +** fts5_vocab.c |
| 232978 | +*/ |
| 232810 | 232979 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) |
| 232811 | 232980 | |
| 232812 | 232981 | #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) |
| 232813 | 232982 | # define NDEBUG 1 |
| 232814 | 232983 | #endif |
| 232815 | 232984 | #if defined(NDEBUG) && defined(SQLITE_DEBUG) |
| 232816 | 232985 | # undef NDEBUG |
| 232817 | 232986 | #endif |
| 232818 | 232987 | |
| 232988 | +#ifdef HAVE_STDINT_H |
| 232989 | +/* #include <stdint.h> */ |
| 232990 | +#endif |
| 232991 | +#ifdef HAVE_INTTYPES_H |
| 232992 | +/* #include <inttypes.h> */ |
| 232993 | +#endif |
| 232819 | 232994 | /* |
| 232820 | 232995 | ** 2014 May 31 |
| 232821 | 232996 | ** |
| 232822 | 232997 | ** The author disclaims copyright to this source code. In place of |
| 232823 | 232998 | ** a legal notice, here is a blessing: |
| | @@ -233114,17 +233289,32 @@ |
| 233114 | 233289 | ** This is used to access token iToken of phrase hit iIdx within the |
| 233115 | 233290 | ** current row. If iIdx is less than zero or greater than or equal to the |
| 233116 | 233291 | ** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise, |
| 233117 | 233292 | ** output variable (*ppToken) is set to point to a buffer containing the |
| 233118 | 233293 | ** matching document token, and (*pnToken) to the size of that buffer in |
| 233119 | | -** bytes. This API is not available if the specified token matches a |
| 233120 | | -** prefix query term. In that case both output variables are always set |
| 233121 | | -** to 0. |
| 233294 | +** bytes. |
| 233122 | 233295 | ** |
| 233123 | 233296 | ** The output text is not a copy of the document text that was tokenized. |
| 233124 | 233297 | ** It is the output of the tokenizer module. For tokendata=1 tables, this |
| 233125 | 233298 | ** includes any embedded 0x00 and trailing data. |
| 233299 | +** |
| 233300 | +** This API may be slow in some cases if the token identified by parameters |
| 233301 | +** iIdx and iToken matched a prefix token in the query. In most cases, the |
| 233302 | +** first call to this API for each prefix token in the query is forced |
| 233303 | +** to scan the portion of the full-text index that matches the prefix |
| 233304 | +** token to collect the extra data required by this API. If the prefix |
| 233305 | +** token matches a large number of token instances in the document set, |
| 233306 | +** this may be a performance problem. |
| 233307 | +** |
| 233308 | +** If the user knows in advance that a query may use this API for a |
| 233309 | +** prefix token, FTS5 may be configured to collect all required data as part |
| 233310 | +** of the initial querying of the full-text index, avoiding the second scan |
| 233311 | +** entirely. This also causes prefix queries that do not use this API to |
| 233312 | +** run more slowly and use more memory. FTS5 may be configured in this way |
| 233313 | +** either on a per-table basis using the [FTS5 insttoken | 'insttoken'] |
| 233314 | +** option, or on a per-query basis using the |
| 233315 | +** [fts5_insttoken | fts5_insttoken()] user function. |
| 233126 | 233316 | ** |
| 233127 | 233317 | ** This API can be quite slow if used with an FTS5 table created with the |
| 233128 | 233318 | ** "detail=none" or "detail=column" option. |
| 233129 | 233319 | ** |
| 233130 | 233320 | ** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale) |
| | @@ -233803,11 +233993,12 @@ |
| 233803 | 233993 | int nUsermerge; /* 'usermerge' setting */ |
| 233804 | 233994 | int nHashSize; /* Bytes of memory for in-memory hash */ |
| 233805 | 233995 | char *zRank; /* Name of rank function */ |
| 233806 | 233996 | char *zRankArgs; /* Arguments to rank function */ |
| 233807 | 233997 | int bSecureDelete; /* 'secure-delete' */ |
| 233808 | | - int nDeleteMerge; /* 'deletemerge' */ |
| 233998 | + int nDeleteMerge; /* 'deletemerge' */ |
| 233999 | + int bPrefixInsttoken; /* 'prefix-insttoken' */ |
| 233809 | 234000 | |
| 233810 | 234001 | /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */ |
| 233811 | 234002 | char **pzErrmsg; |
| 233812 | 234003 | |
| 233813 | 234004 | #ifdef SQLITE_DEBUG |
| | @@ -234060,11 +234251,18 @@ |
| 234060 | 234251 | static int sqlite3Fts5StructureTest(Fts5Index*, void*); |
| 234061 | 234252 | |
| 234062 | 234253 | /* |
| 234063 | 234254 | ** Used by xInstToken(): |
| 234064 | 234255 | */ |
| 234065 | | -static int sqlite3Fts5IterToken(Fts5IndexIter*, i64, int, int, const char**, int*); |
| 234256 | +static int sqlite3Fts5IterToken( |
| 234257 | + Fts5IndexIter *pIndexIter, |
| 234258 | + const char *pToken, int nToken, |
| 234259 | + i64 iRowid, |
| 234260 | + int iCol, |
| 234261 | + int iOff, |
| 234262 | + const char **ppOut, int *pnOut |
| 234263 | +); |
| 234066 | 234264 | |
| 234067 | 234265 | /* |
| 234068 | 234266 | ** Insert or remove data to or from the index. Each time a document is |
| 234069 | 234267 | ** added to or removed from the index, this function is called one or more |
| 234070 | 234268 | ** times. |
| | @@ -238274,10 +238472,23 @@ |
| 238274 | 238472 | if( bVal<0 ){ |
| 238275 | 238473 | *pbBadkey = 1; |
| 238276 | 238474 | }else{ |
| 238277 | 238475 | pConfig->bSecureDelete = (bVal ? 1 : 0); |
| 238278 | 238476 | } |
| 238477 | + } |
| 238478 | + |
| 238479 | + else if( 0==sqlite3_stricmp(zKey, "insttoken") ){ |
| 238480 | + int bVal = -1; |
| 238481 | + if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ |
| 238482 | + bVal = sqlite3_value_int(pVal); |
| 238483 | + } |
| 238484 | + if( bVal<0 ){ |
| 238485 | + *pbBadkey = 1; |
| 238486 | + }else{ |
| 238487 | + pConfig->bPrefixInsttoken = (bVal ? 1 : 0); |
| 238488 | + } |
| 238489 | + |
| 238279 | 238490 | }else{ |
| 238280 | 238491 | *pbBadkey = 1; |
| 238281 | 238492 | } |
| 238282 | 238493 | return rc; |
| 238283 | 238494 | } |
| | @@ -241409,11 +241620,11 @@ |
| 241409 | 241620 | && memcmp(pT->pTerm, pToken, pT->nQueryTerm)==0 |
| 241410 | 241621 | ){ |
| 241411 | 241622 | int rc = sqlite3Fts5PoslistWriterAppend( |
| 241412 | 241623 | &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff |
| 241413 | 241624 | ); |
| 241414 | | - if( rc==SQLITE_OK && pExpr->pConfig->bTokendata && !pT->bPrefix ){ |
| 241625 | + if( rc==SQLITE_OK && (pExpr->pConfig->bTokendata || pT->bPrefix) ){ |
| 241415 | 241626 | int iCol = p->iOff>>32; |
| 241416 | 241627 | int iTokOff = p->iOff & 0x7FFFFFFF; |
| 241417 | 241628 | rc = sqlite3Fts5IndexIterWriteTokendata( |
| 241418 | 241629 | pT->pIter, pToken, nToken, iRowid, iCol, iTokOff |
| 241419 | 241630 | ); |
| | @@ -241602,19 +241813,18 @@ |
| 241602 | 241813 | pPhrase = pExpr->apExprPhrase[iPhrase]; |
| 241603 | 241814 | if( iToken<0 || iToken>=pPhrase->nTerm ){ |
| 241604 | 241815 | return SQLITE_RANGE; |
| 241605 | 241816 | } |
| 241606 | 241817 | pTerm = &pPhrase->aTerm[iToken]; |
| 241607 | | - if( pTerm->bPrefix==0 ){ |
| 241608 | | - if( pExpr->pConfig->bTokendata ){ |
| 241609 | | - rc = sqlite3Fts5IterToken( |
| 241610 | | - pTerm->pIter, iRowid, iCol, iOff+iToken, ppOut, pnOut |
| 241611 | | - ); |
| 241612 | | - }else{ |
| 241613 | | - *ppOut = pTerm->pTerm; |
| 241614 | | - *pnOut = pTerm->nFullTerm; |
| 241615 | | - } |
| 241818 | + if( pExpr->pConfig->bTokendata || pTerm->bPrefix ){ |
| 241819 | + rc = sqlite3Fts5IterToken( |
| 241820 | + pTerm->pIter, pTerm->pTerm, pTerm->nQueryTerm, |
| 241821 | + iRowid, iCol, iOff+iToken, ppOut, pnOut |
| 241822 | + ); |
| 241823 | + }else{ |
| 241824 | + *ppOut = pTerm->pTerm; |
| 241825 | + *pnOut = pTerm->nFullTerm; |
| 241616 | 241826 | } |
| 241617 | 241827 | return rc; |
| 241618 | 241828 | } |
| 241619 | 241829 | |
| 241620 | 241830 | /* |
| | @@ -248425,10 +248635,387 @@ |
| 248425 | 248635 | fts5BufferFree(&tmp); |
| 248426 | 248636 | memset(&out.p[out.n], 0, FTS5_DATA_ZERO_PADDING); |
| 248427 | 248637 | *p1 = out; |
| 248428 | 248638 | } |
| 248429 | 248639 | |
| 248640 | + |
| 248641 | +/* |
| 248642 | +** Iterate through a range of entries in the FTS index, invoking the xVisit |
| 248643 | +** callback for each of them. |
| 248644 | +** |
| 248645 | +** Parameter pToken points to an nToken buffer containing an FTS index term |
| 248646 | +** (i.e. a document term with the preceding 1 byte index identifier - |
| 248647 | +** FTS5_MAIN_PREFIX or similar). If bPrefix is true, then the call visits |
| 248648 | +** all entries for terms that have pToken/nToken as a prefix. If bPrefix |
| 248649 | +** is false, then only entries with pToken/nToken as the entire key are |
| 248650 | +** visited. |
| 248651 | +** |
| 248652 | +** If the current table is a tokendata=1 table, then if bPrefix is true then |
| 248653 | +** each index term is treated separately. However, if bPrefix is false, then |
| 248654 | +** all index terms corresponding to pToken/nToken are collapsed into a single |
| 248655 | +** term before the callback is invoked. |
| 248656 | +** |
| 248657 | +** The callback invoked for each entry visited is specified by paramter xVisit. |
| 248658 | +** Each time it is invoked, it is passed a pointer to the Fts5Index object, |
| 248659 | +** a copy of the 7th paramter to this function (pCtx) and a pointer to the |
| 248660 | +** iterator that indicates the current entry. If the current entry is the |
| 248661 | +** first with a new term (i.e. different from that of the previous entry, |
| 248662 | +** including the very first term), then the final two parameters are passed |
| 248663 | +** a pointer to the term and its size in bytes, respectively. If the current |
| 248664 | +** entry is not the first associated with its term, these two parameters |
| 248665 | +** are passed 0. |
| 248666 | +** |
| 248667 | +** If parameter pColset is not NULL, then it is used to filter entries before |
| 248668 | +** the callback is invoked. |
| 248669 | +*/ |
| 248670 | +static int fts5VisitEntries( |
| 248671 | + Fts5Index *p, /* Fts5 index object */ |
| 248672 | + Fts5Colset *pColset, /* Columns filter to apply, or NULL */ |
| 248673 | + u8 *pToken, /* Buffer containing token */ |
| 248674 | + int nToken, /* Size of buffer pToken in bytes */ |
| 248675 | + int bPrefix, /* True for a prefix scan */ |
| 248676 | + void (*xVisit)(Fts5Index*, void *pCtx, Fts5Iter *pIter, const u8*, int), |
| 248677 | + void *pCtx /* Passed as second argument to xVisit() */ |
| 248678 | +){ |
| 248679 | + const int flags = (bPrefix ? FTS5INDEX_QUERY_SCAN : 0) |
| 248680 | + | FTS5INDEX_QUERY_SKIPEMPTY |
| 248681 | + | FTS5INDEX_QUERY_NOOUTPUT; |
| 248682 | + Fts5Iter *p1 = 0; /* Iterator used to gather data from index */ |
| 248683 | + int bNewTerm = 1; |
| 248684 | + Fts5Structure *pStruct = fts5StructureRead(p); |
| 248685 | + |
| 248686 | + fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1); |
| 248687 | + fts5IterSetOutputCb(&p->rc, p1); |
| 248688 | + for( /* no-op */ ; |
| 248689 | + fts5MultiIterEof(p, p1)==0; |
| 248690 | + fts5MultiIterNext2(p, p1, &bNewTerm) |
| 248691 | + ){ |
| 248692 | + Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ]; |
| 248693 | + int nNew = 0; |
| 248694 | + const u8 *pNew = 0; |
| 248695 | + |
| 248696 | + p1->xSetOutputs(p1, pSeg); |
| 248697 | + if( p->rc ) break; |
| 248698 | + |
| 248699 | + if( bNewTerm ){ |
| 248700 | + nNew = pSeg->term.n; |
| 248701 | + pNew = pSeg->term.p; |
| 248702 | + if( nNew<nToken || memcmp(pToken, pNew, nToken) ) break; |
| 248703 | + } |
| 248704 | + |
| 248705 | + xVisit(p, pCtx, p1, pNew, nNew); |
| 248706 | + } |
| 248707 | + fts5MultiIterFree(p1); |
| 248708 | + |
| 248709 | + fts5StructureRelease(pStruct); |
| 248710 | + return p->rc; |
| 248711 | +} |
| 248712 | + |
| 248713 | + |
| 248714 | +/* |
| 248715 | +** Usually, a tokendata=1 iterator (struct Fts5TokenDataIter) accumulates an |
| 248716 | +** array of these for each row it visits (so all iRowid fields are the same). |
| 248717 | +** Or, for an iterator used by an "ORDER BY rank" query, it accumulates an |
| 248718 | +** array of these for the entire query (in which case iRowid fields may take |
| 248719 | +** a variety of values). |
| 248720 | +** |
| 248721 | +** Each instance in the array indicates the iterator (and therefore term) |
| 248722 | +** associated with position iPos of rowid iRowid. This is used by the |
| 248723 | +** xInstToken() API. |
| 248724 | +** |
| 248725 | +** iRowid: |
| 248726 | +** Rowid for the current entry. |
| 248727 | +** |
| 248728 | +** iPos: |
| 248729 | +** Position of current entry within row. In the usual ((iCol<<32)+iOff) |
| 248730 | +** format (e.g. see macros FTS5_POS2COLUMN() and FTS5_POS2OFFSET()). |
| 248731 | +** |
| 248732 | +** iIter: |
| 248733 | +** If the Fts5TokenDataIter iterator that the entry is part of is |
| 248734 | +** actually an iterator (i.e. with nIter>0, not just a container for |
| 248735 | +** Fts5TokenDataMap structures), then this variable is an index into |
| 248736 | +** the apIter[] array. The corresponding term is that which the iterator |
| 248737 | +** at apIter[iIter] currently points to. |
| 248738 | +** |
| 248739 | +** Or, if the Fts5TokenDataIter iterator is just a container object |
| 248740 | +** (nIter==0), then iIter is an index into the term.p[] buffer where |
| 248741 | +** the term is stored. |
| 248742 | +** |
| 248743 | +** nByte: |
| 248744 | +** In the case where iIter is an index into term.p[], this variable |
| 248745 | +** is the size of the term in bytes. If iIter is an index into apIter[], |
| 248746 | +** this variable is unused. |
| 248747 | +*/ |
| 248748 | +struct Fts5TokenDataMap { |
| 248749 | + i64 iRowid; /* Row this token is located in */ |
| 248750 | + i64 iPos; /* Position of token */ |
| 248751 | + int iIter; /* Iterator token was read from */ |
| 248752 | + int nByte; /* Length of token in bytes (or 0) */ |
| 248753 | +}; |
| 248754 | + |
| 248755 | +/* |
| 248756 | +** An object used to supplement Fts5Iter for tokendata=1 iterators. |
| 248757 | +** |
| 248758 | +** This object serves two purposes. The first is as a container for an array |
| 248759 | +** of Fts5TokenDataMap structures, which are used to find the token required |
| 248760 | +** when the xInstToken() API is used. This is done by the nMapAlloc, nMap and |
| 248761 | +** aMap[] variables. |
| 248762 | +*/ |
| 248763 | +struct Fts5TokenDataIter { |
| 248764 | + int nMapAlloc; /* Allocated size of aMap[] in entries */ |
| 248765 | + int nMap; /* Number of valid entries in aMap[] */ |
| 248766 | + Fts5TokenDataMap *aMap; /* Array of (rowid+pos -> token) mappings */ |
| 248767 | + |
| 248768 | + /* The following are used for prefix-queries only. */ |
| 248769 | + Fts5Buffer terms; |
| 248770 | + |
| 248771 | + /* The following are used for other full-token tokendata queries only. */ |
| 248772 | + int nIter; |
| 248773 | + int nIterAlloc; |
| 248774 | + Fts5PoslistReader *aPoslistReader; |
| 248775 | + int *aPoslistToIter; |
| 248776 | + Fts5Iter *apIter[1]; |
| 248777 | +}; |
| 248778 | + |
| 248779 | +/* |
| 248780 | +** The two input arrays - a1[] and a2[] - are in sorted order. This function |
| 248781 | +** merges the two arrays together and writes the result to output array |
| 248782 | +** aOut[]. aOut[] is guaranteed to be large enough to hold the result. |
| 248783 | +** |
| 248784 | +** Duplicate entries are copied into the output. So the size of the output |
| 248785 | +** array is always (n1+n2) entries. |
| 248786 | +*/ |
| 248787 | +static void fts5TokendataMerge( |
| 248788 | + Fts5TokenDataMap *a1, int n1, /* Input array 1 */ |
| 248789 | + Fts5TokenDataMap *a2, int n2, /* Input array 2 */ |
| 248790 | + Fts5TokenDataMap *aOut /* Output array */ |
| 248791 | +){ |
| 248792 | + int i1 = 0; |
| 248793 | + int i2 = 0; |
| 248794 | + |
| 248795 | + assert( n1>=0 && n2>=0 ); |
| 248796 | + while( i1<n1 || i2<n2 ){ |
| 248797 | + Fts5TokenDataMap *pOut = &aOut[i1+i2]; |
| 248798 | + if( i2>=n2 || (i1<n1 && ( |
| 248799 | + a1[i1].iRowid<a2[i2].iRowid |
| 248800 | + || (a1[i1].iRowid==a2[i2].iRowid && a1[i1].iPos<=a2[i2].iPos) |
| 248801 | + ))){ |
| 248802 | + memcpy(pOut, &a1[i1], sizeof(Fts5TokenDataMap)); |
| 248803 | + i1++; |
| 248804 | + }else{ |
| 248805 | + memcpy(pOut, &a2[i2], sizeof(Fts5TokenDataMap)); |
| 248806 | + i2++; |
| 248807 | + } |
| 248808 | + } |
| 248809 | +} |
| 248810 | + |
| 248811 | + |
| 248812 | +/* |
| 248813 | +** Append a mapping to the token-map belonging to object pT. |
| 248814 | +*/ |
| 248815 | +static void fts5TokendataIterAppendMap( |
| 248816 | + Fts5Index *p, |
| 248817 | + Fts5TokenDataIter *pT, |
| 248818 | + int iIter, |
| 248819 | + int nByte, |
| 248820 | + i64 iRowid, |
| 248821 | + i64 iPos |
| 248822 | +){ |
| 248823 | + if( p->rc==SQLITE_OK ){ |
| 248824 | + if( pT->nMap==pT->nMapAlloc ){ |
| 248825 | + int nNew = pT->nMapAlloc ? pT->nMapAlloc*2 : 64; |
| 248826 | + int nAlloc = nNew * sizeof(Fts5TokenDataMap); |
| 248827 | + Fts5TokenDataMap *aNew; |
| 248828 | + |
| 248829 | + aNew = (Fts5TokenDataMap*)sqlite3_realloc(pT->aMap, nAlloc); |
| 248830 | + if( aNew==0 ){ |
| 248831 | + p->rc = SQLITE_NOMEM; |
| 248832 | + return; |
| 248833 | + } |
| 248834 | + |
| 248835 | + pT->aMap = aNew; |
| 248836 | + pT->nMapAlloc = nNew; |
| 248837 | + } |
| 248838 | + |
| 248839 | + pT->aMap[pT->nMap].iRowid = iRowid; |
| 248840 | + pT->aMap[pT->nMap].iPos = iPos; |
| 248841 | + pT->aMap[pT->nMap].iIter = iIter; |
| 248842 | + pT->aMap[pT->nMap].nByte = nByte; |
| 248843 | + pT->nMap++; |
| 248844 | + } |
| 248845 | +} |
| 248846 | + |
| 248847 | +/* |
| 248848 | +** Sort the contents of the pT->aMap[] array. |
| 248849 | +** |
| 248850 | +** The sorting algorithm requries a malloc(). If this fails, an error code |
| 248851 | +** is left in Fts5Index.rc before returning. |
| 248852 | +*/ |
| 248853 | +static void fts5TokendataIterSortMap(Fts5Index *p, Fts5TokenDataIter *pT){ |
| 248854 | + Fts5TokenDataMap *aTmp = 0; |
| 248855 | + int nByte = pT->nMap * sizeof(Fts5TokenDataMap); |
| 248856 | + |
| 248857 | + aTmp = (Fts5TokenDataMap*)sqlite3Fts5MallocZero(&p->rc, nByte); |
| 248858 | + if( aTmp ){ |
| 248859 | + Fts5TokenDataMap *a1 = pT->aMap; |
| 248860 | + Fts5TokenDataMap *a2 = aTmp; |
| 248861 | + i64 nHalf; |
| 248862 | + |
| 248863 | + for(nHalf=1; nHalf<pT->nMap; nHalf=nHalf*2){ |
| 248864 | + int i1; |
| 248865 | + for(i1=0; i1<pT->nMap; i1+=(nHalf*2)){ |
| 248866 | + int n1 = MIN(nHalf, pT->nMap-i1); |
| 248867 | + int n2 = MIN(nHalf, pT->nMap-i1-n1); |
| 248868 | + fts5TokendataMerge(&a1[i1], n1, &a1[i1+n1], n2, &a2[i1]); |
| 248869 | + } |
| 248870 | + SWAPVAL(Fts5TokenDataMap*, a1, a2); |
| 248871 | + } |
| 248872 | + |
| 248873 | + if( a1!=pT->aMap ){ |
| 248874 | + memcpy(pT->aMap, a1, pT->nMap*sizeof(Fts5TokenDataMap)); |
| 248875 | + } |
| 248876 | + sqlite3_free(aTmp); |
| 248877 | + |
| 248878 | +#ifdef SQLITE_DEBUG |
| 248879 | + { |
| 248880 | + int ii; |
| 248881 | + for(ii=1; ii<pT->nMap; ii++){ |
| 248882 | + Fts5TokenDataMap *p1 = &pT->aMap[ii-1]; |
| 248883 | + Fts5TokenDataMap *p2 = &pT->aMap[ii]; |
| 248884 | + assert( p1->iRowid<p2->iRowid |
| 248885 | + || (p1->iRowid==p2->iRowid && p1->iPos<=p2->iPos) |
| 248886 | + ); |
| 248887 | + } |
| 248888 | + } |
| 248889 | +#endif |
| 248890 | + } |
| 248891 | +} |
| 248892 | + |
| 248893 | +/* |
| 248894 | +** Delete an Fts5TokenDataIter structure and its contents. |
| 248895 | +*/ |
| 248896 | +static void fts5TokendataIterDelete(Fts5TokenDataIter *pSet){ |
| 248897 | + if( pSet ){ |
| 248898 | + int ii; |
| 248899 | + for(ii=0; ii<pSet->nIter; ii++){ |
| 248900 | + fts5MultiIterFree(pSet->apIter[ii]); |
| 248901 | + } |
| 248902 | + fts5BufferFree(&pSet->terms); |
| 248903 | + sqlite3_free(pSet->aPoslistReader); |
| 248904 | + sqlite3_free(pSet->aMap); |
| 248905 | + sqlite3_free(pSet); |
| 248906 | + } |
| 248907 | +} |
| 248908 | + |
| 248909 | + |
| 248910 | +/* |
| 248911 | +** fts5VisitEntries() context object used by fts5SetupPrefixIterTokendata() |
| 248912 | +** to pass data to prefixIterSetupTokendataCb(). |
| 248913 | +*/ |
| 248914 | +typedef struct TokendataSetupCtx TokendataSetupCtx; |
| 248915 | +struct TokendataSetupCtx { |
| 248916 | + Fts5TokenDataIter *pT; /* Object being populated with mappings */ |
| 248917 | + int iTermOff; /* Offset of current term in terms.p[] */ |
| 248918 | + int nTermByte; /* Size of current term in bytes */ |
| 248919 | +}; |
| 248920 | + |
| 248921 | +/* |
| 248922 | +** fts5VisitEntries() callback used by fts5SetupPrefixIterTokendata(). This |
| 248923 | +** callback adds an entry to the Fts5TokenDataIter.aMap[] array for each |
| 248924 | +** position in the current position-list. It doesn't matter that some of |
| 248925 | +** these may be out of order - they will be sorted later. |
| 248926 | +*/ |
| 248927 | +static void prefixIterSetupTokendataCb( |
| 248928 | + Fts5Index *p, |
| 248929 | + void *pCtx, |
| 248930 | + Fts5Iter *p1, |
| 248931 | + const u8 *pNew, |
| 248932 | + int nNew |
| 248933 | +){ |
| 248934 | + TokendataSetupCtx *pSetup = (TokendataSetupCtx*)pCtx; |
| 248935 | + int iPosOff = 0; |
| 248936 | + i64 iPos = 0; |
| 248937 | + |
| 248938 | + if( pNew ){ |
| 248939 | + pSetup->nTermByte = nNew-1; |
| 248940 | + pSetup->iTermOff = pSetup->pT->terms.n; |
| 248941 | + fts5BufferAppendBlob(&p->rc, &pSetup->pT->terms, nNew-1, pNew+1); |
| 248942 | + } |
| 248943 | + |
| 248944 | + while( 0==sqlite3Fts5PoslistNext64( |
| 248945 | + p1->base.pData, p1->base.nData, &iPosOff, &iPos |
| 248946 | + ) ){ |
| 248947 | + fts5TokendataIterAppendMap(p, |
| 248948 | + pSetup->pT, pSetup->iTermOff, pSetup->nTermByte, p1->base.iRowid, iPos |
| 248949 | + ); |
| 248950 | + } |
| 248951 | +} |
| 248952 | + |
| 248953 | + |
| 248954 | +/* |
| 248955 | +** Context object passed by fts5SetupPrefixIter() to fts5VisitEntries(). |
| 248956 | +*/ |
| 248957 | +typedef struct PrefixSetupCtx PrefixSetupCtx; |
| 248958 | +struct PrefixSetupCtx { |
| 248959 | + void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*); |
| 248960 | + void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*); |
| 248961 | + i64 iLastRowid; |
| 248962 | + int nMerge; |
| 248963 | + Fts5Buffer *aBuf; |
| 248964 | + int nBuf; |
| 248965 | + Fts5Buffer doclist; |
| 248966 | + TokendataSetupCtx *pTokendata; |
| 248967 | +}; |
| 248968 | + |
| 248969 | +/* |
| 248970 | +** fts5VisitEntries() callback used by fts5SetupPrefixIter() |
| 248971 | +*/ |
| 248972 | +static void prefixIterSetupCb( |
| 248973 | + Fts5Index *p, |
| 248974 | + void *pCtx, |
| 248975 | + Fts5Iter *p1, |
| 248976 | + const u8 *pNew, |
| 248977 | + int nNew |
| 248978 | +){ |
| 248979 | + PrefixSetupCtx *pSetup = (PrefixSetupCtx*)pCtx; |
| 248980 | + const int nMerge = pSetup->nMerge; |
| 248981 | + |
| 248982 | + if( p1->base.nData>0 ){ |
| 248983 | + if( p1->base.iRowid<=pSetup->iLastRowid && pSetup->doclist.n>0 ){ |
| 248984 | + int i; |
| 248985 | + for(i=0; p->rc==SQLITE_OK && pSetup->doclist.n; i++){ |
| 248986 | + int i1 = i*nMerge; |
| 248987 | + int iStore; |
| 248988 | + assert( i1+nMerge<=pSetup->nBuf ); |
| 248989 | + for(iStore=i1; iStore<i1+nMerge; iStore++){ |
| 248990 | + if( pSetup->aBuf[iStore].n==0 ){ |
| 248991 | + fts5BufferSwap(&pSetup->doclist, &pSetup->aBuf[iStore]); |
| 248992 | + fts5BufferZero(&pSetup->doclist); |
| 248993 | + break; |
| 248994 | + } |
| 248995 | + } |
| 248996 | + if( iStore==i1+nMerge ){ |
| 248997 | + pSetup->xMerge(p, &pSetup->doclist, nMerge, &pSetup->aBuf[i1]); |
| 248998 | + for(iStore=i1; iStore<i1+nMerge; iStore++){ |
| 248999 | + fts5BufferZero(&pSetup->aBuf[iStore]); |
| 249000 | + } |
| 249001 | + } |
| 249002 | + } |
| 249003 | + pSetup->iLastRowid = 0; |
| 249004 | + } |
| 249005 | + |
| 249006 | + pSetup->xAppend( |
| 249007 | + p, (u64)p1->base.iRowid-(u64)pSetup->iLastRowid, p1, &pSetup->doclist |
| 249008 | + ); |
| 249009 | + pSetup->iLastRowid = p1->base.iRowid; |
| 249010 | + } |
| 249011 | + |
| 249012 | + if( pSetup->pTokendata ){ |
| 249013 | + prefixIterSetupTokendataCb(p, (void*)pSetup->pTokendata, p1, pNew, nNew); |
| 249014 | + } |
| 249015 | +} |
| 249016 | + |
| 248430 | 249017 | static void fts5SetupPrefixIter( |
| 248431 | 249018 | Fts5Index *p, /* Index to read from */ |
| 248432 | 249019 | int bDesc, /* True for "ORDER BY rowid DESC" */ |
| 248433 | 249020 | int iIdx, /* Index to scan for data */ |
| 248434 | 249021 | u8 *pToken, /* Buffer containing prefix to match */ |
| | @@ -248435,137 +249022,91 @@ |
| 248435 | 249022 | int nToken, /* Size of buffer pToken in bytes */ |
| 248436 | 249023 | Fts5Colset *pColset, /* Restrict matches to these columns */ |
| 248437 | 249024 | Fts5Iter **ppIter /* OUT: New iterator */ |
| 248438 | 249025 | ){ |
| 248439 | 249026 | Fts5Structure *pStruct; |
| 248440 | | - Fts5Buffer *aBuf; |
| 248441 | | - int nBuf = 32; |
| 248442 | | - int nMerge = 1; |
| 249027 | + PrefixSetupCtx s; |
| 249028 | + TokendataSetupCtx s2; |
| 248443 | 249029 | |
| 248444 | | - void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*); |
| 248445 | | - void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*); |
| 249030 | + memset(&s, 0, sizeof(s)); |
| 249031 | + memset(&s2, 0, sizeof(s2)); |
| 249032 | + |
| 249033 | + s.nMerge = 1; |
| 249034 | + s.iLastRowid = 0; |
| 249035 | + s.nBuf = 32; |
| 249036 | + if( iIdx==0 |
| 249037 | + && p->pConfig->eDetail==FTS5_DETAIL_FULL |
| 249038 | + && p->pConfig->bPrefixInsttoken |
| 249039 | + ){ |
| 249040 | + s.pTokendata = &s2; |
| 249041 | + s2.pT = (Fts5TokenDataIter*)fts5IdxMalloc(p, sizeof(*s2.pT)); |
| 249042 | + } |
| 249043 | + |
| 248446 | 249044 | if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){ |
| 248447 | | - xMerge = fts5MergeRowidLists; |
| 248448 | | - xAppend = fts5AppendRowid; |
| 249045 | + s.xMerge = fts5MergeRowidLists; |
| 249046 | + s.xAppend = fts5AppendRowid; |
| 248449 | 249047 | }else{ |
| 248450 | | - nMerge = FTS5_MERGE_NLIST-1; |
| 248451 | | - nBuf = nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */ |
| 248452 | | - xMerge = fts5MergePrefixLists; |
| 248453 | | - xAppend = fts5AppendPoslist; |
| 249048 | + s.nMerge = FTS5_MERGE_NLIST-1; |
| 249049 | + s.nBuf = s.nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */ |
| 249050 | + s.xMerge = fts5MergePrefixLists; |
| 249051 | + s.xAppend = fts5AppendPoslist; |
| 248454 | 249052 | } |
| 248455 | 249053 | |
| 248456 | | - aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf); |
| 249054 | + s.aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*s.nBuf); |
| 248457 | 249055 | pStruct = fts5StructureRead(p); |
| 248458 | | - assert( p->rc!=SQLITE_OK || (aBuf && pStruct) ); |
| 249056 | + assert( p->rc!=SQLITE_OK || (s.aBuf && pStruct) ); |
| 248459 | 249057 | |
| 248460 | 249058 | if( p->rc==SQLITE_OK ){ |
| 248461 | | - const int flags = FTS5INDEX_QUERY_SCAN |
| 248462 | | - | FTS5INDEX_QUERY_SKIPEMPTY |
| 248463 | | - | FTS5INDEX_QUERY_NOOUTPUT; |
| 249059 | + void *pCtx = (void*)&s; |
| 248464 | 249060 | int i; |
| 248465 | | - i64 iLastRowid = 0; |
| 248466 | | - Fts5Iter *p1 = 0; /* Iterator used to gather data from index */ |
| 248467 | 249061 | Fts5Data *pData; |
| 248468 | | - Fts5Buffer doclist; |
| 248469 | | - int bNewTerm = 1; |
| 248470 | | - |
| 248471 | | - memset(&doclist, 0, sizeof(doclist)); |
| 248472 | 249062 | |
| 248473 | 249063 | /* If iIdx is non-zero, then it is the number of a prefix-index for |
| 248474 | 249064 | ** prefixes 1 character longer than the prefix being queried for. That |
| 248475 | 249065 | ** index contains all the doclists required, except for the one |
| 248476 | 249066 | ** corresponding to the prefix itself. That one is extracted from the |
| 248477 | 249067 | ** main term index here. */ |
| 248478 | 249068 | if( iIdx!=0 ){ |
| 248479 | | - int dummy = 0; |
| 248480 | | - const int f2 = FTS5INDEX_QUERY_SKIPEMPTY|FTS5INDEX_QUERY_NOOUTPUT; |
| 248481 | 249069 | pToken[0] = FTS5_MAIN_PREFIX; |
| 248482 | | - fts5MultiIterNew(p, pStruct, f2, pColset, pToken, nToken, -1, 0, &p1); |
| 248483 | | - fts5IterSetOutputCb(&p->rc, p1); |
| 248484 | | - for(; |
| 248485 | | - fts5MultiIterEof(p, p1)==0; |
| 248486 | | - fts5MultiIterNext2(p, p1, &dummy) |
| 248487 | | - ){ |
| 248488 | | - Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ]; |
| 248489 | | - p1->xSetOutputs(p1, pSeg); |
| 248490 | | - if( p1->base.nData ){ |
| 248491 | | - xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist); |
| 248492 | | - iLastRowid = p1->base.iRowid; |
| 248493 | | - } |
| 248494 | | - } |
| 248495 | | - fts5MultiIterFree(p1); |
| 249070 | + fts5VisitEntries(p, pColset, pToken, nToken, 0, prefixIterSetupCb, pCtx); |
| 248496 | 249071 | } |
| 248497 | 249072 | |
| 248498 | 249073 | pToken[0] = FTS5_MAIN_PREFIX + iIdx; |
| 248499 | | - fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1); |
| 248500 | | - fts5IterSetOutputCb(&p->rc, p1); |
| 248501 | | - |
| 248502 | | - for( /* no-op */ ; |
| 248503 | | - fts5MultiIterEof(p, p1)==0; |
| 248504 | | - fts5MultiIterNext2(p, p1, &bNewTerm) |
| 248505 | | - ){ |
| 248506 | | - Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ]; |
| 248507 | | - int nTerm = pSeg->term.n; |
| 248508 | | - const u8 *pTerm = pSeg->term.p; |
| 248509 | | - p1->xSetOutputs(p1, pSeg); |
| 248510 | | - |
| 248511 | | - assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 ); |
| 248512 | | - if( bNewTerm ){ |
| 248513 | | - if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break; |
| 248514 | | - } |
| 248515 | | - |
| 248516 | | - if( p1->base.nData==0 ) continue; |
| 248517 | | - if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){ |
| 248518 | | - for(i=0; p->rc==SQLITE_OK && doclist.n; i++){ |
| 248519 | | - int i1 = i*nMerge; |
| 248520 | | - int iStore; |
| 248521 | | - assert( i1+nMerge<=nBuf ); |
| 248522 | | - for(iStore=i1; iStore<i1+nMerge; iStore++){ |
| 248523 | | - if( aBuf[iStore].n==0 ){ |
| 248524 | | - fts5BufferSwap(&doclist, &aBuf[iStore]); |
| 248525 | | - fts5BufferZero(&doclist); |
| 248526 | | - break; |
| 248527 | | - } |
| 248528 | | - } |
| 248529 | | - if( iStore==i1+nMerge ){ |
| 248530 | | - xMerge(p, &doclist, nMerge, &aBuf[i1]); |
| 248531 | | - for(iStore=i1; iStore<i1+nMerge; iStore++){ |
| 248532 | | - fts5BufferZero(&aBuf[iStore]); |
| 248533 | | - } |
| 248534 | | - } |
| 248535 | | - } |
| 248536 | | - iLastRowid = 0; |
| 248537 | | - } |
| 248538 | | - |
| 248539 | | - xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist); |
| 248540 | | - iLastRowid = p1->base.iRowid; |
| 248541 | | - } |
| 248542 | | - |
| 248543 | | - assert( (nBuf%nMerge)==0 ); |
| 248544 | | - for(i=0; i<nBuf; i+=nMerge){ |
| 249074 | + fts5VisitEntries(p, pColset, pToken, nToken, 1, prefixIterSetupCb, pCtx); |
| 249075 | + |
| 249076 | + assert( (s.nBuf%s.nMerge)==0 ); |
| 249077 | + for(i=0; i<s.nBuf; i+=s.nMerge){ |
| 248545 | 249078 | int iFree; |
| 248546 | 249079 | if( p->rc==SQLITE_OK ){ |
| 248547 | | - xMerge(p, &doclist, nMerge, &aBuf[i]); |
| 249080 | + s.xMerge(p, &s.doclist, s.nMerge, &s.aBuf[i]); |
| 248548 | 249081 | } |
| 248549 | | - for(iFree=i; iFree<i+nMerge; iFree++){ |
| 248550 | | - fts5BufferFree(&aBuf[iFree]); |
| 249082 | + for(iFree=i; iFree<i+s.nMerge; iFree++){ |
| 249083 | + fts5BufferFree(&s.aBuf[iFree]); |
| 248551 | 249084 | } |
| 248552 | 249085 | } |
| 248553 | | - fts5MultiIterFree(p1); |
| 248554 | 249086 | |
| 248555 | | - pData = fts5IdxMalloc(p, sizeof(*pData)+doclist.n+FTS5_DATA_ZERO_PADDING); |
| 249087 | + pData = fts5IdxMalloc(p, sizeof(*pData)+s.doclist.n+FTS5_DATA_ZERO_PADDING); |
| 249088 | + assert( pData!=0 || p->rc!=SQLITE_OK ); |
| 248556 | 249089 | if( pData ){ |
| 248557 | 249090 | pData->p = (u8*)&pData[1]; |
| 248558 | | - pData->nn = pData->szLeaf = doclist.n; |
| 248559 | | - if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n); |
| 249091 | + pData->nn = pData->szLeaf = s.doclist.n; |
| 249092 | + if( s.doclist.n ) memcpy(pData->p, s.doclist.p, s.doclist.n); |
| 248560 | 249093 | fts5MultiIterNew2(p, pData, bDesc, ppIter); |
| 248561 | 249094 | } |
| 248562 | | - fts5BufferFree(&doclist); |
| 249095 | + |
| 249096 | + assert( (*ppIter)!=0 || p->rc!=SQLITE_OK ); |
| 249097 | + if( p->rc==SQLITE_OK && s.pTokendata ){ |
| 249098 | + fts5TokendataIterSortMap(p, s2.pT); |
| 249099 | + (*ppIter)->pTokenDataIter = s2.pT; |
| 249100 | + s2.pT = 0; |
| 249101 | + } |
| 248563 | 249102 | } |
| 248564 | 249103 | |
| 249104 | + fts5TokendataIterDelete(s2.pT); |
| 249105 | + fts5BufferFree(&s.doclist); |
| 248565 | 249106 | fts5StructureRelease(pStruct); |
| 248566 | | - sqlite3_free(aBuf); |
| 249107 | + sqlite3_free(s.aBuf); |
| 248567 | 249108 | } |
| 248568 | 249109 | |
| 248569 | 249110 | |
| 248570 | 249111 | /* |
| 248571 | 249112 | ** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain |
| | @@ -248815,42 +249356,10 @@ |
| 248815 | 249356 | static void fts5SegIterSetEOF(Fts5SegIter *pSeg){ |
| 248816 | 249357 | fts5DataRelease(pSeg->pLeaf); |
| 248817 | 249358 | pSeg->pLeaf = 0; |
| 248818 | 249359 | } |
| 248819 | 249360 | |
| 248820 | | -/* |
| 248821 | | -** Usually, a tokendata=1 iterator (struct Fts5TokenDataIter) accumulates an |
| 248822 | | -** array of these for each row it visits. Or, for an iterator used by an |
| 248823 | | -** "ORDER BY rank" query, it accumulates an array of these for the entire |
| 248824 | | -** query. |
| 248825 | | -** |
| 248826 | | -** Each instance in the array indicates the iterator (and therefore term) |
| 248827 | | -** associated with position iPos of rowid iRowid. This is used by the |
| 248828 | | -** xInstToken() API. |
| 248829 | | -*/ |
| 248830 | | -struct Fts5TokenDataMap { |
| 248831 | | - i64 iRowid; /* Row this token is located in */ |
| 248832 | | - i64 iPos; /* Position of token */ |
| 248833 | | - int iIter; /* Iterator token was read from */ |
| 248834 | | -}; |
| 248835 | | - |
| 248836 | | -/* |
| 248837 | | -** An object used to supplement Fts5Iter for tokendata=1 iterators. |
| 248838 | | -*/ |
| 248839 | | -struct Fts5TokenDataIter { |
| 248840 | | - int nIter; |
| 248841 | | - int nIterAlloc; |
| 248842 | | - |
| 248843 | | - int nMap; |
| 248844 | | - int nMapAlloc; |
| 248845 | | - Fts5TokenDataMap *aMap; |
| 248846 | | - |
| 248847 | | - Fts5PoslistReader *aPoslistReader; |
| 248848 | | - int *aPoslistToIter; |
| 248849 | | - Fts5Iter *apIter[1]; |
| 248850 | | -}; |
| 248851 | | - |
| 248852 | 249361 | /* |
| 248853 | 249362 | ** This function appends iterator pAppend to Fts5TokenDataIter pIn and |
| 248854 | 249363 | ** returns the result. |
| 248855 | 249364 | */ |
| 248856 | 249365 | static Fts5TokenDataIter *fts5AppendTokendataIter( |
| | @@ -248883,58 +249392,10 @@ |
| 248883 | 249392 | assert( pRet==0 || pRet->nIter<=pRet->nIterAlloc ); |
| 248884 | 249393 | |
| 248885 | 249394 | return pRet; |
| 248886 | 249395 | } |
| 248887 | 249396 | |
| 248888 | | -/* |
| 248889 | | -** Delete an Fts5TokenDataIter structure and its contents. |
| 248890 | | -*/ |
| 248891 | | -static void fts5TokendataIterDelete(Fts5TokenDataIter *pSet){ |
| 248892 | | - if( pSet ){ |
| 248893 | | - int ii; |
| 248894 | | - for(ii=0; ii<pSet->nIter; ii++){ |
| 248895 | | - fts5MultiIterFree(pSet->apIter[ii]); |
| 248896 | | - } |
| 248897 | | - sqlite3_free(pSet->aPoslistReader); |
| 248898 | | - sqlite3_free(pSet->aMap); |
| 248899 | | - sqlite3_free(pSet); |
| 248900 | | - } |
| 248901 | | -} |
| 248902 | | - |
| 248903 | | -/* |
| 248904 | | -** Append a mapping to the token-map belonging to object pT. |
| 248905 | | -*/ |
| 248906 | | -static void fts5TokendataIterAppendMap( |
| 248907 | | - Fts5Index *p, |
| 248908 | | - Fts5TokenDataIter *pT, |
| 248909 | | - int iIter, |
| 248910 | | - i64 iRowid, |
| 248911 | | - i64 iPos |
| 248912 | | -){ |
| 248913 | | - if( p->rc==SQLITE_OK ){ |
| 248914 | | - if( pT->nMap==pT->nMapAlloc ){ |
| 248915 | | - int nNew = pT->nMapAlloc ? pT->nMapAlloc*2 : 64; |
| 248916 | | - int nByte = nNew * sizeof(Fts5TokenDataMap); |
| 248917 | | - Fts5TokenDataMap *aNew; |
| 248918 | | - |
| 248919 | | - aNew = (Fts5TokenDataMap*)sqlite3_realloc(pT->aMap, nByte); |
| 248920 | | - if( aNew==0 ){ |
| 248921 | | - p->rc = SQLITE_NOMEM; |
| 248922 | | - return; |
| 248923 | | - } |
| 248924 | | - |
| 248925 | | - pT->aMap = aNew; |
| 248926 | | - pT->nMapAlloc = nNew; |
| 248927 | | - } |
| 248928 | | - |
| 248929 | | - pT->aMap[pT->nMap].iRowid = iRowid; |
| 248930 | | - pT->aMap[pT->nMap].iPos = iPos; |
| 248931 | | - pT->aMap[pT->nMap].iIter = iIter; |
| 248932 | | - pT->nMap++; |
| 248933 | | - } |
| 248934 | | -} |
| 248935 | | - |
| 248936 | 249397 | /* |
| 248937 | 249398 | ** The iterator passed as the only argument must be a tokendata=1 iterator |
| 248938 | 249399 | ** (pIter->pTokenDataIter!=0). This function sets the iterator output |
| 248939 | 249400 | ** variables (pIter->base.*) according to the contents of the current |
| 248940 | 249401 | ** row. |
| | @@ -248971,11 +249432,11 @@ |
| 248971 | 249432 | int eDetail = pIter->pIndex->pConfig->eDetail; |
| 248972 | 249433 | pIter->base.bEof = 0; |
| 248973 | 249434 | pIter->base.iRowid = iRowid; |
| 248974 | 249435 | |
| 248975 | 249436 | if( nHit==1 && eDetail==FTS5_DETAIL_FULL ){ |
| 248976 | | - fts5TokendataIterAppendMap(pIter->pIndex, pT, iMin, iRowid, -1); |
| 249437 | + fts5TokendataIterAppendMap(pIter->pIndex, pT, iMin, 0, iRowid, -1); |
| 248977 | 249438 | }else |
| 248978 | 249439 | if( nHit>1 && eDetail!=FTS5_DETAIL_NONE ){ |
| 248979 | 249440 | int nReader = 0; |
| 248980 | 249441 | int nByte = 0; |
| 248981 | 249442 | i64 iPrev = 0; |
| | @@ -249224,10 +249685,11 @@ |
| 249224 | 249685 | |
| 249225 | 249686 | if( p->rc==SQLITE_OK ){ |
| 249226 | 249687 | pRet = fts5MultiIterAlloc(p, 0); |
| 249227 | 249688 | } |
| 249228 | 249689 | if( pRet ){ |
| 249690 | + pRet->nSeg = 0; |
| 249229 | 249691 | pRet->pTokenDataIter = pSet; |
| 249230 | 249692 | if( pSet ){ |
| 249231 | 249693 | fts5IterSetOutputsTokendata(pRet); |
| 249232 | 249694 | }else{ |
| 249233 | 249695 | pRet->base.bEof = 1; |
| | @@ -249238,11 +249700,10 @@ |
| 249238 | 249700 | |
| 249239 | 249701 | fts5StructureRelease(pStruct); |
| 249240 | 249702 | fts5BufferFree(&bSeek); |
| 249241 | 249703 | return pRet; |
| 249242 | 249704 | } |
| 249243 | | - |
| 249244 | 249705 | |
| 249245 | 249706 | /* |
| 249246 | 249707 | ** Open a new iterator to iterate though all rowid that match the |
| 249247 | 249708 | ** specified token or token prefix. |
| 249248 | 249709 | */ |
| | @@ -249262,12 +249723,18 @@ |
| 249262 | 249723 | |
| 249263 | 249724 | if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){ |
| 249264 | 249725 | int iIdx = 0; /* Index to search */ |
| 249265 | 249726 | int iPrefixIdx = 0; /* +1 prefix index */ |
| 249266 | 249727 | int bTokendata = pConfig->bTokendata; |
| 249728 | + assert( buf.p!=0 ); |
| 249267 | 249729 | if( nToken>0 ) memcpy(&buf.p[1], pToken, nToken); |
| 249268 | 249730 | |
| 249731 | + /* The NOTOKENDATA flag is set when each token in a tokendata=1 table |
| 249732 | + ** should be treated individually, instead of merging all those with |
| 249733 | + ** a common prefix into a single entry. This is used, for example, by |
| 249734 | + ** queries performed as part of an integrity-check, or by the fts5vocab |
| 249735 | + ** module. */ |
| 249269 | 249736 | if( flags & (FTS5INDEX_QUERY_NOTOKENDATA|FTS5INDEX_QUERY_SCAN) ){ |
| 249270 | 249737 | bTokendata = 0; |
| 249271 | 249738 | } |
| 249272 | 249739 | |
| 249273 | 249740 | /* Figure out which index to search and set iIdx accordingly. If this |
| | @@ -249294,11 +249761,11 @@ |
| 249294 | 249761 | if( nIdxChar==nChar+1 ) iPrefixIdx = iIdx; |
| 249295 | 249762 | } |
| 249296 | 249763 | } |
| 249297 | 249764 | |
| 249298 | 249765 | if( bTokendata && iIdx==0 ){ |
| 249299 | | - buf.p[0] = '0'; |
| 249766 | + buf.p[0] = FTS5_MAIN_PREFIX; |
| 249300 | 249767 | pRet = fts5SetupTokendataIter(p, buf.p, nToken+1, pColset); |
| 249301 | 249768 | }else if( iIdx<=pConfig->nPrefix ){ |
| 249302 | 249769 | /* Straight index lookup */ |
| 249303 | 249770 | Fts5Structure *pStruct = fts5StructureRead(p); |
| 249304 | 249771 | buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx); |
| | @@ -249307,11 +249774,11 @@ |
| 249307 | 249774 | pColset, buf.p, nToken+1, -1, 0, &pRet |
| 249308 | 249775 | ); |
| 249309 | 249776 | fts5StructureRelease(pStruct); |
| 249310 | 249777 | } |
| 249311 | 249778 | }else{ |
| 249312 | | - /* Scan multiple terms in the main index */ |
| 249779 | + /* Scan multiple terms in the main index for a prefix query. */ |
| 249313 | 249780 | int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0; |
| 249314 | 249781 | fts5SetupPrefixIter(p, bDesc, iPrefixIdx, buf.p, nToken+1, pColset,&pRet); |
| 249315 | 249782 | if( pRet==0 ){ |
| 249316 | 249783 | assert( p->rc!=SQLITE_OK ); |
| 249317 | 249784 | }else{ |
| | @@ -249343,11 +249810,12 @@ |
| 249343 | 249810 | ** Move to the next matching rowid. |
| 249344 | 249811 | */ |
| 249345 | 249812 | static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){ |
| 249346 | 249813 | Fts5Iter *pIter = (Fts5Iter*)pIndexIter; |
| 249347 | 249814 | assert( pIter->pIndex->rc==SQLITE_OK ); |
| 249348 | | - if( pIter->pTokenDataIter ){ |
| 249815 | + if( pIter->nSeg==0 ){ |
| 249816 | + assert( pIter->pTokenDataIter ); |
| 249349 | 249817 | fts5TokendataIterNext(pIter, 0, 0); |
| 249350 | 249818 | }else{ |
| 249351 | 249819 | fts5MultiIterNext(pIter->pIndex, pIter, 0, 0); |
| 249352 | 249820 | } |
| 249353 | 249821 | return fts5IndexReturn(pIter->pIndex); |
| | @@ -249380,11 +249848,12 @@ |
| 249380 | 249848 | ** definition of "at or after" depends on whether this iterator iterates |
| 249381 | 249849 | ** in ascending or descending rowid order. |
| 249382 | 249850 | */ |
| 249383 | 249851 | static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){ |
| 249384 | 249852 | Fts5Iter *pIter = (Fts5Iter*)pIndexIter; |
| 249385 | | - if( pIter->pTokenDataIter ){ |
| 249853 | + if( pIter->nSeg==0 ){ |
| 249854 | + assert( pIter->pTokenDataIter ); |
| 249386 | 249855 | fts5TokendataIterNext(pIter, 1, iMatch); |
| 249387 | 249856 | }else{ |
| 249388 | 249857 | fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch); |
| 249389 | 249858 | } |
| 249390 | 249859 | return fts5IndexReturn(pIter->pIndex); |
| | @@ -249398,32 +249867,88 @@ |
| 249398 | 249867 | const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n); |
| 249399 | 249868 | assert_nc( z || n<=1 ); |
| 249400 | 249869 | *pn = n-1; |
| 249401 | 249870 | return (z ? &z[1] : 0); |
| 249402 | 249871 | } |
| 249872 | + |
| 249873 | +/* |
| 249874 | +** pIter is a prefix query. This function populates pIter->pTokenDataIter |
| 249875 | +** with an Fts5TokenDataIter object containing mappings for all rows |
| 249876 | +** matched by the query. |
| 249877 | +*/ |
| 249878 | +static int fts5SetupPrefixIterTokendata( |
| 249879 | + Fts5Iter *pIter, |
| 249880 | + const char *pToken, /* Token prefix to search for */ |
| 249881 | + int nToken /* Size of pToken in bytes */ |
| 249882 | +){ |
| 249883 | + Fts5Index *p = pIter->pIndex; |
| 249884 | + Fts5Buffer token = {0, 0, 0}; |
| 249885 | + TokendataSetupCtx ctx; |
| 249886 | + |
| 249887 | + memset(&ctx, 0, sizeof(ctx)); |
| 249888 | + |
| 249889 | + fts5BufferGrow(&p->rc, &token, nToken+1); |
| 249890 | + assert( token.p!=0 || p->rc!=SQLITE_OK ); |
| 249891 | + ctx.pT = (Fts5TokenDataIter*)sqlite3Fts5MallocZero(&p->rc, sizeof(*ctx.pT)); |
| 249892 | + |
| 249893 | + if( p->rc==SQLITE_OK ){ |
| 249894 | + |
| 249895 | + /* Fill in the token prefix to search for */ |
| 249896 | + token.p[0] = FTS5_MAIN_PREFIX; |
| 249897 | + memcpy(&token.p[1], pToken, nToken); |
| 249898 | + token.n = nToken+1; |
| 249899 | + |
| 249900 | + fts5VisitEntries( |
| 249901 | + p, 0, token.p, token.n, 1, prefixIterSetupTokendataCb, (void*)&ctx |
| 249902 | + ); |
| 249903 | + |
| 249904 | + fts5TokendataIterSortMap(p, ctx.pT); |
| 249905 | + } |
| 249906 | + |
| 249907 | + if( p->rc==SQLITE_OK ){ |
| 249908 | + pIter->pTokenDataIter = ctx.pT; |
| 249909 | + }else{ |
| 249910 | + fts5TokendataIterDelete(ctx.pT); |
| 249911 | + } |
| 249912 | + fts5BufferFree(&token); |
| 249913 | + |
| 249914 | + return fts5IndexReturn(p); |
| 249915 | +} |
| 249403 | 249916 | |
| 249404 | 249917 | /* |
| 249405 | 249918 | ** This is used by xInstToken() to access the token at offset iOff, column |
| 249406 | 249919 | ** iCol of row iRowid. The token is returned via output variables *ppOut |
| 249407 | 249920 | ** and *pnOut. The iterator passed as the first argument must be a tokendata=1 |
| 249408 | 249921 | ** iterator (pIter->pTokenDataIter!=0). |
| 249922 | +** |
| 249923 | +** pToken/nToken: |
| 249409 | 249924 | */ |
| 249410 | 249925 | static int sqlite3Fts5IterToken( |
| 249411 | 249926 | Fts5IndexIter *pIndexIter, |
| 249927 | + const char *pToken, int nToken, |
| 249412 | 249928 | i64 iRowid, |
| 249413 | 249929 | int iCol, |
| 249414 | 249930 | int iOff, |
| 249415 | 249931 | const char **ppOut, int *pnOut |
| 249416 | 249932 | ){ |
| 249417 | 249933 | Fts5Iter *pIter = (Fts5Iter*)pIndexIter; |
| 249418 | 249934 | Fts5TokenDataIter *pT = pIter->pTokenDataIter; |
| 249419 | | - Fts5TokenDataMap *aMap = pT->aMap; |
| 249420 | 249935 | i64 iPos = (((i64)iCol)<<32) + iOff; |
| 249421 | | - |
| 249936 | + Fts5TokenDataMap *aMap = 0; |
| 249422 | 249937 | int i1 = 0; |
| 249423 | | - int i2 = pT->nMap; |
| 249938 | + int i2 = 0; |
| 249424 | 249939 | int iTest = 0; |
| 249940 | + |
| 249941 | + assert( pT || (pToken && pIter->nSeg>0) ); |
| 249942 | + if( pT==0 ){ |
| 249943 | + int rc = fts5SetupPrefixIterTokendata(pIter, pToken, nToken); |
| 249944 | + if( rc!=SQLITE_OK ) return rc; |
| 249945 | + pT = pIter->pTokenDataIter; |
| 249946 | + } |
| 249947 | + |
| 249948 | + i2 = pT->nMap; |
| 249949 | + aMap = pT->aMap; |
| 249425 | 249950 | |
| 249426 | 249951 | while( i2>i1 ){ |
| 249427 | 249952 | iTest = (i1 + i2) / 2; |
| 249428 | 249953 | |
| 249429 | 249954 | if( aMap[iTest].iRowid<iRowid ){ |
| | @@ -249443,13 +249968,19 @@ |
| 249443 | 249968 | } |
| 249444 | 249969 | } |
| 249445 | 249970 | } |
| 249446 | 249971 | |
| 249447 | 249972 | if( i2>i1 ){ |
| 249448 | | - Fts5Iter *pMap = pT->apIter[aMap[iTest].iIter]; |
| 249449 | | - *ppOut = (const char*)pMap->aSeg[0].term.p+1; |
| 249450 | | - *pnOut = pMap->aSeg[0].term.n-1; |
| 249973 | + if( pIter->nSeg==0 ){ |
| 249974 | + Fts5Iter *pMap = pT->apIter[aMap[iTest].iIter]; |
| 249975 | + *ppOut = (const char*)pMap->aSeg[0].term.p+1; |
| 249976 | + *pnOut = pMap->aSeg[0].term.n-1; |
| 249977 | + }else{ |
| 249978 | + Fts5TokenDataMap *p = &aMap[iTest]; |
| 249979 | + *ppOut = (const char*)&pT->terms.p[p->iIter]; |
| 249980 | + *pnOut = aMap[iTest].nByte; |
| 249981 | + } |
| 249451 | 249982 | } |
| 249452 | 249983 | |
| 249453 | 249984 | return SQLITE_OK; |
| 249454 | 249985 | } |
| 249455 | 249986 | |
| | @@ -249457,11 +249988,13 @@ |
| 249457 | 249988 | ** Clear any existing entries from the token-map associated with the |
| 249458 | 249989 | ** iterator passed as the only argument. |
| 249459 | 249990 | */ |
| 249460 | 249991 | static void sqlite3Fts5IndexIterClearTokendata(Fts5IndexIter *pIndexIter){ |
| 249461 | 249992 | Fts5Iter *pIter = (Fts5Iter*)pIndexIter; |
| 249462 | | - if( pIter && pIter->pTokenDataIter ){ |
| 249993 | + if( pIter && pIter->pTokenDataIter |
| 249994 | + && (pIter->nSeg==0 || pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_FULL) |
| 249995 | + ){ |
| 249463 | 249996 | pIter->pTokenDataIter->nMap = 0; |
| 249464 | 249997 | } |
| 249465 | 249998 | } |
| 249466 | 249999 | |
| 249467 | 250000 | /* |
| | @@ -249477,21 +250010,33 @@ |
| 249477 | 250010 | i64 iRowid, int iCol, int iOff |
| 249478 | 250011 | ){ |
| 249479 | 250012 | Fts5Iter *pIter = (Fts5Iter*)pIndexIter; |
| 249480 | 250013 | Fts5TokenDataIter *pT = pIter->pTokenDataIter; |
| 249481 | 250014 | Fts5Index *p = pIter->pIndex; |
| 249482 | | - int ii; |
| 250015 | + i64 iPos = (((i64)iCol)<<32) + iOff; |
| 249483 | 250016 | |
| 249484 | 250017 | assert( p->pConfig->eDetail!=FTS5_DETAIL_FULL ); |
| 249485 | | - assert( pIter->pTokenDataIter ); |
| 249486 | | - |
| 249487 | | - for(ii=0; ii<pT->nIter; ii++){ |
| 249488 | | - Fts5Buffer *pTerm = &pT->apIter[ii]->aSeg[0].term; |
| 249489 | | - if( nToken==pTerm->n-1 && memcmp(pToken, pTerm->p+1, nToken)==0 ) break; |
| 249490 | | - } |
| 249491 | | - if( ii<pT->nIter ){ |
| 249492 | | - fts5TokendataIterAppendMap(p, pT, ii, iRowid, (((i64)iCol)<<32) + iOff); |
| 250018 | + assert( pIter->pTokenDataIter || pIter->nSeg>0 ); |
| 250019 | + if( pIter->nSeg>0 ){ |
| 250020 | + /* This is a prefix term iterator. */ |
| 250021 | + if( pT==0 ){ |
| 250022 | + pT = (Fts5TokenDataIter*)sqlite3Fts5MallocZero(&p->rc, sizeof(*pT)); |
| 250023 | + pIter->pTokenDataIter = pT; |
| 250024 | + } |
| 250025 | + if( pT ){ |
| 250026 | + fts5TokendataIterAppendMap(p, pT, pT->terms.n, nToken, iRowid, iPos); |
| 250027 | + fts5BufferAppendBlob(&p->rc, &pT->terms, nToken, (const u8*)pToken); |
| 250028 | + } |
| 250029 | + }else{ |
| 250030 | + int ii; |
| 250031 | + for(ii=0; ii<pT->nIter; ii++){ |
| 250032 | + Fts5Buffer *pTerm = &pT->apIter[ii]->aSeg[0].term; |
| 250033 | + if( nToken==pTerm->n-1 && memcmp(pToken, pTerm->p+1, nToken)==0 ) break; |
| 250034 | + } |
| 250035 | + if( ii<pT->nIter ){ |
| 250036 | + fts5TokendataIterAppendMap(p, pT, ii, 0, iRowid, iPos); |
| 250037 | + } |
| 249493 | 250038 | } |
| 249494 | 250039 | return fts5IndexReturn(p); |
| 249495 | 250040 | } |
| 249496 | 250041 | |
| 249497 | 250042 | /* |
| | @@ -251392,10 +251937,11 @@ |
| 251392 | 251937 | ** containing a copy of the header from an Fts5Config pointer. |
| 251393 | 251938 | */ |
| 251394 | 251939 | #define FTS5_LOCALE_HDR_SIZE ((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )) |
| 251395 | 251940 | #define FTS5_LOCALE_HDR(pConfig) ((const u8*)(pConfig->pGlobal->aLocaleHdr)) |
| 251396 | 251941 | |
| 251942 | +#define FTS5_INSTTOKEN_SUBTYPE 73 |
| 251397 | 251943 | |
| 251398 | 251944 | /* |
| 251399 | 251945 | ** Each auxiliary function registered with the FTS5 module is represented |
| 251400 | 251946 | ** by an object of the following type. All such objects are stored as part |
| 251401 | 251947 | ** of the Fts5Global.pAux list. |
| | @@ -251931,10 +252477,11 @@ |
| 251931 | 252477 | ){ |
| 251932 | 252478 | /* A MATCH operator or equivalent */ |
| 251933 | 252479 | if( p->usable==0 || iCol<0 ){ |
| 251934 | 252480 | /* As there exists an unusable MATCH constraint this is an |
| 251935 | 252481 | ** unusable plan. Return SQLITE_CONSTRAINT. */ |
| 252482 | + idxStr[iIdxStr] = 0; |
| 251936 | 252483 | return SQLITE_CONSTRAINT; |
| 251937 | 252484 | }else{ |
| 251938 | 252485 | if( iCol==nCol+1 ){ |
| 251939 | 252486 | if( bSeenRank ) continue; |
| 251940 | 252487 | idxStr[iIdxStr++] = 'r'; |
| | @@ -252716,10 +253263,11 @@ |
| 252716 | 253263 | sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */ |
| 252717 | 253264 | sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */ |
| 252718 | 253265 | sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */ |
| 252719 | 253266 | int iCol; /* Column on LHS of MATCH operator */ |
| 252720 | 253267 | char **pzErrmsg = pConfig->pzErrmsg; |
| 253268 | + int bPrefixInsttoken = pConfig->bPrefixInsttoken; |
| 252721 | 253269 | int i; |
| 252722 | 253270 | int iIdxStr = 0; |
| 252723 | 253271 | Fts5Expr *pExpr = 0; |
| 252724 | 253272 | |
| 252725 | 253273 | assert( pConfig->bLock==0 ); |
| | @@ -252751,10 +253299,13 @@ |
| 252751 | 253299 | int bInternal = 0; |
| 252752 | 253300 | |
| 252753 | 253301 | rc = fts5ExtractExprText(pConfig, apVal[i], &zText, &bFreeAndReset); |
| 252754 | 253302 | if( rc!=SQLITE_OK ) goto filter_out; |
| 252755 | 253303 | if( zText==0 ) zText = ""; |
| 253304 | + if( sqlite3_value_subtype(apVal[i])==FTS5_INSTTOKEN_SUBTYPE ){ |
| 253305 | + pConfig->bPrefixInsttoken = 1; |
| 253306 | + } |
| 252756 | 253307 | |
| 252757 | 253308 | iCol = 0; |
| 252758 | 253309 | do{ |
| 252759 | 253310 | iCol = iCol*10 + (idxStr[iIdxStr]-'0'); |
| 252760 | 253311 | iIdxStr++; |
| | @@ -252891,10 +253442,11 @@ |
| 252891 | 253442 | } |
| 252892 | 253443 | |
| 252893 | 253444 | filter_out: |
| 252894 | 253445 | sqlite3Fts5ExprFree(pExpr); |
| 252895 | 253446 | pConfig->pzErrmsg = pzErrmsg; |
| 253447 | + pConfig->bPrefixInsttoken = bPrefixInsttoken; |
| 252896 | 253448 | return rc; |
| 252897 | 253449 | } |
| 252898 | 253450 | |
| 252899 | 253451 | /* |
| 252900 | 253452 | ** This is the xEof method of the virtual table. SQLite calls this |
| | @@ -254886,11 +255438,11 @@ |
| 254886 | 255438 | int nArg, /* Number of args */ |
| 254887 | 255439 | sqlite3_value **apUnused /* Function arguments */ |
| 254888 | 255440 | ){ |
| 254889 | 255441 | assert( nArg==0 ); |
| 254890 | 255442 | UNUSED_PARAM2(nArg, apUnused); |
| 254891 | | - sqlite3_result_text(pCtx, "fts5: 2024-10-21 16:30:22 03a9703e27c44437c39363d0baf82db4ebc94538a0f28411c85dda156f82636e", -1, SQLITE_TRANSIENT); |
| 255443 | + sqlite3_result_text(pCtx, "fts5: 2024-12-30 21:23:53 2b17bc49655c577029919c2d409de994b0d252f8efb5da1ba0913f2c96bee552", -1, SQLITE_TRANSIENT); |
| 254892 | 255444 | } |
| 254893 | 255445 | |
| 254894 | 255446 | /* |
| 254895 | 255447 | ** Implementation of fts5_locale(LOCALE, TEXT) function. |
| 254896 | 255448 | ** |
| | @@ -254949,10 +255501,24 @@ |
| 254949 | 255501 | assert( &pCsr[nText]==&pBlob[nBlob] ); |
| 254950 | 255502 | |
| 254951 | 255503 | sqlite3_result_blob(pCtx, pBlob, nBlob, sqlite3_free); |
| 254952 | 255504 | } |
| 254953 | 255505 | } |
| 255506 | + |
| 255507 | +/* |
| 255508 | +** Implementation of fts5_insttoken() function. |
| 255509 | +*/ |
| 255510 | +static void fts5InsttokenFunc( |
| 255511 | + sqlite3_context *pCtx, /* Function call context */ |
| 255512 | + int nArg, /* Number of args */ |
| 255513 | + sqlite3_value **apArg /* Function arguments */ |
| 255514 | +){ |
| 255515 | + assert( nArg==1 ); |
| 255516 | + (void)nArg; |
| 255517 | + sqlite3_result_value(pCtx, apArg[0]); |
| 255518 | + sqlite3_result_subtype(pCtx, FTS5_INSTTOKEN_SUBTYPE); |
| 255519 | +} |
| 254954 | 255520 | |
| 254955 | 255521 | /* |
| 254956 | 255522 | ** Return true if zName is the extension on one of the shadow tables used |
| 254957 | 255523 | ** by this module. |
| 254958 | 255524 | */ |
| | @@ -255079,13 +255645,20 @@ |
| 255079 | 255645 | ); |
| 255080 | 255646 | } |
| 255081 | 255647 | if( rc==SQLITE_OK ){ |
| 255082 | 255648 | rc = sqlite3_create_function( |
| 255083 | 255649 | db, "fts5_locale", 2, |
| 255084 | | - SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE, |
| 255650 | + SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE|SQLITE_SUBTYPE, |
| 255085 | 255651 | p, fts5LocaleFunc, 0, 0 |
| 255086 | 255652 | ); |
| 255653 | + } |
| 255654 | + if( rc==SQLITE_OK ){ |
| 255655 | + rc = sqlite3_create_function( |
| 255656 | + db, "fts5_insttoken", 1, |
| 255657 | + SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE, |
| 255658 | + p, fts5InsttokenFunc, 0, 0 |
| 255659 | + ); |
| 255087 | 255660 | } |
| 255088 | 255661 | } |
| 255089 | 255662 | |
| 255090 | 255663 | /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file |
| 255091 | 255664 | ** fts5_test_mi.c is compiled and linked into the executable. And call |
| | @@ -258007,22 +258580,22 @@ |
| 258007 | 258580 | int rc = SQLITE_OK; |
| 258008 | 258581 | char aBuf[32]; |
| 258009 | 258582 | char *zOut = aBuf; |
| 258010 | 258583 | int ii; |
| 258011 | 258584 | const unsigned char *zIn = (const unsigned char*)pText; |
| 258012 | | - const unsigned char *zEof = &zIn[nText]; |
| 258013 | | - u32 iCode; |
| 258585 | + const unsigned char *zEof = (zIn ? &zIn[nText] : 0); |
| 258586 | + u32 iCode = 0; |
| 258014 | 258587 | int aStart[3]; /* Input offset of each character in aBuf[] */ |
| 258015 | 258588 | |
| 258016 | 258589 | UNUSED_PARAM(unusedFlags); |
| 258017 | 258590 | |
| 258018 | 258591 | /* Populate aBuf[] with the characters for the first trigram. */ |
| 258019 | 258592 | for(ii=0; ii<3; ii++){ |
| 258020 | 258593 | do { |
| 258021 | 258594 | aStart[ii] = zIn - (const unsigned char*)pText; |
| 258595 | + if( zIn>=zEof ) return SQLITE_OK; |
| 258022 | 258596 | READ_UTF8(zIn, zEof, iCode); |
| 258023 | | - if( iCode==0 ) return SQLITE_OK; |
| 258024 | 258597 | if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam); |
| 258025 | 258598 | }while( iCode==0 ); |
| 258026 | 258599 | WRITE_UTF8(zOut, iCode); |
| 258027 | 258600 | } |
| 258028 | 258601 | |
| | @@ -258039,12 +258612,15 @@ |
| 258039 | 258612 | const char *z1; |
| 258040 | 258613 | |
| 258041 | 258614 | /* Read characters from the input up until the first non-diacritic */ |
| 258042 | 258615 | do { |
| 258043 | 258616 | iNext = zIn - (const unsigned char*)pText; |
| 258617 | + if( zIn>=zEof ){ |
| 258618 | + iCode = 0; |
| 258619 | + break; |
| 258620 | + } |
| 258044 | 258621 | READ_UTF8(zIn, zEof, iCode); |
| 258045 | | - if( iCode==0 ) break; |
| 258046 | 258622 | if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam); |
| 258047 | 258623 | }while( iCode==0 ); |
| 258048 | 258624 | |
| 258049 | 258625 | /* Pass the current trigram back to fts5 */ |
| 258050 | 258626 | rc = xToken(pCtx, 0, aBuf, zOut-aBuf, aStart[0], iNext); |
| | @@ -260077,11 +260653,11 @@ |
| 260077 | 260653 | |
| 260078 | 260654 | return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0); |
| 260079 | 260655 | } |
| 260080 | 260656 | |
| 260081 | 260657 | |
| 260082 | | - |
| 260658 | +/* Here ends the fts5.c composite file. */ |
| 260083 | 260659 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */ |
| 260084 | 260660 | |
| 260085 | 260661 | /************** End of fts5.c ************************************************/ |
| 260086 | 260662 | /************** Begin file stmt.c ********************************************/ |
| 260087 | 260663 | /* |
| | @@ -260433,6 +261009,7 @@ |
| 260433 | 261009 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 260434 | 261010 | |
| 260435 | 261011 | /************** End of stmt.c ************************************************/ |
| 260436 | 261012 | /* Return the source-id for this library */ |
| 260437 | 261013 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 261014 | +#endif /* SQLITE_AMALGAMATION */ |
| 260438 | 261015 | /************************** End of sqlite3.c ******************************/ |
| 260439 | 261016 | |